How to clear backstack snippet in android

Hi, how to clear a fragment back, I use below logic, it does not work ...

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

help me..

+42
android android-fragments
Jun 14 '13 at 11:01
source share
6 answers

try it

 mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
+91
Jun 14 '13 at 11:06 on
source share

The answer above is almost correct, but you need a defender around the list of fragments, as it may 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); } } 
+65
Apr 27 '14 at 19:38
source share
 while (getSupportFragmentManager().getBackStackEntryCount() > 0){ getSupportFragmentManager().popBackStackImmediate(); } 
+15
Jul 31 '14 at 13:24
source share

one way is to tag backstack and if you want to clear it

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

where "myfancyname" should match the line you used with addToBackStack . For example.

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

the backstack name and fragment tag name may be the same, but there are no restrictions in this regard

From the documentation

If set, and the name or identifier of the rear stack entry is specified, then all matching entries will be consumed until a match is found or the bottom of the stack is reached. Otherwise, all entries prior to but not including this entry will be deleted.

if you do not want to use the name for your backstack, you can pass the first parameter

  mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
+14
Jun 14 '13 at 11:05
source share

This is a bit late, but I had the problem itself. You can do:

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

Pretty explanatory; you just get the first record, get your identifier, and then pop up everything before and including the record with that identifier.

+6
Dec 15 '13 at 6:05
source share

The best option I've ever seen is here.

  int count = getSupportFragmentManager().getBackStackEntryCount(); if (count > 0) { getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); } 
0
Oct 02 '17 at 12:19
source share



All Articles