76

I used ActivityCompat.invalidateOptionsMenu(MainActivity.this); so that my menu item "refresh" can automatically be enabled/disabled without the using have to touch the "Menu" option (imagine the user leaves the Menu open... I need the "Refresh" menu item to automatically disabled and enable itself).

The ActivityCompat.invalidateOptionsMenu(MainActivity.this) works fine in Android 11+. But what can I use for android API < 11 ? :S I've searched so much but I cannot find an answer. Can anyone please help me on this?

This works fine in Android API 11+, using the onPrepareOptionsMenu and ActivityCompat.invalidateOptionsMenu(MainActivity.this). The issue is trying to get it done in Android API < 11.

Here is my onPrepareOptionsMenu method:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(menuRefreshEnable){
        menu.getItem(0).setEnabled(true);
    }
    if(!menuRefreshEnable){
        menu.getItem(0).setEnabled(false);
    }       
    return true;
}
Czechnology
  • 14,832
  • 10
  • 62
  • 88
SnitramSD
  • 1,169
  • 3
  • 10
  • 18
  • 1
    I'd suggest you to change your method to smth like @Override public boolean onPrepareOptionsMenu(Menu menu) { menu.getItem(0).setEnabled(menuRefreshEnable); return true; } – Alexander Zhak Nov 27 '13 at 14:16
  • possible duplicate of [How to make a conditional invalidateOptionsMenu() call depending on the API level in Android?](http://stackoverflow.com/questions/7878957/how-to-make-a-conditional-invalidateoptionsmenu-call-depending-on-the-api-leve) – JJD Dec 05 '14 at 15:00

3 Answers3

231

On API < 11 use supportInvalidateOptionsMenu() method

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
5

ActivityCompat.invalidateOptionsMenu() doesn't callback onPrepareOptionsMenu(); it just update the menu directly. Just put some Log.d() and check out by yourself.

This works for me (I'm using API 8):

private Menu mMenu;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.track_fragment, menu);
    mMenu = menu;
    }
...
private void someMethod() {
...
    if (mMenu != null) {
       MenuItem item = mMenu.findItem(R.id.new_track);
       if (item != null) {
            item.setVisible(false);
            ActivityCompat.invalidateOptionsMenu(this.getActivity());
        }
    }
...
}

My someMethod() get called from several places, even before onCreateOptionsMenu(), so I must check mMenu != null.

Sufian
  • 6,405
  • 16
  • 66
  • 120
0

This will return true if the API is above or equal to 11 and therefore supported. Before API 11, the menu is drawn when the menu button is pressed so there is no need for this method as it occurs automatically.

AndroidPenguin
  • 3,445
  • 2
  • 21
  • 42
  • 1
    I need the menu items to be updated without user interaction with the menu button. Imagine that the user leaves the menu open while my async task is running... With the method the menu itens are automatically updated in api 11+. If I remove it, it doesn't work. But I still need the automatic update of the menu itens in android api < 11. – SnitramSD Dec 23 '12 at 09:47
  • 2
    When my async tasks finishes I execute the line ActivityCompat.invalidateOptionsMenu(MainActivity.this); in order to update the menu itens. But it only works in api 11+. How can I force the menu items to be updated in api < 11 without user interaction with the menu button (something similar to ActivityCompat.invalidateOptionsMenu(MainActivity.this); in api < 11)? – SnitramSD Dec 23 '12 at 09:48
  • Pretty much by programatically pressing the menu button twice on devices <11. – AndroidPenguin Dec 23 '12 at 16:16