Search code examples
androidmenuandroid-actionbar

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


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?


Solution

  • 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.