Android invalidateOptionsMenu() for API < 11 Android invalidateOptionsMenu() for API < 11 android android

Android invalidateOptionsMenu() for API < 11


On API < 11 use supportInvalidateOptionsMenu() method


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;@Overridepublic 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.


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.