2017-08-16 1 views
0

Comment puis-je ajouter un fragment et libérer toute la pile arrière d'Android en une action?Ajouter un fragment et libérer des fragments de pile en une seule action dans Android

J'ai essayé:

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

Puis ajouter mon nouveau fragment:

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 

Mais je reçois:

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

Répondre

1

Utilisez ft.addToBackStack (null) à éviter la pile arrière de fragments

par exemple: -

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();