Android 导航抽屉片段状态 [英] Android Navigation Drawer Fragment State

查看:23
本文介绍了Android 导航抽屉片段状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的 Android 应用程序使用导航抽屉.在我的第一个片段中,我有一个片段使用 Facebook 的 Graph API 加载数据.因此,当我的应用程序第一次加载时,它首先转到第一个片段.

I'm currently utilizing the Navigation Drawer for my Android APP. In my first fragment, I've a fragment that loads data using Facebook's Graph API. Thus, when my App is first loaded, it first goes to the first fragment.

然后,我使用导航抽屉单击另一个 Fragment 并查看它.

Then, I use the Navigation Drawer to click on another Fragment and view it.

最后,我再次使用导航抽屉返回第一个 Fragment 并查看它.

And then finally, I reuse the Navigation Drawer to proceed back to the first Fragment and view it.

我面临的问题是,如何继续使用已创建的 Fragment 而不是在选择导航抽屉项时重新创建它.我的片段切换代码如下所示.

My issue that I'm facing is, how do I proceed to utilize the Fragment that has been created once instead of re-creating it whenever the Navigation Drawer Item is selected. My code for the switching of the fragments are as shown below.

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new SelectionFragment();
        break;
    case 1:
        fragment = new HomeFragment();
        break;
    case 2:
        fragment = new PhotosFragment();
        break;
    case 3:
        fragment = new CommunityFragment();
        break;
    case 4:
        fragment = new PagesFragment();
        break;
    case 5:
        fragment = new SplashFragment();
        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

我注意到每次选择该选项时确实都有一个 Fragment 的新"实例.我如何实现创建 Fragment 实例 ONCE 并重用它的逻辑,这样我就不必一遍又一遍地连续加载 Fragment.

I noticed that there is indeed a "new" instance of the Fragment every time whenever the option is selected. How do I go about implementing the logic of creating the Fragment instance ONCE and reusing it, so that I do not have to continuously load the Fragment over and over again.

推荐答案

对于遇到同样问题的人,我已经设法找到了解决方案.

To anyone who encounters the same issue with me,I've managed to find a solution.

在容器框架中,我必须定义我将使用的特定片段视图,如下所示.

In the container frame,I've to define specific fragment views that I'll be utilizing as shown below.

在每个 Fragment 视图中,我必须通过 "android:name" 属性将它与实际 Fragment 本身链接",如下所示.请注意 路径 到 Fragment,例如在我的情况下它是 com.example.confessionsrp.SelectionFragment.

In each Fragment view,I've to "link" it with the actual Fragment itself as shown below via the "android:name" attribute.Do take note of the the path to the Fragment,example for in my case it's com.example.confesssionsrp.SelectionFragment.

<fragment
    android:id="@+id/selectionFragment"
    android:name="com.example.confessionsrp.SelectionFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在 MainActivity(或我们查看片段的 Activity)中,为每个片段创建变量,如下所示.

In the the MainActivity(or the Activity where we're viewing the fragments),create variables for each of the Fragments as shown below.

接下来,在 MainActivity(或您的特定 Activity)的 Oncreate 中,初始化各种片段,如下所示.

Following that,in the Oncreate of the MainActivity(or your specific Activity),initialize the various fragments as shown below.

继续创建一个名为ShowFragment"的新方法,如下所示.

Proceed onto creating a new Method called "ShowFragment" as shown below.

private void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
            if (Session.getActiveSession().isClosed()) {
                mDrawerLayout
                        .setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}

此后在fragment的切换中,手动调用ShowFragment"方法,选中的Fragment如下图所示.

From then on in the switching of the fragments,manually call upon the "ShowFragment" method with the selected Fragment as shown below.

完成所有这些操作,每次查看 Fragment 时都不会重置 Fragment,因此是答案的解决方案.感谢迄今为止帮助过我的任何人 :)!

Doing all of this overall,will not reset the Fragment each time we view it,and therefore is the solution to the answer.Thank you for anyone who has helped me so far :)!

这篇关于Android 导航抽屉片段状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆