Duplicate Menu and Data in Fragments after using FragmentStatePagerAdapter
Asked Answered
C

1

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.

Crossness answered 18/12, 2015 at 6:32 Comment(6)
so basically you want common menu items across all fragments coming in from your activity_menu file and rest from the fragments right?Strigose
In other fragments if you don't want menu then call setHasOptionsMenu(false); for them.Sleep
@PiyushGupta I want menu from every fragment.Crossness
@Crossness well then your last option is the best option.. implementing menu items[Fragment related] + common one in all Fragments..Strigose
@Crossness 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?Strigose
Yes initially it was working fine. I used FragmentPagerAdapter inside 1st fragment for swipe views. After that this problem is started.Crossness
A
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

Average answered 18/12, 2015 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.