48

In some methods of my Activity I want to check the title of menu or know if it is checked or not. How can I get Activity's menu. I need something like this.getMenu()

Bob
  • 22,810
  • 38
  • 143
  • 225

6 Answers6

121

Be wary of invalidateOptionsMenu(). It recreates the entire menu. This has a lot of overhead and will reset embedded components like the SearchView. It took me quite a while to track down why my SearchView would "randomly" close.

I ended up capturing the menu as posted by Dark and then call onPrepareOptionsMenu(Menu) as necessary. This met my requirement without an nasty side effects. Gotcha: Make sure to do a null check in case you call onPrepareOptionsMenu() before the menu is created. I did this as below:

private Menu mOptionsMenu;

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    mOptionsMenu = menu
    ...
}

private void updateOptionsMenu() {
    if (mOptionsMenu != null) {
        onPrepareOptionsMenu(mOptionsMenu);
    }
}
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
Dustin
  • 2,064
  • 1
  • 16
  • 12
  • Awesome answer, For some reason I couldn't figure out which menu to pass to onPrepareOptionsMenu() as I wanted to change it depending on conditions that only happen when my service is started etc etc. Anyway – James W Feb 25 '14 at 22:10
  • I would agree that the overhead is a bit too much and very noticeable if using `invalidateOptionsMenu()`. I would prefer this solution to use less workload. – Nico Dumdum Oct 21 '14 at 07:48
  • 2
    Using this I keep getting null whenever screen is ratated. – levi Nov 19 '14 at 18:46
  • If you have a menu object, it is better than this solution –  Sep 03 '19 at 02:41
  • Don't we have any property or method in Activity to get MenuItems outside onCreateOptionsMenu()/onOptionsItemSelected()? – Nadimuddin Jan 27 '23 at 12:08
32

Call invalidateOptionsMenu() instead of passing menu object around.

Diwakar Bhatia
  • 321
  • 3
  • 2
15

you could do it by passing the Menu object to your Activity class

public class MainActivity extends Activity
{
    ...
    ...
    private Menu _menu = null;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        _menu = menu;
        return true;
    }

    private Menu getMenu()
    {
        //use it like this
        return _menu;
    }
}
Dark
  • 171
  • 1
  • 3
  • 2
    Call invalidateOptionsMenu() is better ! and you just have to provide your update in the method onPrepareOptionsMenu(Menu menu). – AntoineP Feb 15 '13 at 16:18
1

There several callback methods that provide menu as a parameter.

You might wanna manipulate it there.

For example:

onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
onCreateOptionsMenu(Menu menu)
onCreatePanelMenu(int featureId, Menu menu)

There several more, best you take a look in activity documentation and look for your desired method: http://developer.android.com/reference/android/app/Activity.html

Ostkontentitan
  • 6,930
  • 5
  • 53
  • 71
  • in your solution values are not up to date – Bob May 14 '12 at 09:25
  • dont know the exact behaivior but do you think saving the reference in a field variable would do it? Like: onCreateOptionsMenu(Menu menu){ this.menu = menu . . . . and access it asking for title later? – Ostkontentitan May 14 '12 at 09:44
  • I cant believe there is no method that simply returns the Menu object. Like how we can get context, window, layout inflater and more of the activity. –  Sep 03 '19 at 02:36
1

As far as I could understand what you want here is which may help you:

1. Refer this tutorial over option menu.

2. Every time user presses menu button you can check it's title thru getTitle().

3. Or if you want to know the last menu item checked or selected when user has not pressed the menu button then you need to store the preferences when user presses.

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
0

Android now has the Toolbar widget which was a Menu you can set/get. Set the Toolbar in your Activity with some variation of setSupportActionBar(Toolbar) for stuff like onCreateOptionsMenu from a Fragment for example. Thread revived!

Clocker
  • 1,316
  • 2
  • 19
  • 29