How can I switch between two fragments, without re

2019-01-21 19:12发布

I'm working on an android application, that uses a navigation drawer to switch between two fragments. However, each time I switch, the fragment is completely recreated.

Here is the code from my main activity.

/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    android.support.v4.app.Fragment fragment;
    String tag;
    android.support.v4.app.FragmentManager; fragmentManager = getSupportFragmentManager();

    switch(position) {
        case 0:
            if(fragmentManager.findFragmentByTag("one") != null) {
                fragment = fragmentManager.findFragmentByTag("one");
            } else {
                fragment = new OneFragment();
            }
            tag = "one";
            break;
        case 1:
            if(fragmentManager.findFragmentByTag("two") != null) {
                fragment = fragmentManager.findFragmentByTag("two");
            } else {
                fragment = new TwoFragment();
            }
            tag = "two";
            break;
    }

    fragment.setRetainInstance(true);
    fragmentManager.beginTransaction().replace(R.id.container, fragment, tag).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mNavTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

I've set up some debug logging, and every time selectItem is called, one fragment is destroyed, while the other is created.

Is there any way to prevent the fragments from being recreated, and just reuse them instead?

9条回答
Luminary・发光体
2楼-- · 2019-01-21 19:43

Same idea as Tester101 but this is what I ended up using.

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    Fragment oldFragment = fragmentManager.findFragmentByTag( "" + m_lastDrawerSelectPosition );
    if ( oldFragment != null )
        fragmentTransaction.hide( oldFragment );

    Fragment newFragment = fragmentManager.findFragmentByTag( "" + position );
    if ( newFragment == null )
    {
        newFragment = getFragment( position );
        fragmentTransaction.add( R.id.home_content_frame, newFragment, "" + position );
    }

    fragmentTransaction.show( newFragment );
    fragmentTransaction.commit();
查看更多
欢心
3楼-- · 2019-01-21 19:44

I did this before like this:

        if (mPrevFrag != fragment) {

            // Change
            FragmentTransaction ft = fragmentManager.beginTransaction();
            if (mPrevFrag != null){
                ft.hide(mPrevFrag);
            }
            ft.show(fragment);
            ft.commit();
            mPrevFrag = fragment;

        }

(you will need to track your pervious fragment in this solution)

查看更多
Animai°情兽
4楼-- · 2019-01-21 19:45

this is a little late response. if you're using view pager for fragments, set the off screen page limit of the fragment to the number of fragments created.

mViewPager.setOffscreenPageLimit(3); // number of fragments here is 3
查看更多
登录 后发表回答