When and how often is the onPrepareOptionsMenu() method called for ActionBar?

Eugene picture Eugene · Oct 5, 2011 · Viewed 17k times · Source

It is clear for Activities - the method is being called right before the menu is shown. But what about ActionBar when it is always shown? How to trigger it to be invoked to dynamically update menuItems?

Answer

PH7 picture PH7 · Oct 5, 2011

Use invalidateOptionsMenu() to trigger onPrepareOptionMenu() method as suggested by document.

On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items.