10

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

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
virsir
  • 15,159
  • 25
  • 75
  • 109
  • Just a Google search on "onCreateOptionsMenu is called" retrieves this http://stackoverflow.com/questions/7705927/android-when-is-oncreateoptionsmenu-called-during-activity-lifecycle – Cengiz Can Feb 29 '12 at 01:42

4 Answers4

8

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?

Community
  • 1
  • 1
unbekant
  • 1,555
  • 22
  • 31
1

In my tests I discover that onCreateOptionsMenu is called after onResume as you can see too in this complete diagram of the lifecycle:

https://raw.githubusercontent.com/xxv/android-lifecycle/master/complete_android_fragment_lifecycle.png

0

This is called the first time you touch the "options" dedicated button.

I'm trying to figure out when it's called on ActionBar supported also.

Also, you can request activity to do it, (but you need a Menu stub implementation)

activity.onCreatePanelMenu(Window.FEATURE_OPTIONS_PANEL, menu);
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

I believe it is called at the same time as onCreate, just before the menu appears, in this case the actionbar

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • 3
    From my test, it does not even after onResume – virsir Feb 29 '12 at 01:40
  • 4
    I 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 call `onPrepareOptionsMenu()` – FabianCook Feb 29 '12 at 01:43