Android Toolbar Adding Menu Items for different fragments

后端 未结 10 1673
野性不改
野性不改 2020-11-28 22:51

I have a toolbar as well as a navigation drawer. When I start my app, the toolbar and navigation drawer are created. When I click items in the navigation drawer, it starts n

10条回答
  •  误落风尘
    2020-11-28 23:35

    I had the same issue and I wanted to replace the toolbar menu accordingly to the fragment displayed.

    The problem I'm now facing is that the menu is added to the previous one.

    In order to show only the menu for the fragment, I suggest:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         menu.clear(); //Empty the old menu
         inflater.inflate(R.menu.menu_fragment1, menu);
         super.onCreateOptionsMenu(menu, inflater);
    }
    

    Hope it'll help.

提交回复
热议问题