When does onCreateOptionsMenu happen in an ActionBar enabled activity? - android

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 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?

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

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

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);

Related

How do I only trigger onPrepareOptionsMenu and not onCreateOptionsMenu?

According to the Android menu documentation and a similar question, onPrepareOptionsMenu should be used to update menus dynamically. For Android 3.0 and higher, the documentation says:
When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu()
However, when calling invalidateOptionsMenu(), the system calls both, onCreateOptionsMenu() and onPrepareOptionsMenu(). Resuming the activity also either calls both methods (if it was stopped in background) or none.
To me, this sounds like the differentiation between the two methods is obsolete. Is there any situation in which Android only calls onPrepareOptionsMenu() but not onCreateOptionsMenu()? Additionally, is there a way to manually request that Android only calls onPrepareOptionsMenu(), like it is implied in the documentation (cited below)?
[onCreateOptionsMenu()] is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).
Thank you very much!
You are using Activity menu here, so assuming you have single activity app it will be only inflated once, during onCreateOptionsMenu - that's the scenario with a single call.
If you want to change it, then you have to invalidate the current one and inflate a new one - as it's an app's global menu it only make sense if you want to change its contents - you do that using onPrepareOptionsMenu and then inflate it again in onCreateOptionsMenu call.
But if you make lots of menu changes then it amy be better to use to use Fragment specific menus - they only live as long as the their fragment - menus
So to answer your specific question - you cannot NOT trigger the onCreateOptionsMenu. You can override it to do absolutely nothing but then the menu will not be inflated
EDIT - it seems I misunderstood the documentation as well. The key bit for onPrepareOptionsMenu is:
This is called right before the menu is shown, every time it is shown.
Which in practice means that every time you click on the Menu onPrepareOptionsMenu will be called, without calling onCreateOptionsMenu

Android ActoinBar homeAsUp only works once

I'm working on an app that only uses a single Activity and switches out fragments as they are needed with the navigation drawer.
Now we want to navigate back from one of these fragments by using the homeAsUp button in the ActionBar.
I have followed all the steps to set the button up. From disabling the navigation drawer setDrawerIndicatorEnabled(false) and calling setDisplayHomeAsUpEnabled(true) in the fragment's onCreateView().
I also set setHomeButtonEnabled(true) in the MainActivity's onCreate() however because the app is already in the MainActivity, we cannot specify a Parent Activity.
Whenever I run a fresh install of the app, the homeAsUp button works and is registered in the onBackPressed(), not onOptionsItemSelected() method. However, when I close the app and run it again the button does not even register clicks.
In onBackPressed() I check a few conditions, but it does not block the button press.
In onOptionsItemSelected() I check for android.R.id.home.
Unfortunately, I cannot post the code.
This post describes what I'm trying to achieve: Switching between Android Navigation Drawer image and Up caret when using fragments
I managed to fix the issue I was experiencing. It was a very simple mistake.
Because I'm not the original author of this code, I went through the MainActivity thoroughly.
As it turns out the original author called setDisplayHomeAsUpEnabled in the onCreate function (which is extremely long), but near the end he also called setSupportActionBar, which made the first call of setDisplayHomeAsUpEnabled useless. Moving setDisplayHomeAsUpEnabled below setSupportActionBar fixed my problem.
If you did everything correctly, make sure that your code is written in the correct order.
Furthermore, if you use custom toolbars in other fragments, remember to set your original Support Action Bar by calling setSupportActionBar(toolbar.find(this)) in onResume of your MainActivity.

Closing Options Menu on orientation change in Android

this is my first post here so hello everyone and forgive me any mistakes common for newcomers.
In my actual Android 4.0 project im using Options Menu which is opened by clicking on one of ActionBar items. Items of my menu are asynchronously updated so in some cases it opens before changes has been made and this is proper behaviour for me. As we know, after user changes an orientation of the device, whole activity is recreated (same for its menu). This use case is properly handled in my code - state of Activity is saved.
Problem happens when user opens Options Menu and changes orientation when menu is still visible - menu gets recreated and shown. I would like to make it not appear after it has been created.
Is it even possible? I assume that i should do something either in onCreateOptionsMenu() or in onPrepareOptionsMenu() method.
You could use closeOptionsMenu() to close your OptionsMenu manually. If you want to open it at any later time, you could call openOptionsMenu().
I can think of a workaround. set a class variable true in onRestoreInstanceState. override onPrepareOptionsMenu return false if that variable is true and set that variable to false again. It should give you the behavior you want. comment if there's any problem, I'll post the code.

Options menu not showing up in Android

My objective is to you use one menu for all activities. For that, I have a base activity which consists of 2 methods: onCreateOptionsMenu() and onOptionsItemSelected(). In onCreateOptionsMenu(), I am creating a menu using MenuInflater.
Further, I have 2 activities which extends the above BaseActivity so that same menu is shown for both the activities. My issue is that, when my first activity is launched, options menu is shown, I move to second activity from the first. In the second activity also, when I press the menu button, I am able to view the menu. After that, using Back key press, I come to first activity again, the menu is also shown up there, But when I move to the second activity thereafter, menu is not shown to me.
Can you please post code of your Base Activity's onOptionsItemSelected and onCreateOptionsMenu?
Anyway, with no code available. And not enough clarity, I assume that the following will work for you...
add #Override
public void onBackPressed() {
finish();
}
to your base activity
Minimum SDK version could be the cause. If you reduce it to 13-, you should probably see the menu show up again. Good article on this subject: POST

Android searchView triggering onOptionsItemSelected

I'm following this guide to add an actionbar SearchView widget to my activity: http://developer.android.com/training/search/setup.html
I've got it working except on this page: http://developer.android.com/training/search/backward-compat.html
it says that "only versions older than 3.0 call onOptionsItemSelected() when the user selects the search menu item" so I can call onSearchRequested() in onOptionsItemSelected when this search button is pressed for older devices. But when I test that on Jellybean, I press the search button and it expands the searchview but also opens a search dialog over, so I have two search widgets on top of each other.
It seems like onOptionsItemSelected() is still being called when the search button is pressed on a 3.0+ device, but the guide says otherwise. Am I implementing it wrong? I tried making a new project and following the guide exactly, and it has the same behaviour.
This was a misinterpretation on my part. I thought the documentation was saying that the hardware search button would not trigger onOptionsItemSelected and would instead expand the searchView. But it actually just says that the search menu option will not trigger onOptionsItemSelected
To fix this I overrode onSearchRequested and expanded the searchView there

Categories

Resources