1

In my app, I used android navigation drawer. Basically, it’s working fine. In that user can navigate to fragments like About us, Home using navigation drawer. But when I implement a back button on title bar it opens the navigation drawer without going back to the previous fragment.

Ex:-

Think that one user starts the app and now he is directed to home fragment. then the user is navigated to ‘About us’ fragment using navigation drawer, and then he tries to go back to home fragment using the back button. When he presses the back button navigation drawer is opened without heading him to home fragment.

I assume that Android also adds open navigation drawer action to back stack.

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
    } else{
        super.onBackPressed();
    }
}

This how I change fragments in onNavigationItemSelected(MenuItem item)

 if(fragment1 != null){
        FragmentManager fragmentManager = getSupportFragmentManager();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.replace(R.id.screen_area, fragment1).addToBackStack("fragment");

        ft.commit();
    }

so, is there any way to go back to previous fragment page escape opening the navigation drawer? Or is there any way to get any previous fragments?

MarGin
  • 2,078
  • 1
  • 17
  • 28
dilusha_dasanayaka
  • 1,401
  • 2
  • 17
  • 30
  • 1
    if I suggest, you must add your fragment when your drawer is closed means when your drawer is closed then the further fragment is added. So now, you don't need to handle it in your back press method. – Devil10 Apr 06 '18 at 06:10
  • @SahilKumar so that means i need to check weather the drawer is closed or not before make the transaction? – dilusha_dasanayaka Apr 06 '18 at 06:15
  • 1
    No just add the drawer listener to your drawer layout and do all the transactions in your drawerClosed() method – Devil10 Apr 06 '18 at 06:17
  • ok.. if you stuck anywhere, just let me know. I will make an answer. Happy coding – Devil10 Apr 06 '18 at 06:20

1 Answers1

1

I am assuming you are using Toolbar for showing back arrow, You want to disable to drawer once you go to fragment for that you have to do two things.

Enable drawer lock mode when you are creating the fragment

public void setDrawerState(boolean isEnabled) {

        if (isEnabled) {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_UNLOCKED);
            drawerToggle.setDrawerIndicatorEnabled(true);
            drawerToggle.syncState();

        }
        else {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            drawerToggle.onDrawerStateChanged(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            drawerToggle.setDrawerIndicatorEnabled(false);
            drawerToggle.syncState();
        }}

Add back button pop operation inside the fragment.

 Toolbar toolbar = (ToolBar)getActivity().findViewById(R.id.toolbarId);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147