I'm creating a drawer with my own toolbar using setSupportActionBar. Previously I was using
new ActionBarDrawerToggle(this, drawer, toolbar, "open", "close");
but because it was implemented with my own toolbar, my onOptionsItemSelected() method wasn't firing, so using Andrei Lupsa's answer here I changed to the following constructor from the docs and the method began working again.
new ActionBarDrawerToggle(this, drawer, "open", "close");
Unfortunately, now the drawer listener is not working anymore. It displays the hamburger icon on the bar, but doesn't open when pressed. Here's the implementation:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(
this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(drawerToggle);
drawerToggle.syncState();
My AndroidManifest uses the following style:
<style name="AppTheme.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
But I've also tried
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
because I thought it had to do with the ActionBarDrawerToggle no longer recognizing the toolbar, but if that were the case, then why does the hamburger icon still display?
Edit Adding onOptionsItemSelected()
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case R.id.action_settings:
break;
case android.R.id.home:
getSupportFragmentManager().popBackStack();
break;
}
return super.onOptionsItemSelected(item);
}
android.R.id.home
was created to only show when BackStack > 0, so when at the base activity, the drawer would show, and when entering the next fragment, the drawer would switch to the home arrow and be able to bring the user back to the base.