2

I tried so many answers provided by various posts here but nothing worked for me.

Problem- I have navigation drawer that has 6 fragments but single activity. Everything worked fine till I changed 1st ranked fragment in drawer. I wanted Swipe tabs inside first fragment. So I used FragmentStatePagerAdapter.

  • Each fragment has its own menu along with MainActivity Menu.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Notify the system to allow an options menu for this fragment.
        setHasOptionsMenu(true);
    }
    

    And inflated like this:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.story, menu);
    }
    
  • Everything works fine. But When I visit other fragments in navigation drawer then it shows duplicate menu in toolbar. It creates more duplicates if there is space left in toolbar when I visit other fragments.

Try 1 : To solve this problem I initially used:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.clear();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.story, menu);
}

With this I don't get duplicate menu but now I don't see MainActivity menus.

Try 2:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    getActivity().invalidateOptionsMenu();
    inflater.inflate(R.menu.story, menu);
}

With this I get both Fragment and Activity menu but Duplicates are there.

This should be easy to solve but I am not picking up a way to deal with this. Maybe I didn't understand the life cycle well?

My other approach- Implementing all menus in Fragments will do the trick but this should be our last option.

Solution to this - To maintain both Menu all I have to do is this (Very easy solution):

menu.clear();
inflater.inflate(R.menu.story, menu);
getActivity().getMenuInflater().inflate(R.menu.main, menu);

Problem 2 OnOptionsItemSelected method from 1st fragment is getting called in other fragments.

halfer
  • 19,824
  • 17
  • 99
  • 186
Roon13
  • 387
  • 2
  • 23
  • so basically you want common menu items across all fragments coming in from your activity_menu file and rest from the fragments right? – PunitD Dec 18 '15 at 07:40
  • In other fragments if you don't want menu then call `setHasOptionsMenu(false);` for them. – Piyush Dec 18 '15 at 07:40
  • @PiyushGupta I want menu from every fragment. – Roon13 Dec 18 '15 at 07:58
  • @Roon13 well then your last option is the best option.. implementing menu items[Fragment related] + common one in all Fragments.. – PunitD Dec 18 '15 at 08:36
  • @Roon13 i read your question again..So, your fragment was and must be showing menu items properly prior to changing your first fragment to implement FragmentPagerAdapter,right? – PunitD Dec 18 '15 at 09:16
  • Yes initially it was working fine. I used FragmentPagerAdapter inside 1st fragment for swipe views. After that this problem is started. – Roon13 Dec 18 '15 at 09:39

1 Answers1

1
  private void hideAllMenuItems() {
        if (actionBarMenu != null) {
            actionBarMenu.findItem(R.id.action_item1).setVisible(false);
            actionBarMenu.findItem(R.id.action_item2).setVisible(false);
        }
    }


    private void showMenuIcon() {
        if (actionBarMenu != null) {
            hideAllMenuItems();
            if (currentFragment instanceof Fragment1)
                actionBarMenu.findItem(R.id.action_item1).setVisible(true);

            else if (currentFragment instanceof Fragment2)
                actionBarMenu.findItem(R.id.action_item2).setVisible(true);

        }
    }

call shoeMenuIcon() each time new fragment load..

Hope you are looking for this

mhl1291
  • 11
  • 3