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.
Related
For some reason my Fragment never calls onCreateOptionsMenu to inflate my menu, the overflow menu never appears and pressing the menu button in the emulator also does nothing.
And I tried to put setHasOptionsMenu(true) once in onCreateView but nothing happened after this I tried to put ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar) in the same Function but nothing happened again?
you have to Write both of the lines together to make it work in onCreateView Function inside your fragment.
The Lines:
setHasOptionsMenu(true);
((AppCompatActivity)getActivity()).setSupportActionBar(your_toolbar);
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.
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.
I have a 3 page Fragment in my app, but I need each fragment to have a different ActionBar. For one fragment I have set the current code in the OnCreateView method that adds in an EditText to the ActionBar:
//mainActivityContext is the context for the Main Activity (This is a fragment file)
ActionBar actionBar = mainActivityContext.getActionBar();
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
Yet this EditText stays persistent throughout all of the ActionBar menus. I only want it on one. I have tried everything, menu.clear();, setHasOptionsMenu(true);, inflater.inflate(R.menu.different_file, menu);, but nothing has worked.
Any help?
There is a very good approach to go about this situation:
on each fragment's onActivityCreated() method call setHasOptionsMenu(true);
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
now you can override onCreateOptionsMenu() and onOptionsItemSelected() inside each fragment.
And also dont forget to call getActivity().supportInvalidateOptionsMenu(); inside onResume() of fragment.
I think this sample by google will be very helpful.
Since the actions are populated by the activity's options menu you can use Activity#invalidateOptionsMenu(). This will dump the current menu and call your activity's onCreateOptionsMenu/onPrepareOptionsMenu methods again to rebuild it.
If you're using action bar tabs to change your fragment configuration there's a better way. Have each fragment manage its own portion of the menu. These fragments should call setHasOptionsMenu(true). When fragments that have options menu items are added or removed the system will automatically invalidate the options menu and call to each fragment's onCreateOptionsMenu/onPrepareOptionsMenu methods in addition to the activity's. This way each fragment can manage its own items and you don't need to worry about performing menu switching by hand.
I am using a Custom FragmentPagerAdapter for SwipeViews in ActionBar.NAVIGATION_MODE_TABS mode.
I want the Fragments inside the adapter to provide their custom Menus. But I observed that onCreateOptionsMenu() method inside the Fragment is not getting called even when I used setHasOptionsMenu(true);
inside the onCreate() callback of the Fragment.
In short,How can I get custom Options Menu for each Fragment in ViewPager?
Please write the code in onCreateOptionsMenu & onPrepareOptionsMenu of your activity to inflate the from menu xml. You can then customize the menu in your fragment by using these functions inside your fragment again. I tried it and it worked for me.
Actually your problem seems to lie in ViewPager. Either its not instantiated properly or its Visibility is set to Visibility.GONE.
Make sure that ViewPager is Visible on screen by default or there is no code which changes its visibility. Get back to me if problem persists.