3

I have a project using Drawer and Swipe tabs that I created using this tutorial. I'm using the support library to make Material Design available for older android versions. I'm also using ToolBar instead of ActionBar. I have a menu attatched to that ToolBar:

    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    toolbar.inflateMenu(R.menu.menu_main);

What I'm struggling with is having specific menus for each fragment. I tried many answers to other similar questions, like this and this to no avail. Most people are suggesting the implementation of these methods but they only seem to work for ActionBar and in my case I have a android.support.v7.widget.Toolbar.

I also tried using

toolbar.getMenu().findItem(R.id.menuitem).setVisible(false);
supportInvalidateOptionsMenu();

But it also does not work. In this case, my menu just disappears.

I've been searching and struggling with this for weeks to to avail. Am I doing something wrong?

How can I have specific menus for each fragment? I think that hiding menu items may be the easier choice to achieve that, but if you guys have any other way of accomplishing that, I would be grateful.

Community
  • 1
  • 1
Anderson Madeira
  • 420
  • 9
  • 29
  • Are you calling setSupportActionBar(toolbar) at some point in your activity? You should do that and inflate your menu in onCreateOptionsMenu() method. There you can handle your menu logic (whether it's by hiding or inflating different menus) and, as you said calling supportInvalidateOptionsMenu(); when you change between fragments to recreate it. – Daniel Ocampo Oct 13 '15 at 15:08
  • But if I call setSupportActionBar(toolbar) my ActionBarDrawerToggle stops working somehow. – Anderson Madeira Oct 13 '15 at 17:25
  • I don't think we can help you unless you provide more code and explain a bit more what happens in each situation. I think that what I said seems to be the right way to go so please let us know exactly what happens with this approach. – Daniel Ocampo Oct 13 '15 at 18:13
  • I understand your point, sorry for not providing enough code, my code is a little messy and complex to post here. You were right, a simple call to `setSupportActionBar(toolbar)` was the source of all my headache, I needed call that method to get [this](http://stackoverflow.com/a/27126724/3739186) working. Now each fragment has its own menu, inflated in `onCreateOptionsMenu`. Thanks for your help, If you post an answer explaining the usage of `setSupportActionBar` I'd be glad to accept that as the correct answer. – Anderson Madeira Oct 13 '15 at 18:27

1 Answers1

2

You should call setSupportActionBar(toolbar) when you setup your Toolbar (probably in onCreate) and inflate your menu as usual in onCreateOptionsMenu(Menu menu) method. In this method you can handle the different Menus, whether it's by hiding the items or just inflating a different menu based on the fragment.

Then, as you said, call supportInvalidateOptionsMenu(); when you change between fragments to recreate the Menu.

Daniel Ocampo
  • 236
  • 2
  • 13
  • 1
    I thought of calling supportInvalidateOptionsMenu(), but [this](http://stackoverflow.com/a/8309255/3739186) answer offers a more elegant way of achieving that by calling `setHasOptionsMenu(true)` on `onCreate` method of each fragment, which saves me some lines of code. ;) – Anderson Madeira Oct 13 '15 at 18:46
  • Yes, that works well too and seems cleaner, didn't think of that. – Daniel Ocampo Oct 13 '15 at 18:49
  • Worked also for me. I find it difficult to not be lost in the maze of the mix of support libraries and integrated API. – Vincent Hiribarren May 12 '16 at 07:02
  • The best approach is always stick to the support library while you're coding. – Anderson Madeira May 12 '16 at 12:21