Skip to content Skip to sidebar Skip to footer

Handling Fragment Backstack In Navigation Drawer

okay i know there are other questions that on first glance make this one look like a duplicate, but none of these answers work in my case, What i want is the first fragment displa

Solution 1:

When adding the initial fragment, you must not add it to the back stack. You must only do it for the next ones. When the back stack will be empty, the Activity will just finish.

Edit: Here is an explanation of the problem so you can figure out how to fix it:

Each time you add a fragment transaction to the back stack, you allow the user to revert it by pressing the back button and the Activity will return to the state it was before the transaction. If the initial fragment is added to the back stack, then when the user press back, the screen becomes blank, because there was nothing displayed before you added the initial fragment. That's why the initial fragment transaction which adds the first visible fragment to your Activity must not be added to the back stack. Usually you initialize the initial fragment like this:

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState == null) {
        Fragmentfragment=newFirstFragment();
        getSupportFragmentManager().beginTransaction()
            .add(R.id.frame_container, fragment)
            .commit();
    }
}

Solution 2:

BladeCoders answer was more trying to tell me how the backstack works rather than answering my question, i ended up not adding any fragments to the back stack, .addToBackStack(null), and overriding back button in MainActivity, feels like a little bit of a hack but works perfectly

@OverridepublicvoidonBackPressed() {
    FragmentManagerfragmentManager= getSupportFragmentManager();

if (fragmentManager.getBackStackEntryCount() < 1){
        Fragmentfragment=newFirstFragment();
        FragmentTransactionfragmentTransaction=    
        getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, 
        R.anim.pop_enter, R.anim.pop_exit);

        getSupportActionBar().setTitle(mDrawerTitle);

        fragmentTransaction.replace(R.id.frame_container, 
        fragment).addToBackStack("first").commit();

    }else{
        finish();
    }
}

Solution 3:

You can do it even with out backstack its just my point of view to simplify so that it can help some one.

 @Override
publicvoidonBackPressed(){

    Fragment f = getSupportFragmentManager().findFragmentById(R.id.container_body);
    if(f.getClass().getName().equals(HomeFragment.class.getName())){ // here HomeFragment.class.getName() means from which faragment you actually want to exit
        finish();
    }else{
        displayView(0);  //were you want to go when back button is pressed
    }

}





privatevoiddisplayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case0:
            fragment = new HomeFragment();
            title = getString(R.string.app_name);
            break;
        case1:
            fragment = new OffersFragment();
            title = getString(R.string.nav_item_offers);
            break;
        case2:
            fragment = new NotificationFragment();
            title = getString(R.string.nav_item_notifications);
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

Post a Comment for "Handling Fragment Backstack In Navigation Drawer"