如何使用FragmentPagerAdapter来保存片段的状态?

时间:2013-10-31 08:10:39

标签: android android-fragments

前提条件:不要使用ViewPager,在切换选项卡后保存片段状态 我有一个问题,首先是init fragmentOne,检查button2,然后将创建fragmentTwo,检查button3,然后将创建fragmentThree。我在内存中创建了三个片段,现在我想按button2或button1,但页面仍然是fragmentThree。我找不到原因? 这是代码,谢谢!

package com.example.FragmentSaveStatus;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class MyActivity extends FragmentActivity{
    private FragmentManager mFragmentManager;
    private FragmentTransaction mCurTransaction;
    private FrameLayout mContainer;
    private Fragment mCurrentPrimaryItem;
    private RadioButton mButtonOne, mButtonTwo, mButtonThree;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initViews();
    }

    private void initViews() {
        mContainer = (FrameLayout) findViewById(R.id.container);
        mButtonOne = (RadioButton) findViewById(R.id.radio_button0);
        mButtonTwo = (RadioButton) findViewById(R.id.radio_button1);
        mButtonThree = (RadioButton) findViewById(R.id.radio_button2);
        mFragmentManager = getSupportFragmentManager();
        Fragment fragment = (Fragment) mPagerAdapter.instantiateItem(mContainer, mButtonOne.getId());
        mPagerAdapter.setPrimaryItem(mContainer, mButtonOne.getId(), fragment);
        mPagerAdapter.finishUpdate(mContainer);
        mButtonOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Fragment fragment = (Fragment) mPagerAdapter.instantiateItem(mContainer, buttonView.getId());
                    Toast.makeText(MyActivity.this, "one:"+buttonView.getId() +"  "+fragment.getClass().getName(), Toast.LENGTH_SHORT).show();
                    mPagerAdapter.setPrimaryItem(mContainer, buttonView.getId(), fragment);
                    mPagerAdapter.finishUpdate(mContainer);
                }
            }
        });

        mButtonTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Fragment fragment = (Fragment) mPagerAdapter.instantiateItem(mContainer, buttonView.getId());
                    Toast.makeText(MyActivity.this, "two:"+buttonView.getId() +"  "+fragment.getClass().getName(), Toast.LENGTH_SHORT).show();
                    mPagerAdapter.setPrimaryItem(mContainer, buttonView.getId(), fragment);
                    mPagerAdapter.finishUpdate(mContainer);
                }
            }
        });

        mButtonThree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Fragment fragment = (Fragment) mPagerAdapter.instantiateItem(mContainer, buttonView.getId());
                    Toast.makeText(MyActivity.this, "three:"+buttonView.getId() +"  "+fragment.getClass().getName(), Toast.LENGTH_SHORT).show();
                    mPagerAdapter.setPrimaryItem(mContainer, buttonView.getId(), fragment);
                    mPagerAdapter.finishUpdate(mContainer);
                }
            }
        });
    }


    private FragmentPagerAdapter mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case R.id.radio_button0:
                    return new FragmentOne();
                case R.id.radio_button1:
                    return new FragmentTwo();
                case R.id.radio_button2:
                    return new FragmentThree();
                default:
                   return new FragmentOne();
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (mCurTransaction == null) {
                mCurTransaction = mFragmentManager.beginTransaction();
            }

            //Do we already have thei fragment
            String name = makeFragmentName(container.getId(), position);
            Fragment fragment = mFragmentManager.findFragmentByTag(name);
            if (fragment != null) {
                mCurTransaction.attach(fragment);
            } else {
                fragment = getItem(position);
                mCurTransaction.add(container.getId(), fragment,
                        makeFragmentName(container.getId(), position));
            }

            if (fragment != mCurrentPrimaryItem) {
                fragment.setMenuVisibility(false);
                fragment.setUserVisibleHint(false);
            }
            return fragment;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            Fragment fragment = (Fragment)object;
            if (fragment != mCurrentPrimaryItem) {
                if (mCurrentPrimaryItem != null) {
                      mCurrentPrimaryItem.setMenuVisibility(false);
                      mCurrentPrimaryItem.setUserVisibleHint(false);
                }

                if (fragment != null) {
                    fragment.setMenuVisibility(true);
                    fragment.setUserVisibleHint(true);
                }

                mCurrentPrimaryItem = fragment;
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (mCurTransaction == null) {
                mCurTransaction = mFragmentManager.beginTransaction();
            }
            Log.v("fragment", "detach item #"+getItemId(position)+ ": f="+object
                + " v="+((Fragment)object).getView());
            mCurTransaction.detach((Fragment)object);
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            if (mCurTransaction != null) {
                mCurTransaction.commitAllowingStateLoss();
                mCurTransaction = null;
                mFragmentManager.executePendingTransactions();
            }
        }
    };


    private static String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    }

}

1 个答案:

答案 0 :(得分:0)

此示例在片段未被销毁时有效

private int pageID;

@Override
public void onPause() {
super.onPause();
pageID = mViewPager.getCurrentItem(); // 1 save
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

mViewPager.setCurrentItem(pageID, false); // 2 load

}