How to make a conditional invalidateOptionsMenu() call depending on the API level in Android?

Solution 1:

Use

ActivityCompat.invalidateOptionsMenu(Activity activity)

from the compatibility library.

Solution 2:

For the others who are looking for an answer like I was:

If you are using ActionBarSherlock and trying to refresh the action bar buttons on API <11, instead of

Activity.invalidateOptionsMenu()

you can use

SherlockActivity.supportInvalidateOptionsMenu():

Solution 3:

If you are extending ActionBarActivity in your class then you just need this:

supportInvalidateOptionsMenu();

Solution 4:

I dont think there is any need for compatibility library and so on, just do a simple

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    invalidateOptionsMenu(); 
}

You dont need to call it before honeycomb since afaik, onPrepareOptionsMenu() called when menu button is pressed. It works for me.