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