private void changePage(int position, SliderItem item) { mIgnoreBackStack = true; if (mCurrentPage >= 0) { final SliderItem lastItem = mAdapter.getItem(mCurrentPage); final WeakReference<Fragment> ref = lastItem.mLastFragment; final Fragment fragment = ref == null ? null : ref.get(); if (fragment != null && fragment.isAdded() && lastItem.mSaveState) { if (!fragment.isDetached()) { mFragmentManager.beginTransaction().detach(fragment).commit(); mFragmentManager.executePendingTransactions(); } lastItem.mSavedState = mFragmentManager.saveFragmentInstanceState(fragment); } } mCurrentPage = position; mAdapter.notifyDataSetInvalidated(); while (mFragmentManager.popBackStackImmediate()) {} final Fragment fragment = Fragment.instantiate(item.mFragmentClass); if (item.mSavedState != null) { fragment.setInitialSavedState(item.mSavedState); } item.mLastFragment = new WeakReference<Fragment>(fragment); replaceFragment(mFragmentManager, fragment); mIgnoreBackStack = false; }
@Override public void onBackStackChanged() { if (mIgnoreBackStack || mCurrentPage < 0 || mCurrentPage >= mItems.size()) { return; } SliderItem item = mItems.get(mCurrentPage); WeakReference<Fragment> ref = item.mLastFragment; Fragment fragment = ref == null ? null : ref.get(); if (fragment == null || !item.mSaveState) { return; } if (fragment.isAdded() && fragment.isDetached() && !fragment.isVisible()) { item.mSavedState = mFragmentManager.saveFragmentInstanceState(fragment); } }
@Override public void destroyItem(ViewGroup container, int position, Object object) { Fragment fragment = (Fragment) object; if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } if (DEBUG) Log.v( TAG, "Removing item #" + position + ": f=" + object + " v=" + ((Fragment) object).getView()); while (mSavedState.size() <= position) { mSavedState.add(null); } mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment)); mFragments.set(position, null); mCurTransaction.remove(fragment); }
public void destroyAllFragments() { /* if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } */ final int length = mFragments.size(); while (mSavedState.size() <= length) { mSavedState.add(null); } for (int i = 0; i < length; i++) { final Fragment fr = mFragments.get(i); if (fr != null) { mSavedState.set(i, mFragmentManager.saveFragmentInstanceState(fr)); mFragments.set(i, null); // mCurTransaction.remove(fr); } } /* mCurTransaction.commit(); mCurTransaction = null; */ }