Menu in Fragments not showing
Solution 1
I added setHasOptionsMenu(true) in onCreateView and it works fine
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.my_example_fragment, container, false);
}
Solution 2
Use menu.clear() before inflating menus.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.mnulayout, menu);
}
Solution 3
first of all your fragment has to have in the onCreate
setHasOptionsMenu(true);
and then you has to check that your activity DONT have the NoActionBar like theme
its a little of tricky but if your activity has that theme doesn't matter where you call the setHasOptionsMenu(true)
you fragment wont paint her
Solution 4
I had the same problem. I fixed it by using the showAsAction in the item applied to myApp context:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_refresh"
android:icon="@drawable/ic_refresh"
android:title="refresh"
android:visible="true"
myapp:showAsAction="ifRoom|withText"/>
</menu>
Sorry it is too late for you, but it may be useful for other people.
Solution 5
This actually works for me and could be your solution as well.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
El Duderino
Updated on January 19, 2022Comments
-
El Duderino over 2 years
I got pretty helpful hints to my last questions, I hope it won't be different this time :) First, let me explain the situation. I have an activity with 2 fragments. For each fragment I have a xml menu layout, the first has four menu entries, the second one has only one. Now at first the fragment with the four menu entries is loaded. In its onCreate method I set
this.setHasOptionsMenu(true);
the onCreateOptionsMenu method
@Override public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.menu_fragshow, menu); }
The xml file looks like this :
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_refresh" android:icon="@drawable/ic_action_search" android:title="Refresh" android:visible="true" android:showAsAction="ifRoom|withText" /> <item android:id="@+id/menu_clearall" android:icon="@drawable/ic_action_search" android:title="Clear" android:visible="true" android:showAsAction="ifRoom|withText" /> <item android:id="@+id/menu_addcontent" android:icon="@drawable/ic_action_search" android:title="Add Content" android:visible="true" android:showAsAction="ifRoom|withText" /> <item android:id="@+id/menu_newlist" android:icon="@drawable/ic_action_search" android:title="New List" android:visible="true" android:showAsAction="ifRoom|withText" /> </menu>
This works fine. All menu items are there, I can click them and respond. Now when I click on the "Add Content" menu, the other fragment is loaded. There I as well set
this.setHasOptionsMenu(true);
the onCreateOptionsMenu method
@Override public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { Log.i("FragCreateList","onCreateOptionsMenu called"); super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.menu_fragcreatelist, menu); }
The xml file
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_editdone" android:icon="@drawable/ic_action_search" android:title="Edit Done" android:visible="true" android:showAsAction="ifRoom|withText" /> </menu>
But in the new fragment I can't see any menu items. Neither in the action bar, nor when I press the hardware menu button. In the former fragment, two menu items were in the actionbar, the remaining two appeared when the hardware button was pressed. But here nothing is shown. I get the logcat message that the onCreateOptionsMenu method got called, but nothing happens. (I even get a call to my (empty) onPrepareOptionsMenu method, which should be called right before the menu is shown, still nothing) Any clue what could be wrong? Calling invalidateOptionsMenu doesn't do a thing. Besides, I don't know for sure which one to call, as there is a getActivity().invalidateOptionsMenu and a getFragmentManager().invalidateOptionsMenu... Thanks in advance.
Edit :
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15" />
-
El Duderino almost 12 yearsChanged it to @Override public void onPrepareOptionsMenu(Menu menu) { Log.i("FragCreateList","onPrepareOptionsMenu called"); MenuInflater inflater=new MenuInflater(getActivity()); inflater.inflate(R.menu.menu_fragcreatelist, menu); } but still nothing... nothing is showing.
-
Dimanoid almost 12 yearsStrange, I have activity with two fragments, classic schema, activity itself have a menu and each fragment have its own menu. I have onCreateOptionsMenu in activity and onPrepareOptionsMenu in fragments. And I have all three menus showed combined...
-
El Duderino almost 12 yearsMaybe we misunderstood, I don't want all my menus combined, I want those two separate menus shown in two separate fragments, but only one fragment shows a menu. I first had onCreateOptions in my Activity too, but moved all to fragments, as I thought it makes more sense. I just solved my problem with a different design approach, still I'd like to know why it doesn't work :) Thanks for the effort.
-
El Duderino almost 12 yearsHey jsmith, this is about contextual menus, I am talking about the actionbar menu items or hardware button menu items respectively.
-
jsmith almost 12 yearsYes, I understood that. In reading the issue, it seemed to imply that you call setHasOptionsMenu(true) in the fragments onCreate. I'm suggesting you move it to the onActivityCreated() method for both fragments. This applies to options menus and context menus.
-
Eric Brynsvold over 10 yearsNote: Using
android:showAsAction
seems to have no effect when using the support Fragment, so setting up the second namespace with res-auto is critical. Thanks for the help. -
WickedW over 9 years+1 @aglour for this nugget, had everything else setup and was using Support V4 as mentioned by Eric, cannot see how would have fixed this otherwise, thank you.
-
David Gay over 6 years+1, If you have a main activity that switches between multiple fragments, and you are trying to use a custom
<menu>
as an action bar for one of those fragments, this is the solution that you want. -
famfamfam about 5 years@EI Duderino same problem with u, how did u solve it?
-
Nathaniel Hoyt over 3 yearsthis worked for me too, thanks!. i looked at a half dozen tutorials including the official android docs and nowhere was this mentioned. such critical omission has sadly been the rule more than the exception in my journey learning android development....