Fragment not inflating menu after initializing support action toolbar - android

I have an appcompat activity with no action bar theme. I am running a fragment inside the activity to display a list of items. The items are displayed in two modes: list and delete. When the screen opens, it is always in list mode. The action bars are different in the two display modes. In list mode, in the action bar I have two menu items on the right side which are displayed when the fragment opens for the time. When I come back from the delete mode to list mode, the menu items are not inflated then. How do I make the menu items inflate while coming from delete mode to list mode.
Upon entering the screen, the user goes to the list mode. I have setHasOptionMenu(true); in my fragment. So onCreateOptionMenu and onPrepareOtionsMenu both are called and the two menu items are inflated properly.
When I switch from the list mode to delete mode (which happens when you click on the delete menu item displayed in the action bar on the top-left), I inflate a new custom view and remove all the views from the toolbar and then add this custom view to the toolbar to change the action bar as per the delete mode:
View customView = LayoutInflater.from(getActivity()).inflate(
R.layout.delete_mode_toolbar_layout, null);
Toolbar toolbar = mActionBarHelper.getToolbar();
toolbar.removeAllViews();
toolbar.addView(customView);
And it works totally fine with the delete mode.
But now when I come back to the list mode, I again do the same.
View customView = LayoutInflater.from(getActivity()).inflate(R.layout.list_mode_toolbar_layout,null);
Toolbar toolbar = mActionBarHelper.getToolbar();
toolbar.removeAllViews();
toolbar.addView(customView);
Objects.requireNonNull((AppCompatActivity)getActivity()).supportInvalidateOptionsMenu();
But this time in the list mode, the action bar is coming correctly as per the list mode's custom toolbar, and the oncreateoptionsmenu and onprepareoptionsmenu both are being called. But the menu items are not inflated.
Sorry, but due to some restrictions, this is all the code I can share. Please comment your doubts, I can try to clear them in comments.
So please can someone help me understand why the menu items are not inflating after coming back from the delete mode to list mode.
Thanks in advance.

I got a work around which is a solution to my situation but not the solution to my question.
Now when going from list to delete mode, first hide the list mode custom toolbar layout already present in the toolbar (by keeping a reference to the list mode custom toolbar layout), then inflate the delete mode custom toolbar layout and add it to the toolbar using toolbar.addView(view). Now while coming back to the list mode again from the delete mode, just remove the custom delete toolbar layout from the toolbar using toolbar.removeView(view) and then make the list mode toolbar layout, already present in the toolbar, visible.

Related

Android navigation drawer reorder items

I'm facing an weird behaviour of navigation drawer so if someone can help...
I have a navigation drawer filled by an adapter (like list of Object : title and icon).
In Activity
mNavItems = NavBarUtil.listItem(); // List of object
mBinding.navList.setAdapter(new DrawerListAdapter(this, mNavItems));
The view for drawer is just a simple ListView.
The weird behaviour is that when the keyboard appears (whatever it comes), the order of the items in navigationDrawer change... it's like, displaying the keyboard recalculate all the view.
Just wanna precise that all listener for item work fine, the issue is just about the view like icon and title...
Thanks you guys

Swapping menu items based on current fragment

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?

ActionBar With Spinner (NAVIGATION_MODE_LIST)

In my app, I need an ActionBar with a dropdown on it (a spinner / setNavigationMode NAVIGATION_MODE_LIST). The ActionBar needs to be visible in all the views/screens of the app, but the content underneath it changes - it's a list of items that when clicked, a detailed view of the item is seen.
The ActionBar state should be persistent throught the different views (e.g. the selected item in the drop down).
What would be the correct way to do it? I can think of:
A base activity that initializes the ActionBar by calling actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST), attaching an adapter if needed etc. (How to persist the dropdown state?)
Using fragments
Another way?
Which of these ways is preferred?
Thanks

Static and dynamic Items in Actionbar

My app contains a master/detail flow. If I click a ListViewItem a new Fragment is displayed in the detail flow whith its own menu items in the actionbar. I change them with:
#Override
public void onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.klasse, menu);
}
Default the items are displayed at the right side of the screen. Now I want some items which are allways there (not depending on the fragment) on the left side of the screen. So I want some static items on the left in the actionbar and some which can change dynamicly on the right side. How can i realize that?
Or in other words: One OptionMenu for the Activity where the items are static and on the left side and one OptionMenu for the Fragments which can change dynamicly but all in one ActionBar on the top of the Screen. The Event for the static items I want to handel in the Activity.
You are welcome to implement onCreateOptionsMenu() in your activity to have it add action items to the action bar, in addition to those being contributed by your fragments. You can use things like android:orderInCategory to control the sequencing. However, none of the action items will be "on the left side of the screen", as that is reserved for the home affordance, title, and navigation (e.g., drop-down list).
I found a solution that works like a charm for me with static Items on the left side of the ActionBar. See here:
https://stackoverflow.com/a/15667427/2047987

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