简体   繁体   English

使用ChildFragments android维护片段的Backstack

[英]Maintaining Backstack for Fragment with ChildFragments android

I Have an Activity A which calls a Fragment F1. 我有一个活动A,称为片段F1。 Now this Fragment calls another Fragment F2 using below code: 现在,此片段使用以下代码调用另一个片段F2:

Fragment fragment = new F2Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack("fragment");
ft.commit();

Then Fragement F2 calls another Fragment F3 using similar code : 然后Fragement F2使用类似的代码调用另一个Fragment F3:

Fragment fragment = new F3Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack("fragment");
ft.commit();

Fragment F3 has 3 child Fragments (for 3 Tabs) and they are added using tabhost as below: 片段F3有3个子片段(用于3个选项卡),并使用tabhost如下添加它们:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View rootView = inflater.inflate(R.layout.main_tab, container,
                false);

        mTabHost = (FragmentTabHost) rootView
                .findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(),
                R.layout.main_tab);

        Bundle arg1 = new Bundle();

        arg1.putInt("CF1", 1);
        mTabHost.addTab(
                mTabHost.newTabSpec("Tab1").setIndicator("CF1",
                        getResources().getDrawable(R.drawable.tab_left)),
                CF1Fragment, arg1);

        Bundle arg2 = new Bundle();
        arg2.putInt("CF2", 2);
        mTabHost.addTab(
                mTabHost.newTabSpec("Tab2").setIndicator("CF2",
                        getResources().getDrawable(R.drawable.tab_middle)),
                CF2Fragment.class, arg2);

        Bundle arg3 = new Bundle();
        arg3.putInt("CF3", 3);
        mTabHost.addTab(
                mTabHost.newTabSpec("Tab3").setIndicator("CF3",
                        getResources().getDrawable(R.drawable.tab_rigth)),
                CF3Fragment.class, arg2);

        return rootView;

    }

Till this point everything is working fine with proper back navigation. 到此为止,通过适当的向后导航,一切都可以正常工作。 Child Fragment calls a dialogfragment as below 子片段调用对话框片段,如下所示

            ConfirmDialogFragment cd = new ConfirmDialogFragment();
            cd.show(fm, "Confirm Fragment");

In Dialog I have a button, pressing on which has to refresh the CF1 Fragmnet(from where its called). 在对话框中,我有一个按钮,按此按钮必须刷新CF1 Fragmnet(从其调用的位置)。 Its successfully refreshes the CF1 fragment with new list but issue is when I press back button. 它成功刷新了带有新列表的CF1片段,但是问题是当我按下“后退”按钮时。 On pressing back it should go to F3 (from where CF1 was called) but it remains in CF1 with state prior to calling Dialog. 按下时,它应该转到F3(从那里调用CF1),但是在调用Dialog之前,它保持CF1的状态。 Pressing Back again takes it to Fragment F3. 再按一次Back返回片段F3。 I tried many things but nothing seems to be working for me. 我尝试了很多事情,但似乎没有任何效果。 I assume that when Confirm Dialog is called from CF1 it places itself on top of Backstack, so when back is pressed from CF1 it resumes to state from where Dialog Fragment got called. 我假设当从CF1调用Confirm Dialog时,它会将自己置于Backstack的顶部,因此,当从CF1按下Back时,它将恢复到调用Dialog Fragment的状态。 I understand if somehow this isn't placed on backstack while calling dialogfragment , this would be resolved but nothing seem to be working as of now. 我知道如果在调用dialogfragment时以某种方式未将其放在backstack上,则可以解决此问题,但到目前为止似乎没有任何效果。 Please advise. 请指教。

use below code to remove fragments from backstack 使用下面的代码从堆栈中删除片段

FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManagerfragmentManager = getSupportFragmentManager();

            if (fragmentManager .getBackStackEntryCount() > 0){
                fragmentManager .popBackStack();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM