Search code examples
androidmenuandroid-toolbarandroid-appcompatappcompatactivity

Fragment not inflating menu after initializing support action toolbar


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.


Solution

  • 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.