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
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.
If you've developed for Android 3.0 and higher, the system calls onCreateOptionsMenu() when starting the activity, in order to show items to the app bar.
onStop(): Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With