Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Activity from a menu item in Android?

Tags:

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 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);
   }
}
like image 672
Yevgeny Simkin Avatar asked Nov 12 '10 22:11

Yevgeny Simkin


People also ask

How can I get call activity in Android?

If you start the activity with startActivityForResult(Intent, int) , then you can get calling activity by getCallingActivity().

How can I call activity from another activity?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

How do you inflate a menu in activity?

To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object) using MenuInflater. inflate() .

How do I open an activity from an existing activity?

Start new Activity by existing Activity on a button clickUsing the "startactivity(Intent intent)" method and "startActivityforResult(Intent intent, requestCode requestCode)" method.


1 Answers

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.

like image 107
branoholy Avatar answered Oct 07 '22 01:10

branoholy