Android Fragment OnBackPress

时间:2015-02-02 12:00:14

标签: android navigation fragment

我有4个碎片。 A B C D

我只在后台堆放了片段A. 我从片段A - >> B - > C - > d

现在在片段D中的背压上,我应该重定向到片段A但是现在A和D片段一起显示在屏幕上。 如何隐藏片段D?

2 个答案:

答案 0 :(得分:0)

如果你popbackstack ..添加片段A并启动任何片段怎么办...

例如:

Fragment fragmentA = new FragmentA();

Fragment fragmentD = new FragmentD();

FragmentManager manager = getFragmentManager();

FragmentTransaction transaction = manager.beginTransaction();

manager.popBackStack();

transaction.addToBackStack(fragmentA.getClass().getName());

transaction.replace(R.id.frame_container, fragmentD);

transaction.commit();

答案 1 :(得分:0)

步骤1:在主活动中编写包含框架布局的代码,其名称为fragcontainer

 getSupportFragmentManager().beginTransaction().add(R.id.frgContainer,new FragOne(),FragOne.class.getName()).commit();

在“FragOne”上编写代码

 FragThird FragThird = new FragThird();
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        // ft.addToBackStack(FragTrackView.class.getName());
        ft.add(R.id.frgContainer, FragThird, FragFourth.class.getName());
        ft.commit();

同样是添加2片段和最后片段像这样添加

 FragFourth FragFourth= new FragFourth();
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
         ft.addToBackStack(FragFourth.class.getName());
        ft.add(R.id.frgContainer, FragFourth, FragFourth.class.getName());
        ft.commit();