Android: When is onCreateOptionsMenu called during Activity lifecycle?
Asked Answered
P

6

155

I put a couple of breakpoints in onCreate (one at the beginning, and one at the end of the method), and I also put one at the beginning of onCreateOptionsMenu. The onCreate method is called first, and before it finishes onCreateOptionsMenu is called.

I'm trying to separate the Fragment navigation code in my app, so I have a couple of objects that I delegate onCreateOptionsMenu to depending on if the app is running on phone/tablet (I'm using screen size to determine this, my layout file for large screens has a View I check for after the layout is inflated). The problem I'm having is, I create these objects in onCreate, and I'm getting a null pointer exception when I reference the object in onCreateOptionsMenu.

Polash answered 9/10, 2011 at 19:20 Comment(0)
C
114

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.

(I'm using screen size to determine this, my layout file for large screens has a View I check for after the layout is inflated)

That test will break very shortly, once Ice Cream Sandwich ships. From what I can tell, ICS phones will have action bars (though perhaps not system bars).

Crouse answered 9/10, 2011 at 21:15 Comment(6)
I forgot to mention that I'm using the ActionbarSherlock library. Your answer reminded me. That's probably the reason for this behavior, since it's a wrapper on the compatibility library, which puts the menu items in the "ActionBar".Polash
@commonsware - That means on devices and apps which doesn't have action bar. Menu will show up even if onCreateOptionsMenu doesn't have item visible?Midnight
in my case onCreateMenu call after onResumeHaemocyte
Yes I have the same problem... But my code is related to a Fragment.Plugugly
I started having NPE when checking if the nav drawer fragment was open in onCreateOptionsMenu. I had to put null checks in both the activity's onCreateOptionsMenu, as well as the callback that the fragment was using in its onCreateOptionsMenu. Really weird because it was only happening on a couple of screens, but consistently on those.Lemures
onCreateOptionsMenu is called after onResume at least on Android Q (10, SDK 29). Watch out null pointer exception if you try to access menu views from within onResume.Ferree
M
61

In my case on Android 2.3 and with FragmentActivity from v4-support library the order of life-cycle methods invoke is following:

07-18 18:29:21.629  20183-20183/? I/onCreate:
07-18 18:29:21.719  20183-20183/? I/onStart: 
07-18 18:29:21.719  20183-20183/? I/onResume: 
07-18 18:29:21.739  20183-20183/? I/onCreateOptionsMenu:
Mimicry answered 18/7, 2013 at 15:33 Comment(0)
H
31

I found if in onResume() I call

invalidateOptionsMenu();

then onCreateOptionsMenu(Menu menu) is called afterward - as per the activity life cycle (I think that's the correct term here), as indicated by @tir38

@Override
public void onResume() {
    super.onResume();
    invalidateOptionsMenu();
}
Hypotension answered 25/3, 2014 at 3:36 Comment(6)
If u are using actionbarsherlock then call this method supportInvalidateOptionsMenu();Thrifty
Be careful when you say "immediately". It won't truly happen immediately. When you invalidateOptionsMenu, a job to (re)create the options menu will get added to the UI's message queue. Whatever else is in the queue will get run first.Unmeet
Its not perfect solution becuase I am still getting nullpoint exception error after adding invalidateOptionsMenu in onResume.Fib
@Fib you may consider posting a question with the minimal code to reproduce the errorHypotension
@GeneBo I am trying access mOptionsMenu.findItem(R.id.menu_favorite).setIcon(R.drawable.ic_heart_filled); in onCreate but it gives null pointer exception, even I putted invalidateOptionsMenu in onResume.Fib
I would suggest creating a new post with the formatted code listing so we can try to reproduce the error in local dev env. From there we can take a look at the issue first-hand and see about troubleshooting itHypotension
R
21

Addition in above answer, In case of ICS and Honeycomb onCreateOptionsMenu is called after onCreate and onPostCreate while in Gingerbread and earlier versions it is called after onCreate but before onPostCreate. Thats the only difference I found.

Romish answered 11/5, 2012 at 7:53 Comment(0)
T
3

In my experience ActionBarActivity from support v7 onCreateOptionsMenu() called in setContentView() method in the middle of onCreate() it is appear on 4.1.1.

But on 4.4 another story onCreateOptionMenu() called after onCreate(). Also I don't know it may be immediately after, maybe not. But is fact after. I didn't test on other versions but 4.1.1 is first where I had have a trouble with init order.

Triceps answered 21/4, 2014 at 20:30 Comment(1)
Perhaps you need to use supportInvalidateOptionsMenu()?Lucan
N
2

i suggest to create a callback-function in your fragment to avoid timing issues with onResume() and onCreateOptionsMenu().

doing the following works flawless for me:

  1. create and add your fragment to your activity
  2. leave a reference of this fragment in your activity
  3. create a public method doSomethingWithTheMenu() in your fragment
  4. call this method from within your activity when onCreateOptionsMenu(Menu menu) is called.

example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (this.myFragment != null) {
        this.myFragment.doSomethingWithTheMenu(menu);
    }
    return true;
}
Nazarius answered 1/10, 2014 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.