2

I've created a NavControllerActivity, which is based on UINavigationController from iOS. I would like the app bar's menu items to only show the menu items for the currently displayed Fragment.

At the moment, as each new Fragment is pushed onto the nav stack, the menu items just get appended to the existing ones. When I tap Back and pop the Fragments off of the nav stack, then those menu items disappear as one would expect.

However, I'd like to hide/remove/something the existing menu items when the new Fragment is pushed on and show just the menu items for that Fragment. Then when that Fragment is popped off, I'd like to remove its menu items and reinstate the menu items from the Fragment that is now at the top of the stack.

I'm currently having each Fragment generate the menu items in onCreateOptionsMenu().

Is there a way to make Android only show the menu items for the top level fragment in my Activity?

Edit: Do I need to have the Activity directly manage the menu items instead? Essentially be constantly invalidating and replacing the menus whenever I push/pop a Fragment? i.e. Still have the menus really defined and controlled within the Fragment, but have the enclosing Activity passing along the menu creation calls to the Fragment at the top of the stack?

Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229
  • Yes dont overide the onCreateOptionsMenu in Fragment. Let the activity menus dominates. – Enzokie Sep 21 '16 at 21:08
  • That won't work. I need the menus to be based on the individual fragments that are shown, so I MUST override onCreateOptionsMenu in each of my fragments. – Kenny Wyland Sep 21 '16 at 21:11
  • Its hard to tell what does `wont work` means. However you can tell the activity to hide or show a specific menu in fragment by some sort of calling its instance within onStart. – Enzokie Sep 21 '16 at 21:14
  • As in, that design will not work for my app because my app needs to dynamically swap out fragments and so the enclosing Activity that is showing each of these fragments cannot, and should not, be aware of what each of the various menus are. I need those menus to be controlled at the Fragment level in onCreationOptionsMenu. I may need to use the Activity to clear and delegate menu creation at each step to the Fragments, but the Activity cannot be in charge of which specific items are shown. – Kenny Wyland Sep 21 '16 at 21:27
  • @Enzokie It's perfectly valid to use `onCreateOptionsMenu()` in the Fragments, and in many cases it's the optimal solution to do it that way. – Daniel Nugent Sep 21 '16 at 23:07
  • Take a look at the answer here: http://stackoverflow.com/questions/8279981/how-can-i-change-action-bar-actions-dynamically – Daniel Nugent Sep 21 '16 at 23:12
  • @DanielNugent Yes it is absolutely valid, my intention is not stay away from `Fragment#onCreateOptionsMenu` but to give alternate handling in the OP's scenario. – Enzokie Sep 21 '16 at 23:19
  • @DanielNugent Thanks for the response! I looked at that other post before as well, but I don't see the behavior described there. All of my fragments have the `setHasOptionsMenu(true)` but it doesn't clear out the existing menu. I'll keep experimenting. – Kenny Wyland Sep 22 '16 at 21:41
  • Try calling `invalidateOptionsMenu()` in the Activity each time you do a FragmentTransaction.... – Daniel Nugent Sep 22 '16 at 21:48
  • I tried doing the invalidate after the transaction, I'll try doing it before the transaction later tonight. – Kenny Wyland Sep 22 '16 at 22:19
  • I tried invalidating the menu options before pushing my new fragment on, and it doesn't change anything. The new fragment's menu items just get added to the menu, instead of replacing the menu. – Kenny Wyland Sep 28 '16 at 19:55

0 Answers0