1/* 2 * Copyright 2018 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 androidx.fragment.app; 18 19import android.os.Bundle; 20import android.os.Parcelable; 21import android.util.Log; 22import android.view.View; 23import android.view.ViewGroup; 24 25import androidx.viewpager.widget.PagerAdapter; 26 27import java.util.ArrayList; 28 29/** 30 * Implementation of {@link PagerAdapter} that 31 * uses a {@link Fragment} to manage each page. This class also handles 32 * saving and restoring of fragment's state. 33 * 34 * <p>This version of the pager is more useful when there are a large number 35 * of pages, working more like a list view. When pages are not visible to 36 * the user, their entire fragment may be destroyed, only keeping the saved 37 * state of that fragment. This allows the pager to hold on to much less 38 * memory associated with each visited page as compared to 39 * {@link FragmentPagerAdapter} at the cost of potentially more overhead when 40 * switching between pages. 41 * 42 * <p>When using FragmentPagerAdapter the host ViewPager must have a 43 * valid ID set.</p> 44 * 45 * <p>Subclasses only need to implement {@link #getItem(int)} 46 * and {@link #getCount()} to have a working adapter. 47 * 48 * <p>Here is an example implementation of a pager containing fragments of 49 * lists: 50 * 51 * {@sample frameworks/support/samples/Support13Demos/src/main/java/com/example/android/supportv13/app/FragmentStatePagerSupport.java 52 * complete} 53 * 54 * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is: 55 * 56 * {@sample frameworks/support/samples/Support13Demos/src/main/res/layout/fragment_pager.xml 57 * complete} 58 * 59 * <p>The <code>R.layout.fragment_pager_list</code> resource containing each 60 * individual fragment's layout is: 61 * 62 * {@sample frameworks/support/samples/Support13Demos/src/main/res/layout/fragment_pager_list.xml 63 * complete} 64 */ 65public abstract class FragmentStatePagerAdapter extends PagerAdapter { 66 private static final String TAG = "FragmentStatePagerAdapt"; 67 private static final boolean DEBUG = false; 68 69 private final FragmentManager mFragmentManager; 70 private FragmentTransaction mCurTransaction = null; 71 72 private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>(); 73 private ArrayList<Fragment> mFragments = new ArrayList<Fragment>(); 74 private Fragment mCurrentPrimaryItem = null; 75 76 public FragmentStatePagerAdapter(FragmentManager fm) { 77 mFragmentManager = fm; 78 } 79 80 /** 81 * Return the Fragment associated with a specified position. 82 */ 83 public abstract Fragment getItem(int position); 84 85 @Override 86 public void startUpdate(ViewGroup container) { 87 if (container.getId() == View.NO_ID) { 88 throw new IllegalStateException("ViewPager with adapter " + this 89 + " requires a view id"); 90 } 91 } 92 93 @Override 94 public Object instantiateItem(ViewGroup container, int position) { 95 // If we already have this item instantiated, there is nothing 96 // to do. This can happen when we are restoring the entire pager 97 // from its saved state, where the fragment manager has already 98 // taken care of restoring the fragments we previously had instantiated. 99 if (mFragments.size() > position) { 100 Fragment f = mFragments.get(position); 101 if (f != null) { 102 return f; 103 } 104 } 105 106 if (mCurTransaction == null) { 107 mCurTransaction = mFragmentManager.beginTransaction(); 108 } 109 110 Fragment fragment = getItem(position); 111 if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment); 112 if (mSavedState.size() > position) { 113 Fragment.SavedState fss = mSavedState.get(position); 114 if (fss != null) { 115 fragment.setInitialSavedState(fss); 116 } 117 } 118 while (mFragments.size() <= position) { 119 mFragments.add(null); 120 } 121 fragment.setMenuVisibility(false); 122 fragment.setUserVisibleHint(false); 123 mFragments.set(position, fragment); 124 mCurTransaction.add(container.getId(), fragment); 125 126 return fragment; 127 } 128 129 @Override 130 public void destroyItem(ViewGroup container, int position, Object object) { 131 Fragment fragment = (Fragment) object; 132 133 if (mCurTransaction == null) { 134 mCurTransaction = mFragmentManager.beginTransaction(); 135 } 136 if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object 137 + " v=" + ((Fragment)object).getView()); 138 while (mSavedState.size() <= position) { 139 mSavedState.add(null); 140 } 141 mSavedState.set(position, fragment.isAdded() 142 ? mFragmentManager.saveFragmentInstanceState(fragment) : null); 143 mFragments.set(position, null); 144 145 mCurTransaction.remove(fragment); 146 } 147 148 @Override 149 @SuppressWarnings("ReferenceEquality") 150 public void setPrimaryItem(ViewGroup container, int position, Object object) { 151 Fragment fragment = (Fragment)object; 152 if (fragment != mCurrentPrimaryItem) { 153 if (mCurrentPrimaryItem != null) { 154 mCurrentPrimaryItem.setMenuVisibility(false); 155 mCurrentPrimaryItem.setUserVisibleHint(false); 156 } 157 if (fragment != null) { 158 fragment.setMenuVisibility(true); 159 fragment.setUserVisibleHint(true); 160 } 161 mCurrentPrimaryItem = fragment; 162 } 163 } 164 165 @Override 166 public void finishUpdate(ViewGroup container) { 167 if (mCurTransaction != null) { 168 mCurTransaction.commitNowAllowingStateLoss(); 169 mCurTransaction = null; 170 } 171 } 172 173 @Override 174 public boolean isViewFromObject(View view, Object object) { 175 return ((Fragment)object).getView() == view; 176 } 177 178 @Override 179 public Parcelable saveState() { 180 Bundle state = null; 181 if (mSavedState.size() > 0) { 182 state = new Bundle(); 183 Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()]; 184 mSavedState.toArray(fss); 185 state.putParcelableArray("states", fss); 186 } 187 for (int i=0; i<mFragments.size(); i++) { 188 Fragment f = mFragments.get(i); 189 if (f != null && f.isAdded()) { 190 if (state == null) { 191 state = new Bundle(); 192 } 193 String key = "f" + i; 194 mFragmentManager.putFragment(state, key, f); 195 } 196 } 197 return state; 198 } 199 200 @Override 201 public void restoreState(Parcelable state, ClassLoader loader) { 202 if (state != null) { 203 Bundle bundle = (Bundle)state; 204 bundle.setClassLoader(loader); 205 Parcelable[] fss = bundle.getParcelableArray("states"); 206 mSavedState.clear(); 207 mFragments.clear(); 208 if (fss != null) { 209 for (int i=0; i<fss.length; i++) { 210 mSavedState.add((Fragment.SavedState)fss[i]); 211 } 212 } 213 Iterable<String> keys = bundle.keySet(); 214 for (String key: keys) { 215 if (key.startsWith("f")) { 216 int index = Integer.parseInt(key.substring(1)); 217 Fragment f = mFragmentManager.getFragment(bundle, key); 218 if (f != null) { 219 while (mFragments.size() <= index) { 220 mFragments.add(null); 221 } 222 f.setMenuVisibility(false); 223 mFragments.set(index, f); 224 } else { 225 Log.w(TAG, "Bad fragment at key " + key); 226 } 227 } 228 } 229 } 230 } 231} 232