Reputation: 1289
I've tried almost everything I found in another posts of stack overflow but I can not see to find the solution for this bug....
I've tried so far to rebuild the project, clean, make the project again, restart android studio, restart my computer and uninstall the app from the phone.
I have 2 fragments that are very similar, but both of them have different menu layouts.
Here I have the menu for one fragment:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.gabilheri.formulacalculator.main.MainActivity" >
<item
android:id="@+id/delete"
android:icon="@android:drawable/ic_menu_delete"
android:title="Delete"
android:orderInCategory="10"
android:visible="true"
app:showAsAction="always"
/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
And here is the code for the menu in it's fragment:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.log_menu, menu);
}
Here is my other fragment:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.gabilheri.formulacalculator.main.MainActivity">
<item
android:id="@+id/saveTheme"
android:icon="@android:drawable/ic_menu_save"
android:title="Save"
android:orderInCategory="10"
android:visible="true"
app:showAsAction="always"
/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
And here is the code to call the menu in the other fragment:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_themes, menu);
}
I really can't understand why both identical snippets of code work in one fragment but not in the other...
Upvotes: 0
Views: 960
Reputation: 199805
Make sure you call Fragment.setHasOptionsMenu(true) to inform the framework that your Fragment has an options menu - if you don't call that, then onCreateOptionsMenu
will never be called.
Generally you should do this in your Fragment
's onCreate
method.
Upvotes: 2