在Android中的一个操作中添加片段并释放堆栈片段

时间:2017-08-16 13:20:17

标签: android fragmentmanager

如何在一个操作下添加片段并释放Android中的所有后端堆栈?

我试过了:

getSupportFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);

然后添加我的新片段:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
    fragmentTransaction.replace(R.id.content_frame, fragment);
    fragmentTransaction.commitAllowingStateLoss(); 
    // or 
    fragmentTransaction.commit(); // i tried both of them

但我明白了:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

1 个答案:

答案 0 :(得分:1)

使用ft.addToBackStack(null)来避免碎片堆栈

例如: -

SignUpAsFragment signUpAsFragment=new SignUpAsFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.user_signUp_frag_container,signUpAsFragment); // container is your FrameLayout container
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();