安卓导航抽屉片段州 [英] Android Navigation Drawer Fragment State

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

问题描述

我目前使用的导航抽屉为我的Andr​​oid应用程序。在我的第一个片段,我已经使用Facebook的图形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.

然后,我用的是抽屉式导航栏点击另一个片段,并查看它。

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

然后最后,我重复使用导航抽屉继续回到第一个片段,并查看它。

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

我的,我现在面临的问题是,我该如何继续利用已经建立重新创造它时,导航抽屉项被选中的那一次,而不是分片。我的code代表的片段的切换被如下所示。

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

我注意到,有确实的片段时的选项被选中,每次一个新的实例。我该如何去实现创建片段实例的的逻辑和重用它,这样我就不必不断地一遍又一遍的加载片段。

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.

在每个片段看来,我得链接,它与实际的片段本身通过图所示的Android:名称 attribute.Do注意到在路径的片段,比如在我的情况下,它的 com.example.confesssionsrp.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(或我们正在观看的片段活动),为每个片断的变量,如下图所示。

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

随后,在的onCreate 的MainActivity(或您的具体活动)中,初始化各种片段,如下图所示。

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

从此在片段的切换,手动呼吁的 ShowFragment 的方法与所选择的片段,如下图所示。

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

做这一切的整体,不会每次我们认为它的时间重置片段,因此是解决了answer.Thank你的人谁帮我至今:!)

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 :)!

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

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