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
Based on "Klaasvaak" answer above. I am using its subMenus. This works for me :
// Declare and save the menu as global, so it can be called anywhere.
Menu absTopSubMenus;
public boolean onCreateOptionsMenu(Menu menu) {
absTopSubMenus = menu; // Used for re-drawing this menu anywhere in the codes.
// The remainder of your code below
}
Then, to re-draw this, just call :
// Redraw the top sub-menu
absTopSubMenus.clear();
onCreateOptionsMenu(absTopSubMenus);
save a reference to the menu and call:
this.menu.clear();
this.onCreateOptionsMenu(this.menu);
You can now use supportInvalidateOptionsMenu() method from the ActionbarActivity with the support library. So you don't have to check for the version of the sdk. Goes until API 7 and above.
http://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html
do to so, you have to import the v7 of the support library using this
http://developer.android.com/tools/support-library/setup.html
Use
ActivityCompat.invalidateOptionsMenu(Activity activity)
from the compatibility library.
See: https://stackoverflow.com/a/14748687/435855