Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation drawer open after popping fragment backstack

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;
like image 489
Stampede10343 Avatar asked Jan 22 '26 19:01

Stampede10343


1 Answers

replace

drawerLayout.closeDrawers();

with

drawerLayout.closeDrawer();
like image 173
sasuketaichou Avatar answered Jan 24 '26 11:01

sasuketaichou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!