繁体   English   中英

在导航抽屉片段中保留状态

[英]retain state in navigation drawer fragments

我有一个导航抽屉项目,我只是使用Android Studio从“创建新项目>导航抽屉”中进行了修改。 导航抽屉中有几个“菜单”,每个菜单都打开不同的片段。问题是每次我使用抽屉进行导航时,都会重新创建片段。 意味着当用户对片段进行了某些更改后,稍后用户再次访问该片段时,所做的更改将丢失。 我用过setRetainInstance(true); 但这似乎只能改变方向。

这是我的代码的一部分,用于显示所选的片段:

private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }

        Fragment fragment = null;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        FragmentManager fm = getFragmentManager();

        switch (position) {
            case 0:
                fragment = fm.findFragmentByTag("fragmentDashboard");
                if (fragment == null){
                    fragment = new Dashboard();
                    transaction.replace(R.id.container, fragment, "fragmentDashboard");
                    transaction.addToBackStack(null);
                    transaction.commit();
                }else{
                    transaction.remove(getFragmentManager().findFragmentByTag("fragmentDashboard"));
                    transaction.commit();
                    fm.popBackStack();
                }

                break;
            case 1:
                fragment = fm.findFragmentByTag("fragmentExpenses");
                if (fragment == null) {
                    fragment = new Expenses();
                    transaction.replace(R.id.container, fragment, "fragmentExpenses");
                    transaction.addToBackStack(null);
                    transaction.commit();
                }else{
                    transaction.remove(getFragmentManager().findFragmentByTag("fragmentExpenses"));
                    transaction.commit();
                    fm.popBackStack();
                }

                break;


            default:
                break;
        }
    }

编辑:这个问题与导航抽屉保存片段状态是我解决我的问题的解决方案,但是答案太简短了..而且我在android开发中还很新。 有人可以帮忙吗?

尝试使用此代码 ::而不是replacing-fragments ....每次HideShow片段

说明 ::

  • 而不是destroying the fragments ..第一次创建该fragment ,每隔一次find the fragment and use itinstead of replacing it

代替代码 ::

   private void selectItem(int position) {
            mCurrentSelectedPosition = position;
            if (mDrawerListView != null) {
                mDrawerListView.setItemChecked(position, true);
            }
            if (mDrawerLayout != null) {
                mDrawerLayout.closeDrawer(mFragmentContainerView);
            }
            if (mCallbacks != null) {
                mCallbacks.onNavigationDrawerItemSelected(position);
            }

            Fragment fragment = null;
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            FragmentManager fm = getFragmentManager();

            switch (position) {
                case 0:
                    fragment = fm.findFragmentByTag("fragmentDashboard");
                    if (fragment == null){
                        fragment = new Dashboard();
                        transaction.replace(R.id.container, fragment, "fragmentDashboard");
                        transaction.addToBackStack(null);
                        transaction.commit();
                    }else{
                        transaction.remove(getFragmentManager().findFragmentByTag("fragmentDashboard"));
                        transaction.commit();
                        fm.popBackStack();
                    }

                    break;
                case 1:
                    fragment = fm.findFragmentByTag("fragmentExpenses");
                    if (fragment == null) {
                        fragment = new Expenses();
                        transaction.replace(R.id.container, fragment, "fragmentExpenses");
                        transaction.addToBackStack(null);
                        transaction.commit();
                    }else{
                        transaction.remove(getFragmentManager().findFragmentByTag("fragmentExpenses"));
                        transaction.commit();
                        fm.popBackStack();
                    }

                    break;


                default:
                    break;
            }
        }

用这个::

   private void selectItem(int position) {
            mCurrentSelectedPosition = position;
            if (mDrawerListView != null) {
                mDrawerListView.setItemChecked(position, true);
            }
            if (mDrawerLayout != null) {
                mDrawerLayout.closeDrawer(mFragmentContainerView);
            }
            if (mCallbacks != null) {
                mCallbacks.onNavigationDrawerItemSelected(position);
            }

            Fragment fragment = null;
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            FragmentManager fm = getFragmentManager();

            switch (position) {
                case 0:
                    fragment = fm.findFragmentByTag("fragmentDashboard");
                    if (fragment == null){
                        fragment = new Dashboard();
                        transaction.add(R.id.container, fragment, "fragmentDashboard");
                        transaction.addToBackStack(null);
                        transaction.commit();
                    }else{
                        transaction.show(getFragmentManager().findFragmentByTag("fragmentDashboard"));
                        transaction.commit();
                        fm.popBackStack();
                    }

                    break;
                case 1:
                    fragment = fm.findFragmentByTag("fragmentExpenses");
                    if (fragment == null) {
                        fragment = new Expenses();
                        transaction.add(R.id.container, fragment, "fragmentExpenses");
                        transaction.addToBackStack(null);
                        transaction.commit();
                    }else{
                        transaction.show(getFragmentManager().findFragmentByTag("fragmentExpenses"));
                        transaction.commit();
                        fm.popBackStack();
                    }

                    break;


                default:
                    break;
            }
        }

查看开发者网站以获取更多信息

暂无
暂无

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

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