2017-08-16 46 views
0

如何在一個動作中添加一個片段並在Android中釋放所有的背堆棧?在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

使用ft.addToBackStack(空)到避免碎片堆棧

例如: -

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(); 
相關問題