Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Toolbar menu options added by fragment when it is replaced

I have a set up of 3 'top-level' fragments that each use their own Child Fragment Managers to offer drill-down navigation. These top-level fragments are switched around by being replaced using the main activity's Support Fragment Manager.

One of this top-level fragments has a child fragment that adds a menu option to the toolbar/actionbar using setHasOptionsMenu() and onCreateOptionsMenu() and that works just fine.

Now the issue I just noticed is this:

When a new child fragment is added and the child fragment with the menu item is hidden (and the transaction added to the backstack of the top-level fragment), the menu item goes away. Similarly, when the fragment is visible again by undoing the transaction, the menu item comes back. This is desired behavior and seems to be handled entirely by the Fragment framework.

HOWEVER, if the child fragment is visible (and thus, its menu item is present in the toolbar) and I switch top-level fragments, the menu item remains in the toolbar.

I would have expected the menu item to be cleared as not only the child fragment it belongs to has been dumped, but even its parent fragment (on of the top-level fragments) has also been completely replaced (not even added to the backstack, just straight up replaced).

I figure I could just call invalidateOptionsMenu() on the activity whenever the incoming top-level fragment has Resumed, but I feel like there is just something I am missing for it to be handled automatically like when navigating within the top-level fragment.

like image 565
RobertoCuba Avatar asked Apr 14 '17 02:04

RobertoCuba


People also ask

Can we hide the menu on the toolbar in one fragment?

How do I hide a menu item? If you want to change the visibility of your menu items on the go you just need to set a member variable in your activity to remember that you want to hide the menu and call invalidateOptionsMenu() and hide the items in your overridden onCreateOptionsMenu() method.

How do I access the activity toolbar in fragment?

Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen. To use the Toolbar in your fragment, provide an ID and obtain a reference to it in your fragment, as you would with any other view.


1 Answers

The onCreateOptionsMenu method is called after on create or re-create each fragment. What you need to do is clear the menu before inflate the new menu xml. Try to do this:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
    super.onViewCreated(view, savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.your_menu, menu);
}
like image 119
pablopatarca Avatar answered Sep 30 '22 08:09

pablopatarca