The action element still appears after replacing the fragment

I am using a navigation drawer - a project frame with android studio 4+ - and I have two fragments in the menu.

One snippet containing the android.support.v4.view.ViewPager file. The ViewPager contains two fragments, one of which has a menu. When I use a gesture to change a fragment in the viewpager menu and disappear depends on which fragment is now visible - everything is correct.

And now the flow when something is wrong:

if i open the first snippet from the nav-drawer and swype for the snippet with the menu and then swap to the second snippet using the nav-drawer menu still appears - why? I can not understand.

EDIT:

if i add

setHasOptionsMenu(true);

      

and

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

      

for a snippet that shouldn't have a menu then the problem is gone, but still I think the root is forcing it somewhere else and the above solution is messy.

+3


source to share


1 answer


I know I am 2 years late to answer, however this may be of benefit to programmers who are facing the same problem. I had the same problem and solved it by simply calling

setHasOptionsMenu(true)

      



in OnCreateView ().

This ensures that the new fragment has its own options menu, which can then be created using onCreateOptionsMenu (). If you don't want to have any options, just avoid implementing onCreateOptionsMenu ().

+1


source







All Articles