0

前提:不要使用ViewPager,切换tab后保存fragment状态我有问题,先初始化fragmentOne,勾选button2,然后会创建fragmentTwo,勾选button3,然后会创建fragmentThree。我在内存中创建了三个fragment,现在想按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;
    }

}
4

1 回答 1

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

}
于 2013-11-26T15:06:28.373 回答