For some reason, onCreateOptionsMenu() is called AFTER onResume() in my app… Therefore, I just can’t get a hold of the menu while I’m setting up my UI (between onCreate() and onResume()), which results in not being able to setup the corresponding action items for my ActionBar…
The only work-around I’ve found so far is to manually call invalidateOptionsMenu() right before onCreate() returns; that way onCreateOptionsMenu() is immediately called, I get a hold of the menu and then I can finally add the desired action items.
Has anyone experienced this issue? How are you supposed to programmatically setup your action items given onCreateOptionsMenu() is called after onResume()?
My app is running on JellyBean, it uses the built-in ActionBar (no ActionBarSherlock), android:minSdkVersion="14" and android:targetSdkVersion="16"
First consider that perhaps you shouldn’t be doing this. It sounds like your idea might go against typical design patterns for Android. If your menu is changing in response to a user selection, for example, you should use contextual action mode instead.
From the Action Bar API Guide:
From the Menu API Guide:
Barring that, if you do want to change the menu items as you have described, you should make the change in
onPrepareOptionsMenu(). When the event occurs that requires changing the menu items, put the relevant information into a field and callinvalidateOptionsMenu(). OverrideonPrepareOptionsMenu()and check the value of the field to determine which menu items to add/remove.(It would also work to call
invalidateOptionsMenu()and overrideonCreateOptionsMenu()to modify which menu items should be shown, although this approach is not recommended.)More from the Menu API Guide: