简体   繁体   中英

Maintaining Backstack for Fragment with ChildFragments android

I Have an Activity A which calls a Fragment F1. Now this Fragment calls another Fragment F2 using below code:

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 :

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:

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). Its successfully refreshes the CF1 fragment with new list but issue is when I press back button. On pressing back it should go to F3 (from where CF1 was called) but it remains in CF1 with state prior to calling Dialog. Pressing Back again takes it to Fragment 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. 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. Please advise.

use below code to remove fragments from backstack

FragmentManager fragmentManager = getSupportFragmentManager();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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