0

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

打开片段的代码

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


    }
4

2 回答 2

0

在活动中添加 onBackPressed() 方法以检查回栈,如果条目大于零,则调用 popbackstack 方法。

@Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() != 0) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
于 2015-08-13T15:14:37.900 回答
-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();
于 2017-08-23T13:30:54.653 回答