When will the method "onPrepareOptionsMenu" be called? - android

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.

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.

How to change ActionBar Item depending on Fragment?

I am trying to create a selected and unselected Item on a the ActionBar in my app. For example, when the user is in a fragment if they click the Item in the ActionBar, the ImageView will switch from a grey to a red square and if they switch to a different fragment then come back it will still be selected. So each fragment can either be selected or unselected based upon the color of the square and if the user clicked it or not. I am unsure how to implement this, anything helps?
Based on the location of the item in the ActionBar, this can be presented as a fragment option. Each fragment that gets inflated has the chance to inflate its own menu items into the action bar. This can be done by calling setHasOptionsMenu(true) in fragment's onCreate or maybe in the constructor. This will cause the onCreateOptionsMenu / onPrepareOptionsMenu / onOptionsItemSelected methods to be called and this gives you the chance to add your menu item which you can control based on your fragment's state.
First off, for saving state in a fragment, you can use onSaveInstanceState() to remember your selected state. You would need a property on your fragment:
private boolean mSelected;
In your onSaveInstanceState() override you save this value:
outState.putBoolean("selected", mSelected);
Then in onCreate() you can recover the value:
if (savedInstanceState != null) {
mSelected = savedInstanceState.getBoolean("selected");
}
And in onCreateView() you use the selected property to set up your ImageView:
imageView.setBackground(mSelected ? R.color.gray : R.color.red); // for example
So now when your user switches away from the fragment then comes back, the ImageView will have the same selection state as before.
Next thing is the action bar. Fragments can make their own changes to the menu. Start by calling setHasMenuOptions(true) in your fragment's onCreateView(). Then you override onCreateOptionsMenu() and onOptionsItemSelected() just like you would for an activity.

using ActionBar, How and when onPrepareOptionsMenu() invoked?

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.

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