Duplicate Menu and Data in Fragments after using FragmentStatePagerAdapter

北城余情 提交于 2019-12-11 12:37:06

问题


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.


回答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



来源:https://stackoverflow.com/questions/34349728/duplicate-menu-and-data-in-fragments-after-using-fragmentstatepageradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!