使用导航抽屉时,在Backstack中只有一个片段

时间:2015-08-13 05:55:10

标签: android android-fragments fragment-backstack

我正在使用导航抽屉。所以当我的应用程序启动时,我正在调用homeFragment并将其保留在backstack中。现在,如果用户从导航抽屉中选择任何选项,我将打开相应的片段,但不将它们添加到backstack。所以我想要的是,即使用户打开了10个片段,在按下时它们也应该被带回homeFragment。但是使用我的代码,应用程序退出后甚至homeFragment都在后台。

openFragments代码

public static void replaceFragment(FragmentActivity activity, Fragment fragment, boolean addToBackStack) {


        try {
            String backStateName = fragment.getClass().getName();
            String fragmentTag = backStateName;
            FragmentManager manager = activity.getSupportFragmentManager();
            boolean fragmentPopped = manager.popBackStackImmediate(fragmentTag, 0);
            if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) {
                FragmentTransaction ft = manager.beginTransaction();
                ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
                ft.replace(R.id.frag_container, fragment, fragmentTag);
                if (addToBackStack) {
                    ft.addToBackStack(backStateName);
                }
                ft.commit();
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
    } 

来自MainActivity

private void init() {

        setUpToolBar();

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerFrag = (NavigationDrawerFrag) getSupportFragmentManager().findFragmentById(R.id.frag_nav_drawer);
        drawerFrag.setUp(drawerLayout, toolbar, R.id.frag_nav_drawer);

        CommonFunctions.replaceFragment(this, new HomeFrag(), true);


    }

在NavigationDrawer项目上单击

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (position == 1) {
            CommonFunctions.replaceFragment(getActivity(), new ProfileFrag(), false);

        } else if (position == 2) {
            CommonFunctions.replaceFragment(getActivity(), new CelebrityFrag(), false);

        } else if (position == 4) {
            CommonFunctions.replaceFragment(getActivity(), new AboutUsFrag(), false);

        } else if (position == 5) {
            CommonFunctions.replaceFragment(getActivity(), new TermsAndConditions(), false);



        }


        lvNavItems.setItemChecked(position, true);
        //lvNavItems.setSelection(position);
        lvNavItems.setSelected(true);
        mDrawerLayout.closeDrawer(fragContainer);


    }

2 个答案:

答案 0 :(得分:0)

在activity中添加onBackPressed()方法来检查backstack,如果entry大于零则调用popbackstack方法。

@Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() != 0) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }

答案 1 :(得分:-1)

//Put this in your onNavigationItemSelected of MainActivity

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//FrameLayout where you load your home fragment tools:layout="@layout/frag_home"
ft.replace(R.id.content_frame,nav_selected);
if(getSupportFragmentManager().getBackStackEntryCount() != 0){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    getSupportFragmentManager().popBackStack();
}
//Don't add Home screen
if(id != R.id.nav_home){
    ft.addToBackStack(null);
}
ft.commit();