屏幕上的两个动态片段(背面)

Two dynamic fragment on screen (back)

我有一个带有 Fragment 的应用程序。它从屏幕上的两个动态片段开始。在片段 B 上,我有一个列表。当我点击该项目时,我将两个片段更改为 C 和 D。

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

当我按下按钮返回时,只有顶部片段发生变化,片段 D 保持可见,而我想看到 A/B。

这是我的 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(...);

    }
}

您需要一次替换两个片段,然后它会像以前一样返回。

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