55

hi how to clear fragment back stack am using below logic it's not working...

for(int i = 0; i < mFragmentManager.getBackStackEntryCount(); ++i) {            
     mFragmentManager.popBackStack();
}

help me..

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
venu
  • 2,971
  • 6
  • 40
  • 59

6 Answers6

123

Try this

mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
dracula
  • 4,413
  • 6
  • 26
  • 31
  • 6
    Please elaborate as this is one of the best solutions. You may have to call popBackStackImmediate instead if you are adding new stuff to the backstack right after clearing it. – marsbear Aug 17 '13 at 16:29
  • 14
    This would only pop the top state, not clear the backstack if more than one. – J.G.Sebring Dec 18 '14 at 13:10
  • This loads the previous Fragment before commit the transaction with the new Fragment. You can't see it but programmatically is calling `onActivityCreated()` of the previous Fragment before the new one. – VasileM May 10 '19 at 15:20
  • @VasileM and how do we overcome that? – Damia Fuentes Jul 04 '23 at 00:09
82

Answer above is almost correct, but you need a guard around the fragment back list as it can be empty:

private void clearBackStack() {
    FragmentManager manager = getSupportFragmentManager();
    if (manager.getBackStackEntryCount() > 0) {
        FragmentManager.BackStackEntry first = manager.getBackStackEntryAt(0);
         manager.popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
}
johnarleyburns
  • 1,532
  • 12
  • 13
19
while (getSupportFragmentManager().getBackStackEntryCount() > 0){
    getSupportFragmentManager().popBackStackImmediate();
}
JeanK
  • 292
  • 2
  • 4
  • This should be the accepted answer if you elaborated more. Clearing the whole backstack is what we want – HaydenKai Dec 25 '16 at 07:28
  • 2
    But it shows the last one. Usually, after that you just add new fragment to backstack. – Draško Feb 13 '17 at 20:15
  • One caveat here is that popping the backstack in a loop like this will call lifecycle events in all of the fragments it pops (the creation methods as well as the shutdown methods), which might leave you with unintended side effects. – Teun Kooijman Sep 15 '17 at 14:26
17

one way is to tag the backstack and when you want to clear it

mFragmentManager.popBackStack("myfancyname", FragmentManager.POP_BACK_STACK_INCLUSIVE);

where the "myfancyname" should match the string you used with addToBackStack. E.g.

Fragment fancyFragment = new FancyFragment();     
fragmentTransaction.replace(R.id.content_container, fancyFragment, "myfragmentag");
fragmentTransaction.addToBackStack("myfancyname");

the backstack's name and the fragment's tag name can be the same but there are no constrains on this regard

From the documentation

If set, and the name or ID of a back stack entry has been supplied, then all matching entries will be consumed until one that doesn't match is found or the bottom of the stack is reached. Otherwise, all entries up to but not including that entry will be removed.

if you don't want to use a name for your backstack you can pass use a first parameter

 mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • How do you give name to backstack? – Gokhan Arik Feb 03 '14 at 21:01
  • 1
    Gokhan, the name is not the name of the backstack, it's the Fragment's tag name. For his example, the Fragment that he's popping would have been added like this: `mFragmentManager.beginTransaction().add(mFragment, "myfancyname");` – nt-complete Feb 25 '14 at 17:38
12

This is a bit late but I just had this problem myself. You can do:

FragmentManager manager = getFragmentManager();
FragmentManager.BackStackEntry first = manager.getBackStackEntryAt(0);
manager.popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);

Pretty self explanatory; you just get the first entry, get its id, and then pop everything up to and including the entry with that id.

Richard Fung
  • 1,020
  • 6
  • 14
0

The best option I ever seen is here.

            int count = getSupportFragmentManager().getBackStackEntryCount();
            if (count > 0) {
                getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
sajid45
  • 519
  • 5
  • 8