When does onCreateOptionsMenu happen in an ActionBar enabled activity?
Solution 1
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?
Solution 2
In my tests I discover that onCreateOptionsMenu is called after onResume as you can see too in this complete diagram of the lifecycle:
virsir
Updated on July 14, 2022Comments
-
virsir almost 2 years
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
-
virsir about 12 yearsFrom my test, it does not even after onResume
-
FabianCook about 12 yearsI dont believe it will be called after onResume, the menu can only be created once, to change it you need to updating it using
invalidateOptionsMenu()
to request the system to callonPrepareOptionsMenu()