hubert
hubert

Reputation: 933

Drawer/Back button in ActionBar not clickable when in back

I used the solution I found here to have the left button on the Action Bar work as Drawer Toggle when there's nothing in the backstack and back button when there is. Here's the implementation

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    ButterKnife.bind(this);
    setSupportActionBar(mToolbar);
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayShowHomeEnabled(true);
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    navigateTo(R.id.menu_drawer);
    initDrawer();
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
        R.string.main_open_menu, R.string.main_close_menu)
    {
      public void onDrawerClosed(View view){
        syncActionBarState();
      }

      public void onDrawerOpened(View view){
        mDrawerToggle.setDrawerIndicatorEnabled(true);
      }
    };
    mOnBackStackChangedListener =
        new android.support.v4.app.FragmentManager.OnBackStackChangedListener() {
      @Override
      public void onBackStackChanged() {
        syncActionBarState();
      }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getSupportFragmentManager().addOnBackStackChangedListener(mOnBackStackChangedListener);
  }


  private void syncActionBarState(){
    int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
    mDrawerToggle.setDrawerIndicatorEnabled(backStackCount == 0);
    if(backStackCount == 0){
      mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }else{
      mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }
  }

As Drawer Toggle the button works fine. When working as back button it doesn't even trigger OnOptionsItemClickListener. What did I miss?

Upvotes: 0

Views: 131

Answers (1)

Inzimam Tariq IT
Inzimam Tariq IT

Reputation: 6748

You have implemented setDisplayHomeAsUpEnabled() method as

getSupportActionBar().setDisplayHomeAsUpEnabled(true);  

so you can make it work as described here which is a simple way to do so

Upvotes: 1

Related Questions