43

I changed from the original ActionBar to the AppCompat Toolbar and setSupportActionBar(toolbar). When I am using getSupportActionBar() and setDisplayHomeAsUpEnabled(true) for the back arrow, the click never calls onOptionsItemSelected or any other listener method.

Do I have to implement some special listener for it? Befor everything was working just fine.

EDIT: Initialise the ActionBar:

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mActionBar = getSupportActionBar();
mActionBar.setHomeButtonEnabled(true);

and after replacing the content with a Fragment I do this:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mActionBar.setDisplayHomeAsUpEnabled(true);
HpTerm
  • 8,151
  • 12
  • 51
  • 67
Informatic0re
  • 6,300
  • 5
  • 41
  • 56

6 Answers6

114

I know this question has been answered but I found the real cause of the problem after 2 days of frustration.

Take a look at the ActionBarDrawerToggle documentation: https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html

Notice the two constructors there. My mistake was that I was using the second constructor that was taking a toolbar as a parameter. It took me so long to notice the last line in the consturctor documentation: "Please use ActionBarDrawerToggle(Activity, DrawerLayout, int, int) if you are setting the Toolbar as the ActionBar of your activity."

After using the first constructor onOptionsItemSelected() was called with no issues.

Don't forget to call the ActionBarDrawerToggle.onConfigurationChanged() and onOptionsItemSelected() from your activity as described in the last part here: http://developer.android.com/training/implementing-navigation/nav-drawer.html

  • 2
    In short, if I understand correctly: When adding toolbar to parameter of ActionBarDrawer constructor the navuparrow will show when drawer is open and hamburger will show when drawer is closed. home button callback will not be called. While constructing ActionBarDrawer without the toolbar as parameter we can use home button callback. this means we can manage fragments backstack for example. How to sync home button to animate it like in Gmail application? Also while using option without toolbar as paramer hamburger icon just does not show up for me. Any explanation would be appreciated. – Darek Deoniziak Dec 23 '15 at 09:30
  • 2
    Thank you, saved my day also ! There are dozens of questions and responses out there and none of them works for me. I think we should add some keywoards for this question, like - "Navigation drawer not showing hamburger icon" or "Navigation drawer not open on menu icon click on toolbar". – mikron Jan 26 '16 at 15:05
  • In my case it hasn't helped, even a navigation drawer behaviour has changed. So an accepted answer helped (also it exists in http://stackoverflow.com/questions/34903326/toolbar-back-button-is-not-calling-onoptionsitemselected-android). But I couldn't create a menu in onCreateOptionsMenu (though it is triggered). – CoolMind Dec 01 '16 at 11:56
  • If anyone used this solution, but ran into problems using their drawer afterwards, you can see my post [here](http://stackoverflow.com/questions/43102418/drawer-not-working-with-toolbar-as-support-actionbar). – Ben Mar 29 '17 at 19:25
28

I had to implement an OnClickListener for the DrawerToggle:

mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        popStackIfNeeded();
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        mActionBar.setDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    }
});

this fixed my issue.

Informatic0re
  • 6,300
  • 5
  • 41
  • 56
  • 1
    This is not the correct way of doing it, you shouldn't handle toggle click unless you have a good reason to. Please see Andrei's solution. – Genc Oct 27 '18 at 00:12
4

I had several issues using the setSupportActionBar() method. It also ignores certain color themes, so you can't style the back arrow or overflow icon (don't remember which). I just did away with ActionBar integration and use the Toolbar natively. So, as an alternative, you could do that as follows.

Just include the toolbar like you would normally, in your layout, assume it's using an id of @+id/toolbar.

Then, in code:

_toolbar = (Toolbar) findViewById(R.id.toolbar);
_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        handleNavButtonPress();
    }
});
_toolbar.setOnMenuItemClickListener(_menuItemClickListener);
_toolbar.inflateMenu(R.menu.message_list_menu);
Menu menu = _toolbar.getMenu();

In this case, _menuItemClickListener can almost literally be your current onOptionsItemSelected() method renamed. You just don't have to check for menu being null anymore.

To remove items from the menu, just call menu->clear(). So in my onPause, I clear the menus and onResume, I inflate them, in my fragments, and each fragment sets the click handler in onResume. You need to always clean up, because Android won't do that for you in this approach, and the toolbar will keep adding menus every time you inflate.

One last note, to make it all work, you have to disable the action bar completely and remove it from the style.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
reactive-core
  • 1,071
  • 1
  • 7
  • 15
  • the navigationOnClickListener was never called in my case. But now I figured out, that I have to implement the DrawerToggle setToolbarNavigationClickListener. I am using the NavigationDrawer btw., that was the problem I guess. – Informatic0re Oct 27 '14 at 15:15
  • https://developer.android.com/reference/android/support/v7/widget/Toolbar.html#setNavigationOnClickListener(android.view.View.OnClickListener) – reactive-core Sep 21 '15 at 19:24
  • 1
    @IgorGanapolsky, no, it's available in android.support.v7.widget.Toolbar. – CoolMind Dec 01 '16 at 09:17
0

One thing that wasn't mentioned:
If you build the options menu dynamically in onCreateOptionsMenu and return null there, the up button in the action bar will not work.
Works fine if you return the Menu parameter without adding anything into it.

Tested on emulator API 19

Max Izrin
  • 938
  • 1
  • 11
  • 17
0

If you've tried everything and it just doesn't work, you can implement your own click listener like so:

myNavList.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String item = myNavList.getItemAtPosition(position).toString();
        Toast.makeText(this, "You selected " + item, Toast.LENGTH_SHORT).show();
    }
});
Prof
  • 657
  • 1
  • 14
  • 24
0

In my case the setHasOptionsMenu(true); wasn't enabled on onCreateView. Hope this helps someone.

Coder Absolute
  • 5,417
  • 5
  • 26
  • 41