Fragment's onCreateOptionsMenu method is never called - android

I have an application with two activities hosting fragments. My main activity hosts a single fragment, and that fragment is able to define and inflate a menu that goes in the toolbar, no problem.
In the second activity, though, which uses a FragmentStatePagerAdapter to allow horizontal scrolling between items, my fragment does not seem able to define the menu in the toolbar.
Checks:
My whole app is set to use a theme (android:theme="#style/AppTheme") based on Theme.AppCompat.Light.DarkActionBar.
My fragment extends android.support.v4.app.Fragment
setHasOptionsMenu(true); is called from the fragment's onCreate() method
the hosting activity extends AppCompatActivity and does not implement a toolbar menu itself
my fragment overrides void onCreateOptionsMenu(Menu, MenuInflater), but this method seems to never be called
You can have a look at the commit that is supposed to add that menu on GitHub. (Or even look at any part of the code that might be a cause of error.)
Here are the big lines:
CrimeFragment.java:
public class CrimeFragment extends Fragment {
// ...
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
UUID id = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
Log.d(TAG, String.format("Crime id in intent's extra: %s", id.toString()));
mCrime = CrimeLab.get(getActivity()).getCrime(id);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu()"); // <= Never shows in the Android Monitor
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_crime, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_delete_crime:
CrimeLab.get(getActivity()).deleteCrime(mCrime);
getActivity().finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// ...
}
Is there something I'm doing wrong here?

Alright, it took me the time, but I finally found what was wrong with my code. This was a bit tricky.
In an earlier change, I overrode FragmentStatePagerAdapter#setPrimaryItem() in the hosting activity, in order to be informed every time the user switches between pages.
Unfortunately, in that override, I forgot to call super, and that's what was confusing the application, apparently.
I just had to add that call to super, and my menu item suddenly started to show up.
public class CrimePagerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime_pager);
// ...
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
// ...
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object); // <= This line was missing
Crime crime = mCrimes.get(position);
mChangedCrimeIds.add(crime.getId());
}
});
mViewPager.setCurrentItem(CrimeLab.get(this).getPosition(crimeId));
}
// ...
}

Related

Working with OptionsMenu in nested fragment

I use a NavigationDrawer pattern that is implemented in my hostactivity MenuActivity. My navigation has 3 items: Item 1, Item 2, Item 3.
Each itemis bonded to a fragment.
When I click on Item 1, I displayed a fragment A that implements a ViewPager with several fragments (nested fragments).
In my nested fragments, I inflate a menu with the following method (It works fine !) :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
But when I click on another element of my menu (Item 2 -> display Fragment B or Item 3->display Fragment C), my menu (which was inflated in my nested fragment) is always visible but I want it to disappear.
Would you have a solution to this problem? Thank you in advance.
Just save child fragment and then override setMenuVisability:
#Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (savedFragment!= null)
savedFragment.setMenuVisibility(menuVisible);
}
it works for me
When I encountered this problem, I solved it by setting setHasOptionMenu(true) for both the child fragment and the "root" fragment. If the "root" fragment or another child fragment doesn't use option items, it's fine anyway, since you only inflate a menu in the child fragment needing it.
I just came across the problem and solved it by the following:
#Override
public void onDestroyOptionsMenu() {
this.setMenuVisibility(false);
super.onDestroyOptionsMenu();
Log.e(TAG, "onDestroyOptionsMenu");
}
#Override
public void onDestroyView() {
onDestroyOptionsMenu();
super.onDestroyView();
}
I noticed that onDestroyOptionsMenu is not being called so what I only did is call it from the OnDestroyView method and I set the menu visibility to false.
A slightly different approach from SafiS answer:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(...) {
View view = ...
setMenuVisibility(true);
return view;
}
#Override
public void onDestroyView() {
setMenuVisibility(false);
super.onDestroyView();
}
Add setRetainInstance(true) and setHasOptionMenu(true) in onCreate() in fragment.

Android refresh actionbar and title on fragment change

in my app i am developing an activity using the actionbar in NAVIGATION_MODE_TABS-mode.
Each tab is showing a fragment (list, detail).
Initially the list-tab is visibile.
The list is implementing setMultiChoicheModeListener() and modifies the ActionBar and the title of the activity if one or more items are selected.
How can i reset the title and the ActionBar to the inital value (title and actions) when the user clicks on the detail-tab without deselecting the items?
BTW Target-Platform is > 4.1 and i am not using the support library.
Thanks.
public class MyActivity extends Activity {
#Override
protected void onCreate (Bundle savedInstanceState) {
this.actionBar = getActionBar();
this.actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
....
for (Tab tab : getTabs())
{
//here are two tabs added (List and Detail)
this.actionBar.addTab(tab);
}
....
}
protected class NavigationTabListener implements ActionBar.TabListener {
private Fragment fragment;
....
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(newFragmentResourceId, this.fragment);
}
}
}
public class MyListViewFragment extends LinearLayout implements IListViewFragment {
....
#Override
public void initialize() {
inflate(getContext(), listLayoutResourceId, this);
this.myList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
this.myList.setMultiChoiceModeListener(new MultiChoiceModeListener() {
....
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(selectedItemsMenuResourceId, menu);
return true;
}
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
if (checked)
{
this.numberItemsSelected++;
adapter.setNewSelection(position);
}
else
{
this.numberItemsSelected--;
adapter.removeSelection(position);
}
mode.setTitle(getContext().getResources().getQuantityString(
R.plurals.items_selected, this.numberItemsSelected,
Integer.valueOf(this.numberItemsSelected)));
}
....
}
}
I am trying to implement the MVP pattern, but it's still in evaluation phase. The Activity acts as the presenter, the views are in separate classes.
For each Fragment i am also implementing the MVP apttern, but i think this is not interesting to solve the problem.
Some notes to the classes:
MyActivity creates two fragments (one for List, one for Detail view, the detail view has nothing to do with the selected items).
The initial view of the activity is the fragment with the list.
If the user selects some entries I am updating the action bar and the title through the callback of MultiChoiceModeListener.
But the user can now change the fragment by clicking on the "Detail" tab without deselecting the items or clicking to the new elements in the action bar, the result is that the detail fragment is shown, but the title of the activity is still the one I modified in the MultiChoiceModeListener, and there is also the check mark of the action bar visible (auto created by the system).
So the best way is I think to get somehow the current ActionMode, so I can invoke finish() to "reset" the ActionBar and the title.
Make sure you keep reference of the ActionMode in the ActionMode.Callback methods inside your activity which has the ActionBar.TabListener.
When a new tab is selected just finish the action mode, like:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(mActionMode != null){
mActionMode.finish();
}
ft.replace(newFragmentResourceId, this.fragment);
}
Make the ActionMode reference back to null when onDestroyActionMode(ActionMode) is called.

ActionbarSherlock unresponsive in Android 2.3.3 [duplicate]

The following bug will happen on an 2.3 device, my setup works fine on 4.x devices.
I have a ViewPager with some Fragments in it (they're all of the same class).
Every Fragment inflates it's own Menu, because the Menu Items may vary from Fragment to Fragment.
For test purposes, I have set up a Menu Item in the ActionBar (the ActionBar is shown on the bottom in the pic because it's a split ActionBar). When the Item is tapped, a TextView in the Fragment should be set to "clicked". This works in the beginning, but after flicking around a bit, this happens:
When the Menu Item is tapped, nothing happens. Instead, as soon as I swipe to the next Fragment, the next Fragment sets its TextView to "clicked". It seems like the ActionBar and it's Menu are associated with the next Fragment.
Heres a pic
And heres some code:
My Activity:
public class MyActivity extends SherlockFragmentActivity implements
MyFragment.InvalidateListener {
ViewPager viewPager;
SectionsPagerAdapter pagerAdapter;
public void invalidate() {
ActivityCompat.invalidateOptionsMenu(act);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty_viewpager);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(initialIndex);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new MyFragment();
fragment.setHasOptionsMenu(true);
return fragment;
}
// ...
}
My Fragment:
public class MyFragment extends SherlockFragment {
HashSet<ImageView> runningImageTasks = new HashSet<ImageView>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_expose, null);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_grundstueckexpose, menu);
// ...
}
#Override
public boolean onOptionsItemSelected(MenuItem mitem) {
switch (mitem.getItemId()) {
case android.R.id.home:
getActivity().finish();
return true;
case R.id.myitem:
textView.setText("clicked");
return true;
default:
return super.onOptionsItemSelected(mitem);
}
}
}
Has anyone else experienced something like this or has an idea on what could be the problem here?
The problem is that the MotionEvent does not handled correctly by internal class ActionMenuItemView (actually, there is no any specific behavior in this class).
So, I do not resolve initial problem, but I find workaround solution. I just override ActionMenuItemView.dispatchTouchEvent() and handle click and long-click manually using GestureDetector.
You can check this solution on github.
I don't know this exact problem, but I had a problem when flipping the device. The app was crashing. Finally, I've found that was problem of the Pager classes, because I was implementing them like you've implemented your SectionsPagerAdapter class.
I put the public classes that were on the main class on separated classes and the layouts worked well in vertical and horizontal position.
I don't know if this could be the problem, but you could try to create the corresponding classes instead of leaving them on the main class. Anyway, you pass to the SectionsPagerAdapter the FragmentManager, so you will not have any extra problem putting the public classes in their respective files.
Good luck!

Strange bug / behaviours with ViewPager and ActionBar (Sherlock)

The following bug will happen on an 2.3 device, my setup works fine on 4.x devices.
I have a ViewPager with some Fragments in it (they're all of the same class).
Every Fragment inflates it's own Menu, because the Menu Items may vary from Fragment to Fragment.
For test purposes, I have set up a Menu Item in the ActionBar (the ActionBar is shown on the bottom in the pic because it's a split ActionBar). When the Item is tapped, a TextView in the Fragment should be set to "clicked". This works in the beginning, but after flicking around a bit, this happens:
When the Menu Item is tapped, nothing happens. Instead, as soon as I swipe to the next Fragment, the next Fragment sets its TextView to "clicked". It seems like the ActionBar and it's Menu are associated with the next Fragment.
Heres a pic
And heres some code:
My Activity:
public class MyActivity extends SherlockFragmentActivity implements
MyFragment.InvalidateListener {
ViewPager viewPager;
SectionsPagerAdapter pagerAdapter;
public void invalidate() {
ActivityCompat.invalidateOptionsMenu(act);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty_viewpager);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(initialIndex);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new MyFragment();
fragment.setHasOptionsMenu(true);
return fragment;
}
// ...
}
My Fragment:
public class MyFragment extends SherlockFragment {
HashSet<ImageView> runningImageTasks = new HashSet<ImageView>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_expose, null);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_grundstueckexpose, menu);
// ...
}
#Override
public boolean onOptionsItemSelected(MenuItem mitem) {
switch (mitem.getItemId()) {
case android.R.id.home:
getActivity().finish();
return true;
case R.id.myitem:
textView.setText("clicked");
return true;
default:
return super.onOptionsItemSelected(mitem);
}
}
}
Has anyone else experienced something like this or has an idea on what could be the problem here?
The problem is that the MotionEvent does not handled correctly by internal class ActionMenuItemView (actually, there is no any specific behavior in this class).
So, I do not resolve initial problem, but I find workaround solution. I just override ActionMenuItemView.dispatchTouchEvent() and handle click and long-click manually using GestureDetector.
You can check this solution on github.
I don't know this exact problem, but I had a problem when flipping the device. The app was crashing. Finally, I've found that was problem of the Pager classes, because I was implementing them like you've implemented your SectionsPagerAdapter class.
I put the public classes that were on the main class on separated classes and the layouts worked well in vertical and horizontal position.
I don't know if this could be the problem, but you could try to create the corresponding classes instead of leaving them on the main class. Anyway, you pass to the SectionsPagerAdapter the FragmentManager, so you will not have any extra problem putting the public classes in their respective files.
Good luck!

Action items from Viewpager initial fragment not being displayed

In the application I am developing I am using a ViewPager with fragments and each fragment constructs its own menu independently of all of the other fragments in the ViewPager.
The issue is that sometimes the fragments that are initialised by the ViewPager by default (i.e in it's initial state) are not having their items populated into the action items menu. What's worse is that this issue only occurs intermittently. If I swipe through the ViewPager enough so that the fragments are forced to re-initialise them selves, when I swipe back, the menu populates correctly.
Activity code:
package net.solarnz.apps.fragmentsample;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
public class FragmentSampleActivity extends Activity {
private ViewPagerAdapter mViewPagerAdapter;
private ViewPager mViewPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (mViewPagerAdapter == null) {
mViewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
}
mViewPager = (ViewPager) findViewById(R.id.log_pager);
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setCurrentItem(0);
}
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 8;
}
#Override
public Fragment getItem(int position) {
Fragment f = Fragment1.newInstance(position);
// f.setRetainInstance(true);
f.setHasOptionsMenu(true);
return f;
}
}
}
Fragment code:
package net.solarnz.apps.fragmentsample;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class Fragment1 extends Fragment {
int mNum;
static Fragment newInstance(int num) {
Fragment1 f = new Fragment1();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mNum = getArguments() != null ? getArguments().getInt("num") : 0;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_list, menu);
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/log_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_refresh"
android:title="Refresh"
android:icon="#android:drawable/ic_delete"
android:showAsAction="ifRoom|withText" />
</menu>
Action menu being populated:
http://i.stack.imgur.com/QFMDd.png
Action menu not being populated:
http://i.stack.imgur.com/sH5Pp.png
You should read this (by xcolw...)
Through experimentation it seems like the root cause is invalidateOptionsMenu getting called more than one without a break on the main thread to process queued up jobs. A guess - this would matter if some critical part of menu creation was deferred via a post, leaving the action bar in a bad state until it runs.
There are a few spots this can happen that aren't obvious:
calling viewPager.setCurrentItem multiple times for the same item
calling viewPager.setCurrentItem in onCreate of the activity. setCurrentItem causes an option menu invalidate, which is immediately followed by the activity's option menu invalidate
Workarounds I've found for each
Guard the call to viewPager.setCurrentItem
if (viewPager.getCurrentItem() != position)
viewPager.setCurrentItem(position);
Defer the call to viewPager.setCurrentItem in onCreate
public void onCreate(...) {
...
view.post(new Runnable() {
public void run() {
// guarded viewPager.setCurrentItem
}
}
}
After these changes options menu inside the view pager seems to work as expected. I hope someone can shed more light into this.
source http://code.google.com/p/android/issues/detail?id=29472
The simple answer is to not use menus within fragments in the ViewPager.
If you do need to use menus within the fragments, what I suggest is loading the menu's through the onCreateOptionsMenu method in the parent Activity. Obviously you will need to be able to determine which menu to show.
I was able to achieve this by using class reflection.
You will also need to use the invalidateOptionsMenu method each time you switch pages. You will need a OnPageChangeListener to call this when the ViewPager changes pages.
I also had same issue. In my case I have one activity with viewpager that contains two fragments, every fragment inflate its own action menu but fragment actions menu not shown.
View pager adapter code
public class ScreensAdapter extends FragmentPagerAdapter {
public TrackerScreensAdapter(Context context, FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 2;
}
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
}
return fragment;
}
}
Activity on create
screensAdapter = new ScreensAdapter(this, getFragmentManager());
viewPager.setAdapter(screensAdapter);
This way my viewPager has two fragments, every fragment fire its own task in onActivityCreated, obtain data and draw its layout based on obtained data. Also every fragment has onCreateOptionsMenu
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
MyTask task = new MyTask();
task.setTaskListener(this);
task.execute();
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu, menu);
}
Spent many times to solve this problem and figure out why fragment menu not shows.
All that I was need is
screenAdapter = new ScreenAdapter(this, getFragmentManager());
viewPager.post(new Runnable() {
public void run() {
viewPager.setAdapter(screenAdapter);
}
});
In my case I traced the root cause of the issue to what I believe is a bug in FragmentStatePagerAdapter which is calling Fragment#setMenuVisibility to false and failing to properly set it back to true when it restores it's state.
Workarounds:
Use FragmentPagerAdapter instead of FragmentStatePagerAdapter
If you must use FragmentStatePagerAdapter, in your adapter subclass override setPrimaryItem like so:
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
//This is a workaround for a bug in FragmentStatePagerAdapter
Fragment currentItem = getItem(position);
if (currentItem != null) {
currentItem.setMenuVisibility(true);
currentItem.setUserVisibleHint(true);
}
}
First create a method in the sub class of FragmentPagerAdapter to get the current fragment
public SherlockFragment getFragment() {
return currentFragment;
}
#Override
public void onTabSelected(final Tab tab, FragmentTransaction ft) {
((SherlockFragmentActivity) mContext).invalidateOptionsMenu();
Fragment f = ((SherlockFragmentActivity) mContext)
.getSupportFragmentManager().findFragmentByTag(
makeFragmentName(tab.getPosition()));
currentFragment=(SherlockFragment) f;
}
Now override below methods in Main Actvity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (mTabsAdapter.getPositionOfTabSelected() != 0) {
menu.add("Read").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Write").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Clear").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Factory data reset").setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onCreateOptionsMenu(menu);
}
Now call the onOptionItemSelected from activity to fragment
#Override
public boolean onOptionsItemSelected(MenuItem item) {
mTabsAdapter.getFragment().onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}
I solved a very similar issue in which the Action bar icons assigned by the fragment inside of a ViewPager were disappearing onPause(). They would reappear when the Fragment came back into view and the user swiped left or right, but not immediately. The solution was calling notifyDataSetChanged() on the PagerAdapter in the onResume() method of the fragment.
#Override
public void onResume() {
mPagerAdapter.notifyDataSetChanged();
}
I was having this problem with the Action Bar Items while I was using HorizontalScrollView to show the tabs but I changed to PagerTitleStrip and the problem was solved.
Perhaps this information can help someone else.
My solution to this problem was to only inflate fragment menus if the fragment is currently visible. This solution may be too specific for your purposes, but it might help someone.
In the main activity:
boolean isFragmentVisible(int fragmentIndex) { ... }
In onCreateOptionsMenu() in your fragment:
if ( getActivity().isFragmentVisible(HOME_FRAGMENT_POS) ) {
inflater.inflate(R.menu.menu_home_fragment, menu);
}

Categories

Resources