如何从backstack中删除碎片

时间:2015-06-09 09:45:54

标签: android fragment back-stack fragment-backstack

有一个包含5个片段的活动(向导)。

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

在成功片段上点击SubmitMore按钮后,我想从后台删除Frag1,Frag2和SubmitFrag并返回StartFrag。怎么做?

3 个答案:

答案 0 :(得分:2)

你可以试试这个

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

答案 1 :(得分:2)

尝试这个

   mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

答案 2 :(得分:1)

你可以试试这个

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