Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does onCreateOptionsMenu happen in an ActionBar enabled activity?

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

like image 993
virsir Avatar asked Feb 29 '12 01:02

virsir


People also ask

When onCreateOptionsMenu is called?

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.

What is onCreateOptionsMenu in Android?

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.

When a new activity comes on top of your activity completely then what is the life cycle function that gets executed in the Old activity?

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.

Why do we need to call setContentView () in onCreate () of activity class?

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.


1 Answers

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?

like image 55
unbekant Avatar answered Oct 21 '22 14:10

unbekant