3

I have a particular situation where, I have a parent Activity which extends ActionBarActivity in that Activity I declare and initialize my DrawerLayout with all the boiler plate code for implementing a Navigation Drawer

Then to save time I created new Activities which extend this DrawerActivity in my code so you can open the navigation drawer in all these activities.

The problem occurs when this happens:

Assume Activities are:

Activity A = [A]
Activity B = [B]

Both extend DrawerActivity

[A] --- Open Drawer and Open --> [B] --- Press Back Button ---> [A]

The Navigation Drawer opens from the Home Button when you are in [A], but when I press the back button from [B] I can't open the Navigation Drawer from the Home Button but I can slide out the drawer.

Can someone explain to me what I am doing wrong here, I was wondering if there was an issue with ActionBarDrawerToggle.syncState() but I tried implementing that everywhere I could and it didn't solve the problem.

Like I mentioned above all the boiler plate code is already written, example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id) {
        case android.R.id.home:
            if (!mDrawerLayout.isDrawerOpen(recyclerView)) {
                mDrawerLayout.openDrawer(recyclerView);
                mActionBarDrawerToggle.syncState();
            } else if (mDrawerLayout.isDrawerOpen(recyclerView)) {
                mDrawerLayout.closeDrawer(recyclerView);
                mActionBarDrawerToggle.syncState();
            }
            break;
        case R.id.action_logout:
            new DeauthorizeTask().execute();
    }
    return super.onOptionsItemSelected(item);
}
AndyRoid
  • 5,062
  • 8
  • 38
  • 73

1 Answers1

3

Make sure you have overriden onOptionsItemSelected() correctly.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • Actually I might have forgotten the portion where you call the .onOptionItemSelected(item) on the DrawerToggle. Is there any way that you would suggest this for switch statements? Please see code above. – AndyRoid Mar 15 '15 at 06:50
  • Don't worry about providing sample code, surrounding the switch statement with that if statement solved the issue. Thanks Apurva! – AndyRoid Mar 15 '15 at 06:52
  • 1
    i am getting the same issue but this answer does not solve it – Pankaj Nimgade Feb 22 '16 at 19:38