Within my apps I often enable/disable menu entries and do make them visible from onPrepareOptionsMenu.
Today I started to add the android:showAsAction menu attribute
Kudos to @Klaasvaak for showing us the way here. I use the following which works on both pre- and post- API Level 11:
private void invalidOptionsMenuHelper() {
if (Build.VERSION.SDK_INT >= 11) {
invalidateOptionsMenu();
} else if (mOptionsMenu != null) {
mOptionsMenu.clear();
onCreateOptionsMenu(mOptionsMenu);
}
}
Of course, you must save a reference to the menu (mOptionsMenu in this case) which I accomplish via:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
mOptionsMenu = menu;
//... create and manage the menu normally
}