2
votes

So I have an activity with a fragment in it, the fragment has a navigation drawer and the page's content. When I open the drawer and click an item the fragment is replaced with a new fragment. When I press the back button I call popBackStack on the fragment manager and it goes back to the first fragment but the navigation drawer is open.

A couple things to note: When pressing an item in the drawer I call closeDrawers on the drawer layout and the drawer closes while the fragment is replaced. If I press the UP button in the action bar I can replace the fragment container with a NEW main fragment, but I'd prefer to be able to pop the fragment off the stack.

Why is the drawer sticking open when I return to the main fragment?

In the click listener for drawer items:

case 4:
        drawerLayout.closeDrawers();
        Fragment aboutFragment = new AboutFragment();
        fragmentTransaction.replace(R.id.fragment_container, aboutFragment, "AboutFragment");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        break;

This is the UP arrow in the action bar, this works but is hacky:

case android.R.id.home:
    getFragmentManager()
            .beginTransaction()
            .replace(R.id.fragment_container, new MainActivityFragment())
            .commit();
    break;
2

2 Answers

1
votes

replace

drawerLayout.closeDrawers();

with

drawerLayout.closeDrawer();
0
votes

In the onCreateView of your firt fragment, just close the drawer mDrawerLayout.closeDrawer(Gravity.LEFT);