using ActionBar, How and when onPrepareOptionsMenu() invoked? - android

How and when overridden method onPrepareOptionsMenu() called ?
i have seen, onPrepareOptionsMenu() will be called automatically after onCreateOptionsMenu().
And if i called invalidateOptionsMenu() on any action-item click event, then , it will be called like ,
onPrepareOptionsMenu() -> onCreateOptionsMenu() -> onPrepareOptionsMenu()
Why onPrepareOptionsMenu() is called twice ?
And
My question is how to update ActionBar item at Runtime, on two different action-item click event ? Otherwise it is called same implementation as written with in onPrepareOptionsMenu().

In Android 3.0+ the Action Bar forces the options menu to be built early so that items chosen to show as actions may be displayed when the activity first becomes visible. The Activity method invalidateOptionsMenu forces the entire menu to be destroyed and recreated from onCreateOptionsMenu(android.view.Menu), offering a similar though heavier-weight opportunity to change the menu's contents. Normally this functionality is used to support a changing configuration of Fragments.
You can call invalidateOptionsMenu to recreate actionbar menu if you want to change/update actionbar menu items, such as make some menu item VISIBLE/INVISIBLE.

in my case , i want to load dynamic content for more than one Action-item click event. No need to override onPrepareOptionsMenu(), instead i have instantiating flag value on onOptionsItemSelected and called invalidateOptionsMenu , So it will call onCreateOptionsMenu on every occurrence of invalidateOptionsMenu. And make conditions accordingly in onCreateOptionsMenu.This solved the issue.

Related

onPrepareOptionsMenu called after onCreateView

I have fragment which shows menu on action bar.
onPrepareOptionsMenu() is getting called after onCreateView()
Is that normal behavior? Is there any documentation that show this kind of stuff?
Yes its normal since onCreateView() first inflate the layout and then you can prepare the menu items and add listener to it.

When will the method "onPrepareOptionsMenu" be called?

I create a menu.xml , only one action item with the "always" inside it.
now I want to use the this method to change the menu's visible ,remove old action item , add new action item , change the old action item's icon ...
so how to implement this ?
I don't want the onCreateOptionMenu to be called more than once as the activity has presented by Android system.
onPrepareOptionsMenu() will be called automatically after onCreateOptionsMenu().
And if you called invalidateOptionsMenu() on any action-item click event, then, it will be called again.

Android - Settings menu of the main activity

I have a settings menu item that is generated in MainActivity. All the MainActivity's job is to contain fragments that actually do the job. Those fragments have their own action bar items and they work as expected.
The settings menu item should show/hide, depending on a certain condition that is changing between fragments, so assuming the code is on MainActivity, it should be:
if (condition) menu.findItem(R.id.action_settings).setVisible(false);
else menu.findItem(R.id.action_settings).setVisible(true);
Now I'm wondering where should I place the code because the condition changes between fragments but the actual menu item is on MainActivity.
Your condition might be OK and you can put it in your Activity's onPrepareOptionsMenu().
As you said this gets called just one time when you enter the Activity (with some exceptions).
You need to force the system to recall the Activity onPrepareOptionsMenu() when switching through fragments, and that is done - from anywhere - with the line invalidateOptionsMenu() (or, if necessary, getActivity().invalidateOptionsMenu()).
Good points to paste it might be Fragments onResume(), or any other point at which you think your condition state has changed.
Depending on your libraries, there's also a support method for API<11 called supportInvalidateOptionsMenu().
This should be called each time you want your Menu to change. However, if your fragments just add some items to a base, activity menu, take a look at this.
i think it is gonna work for you
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

Android: hide action bar menu views regardless the menu items

I am using navigation drawer to switch between fragments.
And my action bar menu items are different regarding to the fragments.
I want to hide the menu item views when drawer is opened.
Referring to the doc, it suggest to find the menu item by ID and hide the item.
However, my menu items are different with fragments, so, how can I simply hide/show the menu item views on the action bar? Is there some flag to control it?
By the way, I see in the DOC it says I can return false in onPrepareOptionsMenu() to make it not displayed, however I tried and ended up in vain. Did I misunderstand it?
public boolean onPrepareOptionsMenu (Menu menu)
Added in API level 1
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
The default implementation updates the system menu items based on the activity's state. Deriving classes should always call through to the base class implementation.
Parameters
menu The options menu as last shown or first initialized by onCreateOptionsMenu().
Returns
You must return true for the menu to be displayed; if you return false it will not be shown.
If people are still visiting the link, this is how we disable the options menu or options menu items for a fragment.
1) In the onCreateView() method of fragment add the following code
setHasOptionsMenu(true);// then only we can work with the menu items.Without this onPrepareOptionsMenu() method is not called
.
Now override the onPreparedOptionsMenu() method and hide the menu items. If you hide all the menu items the menu will not at all be shown. or use menu.clear() to clear all the menu items
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_contacts).setVisible(false);
--------------
}

Different menu for different fragments

I have an activity which has 2 fragments.
1 fragment is visible at a time and each fragment has a different option menu.
I can achieve this behavior by 2 different ways.
1 - I can add different menu for each fragment by calling onCreateOptionsMenu in each friengment.
2 - I can have only one menu at activity level and can select to show particular option in onPrepareOptionsMenu
What I want to know is:
Which is the preferable way to implement this functionality?
What is recommended?
Hope this helps
Adding items to the Action Bar
Your fragments can contribute menu items to the activity's Options Menu (and, consequently, the Action Bar) by implementing onCreateOptionsMenu(). In order for this method to receive calls, however, you must call setHasOptionsMenu() during onCreate(), to indicate that the fragment would like to add items to the Options Menu (otherwise, the fragment will not receive a call to onCreateOptionsMenu()).
Any items that you then add to the Options Menu from the fragment are appended to the existing menu items. The fragment also receives callbacks to onOptionsItemSelected() when a menu item is selected.
You can also register a view in your fragment layout to provide a context menu by calling registerForContextMenu(). When the user opens the context menu, the fragment receives a call to onCreateContextMenu(). When the user selects an item, the fragment receives a call to onContextItemSelected().
http://developer.android.com/guide/components/fragments.html
I would follow the first option as having a dedicated resource menu for each fragment seems cleaner and also reduces the code complexity you would have in order to maintain what is visible and what is not (if you would go through onPrepareOptionsMenu and have code to hide & show different menus).
If you have some actions in your fragments, then you could create a base fragment class that each of your fragments would extend from.

Categories

Resources