开发者

How to call Activity from a menu item in Android?

I'm attempting to call startActivity(myIntent) from the click of a menu button but my application crashes at that point.

The same startActivity call works fine from a regular button click, so, I assume the menu button is missing information about the context? Or maybe I'm totally off the mark here.

So... what's the correct way to have a menu item take me to a specific Activity?

I've revised my code based on the initial set of advice. Still crashing in the same place. The debugger doesn't enter the exception clause, the app just dies.

[EDITED WITH CODE SNIPPET]

public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.menu, menu);
  return 开发者_JAVA百科true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
   try{
    switch (item.getItemId()) {
    case R.id.menuItemLang:            
        startActivity(new Intent("com.my.project.SETTINGS"));
        return true;        
    default:
        return super.onOptionsItemSelected(item);
    }
   }catch(Exception e){
      log(e);
   }
}


First option

You have to override onOptionsItemSelected method in your Activity, which is called when user clicks on the item in Options menu. In the method you can check what item has been clicked.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.menu_item1:
        Intent intent = new Intent(this, ActivityForItemOne.class);
        this.startActivity(intent);
        break;
    case R.id.menu_item2:
        // another startActivity, this is for item with id "menu_item2"
        break;
    default:
        return super.onOptionsItemSelected(item);
    }

    return true;
}

There is also onContextItemSelected method which works similary, but for Context menu (I'm not sure, what menu you mean).

More information at http://developer.android.com/guide/topics/ui/menus.html

EDIT:

Second option

I think the first option is easier, but from your code I see, that you want to start activity as an action (because of String parameter in Intent constructor). To do this, you need to specify an action in your AndroidManifest.xml. So, if I would start activity ActivityForItemOne (from previous example) the <application> element in AndroidManifest.xml would look like this:

<application ...>
    ...

    <activity android:label="Activity For First Item" android:name=".ActivityForItemOne">
        <intent-filter>
            <action android:name="my.app.ITEMONE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

And the Intent will be:

Intent intent = new Intent("my.app.ITEMONE");

The my.app. is package of your application. It's not necessary to use your application package, but it's recommended for uniqueness of actions.

More information at:

Class Intent - Action and Category constants

Action element

Intents and Intent Filters

Hope this solve your problem.


More optimice:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.item1:
            return true;
        case R.id.item2:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


if There have 2 Class 1 MainActivity 2 Welcome then you need to go from welcom>MainActivity

@Override public boolean onCreateOptionsMenu(Menu menu) {

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.logout:
            Intent intent = new Intent(this, MainActivity.class);
            this.startActivity(intent);
            break;
        case R.id.settings:
            // another startActivity, this is for item with id "menu_item2"
            break;
        default:
            return super.onOptionsItemSelected(item);
    }

    return true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜