Android: save Snippet

Is it possible to save a fragment while I call replaceout FragmentManagerto open a new fragment?

Basically, I don’t want to pause a fragment while navigating (through a method replace) to another fragment.

Is it possible? Or is it the right approach to always create an instance of a new fragment every time I need to open it and restore its previous state?

Thank!

+4
source share
1 answer

FragmentManger . onDestroyView(), onDestroy() onDetach() . , , FragmentManger hide() show()! , .

, .

        fragmentManager.beginTransaction()
            .add(R.id.new_card_container, FragmentA)
            .add(R.id.new_card_container,FragmentB)
            .hide(FragmentB)
            .commit();

, show() . show() FragmentA, , , FragmentB, , .

, .

public void showOtherFragment() {

    if(FragmentB.isHidden()){
        fragmentManager.beginTransaction()
                .show(FragmentB).commit();

    } else {
        fragmentManager.beginTransaction()
                .hide(FragmentB).commit();
    }
}

, , , ( !), , onDistroyView .

, , onCreateView(), ( !), onCreateView() , , , - FragmentManger ( bundle). , : 1) onSaveInstaneState().

@Override
protected void onSaveInstanceState(Bundle outState) {
    fragmentManager.beginTransaction()
            .detach(FragmentA)
            .detach(FragmentB)
            .commit();

    super.onSaveInstanceState(outState);
}

, , , EditText, , . , , , , !

onSaveInstaneState(), .

@Override
protected void onSaveInstanceState(Bundle outState) {
    boolean isFragAVisible = true;
    if(!FragmentB.isHidden())
        isFragAVisible = false;

    outState.putBoolean("isFragAVisible",isFragAVisible);

    super.onSaveInstanceState(outState);
}

onCreate , , savedInstanceState == null. , , . . . , , .

    fragmentManager = getFragmentManager();

    if(savedInstanceState == null){

        FragmentA = new FragmentA();
        FragmentB = new FragmentB();
        fragmentManager.beginTransaction()
                .add(R.id.new_card_container, FragmentA, "fragA")
                .add(R.id.new_card_container, FragmentB, "fragB")
                .hide(FragmentB)
                .commit();

    } else {
        FragmentA = (FragmentA) fragmentManager.findFragmentByTag("fragA");
        FragmentB = (FragmentB) fragmentManager.findFragmentByTag("fragB");

        boolean isFragAVisible = savedInstanceState.getBoolean("isFragAVisible");
        if(isFragAVisible)
            fragmentManager.beginTransaction()
                    .hide(FragmentB)
                    .commit();
        else
            fragmentManager.beginTransaction()
                    .hide(FragmetA) //only if using transaction animation
                    .commit();
    }

, . , FragmentA. , FragmentB FragmentA, FragmentB ( ), FragmentB, , FragmentA ( ). - ( developer.goodle.com)

public void flipCard(String direction) {
    int animationEnter, animationLeave;
    if(direction == "left"){
        animationEnter = R.animator.card_flip_right_in;
        animationLeave = R.animator.card_flip_right_out;
    } else {
        animationEnter = R.animator.card_flip_left_in;
        animationLeave = R.animator.card_flip_left_out;
    }

    if(cardBack.isHidden()){
        fragmentManager.beginTransaction()
                .setCustomAnimations(animationEnter, animationLeave)
                .hide(cardFront)
                .show(cardBack)
                .commit();

    } else {
        fragmentManager.beginTransaction()
                .setCustomAnimations(animationEnter,animationLeave)
                .hide(cardBack)
                .show(cardFront)
                .commit();
    }
}
+7

All Articles