Swapping menu items based on current fragment - android

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?

Related

How to set toolbar.setNavigationOnClickListener only for current fragment?

Started new project from navigation drawer activity template. By default, all drawer menu items are at the same navigation level. All fragments display hamburger menu button, that shows drawer.
I need to keep all items in drawer, but place Home as top level item and others as it's children like this:
Tried to overwrite toolbar?.setNavigationOnClickListener { } in Gallery fragment, but it also affects Home fragment and I didn't find how to restore default behavior.
How can I set Home as navigation parent for others or how to set navigation click listener to only one fragment?
For example you have set tag for every fragment to trace when you start new fragment and now check with Home and set click event..
Fragment home = getSupportFragmentManager().findFragmentByTag("home");

Android Menu duplication fixed with clear but it clears the parent activity menu

I have created different fragment and populated some menu based on the fragment. I was getting duplicate menu items so I used clear as suggested various stackoveflow posts but the problem is when I use clear in fragment it also clears the main activity menu items.
Activity and all fragments inside it use the same Menu instance. So no matter where you call clear() method, all items will be deleted.
Menu items that are used across all child fragments should be created inside Activity. Fragment-specific options should be created inside corresponding fragment. Ideally you should not have duplicated menu items. If two fragments have same menu item but third doesn't -- prefer create menu item inside every fragment but not single menu item inside activity. With this approach you will not get duplicates.
But If you want just to delete duplicates you can use menu.removeItem(itemId) method. Also you can hide redundant item with menuItem.setVisible(false).
When I switch between Fragments, I get duplicated menu
Maybe you have two active fragments at the same time. It can be when you use add method instead replace.

how to disappear `BottomNavigationView` in some fragments?

I have a BottomNavigationBar in the MainActivity. I want to hide it in the some fragments.I saw this question but its solutions didn't work for me.How to hide bottom navigation bar in specific fragment?
I have three items with the following names.MainPageFragment and ListQuestionFragment and ListAnswerQuestion.ListQuestionFragment and ListAnswerQuestion contain the list of items and when User click every item a new fragment will be replaced.These fragments are the details of those item. I want to hide BottomNavigationBar in these fragments.

Different menu for different fragments

I have an activity which has 2 fragments.
1 fragment is visible at a time and each fragment has a different option menu.
I can achieve this behavior by 2 different ways.
1 - I can add different menu for each fragment by calling onCreateOptionsMenu in each friengment.
2 - I can have only one menu at activity level and can select to show particular option in onPrepareOptionsMenu
What I want to know is:
Which is the preferable way to implement this functionality?
What is recommended?
Hope this helps
Adding items to the Action Bar
Your fragments can contribute menu items to the activity's Options Menu (and, consequently, the Action Bar) by implementing onCreateOptionsMenu(). In order for this method to receive calls, however, you must call setHasOptionsMenu() during onCreate(), to indicate that the fragment would like to add items to the Options Menu (otherwise, the fragment will not receive a call to onCreateOptionsMenu()).
Any items that you then add to the Options Menu from the fragment are appended to the existing menu items. The fragment also receives callbacks to onOptionsItemSelected() when a menu item is selected.
You can also register a view in your fragment layout to provide a context menu by calling registerForContextMenu(). When the user opens the context menu, the fragment receives a call to onCreateContextMenu(). When the user selects an item, the fragment receives a call to onContextItemSelected().
http://developer.android.com/guide/components/fragments.html
I would follow the first option as having a dedicated resource menu for each fragment seems cleaner and also reduces the code complexity you would have in order to maintain what is visible and what is not (if you would go through onPrepareOptionsMenu and have code to hide & show different menus).
If you have some actions in your fragments, then you could create a base fragment class that each of your fragments would extend from.

Android - ActionBar when splitscreen (List/Detail)

I am new to fragments and Ive been wondering whats the correct answer to this.
I am having simple List > Detail type of application, where this was originally 2 activities, no fragments, each had their own actionbar (menu).
Now I want to migrate it to fragments.
Using fragments I created a classic tablet split view with list on the left, detail on the right. Obviously that being only one activity, only the list's actionbar (action items) is being displayed.
Where should I put the action items from the detail? What the correct pattern? Should I create an alternative resource folder with custom menu file for tablets which would consist of both list's and detail's menu items?
Thanks !
Just create a new fragment_details.xml for the details fragment's menu entries.
Inflate the menu directly from the fragment.
Inflate the base menu from the list fragment.
The activity should have no menu attached at all as all menu entries are attached to the one or the other fragment.
In the tablet case, android will merge the fragment's menu with the one from the list fragment.
On smaller screens, when only the details are shown, the fragment's menu is shown.

Categories

Resources