How to remove Fragments from backstack

5.8k views Asked by At

There is an Activity with 5 fragments (wizard).

StartFrag  -(start)-> Frag1 -(next)-> Frag2 -(next)-> SubmitFrag
-(submit)-> SuccessFrag.

After Taping SubmitMore button on Success fragment, I want to remove Frag1, Frag2 and SubmitFrag from backstage and return to StartFrag. How to do that?

3

There are 3 answers

0
Manish DEWAN On BEST ANSWER

You could try this

 FragmentManager fm = getFragmentManager();
 FragmentTransaction ft=fm.beginTransaction();
 ft.add(R.id.group,startFrag,"");
 ft.addtoBackStack("startFrag");
 ft.commit();

    enter code here

 // add other 4 fragments here


 when you want to go startFrag on click of some button you can try below code.
 fm.popBackStack("startFrag",0);
 //where startFrag is the tag which you specify when you called
 //addtoBackStack("startFrag")
0
funkomatic On

You could try this

FragmentManager fm = getFragmentManager(); 
int count = fm.getBackStackEntryCount();
for(int i = 0; i < count; ++i) {    
fm.popBackStack();
}
0
SAM On

try this one

   mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);