EltMrx
EltMrx

Reputation: 37

How can I access my own activities when pressing the Android Menu Button?

In many applications you can access preferences, settings and what not via the Android Menu.

I want to be able to link to an activity in the App that I created.

Any help and/or code would be much appreciated.

-EltMrx

Upvotes: 1

Views: 203

Answers (2)

dLobatog
dLobatog

Reputation: 1741

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

Class Intent - Action and Category constants http://developer.android.com/reference/android/content/Intent.html

Action element http://developer.android.com/guide/topics/manifest/action-element.html

Intents and Intent Filters http://developer.android.com/guide/topics/intents/intents-filters.html

Hope this solve your problem.

CREDITS TO ICEMAN, How to call Activity from a menu item in Android?

Upvotes: 4

Egor
Egor

Reputation: 40193

This can be helpful to you.

Upvotes: 0

Related Questions