【问题标题】:retain state in navigation drawer fragments在导航抽屉片段中保留状态
【发布时间】:2014-08-06 03:45:38
【问题描述】:

我有一个导航抽屉项目,我只是使用 Android Studio 从“创建新项目 > 导航抽屉”中修改它。导航抽屉中有几个“菜单”,每个菜单都会打开不同的片段。问题是每次我使用抽屉导航时,都会重新创建片段。意思是当用户对一个fragment做了一些改动,之后当用户再次访问fragment时,这些改动就丢失了。我用过 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;
        }
    }

编辑:这个问题Save fragment state with navigation drawer 是我解决问题的最接近的解决方案,但答案太简短了......而且我在 android 开发方面还很陌生。那么有人可以帮忙吗?

【问题讨论】:

    标签: android android-fragments navigation-drawer


    【解决方案1】:

    试试这个代码 :: 而不是replacing-fragments .... 每次HideShow 的片段

    说明::

    • 而不是destroying the fragments .... 创建fragment 为 第一次和每隔一次find the fragment and use it, instead 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;
                }
            }
    

    Check Developers site for more info

    【讨论】:

    • 据我所知,只需将 'if' 中的代码 'transaction.replace' 替换为 'transaction.add' 并将 'else 中的代码 'transaction.remove' 替换为 'transaction.show' ' 正确的?如果是的话,我已经尝试过了,但它不起作用。此外,片段的 UI 变得疯狂,就我在第一个片段中创建的对象而言,将出现在第二个片段中。
    • @imin ..... 嗯....我没有测试过)....尝试使用您的初始配置替换...您可以使用应用程序变量...但我不推荐它,因为数据繁重...所以尝试传递捆绑在片段之间,并使用标志来检查和设置片段中的数据!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多