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);
Related
How can i make the fragments on the backstack not to trigger the onOptionsItemSelected(). Because every time i create a new fragment it always trigger the onOptionsItemSelected() if i select an item in menu. Also this fragment will be reused throughout the activity so "return true" on onOptionsItemSelected() is not an option because the fragment that was created still the same with the backstack fragment.
It appears that onOptionsItemSelected will always be called in every new fragment that is added in activity Menu documentation , so to solve my problem is that I added a validation which appears this one
<CurrentFragment>this.equals(fragmentManager.findFragmentById(R.id.frame_container) to validate if the fragment displayed/ attached in activity is the fragment that trigger the onOptionsItemSelected.
Hope this will help to anyone who will have this kind of behaviour on fragments.
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.
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.
I'm having trouble getting onPrepareOptionsMenu() to work correctly. No matter what I try, I can't get it to be called.
The architecture of my application is as follows: my main Activity contains a Fragment which holds a ViewPager, which in turn has four child Fragment instances. Currently, I'm (successfully) populating the initial options menu in the Activity with onCreateOptionsMenu(), but I wish to add some extra options corresponding to each active child Fragment.
Within my child Fragments, I've made sure that setHasOptionsMenu(true) is being called in onCreate() and that getSherlockActivity().supportInvalidateOptionsMenu() is called in onActivityCreated(). In the main Activity, onCreateOptionsMenu() is called once after the child Fragments are instantiated, regardless of whether they contain supportInvalidateOptionsMenu() or not. It is subsequently called again whenever the Fragment in the Pager is replaced.
I've tried moving the onPrepareOptionsMenu() code up a level to the parent Fragment and to the main Activity - to no avail, which suggests that the problem is not related to my use of nested Fragments. I've also tried moving the onCreateOptionsMenu() method down from the main Activity to both fragments, where it goes uncalled.
If anyone has any idea what's going on here I'd be very grateful, as I've been stuck on this for two days now.
UPDATE - I've worked out exactly what the problem is now, only I'm still not sure how to fix it. I'm testing on 4.2 and it seems that onCreateOptionsMenu() is called after onResume() in the Activity - as seen in onCreateOptionsMenu called after onResume on JB 4.2. I assume this means that fragments can't call onPrepareOptionsMenu() because the menu doesn't even exist until after they're instantiated. So how do I solve this?
It was, somewhat inevitably, an issue with ActionBarSherlock that I'd overlooked when checking its issues page on github. Basically, the functionality for options menus in nested fragments has not yet been added, see: https://github.com/JakeWharton/ActionBarSherlock/issues/828. My solution for now is to replace Watson.java with one of the modified files in the comments.