Menu in Fragments not showing
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" />
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);
}
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);
}