How to update a menu item shown in the ActionBar? How to update a menu item shown in the ActionBar? android android

How to update a menu item shown in the ActionBar?


Option #1: Try invalidateOptionsMenu(). I don't know if this will force an immediate redraw of the action bar or not.

Option #2: See if getActionView() returns anything for the affected MenuItem. It is possible that showAsAction simply automatically creates action views for you. If so, you can presumably enable/disable that View.

I can't seem to find a way to get the currently set Menu to manipulate it except for in onPrepareOptionMenu.

You can hang onto the Menu object you were handed in onCreateOptionsMenu(). Quoting the docs:

You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.


in my case: invalidateOptionsMenu just re-setted the text to the original one,but directly accessing the menu item and re-writing the desire text worked without problems:

if (mnuTopMenuActionBar_ != null) {    MenuItem mnuPageIndex = mnuTopMenuActionBar_        .findItem(R.id.menu_magazin_pageOfPage_text);    if (mnuPageIndex != null) {        if (getScreenOrientation() == 1) {            mnuPageIndex.setTitle((i + 1) + " von " + pages);        }        else {            mnuPageIndex.setTitle(                (i + 1) + " + " + (i + 2) + " " + " von " + pages);        }        // invalidateOptionsMenu();    }}

due to the comment below, I was able to access the menu item via the following code:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.magazine_swipe_activity, menu);    mnuTopMenuActionBar_ = menu;    return true;}


To refresh menu from Fragment simply call:

getActivity().invalidateOptionsMenu();