When does onCreateOptionsMenu happen in an ActionBar enabled activity?

virsir picture virsir · Feb 29, 2012 · Viewed 17.5k times · Source

I know the menu item will be set as action icons in the ActionBar.

I want to know exactly this onCreateOptionsMenu function, when does it called in the activity lifecycle.

From my test, it does not even after onResume

Answer

unbekant picture unbekant · Feb 29, 2012

The documentation says the following:

public boolean onCreateOptionsMenu (Menu menu)

Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu. This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).

Further explanation here: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29

And quoting what CommonsWare put on another related question:

The onCreate method is called first, and before it finishes onCreateOptionsMenu is called.

That will be true on devices and apps with an official Honeycomb-style action bar. If there is no action bar, onCreateOptionsMenu() should not get called until the user calls up the menu, typically by pressing the MENU button.

Link here: Android: When is onCreateOptionsMenu called during Activity lifecycle?