Two dynamic fragment on screen (back)

70 views Asked by At

I have an application with Fragment. It start with two dynamic fragments on screen. On the fragment B, I have a list. When I click on the item, I change the two fragments to C and D.

_____    _____                      _____               _____
| A |    | C |                      | A |               | A |
_____ => _____  when back, I have : _____  but I would  _____
| B |    | D |                      | D |               | B |
_____    _____                      _____               _____

When I press button back, I have only the top fragment which change, the Fragment D stay visible whereas I want see A/B.

This is my class :

public class MainActivity extends Activity implements
        FragmentBListener,
        FragmentCListener {

    private static final String TAG_FRAGMENT_A = "FragmentATag";
    private static final String TAG_FRAGMENT_B = "FragmentBTag";
    private static final String TAG_FRAGMENT_C = "FragmentCTag";
    private static final String TAG_FRAGMENT_D = "FragmentDTag";

    FragmentManager fragmentManager = getFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
        initFragments();
    }

    private void initFragments() {
        setFragment(TAG_FRAGMENT_A, R.id.top_framelayout);
        setFragment(TAG_FRAGMENT_B, R.id.main_framelayout);
    }

    private Fragment setFragment( String tagFragment, int containerViewId) {
        FragmentTransaction fTransaction = fragmentManager.beginTransaction();
        Fragment mFragment = null;
        if(tagFragment.equals(TAG_FRAGMENT_A))      mFragment = setFragmentA(fTransaction, containerViewId);
        else if(tagFragment.equals(TAG_FRAGMENT_B)) mFragment = setFragmentB(fTransaction, containerViewId);
        else if(tagFragment.equals(TAG_FRAGMENT_C)) mFragment = setFragmentC(fTransaction, containerViewId);
        else if(tagFragment.equals(TAG_FRAGMENT_D)) mFragment = setFragmentD(fTransaction, containerViewId);
        fTransaction.addToBackStack(null);
        fTransaction.commit();
        return mFragment;
    }

    private Fragment setFragmentA(FragmentTransaction fTransaction, int containerViewId) {
        FragmentA fragment = (FragmentA) fragmentManager.findFragmentByTag(TAG_FRAGMENTA);
        if (fragment == null) {
            fragment = new FragmentA();
            if(fragment != null)
                fTransaction.replace(containerViewId, fragment,TAG_FRAGMENTA);
        } 
        return fragment;
    }

    private Fragment setFragmentB(FragmentTransaction fTransaction, int containerViewId) {
        FragmentB fragment = (FragmentB) fragmentManager.findFragmentByTag(TAG_FRAGMENT_B);
        if (fragment == null) {
            fragment = new FragmentB();
            if(fragment != null)
                fTransaction.replace(containerViewId, fragment,TAG_FRAGMENT_B);
        }
        return fragment;
    }

    private Fragment setFragmentC(FragmentTransaction fTransaction, int containerViewId) {
        FragmentC fragment = (FragmentC) fragmentManager.findFragmentByTag(TAG_FRAGMENT_C);
        if (fragment == null) {
            fragment = new FragmentC();
            if(fragment != null)
                fTransaction.replace(containerViewId, fragment,TAG_FRAGMENT_C);
        } 
        return fragment;
    }

    private Fragment setFragmentD(FragmentTransaction fTransaction, int containerViewId) {
        FragmentD fragment = (FragmentD) fragmentManager.findFragmentByTag(TAG_FRAGMENT_D);
        if (fragment == null) {
            fragment = new FragmentD();
            if(fragment != null)
                fTransaction.replace(containerViewId, fragment,TAG_FRAGMENT_D);
        }
        return fragment;
    }

    @Override
    public void onUpdateFragmentB() {
        FragmentB fragmentB = (FragmentB) fragmentManager.findFragmentByTag(TAG_FRAGMENT_B);
        if (fragmentB != null) {
            fragmentB.setData(...);
        } 

    }

    @Override
    public void onUpdateD() {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentD fragment = (FragmentD) fragmentManager.findFragmentByTag(TAG_FRAGMENT_D);
        fragment.setData();
    }

    @Override
    public void changeFragments() {
        FragmentD fragmentD = (FragmentD) setFragment(TAG_FRAGMENT_D, R.id.main_framelayout);
        if(fragmentD != null)
            fragmentD.setData(...);

        FragmentC fragmentC = (FragmentC) setFragment(TAG_FRAGMENT_C, R.id.top_framelayout);
        if(fragmentC != null)
            fragmentC.setData(...);
    }


    @Override
    public void onUpdateFragmentsAB {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentB fragmentB =(FragmentB) fragmentManager.findFragmentByTag(TAG_FRAGMENT_B);
        if(fragmentB != null)
            fragmentB.setData(...);
        FragmentA fragmentA = (FragmentA) fragmentManager.findFragmentByTag(TAG_FRAGMENT_A);
        if(fragmentA != null)
            fragmentA.setData(...);

    }
}
1

There are 1 answers

2
Krishna V On BEST ANSWER

You need to replace the both fragments at a time then it come back as previous one, as like .

 public void setFragment(Fragment topFrag, Fragment bottomFrag){
        FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.frag_top, topFrag);
        transaction.replace(R.id.frag_bottom, bottomFrag);
        transaction.addToBackStack(null);
        transaction.commit();
    }