Reputation: 436
I'm using navigation drawer - project frame from android studio 4+ - and I have two fragments in menu.
One fragment which contains android.support.v4.view.ViewPager. ViewPager contains two fragments, one of them has menu. When I use gesture to change fragment in viewpager menu shows and disappear depends on which fragment is visible now - everything is correct.
And now flow when something is wrong:
if I open first fragment from navigation drawer and swype to fragment with menu, and then change to second fragment using navigation drawer menu still appear - why? I cant understand.
EDIT:
if I add
setHasOptionsMenu(true);
and
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
super.onPrepareOptionsMenu(menu);
}
to fragment which shouldn't has menu then problem is gone, but still I think root cause it somewhere else and above solution is dirty.
Upvotes: 3
Views: 547
Reputation: 71
I know that I am 2 years late to answer, however this may benefit programmers that face the same problem. I had the same issue and I solved it by simply calling
setHasOptionsMenu(true)
in OnCreateView().
This will ensure that the new fragment has its own options menu which then can be created with onCreateOptionsMenu(). If you don't want to have any options menu just avoid implementing onCreateOptionsMenu().
Upvotes: 5