FragmentStatePagerAdapter.java revision 2a4d8518f36346ea25a22a736453ff28f2954165
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    private Fragment mCurrentPrimaryItem = null;
37
38    public FragmentStatePagerAdapter(FragmentManager fm) {
39        mFragmentManager = fm;
40    }
41
42    /**
43     * Return the Fragment associated with a specified position.
44     */
45    public abstract Fragment getItem(int position);
46
47    @Override
48    public void startUpdate(View container) {
49    }
50
51    @Override
52    public Object instantiateItem(View container, int position) {
53        // If we already have this item instantiated, there is nothing
54        // to do.  This can happen when we are restoring the entire pager
55        // from its saved state, where the fragment manager has already
56        // taken care of restoring the fragments we previously had instantiated.
57        if (mFragments.size() > position) {
58            Fragment f = mFragments.get(position);
59            if (f != null) {
60                return f;
61            }
62        }
63
64        if (mCurTransaction == null) {
65            mCurTransaction = mFragmentManager.beginTransaction();
66        }
67
68        Fragment fragment = getItem(position);
69        if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
70        if (mSavedState.size() > position) {
71            Fragment.SavedState fss = mSavedState.get(position);
72            if (fss != null) {
73                fragment.setInitialSavedState(fss);
74            }
75        }
76        while (mFragments.size() <= position) {
77            mFragments.add(null);
78        }
79        fragment.setMenuVisibility(false);
80        mFragments.set(position, fragment);
81        mCurTransaction.add(container.getId(), fragment);
82
83        return fragment;
84    }
85
86    @Override
87    public void destroyItem(View container, int position, Object object) {
88        Fragment fragment = (Fragment)object;
89
90        if (mCurTransaction == null) {
91            mCurTransaction = mFragmentManager.beginTransaction();
92        }
93        if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
94                + " v=" + ((Fragment)object).getView());
95        while (mSavedState.size() <= position) {
96            mSavedState.add(null);
97        }
98        mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
99        mFragments.set(position, null);
100
101        mCurTransaction.remove(fragment);
102    }
103
104    @Override
105    public void setPrimaryItem(View container, int position, Object object) {
106        Fragment fragment = (Fragment)object;
107        if (fragment != mCurrentPrimaryItem) {
108            if (mCurrentPrimaryItem != null) {
109                mCurrentPrimaryItem.setMenuVisibility(false);
110            }
111            if (fragment != null) {
112                fragment.setMenuVisibility(true);
113            }
114            mCurrentPrimaryItem = fragment;
115        }
116    }
117
118    @Override
119    public void finishUpdate(View container) {
120        if (mCurTransaction != null) {
121            mCurTransaction.commitAllowingStateLoss();
122            mCurTransaction = null;
123            mFragmentManager.executePendingTransactions();
124        }
125    }
126
127    @Override
128    public boolean isViewFromObject(View view, Object object) {
129        return ((Fragment)object).getView() == view;
130    }
131
132    @Override
133    public Parcelable saveState() {
134        Bundle state = null;
135        if (mSavedState.size() > 0) {
136            state = new Bundle();
137            Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
138            mSavedState.toArray(fss);
139            state.putParcelableArray("states", fss);
140        }
141        for (int i=0; i<mFragments.size(); i++) {
142            Fragment f = mFragments.get(i);
143            if (f != null) {
144                if (state == null) {
145                    state = new Bundle();
146                }
147                String key = "f" + i;
148                mFragmentManager.putFragment(state, key, f);
149            }
150        }
151        return state;
152    }
153
154    @Override
155    public void restoreState(Parcelable state, ClassLoader loader) {
156        if (state != null) {
157            Bundle bundle = (Bundle)state;
158            bundle.setClassLoader(loader);
159            Parcelable[] fss = bundle.getParcelableArray("states");
160            mSavedState.clear();
161            mFragments.clear();
162            if (fss != null) {
163                for (int i=0; i<fss.length; i++) {
164                    mSavedState.add((Fragment.SavedState)fss[i]);
165                }
166            }
167            Iterable<String> keys = bundle.keySet();
168            for (String key: keys) {
169                if (key.startsWith("f")) {
170                    int index = Integer.parseInt(key.substring(1));
171                    Fragment f = mFragmentManager.getFragment(bundle, key);
172                    if (f != null) {
173                        while (mFragments.size() <= index) {
174                            mFragments.add(null);
175                        }
176                        f.setMenuVisibility(false);
177                        mFragments.set(index, f);
178                    } else {
179                        Log.w(TAG, "Bad fragment at key " + key);
180                    }
181                }
182            }
183        }
184    }
185}
186