How do I only trigger onPrepareOptionsMenu and not onCreateOptionsMenu? - android

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

Related

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.

Android OnPrepareOptionsMenu does not work as expected on API 16

In my app I have one fragment that I add by calling replace() on fragmentTransaction.
This fragment, let´s call it Fragment1, has some menu items that are added to action bar by calling onCreateOptionsMenu and setHasOptionsMenu(true) in onCreate().
From this fragment I then add another fragment that is added by calling add() on FragmentTransaction.
This fragment2 also has options menu but removes options for the first fragment. Then, on API 19 when I click on Back Button, it takes me back to Fragment1 and the options for this menu are added back, since the activity that hosts both of these fragments calls onCreateOptionsMenu and then onPrepare options menu. Also the same methods are called in Fragment1.
When I run my app on API 16, all the mentioned methods are called as well, but either the Menu Items for Fragment1 are not added back at all, or the three-dot-menu-group is added to a wrong place. I assume there is something different in the lower API, maybe even a bug. How should I fix this issue? Anyone with some experience?
This one is what it looks like when going back from the second fragment ot the first one and what it should look like on API 16:
This is how it is supposed to work and what it looks like on API 19 after going back from the second fragment to the first one:
I don´t attach any code because I think it is not relevant for this question. There is just some different behavior in different platforms and I just don´t see what I should do about it. Thanks for any help.

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.

Clearing an ActionItem in refresh code

I am trying to clear an ActionItem in refresh code. I am using the ActiomBarSherlock component and I have my action item filtering but want to clear it when not in a menu driven event.
Can someone help me with the proper way to access it without holding onto my menu in a global variable? I tried to just reference the TextView directly but it is returning null.
There is nothing wrong with keeping a reference to a MenuItem to interact with it at a later time. You can also call invalidateOptionsMenu so that onCreateOptionsMenu will be called again and you can update the menu state.

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

Categories

Resources