如何从后台删除片段

How to remove Fragments from backstack

有一个 Activity 有 5 个碎片(向导)。

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

点击 Success 片段上的 SubmitMore 按钮后,我想从后台删除 Frag1、Frag2 和 SubmitFrag,并 return 到 StartFrag。怎么做?

你可以试试这个

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

试试这个

   mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

你可以试试这个

 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")