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.
clear()
instead ofinvalidateOptionsMenu()
as OP suggests? – Argentiferous