FragmentStatePagerAdapter.java revision 5c1637087453de15e31861f073eae5133c4e9f7b
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.app; 18 19import java.util.ArrayList; 20 21import android.os.Bundle; 22import android.os.Parcelable; 23import android.support.v4.view.PagerAdapter; 24import android.util.Log; 25import android.view.View; 26 27public abstract class FragmentStatePagerAdapter extends PagerAdapter { 28 private static final String TAG = "FragmentStatePagerAdapter"; 29 private static final boolean DEBUG = false; 30 31 private final FragmentManager mFragmentManager; 32 private FragmentTransaction mCurTransaction = null; 33 34 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>(); 35 private ArrayList<Fragment> mFragments = new ArrayList<Fragment>(); 36 37 public FragmentStatePagerAdapter(FragmentManager fm) { 38 mFragmentManager = fm; 39 } 40 41 /** 42 * Return the Fragment associated with a specified position. 43 */ 44 public abstract Fragment getItem(int position); 45 46 @Override 47 public void startUpdate(View container) { 48 } 49 50 @Override 51 public Object instantiateItem(View container, int position) { 52 // If we already have this item instantiated, there is nothing 53 // to do. This can happen when we are restoring the entire pager 54 // from its saved state, where the fragment manager has already 55 // taken care of restoring the fragments we previously had instantiated. 56 if (mFragments.size() > position) { 57 Fragment f = mFragments.get(position); 58 if (f != null) { 59 return f; 60 } 61 } 62 63 if (mCurTransaction == null) { 64 mCurTransaction = mFragmentManager.beginTransaction(); 65 } 66 67 Fragment fragment = getItem(position); 68 if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment); 69 if (mSavedState.size() > position) { 70 Fragment.SavedState fss = mSavedState.get(position); 71 if (fss != null) { 72 fragment.setInitialSavedState(fss); 73 } 74 } 75 while (mFragments.size() <= position) { 76 mFragments.add(null); 77 } 78 mFragments.set(position, fragment); 79 mCurTransaction.add(container.getId(), fragment); 80 81 return fragment; 82 } 83 84 @Override 85 public void destroyItem(View container, int position, Object object) { 86 Fragment fragment = (Fragment)object; 87 88 if (mCurTransaction == null) { 89 mCurTransaction = mFragmentManager.beginTransaction(); 90 } 91 if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object 92 + " v=" + ((Fragment)object).getView()); 93 while (mSavedState.size() <= position) { 94 mSavedState.add(null); 95 } 96 mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment)); 97 mFragments.set(position, null); 98 99 mCurTransaction.remove(fragment); 100 } 101 102 @Override 103 public void finishUpdate(View container) { 104 if (mCurTransaction != null) { 105 mCurTransaction.commit(); 106 mCurTransaction = null; 107 mFragmentManager.executePendingTransactions(); 108 } 109 } 110 111 @Override 112 public boolean isViewFromObject(View view, Object object) { 113 return ((Fragment)object).getView() == view; 114 } 115 116 @Override 117 public Parcelable saveState() { 118 Bundle state = null; 119 if (mSavedState.size() > 0) { 120 state = new Bundle(); 121 Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()]; 122 mSavedState.toArray(fss); 123 state.putParcelableArray("states", fss); 124 } 125 for (int i=0; i<mFragments.size(); i++) { 126 Fragment f = mFragments.get(i); 127 if (f != null) { 128 if (state == null) { 129 state = new Bundle(); 130 } 131 String key = "f" + i; 132 mFragmentManager.putFragment(state, key, f); 133 } 134 } 135 return state; 136 } 137 138 @Override 139 public void restoreState(Parcelable state, ClassLoader loader) { 140 if (state != null) { 141 Bundle bundle = (Bundle)state; 142 bundle.setClassLoader(loader); 143 Parcelable[] fss = bundle.getParcelableArray("states"); 144 mSavedState.clear(); 145 mFragments.clear(); 146 if (fss != null) { 147 for (int i=0; i<fss.length; i++) { 148 mSavedState.add((Fragment.SavedState)fss[i]); 149 } 150 } 151 Iterable<String> keys = bundle.keySet(); 152 for (String key: keys) { 153 if (key.startsWith("f")) { 154 int index = Integer.parseInt(key.substring(1)); 155 Fragment f = mFragmentManager.getFragment(bundle, key); 156 if (f != null) { 157 while (mFragments.size() <= index) { 158 mFragments.add(null); 159 } 160 mFragments.set(index, f); 161 } else { 162 Log.w(TAG, "Bad fragment at key " + key); 163 } 164 } 165 } 166 } 167 } 168} 169