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

时间:2014-08-06 03:45:38

标签: android android-fragments navigation-drawer

我有一个导航抽屉项目,我只是使用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;
        }
    }

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

1 个答案:

答案 0 :(得分:0)

试用此代码 ::而不是replacing-fragments .... EveryTime 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;
            }
        }

<强> Check Developers site for more info

相关问题