15

I have checked and tried out all the possible codes here but those didn't work for me as i am replacing the layout only not having different toolbar.I am using fragments and NavigationDrawer, the navigationdrawer has listview, when a listitem is clicked it is replacing the layout with the following fragments. But how do i hide the menu or modify it based on different layout appeard?

AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62
silverFoxA
  • 4,549
  • 7
  • 33
  • 73

5 Answers5

49

If you want to control the option menu from Fragment you need to call setHasOptionsMenu in the onCreate of the Fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

Then you can add, remove or clear the menu overriding onCreateOptionsMenu. For example this code remove every item in options menu:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
3

it works for me, u can try this code

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }



 @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.findItem(R.id.action_cart).setVisible(false);
        menu.findItem(R.id.action_search).setVisible(false);
        menu.findItem(R.id.overflow).setVisible(false);
    }
Ashwin H
  • 695
  • 7
  • 24
1

Since you did not provide the code of the things that you tried, it is difficult to say what might work.

I worked in an app that also had a NavigationDrawer and for me, what made the difference was to call menu.clear() in onCreateOptionsMenu of each fragment.

A sample code would look like this:

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (!mNavigationDrawerIsOpened) {
        // Clear the menu from menuitems added from other fragments
        menu.clear();

        // Inflate your new menu
        inflater.inflate(R.menu.new_fragment, menu);

        // get the menu items of that new menu
        MenuItem item = menu.findItem(R.id.action_traffic_spinner);
    }
}
AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62
  • i am not planning to hide it on navigation drawer open kindly check my question – silverFoxA Apr 16 '15 at 11:42
  • I am not sure if I am understanding you, but if what bothers you is the `if`, just remove it. It was an `if` that I had because I do not want to show the MenuItems when the Drawer is open. It is the recommended behavior for the NavigationDrawer. – AlvaroSantisteban Apr 16 '15 at 11:46
1

Since setHasOptionsMenu() and onCreateOptionsMenu() is now deprecated, you can do this also with the following:

For example in the onCreateView()-method you could remove your menu item with:

requireActivity().addMenuProvider(new MenuProvider() {
    @Override
    public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
        menu.removeItem(R.id.menuItemToRemove);
    }
    @Override
    public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
        return false; //report no menu selection handled
    }
});

And in the onStop()-method you could add it again with:

requireActivity().addMenuProvider(new MenuProvider() {
    @Override
    public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.menu_main, menu);
    }
    @Override
    public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
        return false; //report no menu selection handled
    }
});
Itchy
  • 2,263
  • 28
  • 41
0

setHasOptionsMenu() and onCreateOptionsMenu() is now deprecated, you can do this also with the following:

requireActivity().addMenuProvider(new MenuProvider() {
        @Override
        public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
            menuInflater.inflate(R.menu.menu_reporte,menu);
        }

        @Override
        public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {

            switch(menuItem.getItemId())
            {
                case R.menu.menu_reporte:

                    break;
            }
            return true;
        }
    },getViewLifecycleOwner(), Lifecycle.State.RESUMED);
Magma
  • 1