FragmentManager.java revision ff22d81f6561f6cdd2a91eb63238c41079927a22
10d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks/*
20d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * Copyright (C) 2011 The Android Open Source Project
30d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks *
40d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * Licensed under the Apache License, Version 2.0 (the "License");
50d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * you may not use this file except in compliance with the License.
60d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * You may obtain a copy of the License at
70d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks *
80d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks *      http://www.apache.org/licenses/LICENSE-2.0
90d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks *
100d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * Unless required by applicable law or agreed to in writing, software
110d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * distributed under the License is distributed on an "AS IS" BASIS,
120d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * See the License for the specific language governing permissions and
140d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * limitations under the License.
150d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks */
160d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks
170d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubankspackage android.support.v4.app;
180d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks
190d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.content.Context;
200d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.content.res.Configuration;
210d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.content.res.Resources.NotFoundException;
220d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.content.res.TypedArray;
230d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.os.Build;
240d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.os.Bundle;
250d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.os.Handler;
260d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.os.Looper;
270d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.os.Parcel;
280d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.os.Parcelable;
290d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.annotation.CallSuper;
300d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.annotation.IdRes;
310d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.annotation.StringRes;
320d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.v4.os.BuildCompat;
330d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.v4.util.DebugUtils;
340d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.v4.util.LogWriter;
350d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.v4.view.LayoutInflaterFactory;
360d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.support.v4.view.ViewCompat;
370d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.util.AttributeSet;
380d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.util.Log;
390d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.util.SparseArray;
400d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.AccelerateInterpolator;
410d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.AlphaAnimation;
420d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.Animation;
430d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.AnimationSet;
440d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.AnimationUtils;
450d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.DecelerateInterpolator;
460d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.Interpolator;
470d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.ScaleAnimation;
480d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.animation.Animation.AnimationListener;
490d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.Menu;
500d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.MenuInflater;
510d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.MenuItem;
520d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.View;
530d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport android.view.ViewGroup;
540d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks
550d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport java.io.FileDescriptor;
560d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport java.io.PrintWriter;
570d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport java.lang.reflect.Field;
580d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport java.util.ArrayList;
590d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport java.util.Arrays;
600d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanksimport java.util.List;
610d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks
620d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks/**
630d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * Static library support version of the framework's {@link android.app.FragmentManager}.
640d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * Used to write apps that run on platforms prior to Android 3.0.  When running
650d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * on Android 3.0 or above, this implementation is still used; it does not try
660d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * to switch to the framework's implementation.  See the framework {@link FragmentManager}
670d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * documentation for a class overview.
680d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks *
690d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * <p>Your activity must derive from {@link FragmentActivity} to use this. From such an activity,
700d13f3929129bf7e34bde1ed3670195c37a180ebArthur Eubanks * you can acquire the {@link FragmentManager} by calling
71 * {@link FragmentActivity#getSupportFragmentManager}.
72 */
73public abstract class FragmentManager {
74    /**
75     * Representation of an entry on the fragment back stack, as created
76     * with {@link FragmentTransaction#addToBackStack(String)
77     * FragmentTransaction.addToBackStack()}.  Entries can later be
78     * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
79     * FragmentManager.getBackStackEntry()}.
80     *
81     * <p>Note that you should never hold on to a BackStackEntry object;
82     * the identifier as returned by {@link #getId} is the only thing that
83     * will be persisted across activity instances.
84     */
85    public interface BackStackEntry {
86        /**
87         * Return the unique identifier for the entry.  This is the only
88         * representation of the entry that will persist across activity
89         * instances.
90         */
91        public int getId();
92
93        /**
94         * Get the name that was supplied to
95         * {@link FragmentTransaction#addToBackStack(String)
96         * FragmentTransaction.addToBackStack(String)} when creating this entry.
97         */
98        public String getName();
99
100        /**
101         * Return the full bread crumb title resource identifier for the entry,
102         * or 0 if it does not have one.
103         */
104        @StringRes
105        public int getBreadCrumbTitleRes();
106
107        /**
108         * Return the short bread crumb title resource identifier for the entry,
109         * or 0 if it does not have one.
110         */
111        @StringRes
112        public int getBreadCrumbShortTitleRes();
113
114        /**
115         * Return the full bread crumb title for the entry, or null if it
116         * does not have one.
117         */
118        public CharSequence getBreadCrumbTitle();
119
120        /**
121         * Return the short bread crumb title for the entry, or null if it
122         * does not have one.
123         */
124        public CharSequence getBreadCrumbShortTitle();
125    }
126
127    /**
128     * Interface to watch for changes to the back stack.
129     */
130    public interface OnBackStackChangedListener {
131        /**
132         * Called whenever the contents of the back stack change.
133         */
134        public void onBackStackChanged();
135    }
136
137    /**
138     * Start a series of edit operations on the Fragments associated with
139     * this FragmentManager.
140     *
141     * <p>Note: A fragment transaction can only be created/committed prior
142     * to an activity saving its state.  If you try to commit a transaction
143     * after {@link FragmentActivity#onSaveInstanceState FragmentActivity.onSaveInstanceState()}
144     * (and prior to a following {@link FragmentActivity#onStart FragmentActivity.onStart}
145     * or {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will get an error.
146     * This is because the framework takes care of saving your current fragments
147     * in the state, and if changes are made after the state is saved then they
148     * will be lost.</p>
149     */
150    public abstract FragmentTransaction beginTransaction();
151
152    /**
153     * @hide -- remove once prebuilts are in.
154     * @deprecated
155     */
156    @Deprecated
157    public FragmentTransaction openTransaction() {
158        return beginTransaction();
159    }
160
161    /**
162     * After a {@link FragmentTransaction} is committed with
163     * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
164     * is scheduled to be executed asynchronously on the process's main thread.
165     * If you want to immediately executing any such pending operations, you
166     * can call this function (only from the main thread) to do so.  Note that
167     * all callbacks and other related behavior will be done from within this
168     * call, so be careful about where this is called from.
169     *
170     * <p>If you are committing a single transaction that does not modify the
171     * fragment back stack, strongly consider using
172     * {@link FragmentTransaction#commitNow()} instead. This can help avoid
173     * unwanted side effects when other code in your app has pending committed
174     * transactions that expect different timing.</p>
175     *
176     * @return Returns true if there were any pending transactions to be
177     * executed.
178     */
179    public abstract boolean executePendingTransactions();
180
181    /**
182     * Finds a fragment that was identified by the given id either when inflated
183     * from XML or as the container ID when added in a transaction.  This first
184     * searches through fragments that are currently added to the manager's
185     * activity; if no such fragment is found, then all fragments currently
186     * on the back stack associated with this ID are searched.
187     * @return The fragment if found or null otherwise.
188     */
189    public abstract Fragment findFragmentById(@IdRes int id);
190
191    /**
192     * Finds a fragment that was identified by the given tag either when inflated
193     * from XML or as supplied when added in a transaction.  This first
194     * searches through fragments that are currently added to the manager's
195     * activity; if no such fragment is found, then all fragments currently
196     * on the back stack are searched.
197     * @return The fragment if found or null otherwise.
198     */
199    public abstract Fragment findFragmentByTag(String tag);
200
201    /**
202     * Flag for {@link #popBackStack(String, int)}
203     * and {@link #popBackStack(int, int)}: If set, and the name or ID of
204     * a back stack entry has been supplied, then all matching entries will
205     * be consumed until one that doesn't match is found or the bottom of
206     * the stack is reached.  Otherwise, all entries up to but not including that entry
207     * will be removed.
208     */
209    public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
210
211    /**
212     * Pop the top state off the back stack.  Returns true if there was one
213     * to pop, else false.  This function is asynchronous -- it enqueues the
214     * request to pop, but the action will not be performed until the application
215     * returns to its event loop.
216     */
217    public abstract void popBackStack();
218
219    /**
220     * Like {@link #popBackStack()}, but performs the operation immediately
221     * inside of the call.  This is like calling {@link #executePendingTransactions()}
222     * afterwards.
223     * @return Returns true if there was something popped, else false.
224     */
225    public abstract boolean popBackStackImmediate();
226
227    /**
228     * Pop the last fragment transition from the manager's fragment
229     * back stack.  If there is nothing to pop, false is returned.
230     * This function is asynchronous -- it enqueues the
231     * request to pop, but the action will not be performed until the application
232     * returns to its event loop.
233     *
234     * @param name If non-null, this is the name of a previous back state
235     * to look for; if found, all states up to that state will be popped.  The
236     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
237     * the named state itself is popped. If null, only the top state is popped.
238     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
239     */
240    public abstract void popBackStack(String name, int flags);
241
242    /**
243     * Like {@link #popBackStack(String, int)}, but performs the operation immediately
244     * inside of the call.  This is like calling {@link #executePendingTransactions()}
245     * afterwards.
246     * @return Returns true if there was something popped, else false.
247     */
248    public abstract boolean popBackStackImmediate(String name, int flags);
249
250    /**
251     * Pop all back stack states up to the one with the given identifier.
252     * This function is asynchronous -- it enqueues the
253     * request to pop, but the action will not be performed until the application
254     * returns to its event loop.
255     *
256     * @param id Identifier of the stated to be popped. If no identifier exists,
257     * false is returned.
258     * The identifier is the number returned by
259     * {@link FragmentTransaction#commit() FragmentTransaction.commit()}.  The
260     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
261     * the named state itself is popped.
262     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
263     */
264    public abstract void popBackStack(int id, int flags);
265
266    /**
267     * Like {@link #popBackStack(int, int)}, but performs the operation immediately
268     * inside of the call.  This is like calling {@link #executePendingTransactions()}
269     * afterwards.
270     * @return Returns true if there was something popped, else false.
271     */
272    public abstract boolean popBackStackImmediate(int id, int flags);
273
274    /**
275     * Return the number of entries currently in the back stack.
276     */
277    public abstract int getBackStackEntryCount();
278
279    /**
280     * Return the BackStackEntry at index <var>index</var> in the back stack;
281     * entries start index 0 being the bottom of the stack.
282     */
283    public abstract BackStackEntry getBackStackEntryAt(int index);
284
285    /**
286     * Add a new listener for changes to the fragment back stack.
287     */
288    public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
289
290    /**
291     * Remove a listener that was previously added with
292     * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
293     */
294    public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
295
296    /**
297     * Put a reference to a fragment in a Bundle.  This Bundle can be
298     * persisted as saved state, and when later restoring
299     * {@link #getFragment(Bundle, String)} will return the current
300     * instance of the same fragment.
301     *
302     * @param bundle The bundle in which to put the fragment reference.
303     * @param key The name of the entry in the bundle.
304     * @param fragment The Fragment whose reference is to be stored.
305     */
306    public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
307
308    /**
309     * Retrieve the current Fragment instance for a reference previously
310     * placed with {@link #putFragment(Bundle, String, Fragment)}.
311     *
312     * @param bundle The bundle from which to retrieve the fragment reference.
313     * @param key The name of the entry in the bundle.
314     * @return Returns the current Fragment instance that is associated with
315     * the given reference.
316     */
317    public abstract Fragment getFragment(Bundle bundle, String key);
318
319    /**
320     * Get a list of all fragments that have been added to the fragment manager.
321     *
322     * @return The list of all fragments or null if none.
323     * @hide
324     */
325    public abstract List<Fragment> getFragments();
326
327    /**
328     * Save the current instance state of the given Fragment.  This can be
329     * used later when creating a new instance of the Fragment and adding
330     * it to the fragment manager, to have it create itself to match the
331     * current state returned here.  Note that there are limits on how
332     * this can be used:
333     *
334     * <ul>
335     * <li>The Fragment must currently be attached to the FragmentManager.
336     * <li>A new Fragment created using this saved state must be the same class
337     * type as the Fragment it was created from.
338     * <li>The saved state can not contain dependencies on other fragments --
339     * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
340     * store a fragment reference because that reference may not be valid when
341     * this saved state is later used.  Likewise the Fragment's target and
342     * result code are not included in this state.
343     * </ul>
344     *
345     * @param f The Fragment whose state is to be saved.
346     * @return The generated state.  This will be null if there was no
347     * interesting state created by the fragment.
348     */
349    public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
350
351    /**
352     * Returns true if the final {@link android.app.Activity#onDestroy() Activity.onDestroy()}
353     * call has been made on the FragmentManager's Activity, so this instance is now dead.
354     */
355    public abstract boolean isDestroyed();
356
357    /**
358     * Print the FragmentManager's state into the given stream.
359     *
360     * @param prefix Text to print at the front of each line.
361     * @param fd The raw file descriptor that the dump is being sent to.
362     * @param writer A PrintWriter to which the dump is to be set.
363     * @param args Additional arguments to the dump request.
364     */
365    public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
366
367    /**
368     * Control whether the framework's internal fragment manager debugging
369     * logs are turned on.  If enabled, you will see output in logcat as
370     * the framework performs fragment operations.
371     */
372    public static void enableDebugLogging(boolean enabled) {
373        FragmentManagerImpl.DEBUG = enabled;
374    }
375}
376
377final class FragmentManagerState implements Parcelable {
378    FragmentState[] mActive;
379    int[] mAdded;
380    BackStackState[] mBackStack;
381
382    public FragmentManagerState() {
383    }
384
385    public FragmentManagerState(Parcel in) {
386        mActive = in.createTypedArray(FragmentState.CREATOR);
387        mAdded = in.createIntArray();
388        mBackStack = in.createTypedArray(BackStackState.CREATOR);
389    }
390
391    public int describeContents() {
392        return 0;
393    }
394
395    public void writeToParcel(Parcel dest, int flags) {
396        dest.writeTypedArray(mActive, flags);
397        dest.writeIntArray(mAdded);
398        dest.writeTypedArray(mBackStack, flags);
399    }
400
401    public static final Parcelable.Creator<FragmentManagerState> CREATOR
402            = new Parcelable.Creator<FragmentManagerState>() {
403        public FragmentManagerState createFromParcel(Parcel in) {
404            return new FragmentManagerState(in);
405        }
406
407        public FragmentManagerState[] newArray(int size) {
408            return new FragmentManagerState[size];
409        }
410    };
411}
412
413/**
414 * Container for fragments associated with an activity.
415 */
416final class FragmentManagerImpl extends FragmentManager implements LayoutInflaterFactory {
417    static boolean DEBUG = false;
418    static final String TAG = "FragmentManager";
419
420    static final boolean HONEYCOMB = android.os.Build.VERSION.SDK_INT >= 11;
421
422    static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
423    static final String TARGET_STATE_TAG = "android:target_state";
424    static final String VIEW_STATE_TAG = "android:view_state";
425    static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
426
427    static class AnimateOnHWLayerIfNeededListener implements AnimationListener {
428        private AnimationListener mOrignalListener;
429        private boolean mShouldRunOnHWLayer;
430        private View mView;
431
432        public AnimateOnHWLayerIfNeededListener(final View v, Animation anim) {
433            if (v == null || anim == null) {
434                return;
435            }
436            mView = v;
437        }
438
439        public AnimateOnHWLayerIfNeededListener(final View v, Animation anim,
440                AnimationListener listener) {
441            if (v == null || anim == null) {
442                return;
443            }
444            mOrignalListener = listener;
445            mView = v;
446            mShouldRunOnHWLayer = true;
447        }
448
449        @Override
450        @CallSuper
451        public void onAnimationStart(Animation animation) {
452            if (mOrignalListener != null) {
453                mOrignalListener.onAnimationStart(animation);
454            }
455        }
456
457        @Override
458        @CallSuper
459        public void onAnimationEnd(Animation animation) {
460            if (mView != null && mShouldRunOnHWLayer) {
461                // If we're attached to a window, assume we're in the normal performTraversals
462                // drawing path for Animations running. It's not safe to change the layer type
463                // during drawing, so post it to the View to run later. If we're not attached
464                // or we're running on N and above, post it to the view. If we're not on N and
465                // not attached, do it right now since existing platform versions don't run the
466                // hwui renderer for detached views off the UI thread making changing layer type
467                // safe, but posting may not be.
468                // Prior to N posting to a detached view from a non-Looper thread could cause
469                // leaks, since the thread-local run queue on a non-Looper thread would never
470                // be flushed.
471                if (ViewCompat.isAttachedToWindow(mView) || BuildCompat.isAtLeastN()) {
472                    mView.post(new Runnable() {
473                        @Override
474                        public void run() {
475                            ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null);
476                        }
477                    });
478                } else {
479                    ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null);
480                }
481            }
482            if (mOrignalListener != null) {
483                mOrignalListener.onAnimationEnd(animation);
484            }
485        }
486
487        @Override
488        public void onAnimationRepeat(Animation animation) {
489            if (mOrignalListener != null) {
490                mOrignalListener.onAnimationRepeat(animation);
491            }
492        }
493    }
494
495    ArrayList<Runnable> mPendingActions;
496    Runnable[] mTmpActions;
497    boolean mExecutingActions;
498
499    ArrayList<Fragment> mActive;
500    ArrayList<Fragment> mAdded;
501    ArrayList<Integer> mAvailIndices;
502    ArrayList<BackStackRecord> mBackStack;
503    ArrayList<Fragment> mCreatedMenus;
504
505    // Must be accessed while locked.
506    ArrayList<BackStackRecord> mBackStackIndices;
507    ArrayList<Integer> mAvailBackStackIndices;
508
509    ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
510
511    int mCurState = Fragment.INITIALIZING;
512    FragmentHostCallback mHost;
513    FragmentController mController;
514    FragmentContainer mContainer;
515    Fragment mParent;
516
517    static Field sAnimationListenerField = null;
518
519    boolean mNeedMenuInvalidate;
520    boolean mStateSaved;
521    boolean mDestroyed;
522    String mNoTransactionsBecause;
523    boolean mHavePendingDeferredStart;
524
525    // Temporary vars for state save and restore.
526    Bundle mStateBundle = null;
527    SparseArray<Parcelable> mStateArray = null;
528
529    Runnable mExecCommit = new Runnable() {
530        @Override
531        public void run() {
532            execPendingActions();
533        }
534    };
535
536    static boolean modifiesAlpha(Animation anim) {
537        if (anim instanceof AlphaAnimation) {
538            return true;
539        } else if (anim instanceof AnimationSet) {
540            List<Animation> anims = ((AnimationSet) anim).getAnimations();
541            for (int i = 0; i < anims.size(); i++) {
542                if (anims.get(i) instanceof AlphaAnimation) {
543                    return true;
544                }
545            }
546        }
547        return false;
548    }
549
550    static boolean shouldRunOnHWLayer(View v, Animation anim) {
551        return Build.VERSION.SDK_INT >= 19
552                && ViewCompat.getLayerType(v) == ViewCompat.LAYER_TYPE_NONE
553                && ViewCompat.hasOverlappingRendering(v)
554                && modifiesAlpha(anim);
555    }
556
557    private void throwException(RuntimeException ex) {
558        Log.e(TAG, ex.getMessage());
559        Log.e(TAG, "Activity state:");
560        LogWriter logw = new LogWriter(TAG);
561        PrintWriter pw = new PrintWriter(logw);
562        if (mHost != null) {
563            try {
564                mHost.onDump("  ", null, pw, new String[] { });
565            } catch (Exception e) {
566                Log.e(TAG, "Failed dumping state", e);
567            }
568        } else {
569            try {
570                dump("  ", null, pw, new String[] { });
571            } catch (Exception e) {
572                Log.e(TAG, "Failed dumping state", e);
573            }
574        }
575        throw ex;
576    }
577
578    @Override
579    public FragmentTransaction beginTransaction() {
580        return new BackStackRecord(this);
581    }
582
583    @Override
584    public boolean executePendingTransactions() {
585        return execPendingActions();
586    }
587
588    @Override
589    public void popBackStack() {
590        enqueueAction(new Runnable() {
591            @Override public void run() {
592                popBackStackState(mHost.getHandler(), null, -1, 0);
593            }
594        }, false);
595    }
596
597    @Override
598    public boolean popBackStackImmediate() {
599        checkStateLoss();
600        executePendingTransactions();
601        return popBackStackState(mHost.getHandler(), null, -1, 0);
602    }
603
604    @Override
605    public void popBackStack(final String name, final int flags) {
606        enqueueAction(new Runnable() {
607            @Override public void run() {
608                popBackStackState(mHost.getHandler(), name, -1, flags);
609            }
610        }, false);
611    }
612
613    @Override
614    public boolean popBackStackImmediate(String name, int flags) {
615        checkStateLoss();
616        executePendingTransactions();
617        return popBackStackState(mHost.getHandler(), name, -1, flags);
618    }
619
620    @Override
621    public void popBackStack(final int id, final int flags) {
622        if (id < 0) {
623            throw new IllegalArgumentException("Bad id: " + id);
624        }
625        enqueueAction(new Runnable() {
626            @Override public void run() {
627                popBackStackState(mHost.getHandler(), null, id, flags);
628            }
629        }, false);
630    }
631
632    @Override
633    public boolean popBackStackImmediate(int id, int flags) {
634        checkStateLoss();
635        executePendingTransactions();
636        if (id < 0) {
637            throw new IllegalArgumentException("Bad id: " + id);
638        }
639        return popBackStackState(mHost.getHandler(), null, id, flags);
640    }
641
642    @Override
643    public int getBackStackEntryCount() {
644        return mBackStack != null ? mBackStack.size() : 0;
645    }
646
647    @Override
648    public BackStackEntry getBackStackEntryAt(int index) {
649        return mBackStack.get(index);
650    }
651
652    @Override
653    public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
654        if (mBackStackChangeListeners == null) {
655            mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
656        }
657        mBackStackChangeListeners.add(listener);
658    }
659
660    @Override
661    public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
662        if (mBackStackChangeListeners != null) {
663            mBackStackChangeListeners.remove(listener);
664        }
665    }
666
667    @Override
668    public void putFragment(Bundle bundle, String key, Fragment fragment) {
669        if (fragment.mIndex < 0) {
670            throwException(new IllegalStateException("Fragment " + fragment
671                    + " is not currently in the FragmentManager"));
672        }
673        bundle.putInt(key, fragment.mIndex);
674    }
675
676    @Override
677    public Fragment getFragment(Bundle bundle, String key) {
678        int index = bundle.getInt(key, -1);
679        if (index == -1) {
680            return null;
681        }
682        if (index >= mActive.size()) {
683            throwException(new IllegalStateException("Fragment no longer exists for key "
684                    + key + ": index " + index));
685        }
686        Fragment f = mActive.get(index);
687        if (f == null) {
688            throwException(new IllegalStateException("Fragment no longer exists for key "
689                    + key + ": index " + index));
690        }
691        return f;
692    }
693
694    @Override
695    public List<Fragment> getFragments() {
696        return mActive;
697    }
698
699    @Override
700    public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
701        if (fragment.mIndex < 0) {
702            throwException( new IllegalStateException("Fragment " + fragment
703                    + " is not currently in the FragmentManager"));
704        }
705        if (fragment.mState > Fragment.INITIALIZING) {
706            Bundle result = saveFragmentBasicState(fragment);
707            return result != null ? new Fragment.SavedState(result) : null;
708        }
709        return null;
710    }
711
712    @Override
713    public boolean isDestroyed() {
714        return mDestroyed;
715    }
716
717    @Override
718    public String toString() {
719        StringBuilder sb = new StringBuilder(128);
720        sb.append("FragmentManager{");
721        sb.append(Integer.toHexString(System.identityHashCode(this)));
722        sb.append(" in ");
723        if (mParent != null) {
724            DebugUtils.buildShortClassTag(mParent, sb);
725        } else {
726            DebugUtils.buildShortClassTag(mHost, sb);
727        }
728        sb.append("}}");
729        return sb.toString();
730    }
731
732    @Override
733    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
734        String innerPrefix = prefix + "    ";
735
736        int N;
737        if (mActive != null) {
738            N = mActive.size();
739            if (N > 0) {
740                writer.print(prefix); writer.print("Active Fragments in ");
741                        writer.print(Integer.toHexString(System.identityHashCode(this)));
742                        writer.println(":");
743                for (int i=0; i<N; i++) {
744                    Fragment f = mActive.get(i);
745                    writer.print(prefix); writer.print("  #"); writer.print(i);
746                            writer.print(": "); writer.println(f);
747                    if (f != null) {
748                        f.dump(innerPrefix, fd, writer, args);
749                    }
750                }
751            }
752        }
753
754        if (mAdded != null) {
755            N = mAdded.size();
756            if (N > 0) {
757                writer.print(prefix); writer.println("Added Fragments:");
758                for (int i=0; i<N; i++) {
759                    Fragment f = mAdded.get(i);
760                    writer.print(prefix); writer.print("  #"); writer.print(i);
761                            writer.print(": "); writer.println(f.toString());
762                }
763            }
764        }
765
766        if (mCreatedMenus != null) {
767            N = mCreatedMenus.size();
768            if (N > 0) {
769                writer.print(prefix); writer.println("Fragments Created Menus:");
770                for (int i=0; i<N; i++) {
771                    Fragment f = mCreatedMenus.get(i);
772                    writer.print(prefix); writer.print("  #"); writer.print(i);
773                            writer.print(": "); writer.println(f.toString());
774                }
775            }
776        }
777
778        if (mBackStack != null) {
779            N = mBackStack.size();
780            if (N > 0) {
781                writer.print(prefix); writer.println("Back Stack:");
782                for (int i=0; i<N; i++) {
783                    BackStackRecord bs = mBackStack.get(i);
784                    writer.print(prefix); writer.print("  #"); writer.print(i);
785                            writer.print(": "); writer.println(bs.toString());
786                    bs.dump(innerPrefix, fd, writer, args);
787                }
788            }
789        }
790
791        synchronized (this) {
792            if (mBackStackIndices != null) {
793                N = mBackStackIndices.size();
794                if (N > 0) {
795                    writer.print(prefix); writer.println("Back Stack Indices:");
796                    for (int i=0; i<N; i++) {
797                        BackStackRecord bs = mBackStackIndices.get(i);
798                        writer.print(prefix); writer.print("  #"); writer.print(i);
799                                writer.print(": "); writer.println(bs);
800                    }
801                }
802            }
803
804            if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
805                writer.print(prefix); writer.print("mAvailBackStackIndices: ");
806                        writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
807            }
808        }
809
810        if (mPendingActions != null) {
811            N = mPendingActions.size();
812            if (N > 0) {
813                writer.print(prefix); writer.println("Pending Actions:");
814                for (int i=0; i<N; i++) {
815                    Runnable r = mPendingActions.get(i);
816                    writer.print(prefix); writer.print("  #"); writer.print(i);
817                            writer.print(": "); writer.println(r);
818                }
819            }
820        }
821
822        writer.print(prefix); writer.println("FragmentManager misc state:");
823        writer.print(prefix); writer.print("  mHost="); writer.println(mHost);
824        writer.print(prefix); writer.print("  mContainer="); writer.println(mContainer);
825        if (mParent != null) {
826            writer.print(prefix); writer.print("  mParent="); writer.println(mParent);
827        }
828        writer.print(prefix); writer.print("  mCurState="); writer.print(mCurState);
829                writer.print(" mStateSaved="); writer.print(mStateSaved);
830                writer.print(" mDestroyed="); writer.println(mDestroyed);
831        if (mNeedMenuInvalidate) {
832            writer.print(prefix); writer.print("  mNeedMenuInvalidate=");
833                    writer.println(mNeedMenuInvalidate);
834        }
835        if (mNoTransactionsBecause != null) {
836            writer.print(prefix); writer.print("  mNoTransactionsBecause=");
837                    writer.println(mNoTransactionsBecause);
838        }
839        if (mAvailIndices != null && mAvailIndices.size() > 0) {
840            writer.print(prefix); writer.print("  mAvailIndices: ");
841                    writer.println(Arrays.toString(mAvailIndices.toArray()));
842        }
843    }
844
845    static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
846    static final Interpolator DECELERATE_CUBIC = new DecelerateInterpolator(1.5f);
847    static final Interpolator ACCELERATE_QUINT = new AccelerateInterpolator(2.5f);
848    static final Interpolator ACCELERATE_CUBIC = new AccelerateInterpolator(1.5f);
849
850    static final int ANIM_DUR = 220;
851
852    static Animation makeOpenCloseAnimation(Context context, float startScale,
853            float endScale, float startAlpha, float endAlpha) {
854        AnimationSet set = new AnimationSet(false);
855        ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale,
856                Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
857        scale.setInterpolator(DECELERATE_QUINT);
858        scale.setDuration(ANIM_DUR);
859        set.addAnimation(scale);
860        AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha);
861        alpha.setInterpolator(DECELERATE_CUBIC);
862        alpha.setDuration(ANIM_DUR);
863        set.addAnimation(alpha);
864        return set;
865    }
866
867    static Animation makeFadeAnimation(Context context, float start, float end) {
868        AlphaAnimation anim = new AlphaAnimation(start, end);
869        anim.setInterpolator(DECELERATE_CUBIC);
870        anim.setDuration(ANIM_DUR);
871        return anim;
872    }
873
874    Animation loadAnimation(Fragment fragment, int transit, boolean enter,
875            int transitionStyle) {
876        Animation animObj = fragment.onCreateAnimation(transit, enter,
877                fragment.mNextAnim);
878        if (animObj != null) {
879            return animObj;
880        }
881
882        if (fragment.mNextAnim != 0) {
883            Animation anim = AnimationUtils.loadAnimation(mHost.getContext(), fragment.mNextAnim);
884            if (anim != null) {
885                return anim;
886            }
887        }
888
889        if (transit == 0) {
890            return null;
891        }
892
893        int styleIndex = transitToStyleIndex(transit, enter);
894        if (styleIndex < 0) {
895            return null;
896        }
897
898        switch (styleIndex) {
899            case ANIM_STYLE_OPEN_ENTER:
900                return makeOpenCloseAnimation(mHost.getContext(), 1.125f, 1.0f, 0, 1);
901            case ANIM_STYLE_OPEN_EXIT:
902                return makeOpenCloseAnimation(mHost.getContext(), 1.0f, .975f, 1, 0);
903            case ANIM_STYLE_CLOSE_ENTER:
904                return makeOpenCloseAnimation(mHost.getContext(), .975f, 1.0f, 0, 1);
905            case ANIM_STYLE_CLOSE_EXIT:
906                return makeOpenCloseAnimation(mHost.getContext(), 1.0f, 1.075f, 1, 0);
907            case ANIM_STYLE_FADE_ENTER:
908                return makeFadeAnimation(mHost.getContext(), 0, 1);
909            case ANIM_STYLE_FADE_EXIT:
910                return makeFadeAnimation(mHost.getContext(), 1, 0);
911        }
912
913        if (transitionStyle == 0 && mHost.onHasWindowAnimations()) {
914            transitionStyle = mHost.onGetWindowAnimations();
915        }
916        if (transitionStyle == 0) {
917            return null;
918        }
919
920        //TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
921        //        com.android.internal.R.styleable.FragmentAnimation);
922        //int anim = attrs.getResourceId(styleIndex, 0);
923        //attrs.recycle();
924
925        //if (anim == 0) {
926        //    return null;
927        //}
928
929        //return AnimatorInflater.loadAnimator(mActivity, anim);
930        return null;
931    }
932
933    public void performPendingDeferredStart(Fragment f) {
934        if (f.mDeferStart) {
935            if (mExecutingActions) {
936                // Wait until we're done executing our pending transactions
937                mHavePendingDeferredStart = true;
938                return;
939            }
940            f.mDeferStart = false;
941            moveToState(f, mCurState, 0, 0, false);
942        }
943    }
944
945    /**
946     * Sets the to be animated view on hardware layer during the animation. Note
947     * that calling this will replace any existing animation listener on the animation
948     * with a new one, as animations do not support more than one listeners. Therefore,
949     * animations that already have listeners should do the layer change operations
950     * in their existing listeners, rather than calling this function.
951     */
952    private void setHWLayerAnimListenerIfAlpha(final View v, Animation anim) {
953        if (v == null || anim == null) {
954            return;
955        }
956        if (shouldRunOnHWLayer(v, anim)) {
957            AnimationListener originalListener = null;
958            try {
959                if (sAnimationListenerField == null) {
960                    sAnimationListenerField = Animation.class.getDeclaredField("mListener");
961                    sAnimationListenerField.setAccessible(true);
962                }
963                originalListener = (AnimationListener) sAnimationListenerField.get(anim);
964            } catch (NoSuchFieldException e) {
965                Log.e(TAG, "No field with the name mListener is found in Animation class", e);
966            } catch (IllegalAccessException e) {
967                Log.e(TAG, "Cannot access Animation's mListener field", e);
968            }
969            // If there's already a listener set on the animation, we need wrap the new listener
970            // around the existing listener, so that they will both get animation listener
971            // callbacks.
972            ViewCompat.setLayerType(v, ViewCompat.LAYER_TYPE_HARDWARE, null);
973            anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener(v, anim,
974                    originalListener));
975        }
976    }
977
978    boolean isStateAtLeast(int state) {
979        return mCurState >= state;
980    }
981
982    void moveToState(Fragment f, int newState, int transit, int transitionStyle,
983            boolean keepActive) {
984        // Fragments that are not currently added will sit in the onCreate() state.
985        if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
986            newState = Fragment.CREATED;
987        }
988        if (f.mRemoving && newState > f.mState) {
989            // While removing a fragment, we can't change it to a higher state.
990            newState = f.mState;
991        }
992        // Defer start if requested; don't allow it to move to STARTED or higher
993        // if it's not already started.
994        if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
995            newState = Fragment.STOPPED;
996        }
997        if (f.mState < newState) {
998            // For fragments that are created from a layout, when restoring from
999            // state we don't want to allow them to be created until they are
1000            // being reloaded from the layout.
1001            if (f.mFromLayout && !f.mInLayout) {
1002                return;
1003            }
1004            if (f.mAnimatingAway != null) {
1005                // The fragment is currently being animated...  but!  Now we
1006                // want to move our state back up.  Give up on waiting for the
1007                // animation, move to whatever the final state should be once
1008                // the animation is done, and then we can proceed from there.
1009                f.mAnimatingAway = null;
1010                moveToState(f, f.mStateAfterAnimating, 0, 0, true);
1011            }
1012            switch (f.mState) {
1013                case Fragment.INITIALIZING:
1014                    if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
1015                    if (f.mSavedFragmentState != null) {
1016                        f.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader());
1017                        f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
1018                                FragmentManagerImpl.VIEW_STATE_TAG);
1019                        f.mTarget = getFragment(f.mSavedFragmentState,
1020                                FragmentManagerImpl.TARGET_STATE_TAG);
1021                        if (f.mTarget != null) {
1022                            f.mTargetRequestCode = f.mSavedFragmentState.getInt(
1023                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
1024                        }
1025                        f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
1026                                FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
1027                        if (!f.mUserVisibleHint) {
1028                            f.mDeferStart = true;
1029                            if (newState > Fragment.STOPPED) {
1030                                newState = Fragment.STOPPED;
1031                            }
1032                        }
1033                    }
1034                    f.mHost = mHost;
1035                    f.mParentFragment = mParent;
1036                    f.mFragmentManager = mParent != null
1037                            ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl();
1038                    f.mCalled = false;
1039                    f.onAttach(mHost.getContext());
1040                    if (!f.mCalled) {
1041                        throw new SuperNotCalledException("Fragment " + f
1042                                + " did not call through to super.onAttach()");
1043                    }
1044                    if (f.mParentFragment == null) {
1045                        mHost.onAttachFragment(f);
1046                    }
1047
1048                    if (!f.mRetaining) {
1049                        f.performCreate(f.mSavedFragmentState);
1050                    } else {
1051                        f.restoreChildFragmentState(f.mSavedFragmentState);
1052                        f.mState = Fragment.CREATED;
1053                    }
1054                    f.mRetaining = false;
1055                    if (f.mFromLayout) {
1056                        // For fragments that are part of the content view
1057                        // layout, we need to instantiate the view immediately
1058                        // and the inflater will take care of adding it.
1059                        f.mView = f.performCreateView(f.getLayoutInflater(
1060                                f.mSavedFragmentState), null, f.mSavedFragmentState);
1061                        if (f.mView != null) {
1062                            f.mInnerView = f.mView;
1063                            if (Build.VERSION.SDK_INT >= 11) {
1064                                ViewCompat.setSaveFromParentEnabled(f.mView, false);
1065                            } else {
1066                                f.mView = NoSaveStateFrameLayout.wrap(f.mView);
1067                            }
1068                            if (f.mHidden) f.mView.setVisibility(View.GONE);
1069                            f.onViewCreated(f.mView, f.mSavedFragmentState);
1070                        } else {
1071                            f.mInnerView = null;
1072                        }
1073                    }
1074                case Fragment.CREATED:
1075                    if (newState > Fragment.CREATED) {
1076                        if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
1077                        if (!f.mFromLayout) {
1078                            ViewGroup container = null;
1079                            if (f.mContainerId != 0) {
1080                                if (f.mContainerId == View.NO_ID) {
1081                                    throwException(new IllegalArgumentException(
1082                                            "Cannot create fragment "
1083                                                    + f
1084                                                    + " for a container view with no id"));
1085                                }
1086                                container = (ViewGroup) mContainer.onFindViewById(f.mContainerId);
1087                                if (container == null && !f.mRestored) {
1088                                    String resName;
1089                                    try {
1090                                        resName = f.getResources().getResourceName(f.mContainerId);
1091                                    } catch (NotFoundException e) {
1092                                        resName = "unknown";
1093                                    }
1094                                    throwException(new IllegalArgumentException(
1095                                            "No view found for id 0x"
1096                                            + Integer.toHexString(f.mContainerId) + " ("
1097                                            + resName
1098                                            + ") for fragment " + f));
1099                                }
1100                            }
1101                            f.mContainer = container;
1102                            f.mView = f.performCreateView(f.getLayoutInflater(
1103                                    f.mSavedFragmentState), container, f.mSavedFragmentState);
1104                            if (f.mView != null) {
1105                                f.mInnerView = f.mView;
1106                                if (Build.VERSION.SDK_INT >= 11) {
1107                                    ViewCompat.setSaveFromParentEnabled(f.mView, false);
1108                                } else {
1109                                    f.mView = NoSaveStateFrameLayout.wrap(f.mView);
1110                                }
1111                                if (container != null) {
1112                                    Animation anim = loadAnimation(f, transit, true,
1113                                            transitionStyle);
1114                                    if (anim != null) {
1115                                        setHWLayerAnimListenerIfAlpha(f.mView, anim);
1116                                        f.mView.startAnimation(anim);
1117                                    }
1118                                    container.addView(f.mView);
1119                                }
1120                                if (f.mHidden) f.mView.setVisibility(View.GONE);
1121                                f.onViewCreated(f.mView, f.mSavedFragmentState);
1122                            } else {
1123                                f.mInnerView = null;
1124                            }
1125                        }
1126
1127                        f.performActivityCreated(f.mSavedFragmentState);
1128                        if (f.mView != null) {
1129                            f.restoreViewState(f.mSavedFragmentState);
1130                        }
1131                        f.mSavedFragmentState = null;
1132                    }
1133                case Fragment.ACTIVITY_CREATED:
1134                    if (newState > Fragment.ACTIVITY_CREATED) {
1135                        f.mState = Fragment.STOPPED;
1136                    }
1137                case Fragment.STOPPED:
1138                    if (newState > Fragment.STOPPED) {
1139                        if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
1140                        f.performStart();
1141                    }
1142                case Fragment.STARTED:
1143                    if (newState > Fragment.STARTED) {
1144                        if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
1145                        f.performResume();
1146                        f.mSavedFragmentState = null;
1147                        f.mSavedViewState = null;
1148                    }
1149            }
1150        } else if (f.mState > newState) {
1151            switch (f.mState) {
1152                case Fragment.RESUMED:
1153                    if (newState < Fragment.RESUMED) {
1154                        if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
1155                        f.performPause();
1156                    }
1157                case Fragment.STARTED:
1158                    if (newState < Fragment.STARTED) {
1159                        if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
1160                        f.performStop();
1161                    }
1162                case Fragment.STOPPED:
1163                    if (newState < Fragment.STOPPED) {
1164                        if (DEBUG) Log.v(TAG, "movefrom STOPPED: " + f);
1165                        f.performReallyStop();
1166                    }
1167                case Fragment.ACTIVITY_CREATED:
1168                    if (newState < Fragment.ACTIVITY_CREATED) {
1169                        if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
1170                        if (f.mView != null) {
1171                            // Need to save the current view state if not
1172                            // done already.
1173                            if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) {
1174                                saveFragmentViewState(f);
1175                            }
1176                        }
1177                        f.performDestroyView();
1178                        if (f.mView != null && f.mContainer != null) {
1179                            Animation anim = null;
1180                            if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
1181                                anim = loadAnimation(f, transit, false,
1182                                        transitionStyle);
1183                            }
1184                            if (anim != null) {
1185                                final Fragment fragment = f;
1186                                f.mAnimatingAway = f.mView;
1187                                f.mStateAfterAnimating = newState;
1188                                final View viewToAnimate = f.mView;
1189                                anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener(
1190                                        viewToAnimate, anim) {
1191                                    @Override
1192                                    public void onAnimationEnd(Animation animation) {
1193                                        super.onAnimationEnd(animation);
1194                                        if (fragment.mAnimatingAway != null) {
1195                                            fragment.mAnimatingAway = null;
1196                                            moveToState(fragment, fragment.mStateAfterAnimating,
1197                                                    0, 0, false);
1198                                        }
1199                                    }
1200                                });
1201                                f.mView.startAnimation(anim);
1202                            }
1203                            f.mContainer.removeView(f.mView);
1204                        }
1205                        f.mContainer = null;
1206                        f.mView = null;
1207                        f.mInnerView = null;
1208                    }
1209                case Fragment.CREATED:
1210                    if (newState < Fragment.CREATED) {
1211                        if (mDestroyed) {
1212                            if (f.mAnimatingAway != null) {
1213                                // The fragment's containing activity is
1214                                // being destroyed, but this fragment is
1215                                // currently animating away.  Stop the
1216                                // animation right now -- it is not needed,
1217                                // and we can't wait any more on destroying
1218                                // the fragment.
1219                                View v = f.mAnimatingAway;
1220                                f.mAnimatingAway = null;
1221                                v.clearAnimation();
1222                            }
1223                        }
1224                        if (f.mAnimatingAway != null) {
1225                            // We are waiting for the fragment's view to finish
1226                            // animating away.  Just make a note of the state
1227                            // the fragment now should move to once the animation
1228                            // is done.
1229                            f.mStateAfterAnimating = newState;
1230                            newState = Fragment.CREATED;
1231                        } else {
1232                            if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1233                            if (!f.mRetaining) {
1234                                f.performDestroy();
1235                            } else {
1236                                f.mState = Fragment.INITIALIZING;
1237                            }
1238
1239                            f.performDetach();
1240                            if (!keepActive) {
1241                                if (!f.mRetaining) {
1242                                    makeInactive(f);
1243                                } else {
1244                                    f.mHost = null;
1245                                    f.mParentFragment = null;
1246                                    f.mFragmentManager = null;
1247                                }
1248                            }
1249                        }
1250                    }
1251            }
1252        }
1253
1254        if (f.mState != newState) {
1255            Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; "
1256                    + "expected state " + newState + " found " + f.mState);
1257            f.mState = newState;
1258        }
1259    }
1260
1261    void moveToState(Fragment f) {
1262        moveToState(f, mCurState, 0, 0, false);
1263    }
1264
1265    void moveToState(int newState, boolean always) {
1266        moveToState(newState, 0, 0, always);
1267    }
1268
1269    void moveToState(int newState, int transit, int transitStyle, boolean always) {
1270        if (mHost == null && newState != Fragment.INITIALIZING) {
1271            throw new IllegalStateException("No host");
1272        }
1273
1274        if (!always && mCurState == newState) {
1275            return;
1276        }
1277
1278        mCurState = newState;
1279        if (mActive != null) {
1280            boolean loadersRunning = false;
1281            for (int i=0; i<mActive.size(); i++) {
1282                Fragment f = mActive.get(i);
1283                if (f != null) {
1284                    moveToState(f, newState, transit, transitStyle, false);
1285                    if (f.mLoaderManager != null) {
1286                        loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1287                    }
1288                }
1289            }
1290
1291            if (!loadersRunning) {
1292                startPendingDeferredFragments();
1293            }
1294
1295            if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) {
1296                mHost.onSupportInvalidateOptionsMenu();
1297                mNeedMenuInvalidate = false;
1298            }
1299        }
1300    }
1301
1302    void startPendingDeferredFragments() {
1303        if (mActive == null) return;
1304
1305        for (int i=0; i<mActive.size(); i++) {
1306            Fragment f = mActive.get(i);
1307            if (f != null) {
1308                performPendingDeferredStart(f);
1309            }
1310        }
1311    }
1312
1313    void makeActive(Fragment f) {
1314        if (f.mIndex >= 0) {
1315            return;
1316        }
1317
1318        if (mAvailIndices == null || mAvailIndices.size() <= 0) {
1319            if (mActive == null) {
1320                mActive = new ArrayList<Fragment>();
1321            }
1322            f.setIndex(mActive.size(), mParent);
1323            mActive.add(f);
1324
1325        } else {
1326            f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent);
1327            mActive.set(f.mIndex, f);
1328        }
1329        if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
1330    }
1331
1332    void makeInactive(Fragment f) {
1333        if (f.mIndex < 0) {
1334            return;
1335        }
1336
1337        if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
1338        mActive.set(f.mIndex, null);
1339        if (mAvailIndices == null) {
1340            mAvailIndices = new ArrayList<Integer>();
1341        }
1342        mAvailIndices.add(f.mIndex);
1343        mHost.inactivateFragment(f.mWho);
1344        f.initState();
1345    }
1346
1347    public void addFragment(Fragment fragment, boolean moveToStateNow) {
1348        if (mAdded == null) {
1349            mAdded = new ArrayList<Fragment>();
1350        }
1351        if (DEBUG) Log.v(TAG, "add: " + fragment);
1352        makeActive(fragment);
1353        if (!fragment.mDetached) {
1354            if (mAdded.contains(fragment)) {
1355                throw new IllegalStateException("Fragment already added: " + fragment);
1356            }
1357            mAdded.add(fragment);
1358            fragment.mAdded = true;
1359            fragment.mRemoving = false;
1360            if (fragment.mHasMenu && fragment.mMenuVisible) {
1361                mNeedMenuInvalidate = true;
1362            }
1363            if (moveToStateNow) {
1364                moveToState(fragment);
1365            }
1366        }
1367    }
1368
1369    public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
1370        if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
1371        final boolean inactive = !fragment.isInBackStack();
1372        if (!fragment.mDetached || inactive) {
1373            if (mAdded != null) {
1374                mAdded.remove(fragment);
1375            }
1376            if (fragment.mHasMenu && fragment.mMenuVisible) {
1377                mNeedMenuInvalidate = true;
1378            }
1379            fragment.mAdded = false;
1380            fragment.mRemoving = true;
1381            moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED,
1382                    transition, transitionStyle, false);
1383        }
1384    }
1385
1386    public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
1387        if (DEBUG) Log.v(TAG, "hide: " + fragment);
1388        if (!fragment.mHidden) {
1389            fragment.mHidden = true;
1390            if (fragment.mView != null) {
1391                Animation anim = loadAnimation(fragment, transition, false,
1392                        transitionStyle);
1393                if (anim != null) {
1394                    setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1395                    fragment.mView.startAnimation(anim);
1396                }
1397                fragment.mView.setVisibility(View.GONE);
1398            }
1399            if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1400                mNeedMenuInvalidate = true;
1401            }
1402            fragment.onHiddenChanged(true);
1403        }
1404    }
1405
1406    public void showFragment(Fragment fragment, int transition, int transitionStyle) {
1407        if (DEBUG) Log.v(TAG, "show: " + fragment);
1408        if (fragment.mHidden) {
1409            fragment.mHidden = false;
1410            if (fragment.mView != null) {
1411                Animation anim = loadAnimation(fragment, transition, true,
1412                        transitionStyle);
1413                if (anim != null) {
1414                    setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1415                    fragment.mView.startAnimation(anim);
1416                }
1417                fragment.mView.setVisibility(View.VISIBLE);
1418            }
1419            if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1420                mNeedMenuInvalidate = true;
1421            }
1422            fragment.onHiddenChanged(false);
1423        }
1424    }
1425
1426    public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
1427        if (DEBUG) Log.v(TAG, "detach: " + fragment);
1428        if (!fragment.mDetached) {
1429            fragment.mDetached = true;
1430            if (fragment.mAdded) {
1431                // We are not already in back stack, so need to remove the fragment.
1432                if (mAdded != null) {
1433                    if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
1434                    mAdded.remove(fragment);
1435                }
1436                if (fragment.mHasMenu && fragment.mMenuVisible) {
1437                    mNeedMenuInvalidate = true;
1438                }
1439                fragment.mAdded = false;
1440                moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false);
1441            }
1442        }
1443    }
1444
1445    public void attachFragment(Fragment fragment, int transition, int transitionStyle) {
1446        if (DEBUG) Log.v(TAG, "attach: " + fragment);
1447        if (fragment.mDetached) {
1448            fragment.mDetached = false;
1449            if (!fragment.mAdded) {
1450                if (mAdded == null) {
1451                    mAdded = new ArrayList<Fragment>();
1452                }
1453                if (mAdded.contains(fragment)) {
1454                    throw new IllegalStateException("Fragment already added: " + fragment);
1455                }
1456                if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
1457                mAdded.add(fragment);
1458                fragment.mAdded = true;
1459                if (fragment.mHasMenu && fragment.mMenuVisible) {
1460                    mNeedMenuInvalidate = true;
1461                }
1462                moveToState(fragment, mCurState, transition, transitionStyle, false);
1463            }
1464        }
1465    }
1466
1467    public Fragment findFragmentById(int id) {
1468        if (mAdded != null) {
1469            // First look through added fragments.
1470            for (int i=mAdded.size()-1; i>=0; i--) {
1471                Fragment f = mAdded.get(i);
1472                if (f != null && f.mFragmentId == id) {
1473                    return f;
1474                }
1475            }
1476        }
1477        if (mActive != null) {
1478            // Now for any known fragment.
1479            for (int i=mActive.size()-1; i>=0; i--) {
1480                Fragment f = mActive.get(i);
1481                if (f != null && f.mFragmentId == id) {
1482                    return f;
1483                }
1484            }
1485        }
1486        return null;
1487    }
1488
1489    public Fragment findFragmentByTag(String tag) {
1490        if (mAdded != null && tag != null) {
1491            // First look through added fragments.
1492            for (int i=mAdded.size()-1; i>=0; i--) {
1493                Fragment f = mAdded.get(i);
1494                if (f != null && tag.equals(f.mTag)) {
1495                    return f;
1496                }
1497            }
1498        }
1499        if (mActive != null && tag != null) {
1500            // Now for any known fragment.
1501            for (int i=mActive.size()-1; i>=0; i--) {
1502                Fragment f = mActive.get(i);
1503                if (f != null && tag.equals(f.mTag)) {
1504                    return f;
1505                }
1506            }
1507        }
1508        return null;
1509    }
1510
1511    public Fragment findFragmentByWho(String who) {
1512        if (mActive != null && who != null) {
1513            for (int i=mActive.size()-1; i>=0; i--) {
1514                Fragment f = mActive.get(i);
1515                if (f != null && (f=f.findFragmentByWho(who)) != null) {
1516                    return f;
1517                }
1518            }
1519        }
1520        return null;
1521    }
1522
1523    private void checkStateLoss() {
1524        if (mStateSaved) {
1525            throw new IllegalStateException(
1526                    "Can not perform this action after onSaveInstanceState");
1527        }
1528        if (mNoTransactionsBecause != null) {
1529            throw new IllegalStateException(
1530                    "Can not perform this action inside of " + mNoTransactionsBecause);
1531        }
1532    }
1533
1534    /**
1535     * Adds an action to the queue of pending actions.
1536     *
1537     * @param action the action to add
1538     * @param allowStateLoss whether to allow loss of state information
1539     * @throws IllegalStateException if the activity has been destroyed
1540     */
1541    public void enqueueAction(Runnable action, boolean allowStateLoss) {
1542        if (!allowStateLoss) {
1543            checkStateLoss();
1544        }
1545        synchronized (this) {
1546            if (mDestroyed || mHost == null) {
1547                throw new IllegalStateException("Activity has been destroyed");
1548            }
1549            if (mPendingActions == null) {
1550                mPendingActions = new ArrayList<Runnable>();
1551            }
1552            mPendingActions.add(action);
1553            if (mPendingActions.size() == 1) {
1554                mHost.getHandler().removeCallbacks(mExecCommit);
1555                mHost.getHandler().post(mExecCommit);
1556            }
1557        }
1558    }
1559
1560    public int allocBackStackIndex(BackStackRecord bse) {
1561        synchronized (this) {
1562            if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1563                if (mBackStackIndices == null) {
1564                    mBackStackIndices = new ArrayList<BackStackRecord>();
1565                }
1566                int index = mBackStackIndices.size();
1567                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1568                mBackStackIndices.add(bse);
1569                return index;
1570
1571            } else {
1572                int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1573                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1574                mBackStackIndices.set(index, bse);
1575                return index;
1576            }
1577        }
1578    }
1579
1580    public void setBackStackIndex(int index, BackStackRecord bse) {
1581        synchronized (this) {
1582            if (mBackStackIndices == null) {
1583                mBackStackIndices = new ArrayList<BackStackRecord>();
1584            }
1585            int N = mBackStackIndices.size();
1586            if (index < N) {
1587                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1588                mBackStackIndices.set(index, bse);
1589            } else {
1590                while (N < index) {
1591                    mBackStackIndices.add(null);
1592                    if (mAvailBackStackIndices == null) {
1593                        mAvailBackStackIndices = new ArrayList<Integer>();
1594                    }
1595                    if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1596                    mAvailBackStackIndices.add(N);
1597                    N++;
1598                }
1599                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1600                mBackStackIndices.add(bse);
1601            }
1602        }
1603    }
1604
1605    public void freeBackStackIndex(int index) {
1606        synchronized (this) {
1607            mBackStackIndices.set(index, null);
1608            if (mAvailBackStackIndices == null) {
1609                mAvailBackStackIndices = new ArrayList<Integer>();
1610            }
1611            if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1612            mAvailBackStackIndices.add(index);
1613        }
1614    }
1615
1616    public void execSingleAction(Runnable action, boolean allowStateLoss) {
1617        if (mExecutingActions) {
1618            throw new IllegalStateException("FragmentManager is already executing transactions");
1619        }
1620
1621        if (Looper.myLooper() != mHost.getHandler().getLooper()) {
1622            throw new IllegalStateException("Must be called from main thread of fragment host");
1623        }
1624
1625        if (!allowStateLoss) {
1626            checkStateLoss();
1627        }
1628
1629        mExecutingActions = true;
1630        action.run();
1631        mExecutingActions = false;
1632
1633        doPendingDeferredStart();
1634    }
1635
1636    /**
1637     * Only call from main thread!
1638     */
1639    public boolean execPendingActions() {
1640        if (mExecutingActions) {
1641            throw new IllegalStateException("FragmentManager is already executing transactions");
1642        }
1643
1644        if (Looper.myLooper() != mHost.getHandler().getLooper()) {
1645            throw new IllegalStateException("Must be called from main thread of fragment host");
1646        }
1647
1648        boolean didSomething = false;
1649
1650        while (true) {
1651            int numActions;
1652
1653            synchronized (this) {
1654                if (mPendingActions == null || mPendingActions.size() == 0) {
1655                    break;
1656                }
1657
1658                numActions = mPendingActions.size();
1659                if (mTmpActions == null || mTmpActions.length < numActions) {
1660                    mTmpActions = new Runnable[numActions];
1661                }
1662                mPendingActions.toArray(mTmpActions);
1663                mPendingActions.clear();
1664                mHost.getHandler().removeCallbacks(mExecCommit);
1665            }
1666
1667            mExecutingActions = true;
1668            for (int i=0; i<numActions; i++) {
1669                mTmpActions[i].run();
1670                mTmpActions[i] = null;
1671            }
1672            mExecutingActions = false;
1673            didSomething = true;
1674        }
1675
1676        doPendingDeferredStart();
1677
1678        return didSomething;
1679    }
1680
1681    void doPendingDeferredStart() {
1682        if (mHavePendingDeferredStart) {
1683            boolean loadersRunning = false;
1684            for (int i = 0; i < mActive.size(); i++) {
1685                Fragment f = mActive.get(i);
1686                if (f != null && f.mLoaderManager != null) {
1687                    loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1688                }
1689            }
1690            if (!loadersRunning) {
1691                mHavePendingDeferredStart = false;
1692                startPendingDeferredFragments();
1693            }
1694        }
1695    }
1696
1697    void reportBackStackChanged() {
1698        if (mBackStackChangeListeners != null) {
1699            for (int i=0; i<mBackStackChangeListeners.size(); i++) {
1700                mBackStackChangeListeners.get(i).onBackStackChanged();
1701            }
1702        }
1703    }
1704
1705    void addBackStackState(BackStackRecord state) {
1706        if (mBackStack == null) {
1707            mBackStack = new ArrayList<BackStackRecord>();
1708        }
1709        mBackStack.add(state);
1710        reportBackStackChanged();
1711    }
1712
1713    @SuppressWarnings("unused")
1714    boolean popBackStackState(Handler handler, String name, int id, int flags) {
1715        if (mBackStack == null) {
1716            return false;
1717        }
1718        if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) {
1719            int last = mBackStack.size()-1;
1720            if (last < 0) {
1721                return false;
1722            }
1723            final BackStackRecord bss = mBackStack.remove(last);
1724            SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
1725            SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
1726            if (mCurState >= Fragment.CREATED) {
1727                bss.calculateBackFragments(firstOutFragments, lastInFragments);
1728            }
1729            bss.popFromBackStack(true, null, firstOutFragments, lastInFragments);
1730            reportBackStackChanged();
1731        } else {
1732            int index = -1;
1733            if (name != null || id >= 0) {
1734                // If a name or ID is specified, look for that place in
1735                // the stack.
1736                index = mBackStack.size()-1;
1737                while (index >= 0) {
1738                    BackStackRecord bss = mBackStack.get(index);
1739                    if (name != null && name.equals(bss.getName())) {
1740                        break;
1741                    }
1742                    if (id >= 0 && id == bss.mIndex) {
1743                        break;
1744                    }
1745                    index--;
1746                }
1747                if (index < 0) {
1748                    return false;
1749                }
1750                if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
1751                    index--;
1752                    // Consume all following entries that match.
1753                    while (index >= 0) {
1754                        BackStackRecord bss = mBackStack.get(index);
1755                        if ((name != null && name.equals(bss.getName()))
1756                                || (id >= 0 && id == bss.mIndex)) {
1757                            index--;
1758                            continue;
1759                        }
1760                        break;
1761                    }
1762                }
1763            }
1764            if (index == mBackStack.size()-1) {
1765                return false;
1766            }
1767            final ArrayList<BackStackRecord> states
1768                    = new ArrayList<BackStackRecord>();
1769            for (int i=mBackStack.size()-1; i>index; i--) {
1770                states.add(mBackStack.remove(i));
1771            }
1772            final int LAST = states.size()-1;
1773            SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
1774            SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
1775            if (mCurState >= Fragment.CREATED) {
1776                for (int i = 0; i <= LAST; i++) {
1777                    states.get(i).calculateBackFragments(firstOutFragments, lastInFragments);
1778                }
1779            }
1780            BackStackRecord.TransitionState state = null;
1781            for (int i=0; i<=LAST; i++) {
1782                if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i));
1783                state = states.get(i).popFromBackStack(i == LAST, state,
1784                        firstOutFragments, lastInFragments);
1785            }
1786            reportBackStackChanged();
1787        }
1788        return true;
1789    }
1790
1791    FragmentManagerNonConfig retainNonConfig() {
1792        ArrayList<Fragment> fragments = null;
1793        ArrayList<FragmentManagerNonConfig> childFragments = null;
1794        if (mActive != null) {
1795            for (int i=0; i<mActive.size(); i++) {
1796                Fragment f = mActive.get(i);
1797                if (f != null) {
1798                    if (f.mRetainInstance) {
1799                        if (fragments == null) {
1800                            fragments = new ArrayList<Fragment>();
1801                        }
1802                        fragments.add(f);
1803                        f.mRetaining = true;
1804                        f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
1805                        if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
1806                    }
1807                    boolean addedChild = false;
1808                    if (f.mChildFragmentManager != null) {
1809                        FragmentManagerNonConfig child = f.mChildFragmentManager.retainNonConfig();
1810                        if (child != null) {
1811                            if (childFragments == null) {
1812                                childFragments = new ArrayList<FragmentManagerNonConfig>();
1813                                for (int j = 0; j < i; j++) {
1814                                    childFragments.add(null);
1815                                }
1816                            }
1817                            childFragments.add(child);
1818                            addedChild = true;
1819                        }
1820                    }
1821                    if (childFragments != null && !addedChild) {
1822                        childFragments.add(null);
1823                    }
1824                }
1825            }
1826        }
1827        if (fragments == null && childFragments == null) {
1828            return null;
1829        }
1830        return new FragmentManagerNonConfig(fragments, childFragments);
1831    }
1832
1833    void saveFragmentViewState(Fragment f) {
1834        if (f.mInnerView == null) {
1835            return;
1836        }
1837        if (mStateArray == null) {
1838            mStateArray = new SparseArray<Parcelable>();
1839        } else {
1840            mStateArray.clear();
1841        }
1842        f.mInnerView.saveHierarchyState(mStateArray);
1843        if (mStateArray.size() > 0) {
1844            f.mSavedViewState = mStateArray;
1845            mStateArray = null;
1846        }
1847    }
1848
1849    Bundle saveFragmentBasicState(Fragment f) {
1850        Bundle result = null;
1851
1852        if (mStateBundle == null) {
1853            mStateBundle = new Bundle();
1854        }
1855        f.performSaveInstanceState(mStateBundle);
1856        if (!mStateBundle.isEmpty()) {
1857            result = mStateBundle;
1858            mStateBundle = null;
1859        }
1860
1861        if (f.mView != null) {
1862            saveFragmentViewState(f);
1863        }
1864        if (f.mSavedViewState != null) {
1865            if (result == null) {
1866                result = new Bundle();
1867            }
1868            result.putSparseParcelableArray(
1869                    FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
1870        }
1871        if (!f.mUserVisibleHint) {
1872            if (result == null) {
1873                result = new Bundle();
1874            }
1875            // Only add this if it's not the default value
1876            result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
1877        }
1878
1879        return result;
1880    }
1881
1882    Parcelable saveAllState() {
1883        // Make sure all pending operations have now been executed to get
1884        // our state update-to-date.
1885        execPendingActions();
1886
1887        if (HONEYCOMB) {
1888            // As of Honeycomb, we save state after pausing.  Prior to that
1889            // it is before pausing.  With fragments this is an issue, since
1890            // there are many things you may do after pausing but before
1891            // stopping that change the fragment state.  For those older
1892            // devices, we will not at this point say that we have saved
1893            // the state, so we will allow them to continue doing fragment
1894            // transactions.  This retains the same semantics as Honeycomb,
1895            // though you do have the risk of losing the very most recent state
1896            // if the process is killed...  we'll live with that.
1897            mStateSaved = true;
1898        }
1899
1900        if (mActive == null || mActive.size() <= 0) {
1901            return null;
1902        }
1903
1904        // First collect all active fragments.
1905        int N = mActive.size();
1906        FragmentState[] active = new FragmentState[N];
1907        boolean haveFragments = false;
1908        for (int i=0; i<N; i++) {
1909            Fragment f = mActive.get(i);
1910            if (f != null) {
1911                if (f.mIndex < 0) {
1912                    throwException(new IllegalStateException(
1913                            "Failure saving state: active " + f
1914                            + " has cleared index: " + f.mIndex));
1915                }
1916
1917                haveFragments = true;
1918
1919                FragmentState fs = new FragmentState(f);
1920                active[i] = fs;
1921
1922                if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
1923                    fs.mSavedFragmentState = saveFragmentBasicState(f);
1924
1925                    if (f.mTarget != null) {
1926                        if (f.mTarget.mIndex < 0) {
1927                            throwException(new IllegalStateException(
1928                                    "Failure saving state: " + f
1929                                    + " has target not in fragment manager: " + f.mTarget));
1930                        }
1931                        if (fs.mSavedFragmentState == null) {
1932                            fs.mSavedFragmentState = new Bundle();
1933                        }
1934                        putFragment(fs.mSavedFragmentState,
1935                                FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
1936                        if (f.mTargetRequestCode != 0) {
1937                            fs.mSavedFragmentState.putInt(
1938                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
1939                                    f.mTargetRequestCode);
1940                        }
1941                    }
1942
1943                } else {
1944                    fs.mSavedFragmentState = f.mSavedFragmentState;
1945                }
1946
1947                if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
1948                        + fs.mSavedFragmentState);
1949            }
1950        }
1951
1952        if (!haveFragments) {
1953            if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
1954            return null;
1955        }
1956
1957        int[] added = null;
1958        BackStackState[] backStack = null;
1959
1960        // Build list of currently added fragments.
1961        if (mAdded != null) {
1962            N = mAdded.size();
1963            if (N > 0) {
1964                added = new int[N];
1965                for (int i=0; i<N; i++) {
1966                    added[i] = mAdded.get(i).mIndex;
1967                    if (added[i] < 0) {
1968                        throwException(new IllegalStateException(
1969                                "Failure saving state: active " + mAdded.get(i)
1970                                + " has cleared index: " + added[i]));
1971                    }
1972                    if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
1973                            + ": " + mAdded.get(i));
1974                }
1975            }
1976        }
1977
1978        // Now save back stack.
1979        if (mBackStack != null) {
1980            N = mBackStack.size();
1981            if (N > 0) {
1982                backStack = new BackStackState[N];
1983                for (int i=0; i<N; i++) {
1984                    backStack[i] = new BackStackState(mBackStack.get(i));
1985                    if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
1986                            + ": " + mBackStack.get(i));
1987                }
1988            }
1989        }
1990
1991        FragmentManagerState fms = new FragmentManagerState();
1992        fms.mActive = active;
1993        fms.mAdded = added;
1994        fms.mBackStack = backStack;
1995        return fms;
1996    }
1997
1998    void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) {
1999        // If there is no saved state at all, then there can not be
2000        // any nonConfig fragments either, so that is that.
2001        if (state == null) return;
2002        FragmentManagerState fms = (FragmentManagerState)state;
2003        if (fms.mActive == null) return;
2004
2005        List<FragmentManagerNonConfig> childNonConfigs = null;
2006
2007        // First re-attach any non-config instances we are retaining back
2008        // to their saved state, so we don't try to instantiate them again.
2009        if (nonConfig != null) {
2010            List<Fragment> nonConfigFragments = nonConfig.getFragments();
2011            childNonConfigs = nonConfig.getChildNonConfigs();
2012            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2013            for (int i = 0; i < count; i++) {
2014                Fragment f = nonConfigFragments.get(i);
2015                if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
2016                FragmentState fs = fms.mActive[f.mIndex];
2017                fs.mInstance = f;
2018                f.mSavedViewState = null;
2019                f.mBackStackNesting = 0;
2020                f.mInLayout = false;
2021                f.mAdded = false;
2022                f.mTarget = null;
2023                if (fs.mSavedFragmentState != null) {
2024                    fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader());
2025                    f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
2026                            FragmentManagerImpl.VIEW_STATE_TAG);
2027                    f.mSavedFragmentState = fs.mSavedFragmentState;
2028                }
2029            }
2030        }
2031
2032        // Build the full list of active fragments, instantiating them from
2033        // their saved state.
2034        mActive = new ArrayList<>(fms.mActive.length);
2035        if (mAvailIndices != null) {
2036            mAvailIndices.clear();
2037        }
2038        for (int i=0; i<fms.mActive.length; i++) {
2039            FragmentState fs = fms.mActive[i];
2040            if (fs != null) {
2041                FragmentManagerNonConfig childNonConfig = null;
2042                if (childNonConfigs != null && i < childNonConfigs.size()) {
2043                    childNonConfig = childNonConfigs.get(i);
2044                }
2045                Fragment f = fs.instantiate(mHost, mParent, childNonConfig);
2046                if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
2047                mActive.add(f);
2048                // Now that the fragment is instantiated (or came from being
2049                // retained above), clear mInstance in case we end up re-restoring
2050                // from this FragmentState again.
2051                fs.mInstance = null;
2052            } else {
2053                mActive.add(null);
2054                if (mAvailIndices == null) {
2055                    mAvailIndices = new ArrayList<Integer>();
2056                }
2057                if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
2058                mAvailIndices.add(i);
2059            }
2060        }
2061
2062        // Update the target of all retained fragments.
2063        if (nonConfig != null) {
2064            List<Fragment> nonConfigFragments = nonConfig.getFragments();
2065            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2066            for (int i = 0; i < count; i++) {
2067                Fragment f = nonConfigFragments.get(i);
2068                if (f.mTargetIndex >= 0) {
2069                    if (f.mTargetIndex < mActive.size()) {
2070                        f.mTarget = mActive.get(f.mTargetIndex);
2071                    } else {
2072                        Log.w(TAG, "Re-attaching retained fragment " + f
2073                                + " target no longer exists: " + f.mTargetIndex);
2074                        f.mTarget = null;
2075                    }
2076                }
2077            }
2078        }
2079
2080        // Build the list of currently added fragments.
2081        if (fms.mAdded != null) {
2082            mAdded = new ArrayList<Fragment>(fms.mAdded.length);
2083            for (int i=0; i<fms.mAdded.length; i++) {
2084                Fragment f = mActive.get(fms.mAdded[i]);
2085                if (f == null) {
2086                    throwException(new IllegalStateException(
2087                            "No instantiated fragment for index #" + fms.mAdded[i]));
2088                }
2089                f.mAdded = true;
2090                if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
2091                if (mAdded.contains(f)) {
2092                    throw new IllegalStateException("Already added!");
2093                }
2094                mAdded.add(f);
2095            }
2096        } else {
2097            mAdded = null;
2098        }
2099
2100        // Build the back stack.
2101        if (fms.mBackStack != null) {
2102            mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
2103            for (int i=0; i<fms.mBackStack.length; i++) {
2104                BackStackRecord bse = fms.mBackStack[i].instantiate(this);
2105                if (DEBUG) {
2106                    Log.v(TAG, "restoreAllState: back stack #" + i
2107                        + " (index " + bse.mIndex + "): " + bse);
2108                    LogWriter logw = new LogWriter(TAG);
2109                    PrintWriter pw = new PrintWriter(logw);
2110                    bse.dump("  ", pw, false);
2111                }
2112                mBackStack.add(bse);
2113                if (bse.mIndex >= 0) {
2114                    setBackStackIndex(bse.mIndex, bse);
2115                }
2116            }
2117        } else {
2118            mBackStack = null;
2119        }
2120    }
2121
2122    public void attachController(FragmentHostCallback host,
2123            FragmentContainer container, Fragment parent) {
2124        if (mHost != null) throw new IllegalStateException("Already attached");
2125        mHost = host;
2126        mContainer = container;
2127        mParent = parent;
2128    }
2129
2130    public void noteStateNotSaved() {
2131        mStateSaved = false;
2132    }
2133
2134    public void dispatchCreate() {
2135        mStateSaved = false;
2136        moveToState(Fragment.CREATED, false);
2137    }
2138
2139    public void dispatchActivityCreated() {
2140        mStateSaved = false;
2141        moveToState(Fragment.ACTIVITY_CREATED, false);
2142    }
2143
2144    public void dispatchStart() {
2145        mStateSaved = false;
2146        moveToState(Fragment.STARTED, false);
2147    }
2148
2149    public void dispatchResume() {
2150        mStateSaved = false;
2151        moveToState(Fragment.RESUMED, false);
2152    }
2153
2154    public void dispatchPause() {
2155        moveToState(Fragment.STARTED, false);
2156    }
2157
2158    public void dispatchStop() {
2159        // See saveAllState() for the explanation of this.  We do this for
2160        // all platform versions, to keep our behavior more consistent between
2161        // them.
2162        mStateSaved = true;
2163
2164        moveToState(Fragment.STOPPED, false);
2165    }
2166
2167    public void dispatchReallyStop() {
2168        moveToState(Fragment.ACTIVITY_CREATED, false);
2169    }
2170
2171    public void dispatchDestroyView() {
2172        moveToState(Fragment.CREATED, false);
2173    }
2174
2175    public void dispatchDestroy() {
2176        mDestroyed = true;
2177        execPendingActions();
2178        moveToState(Fragment.INITIALIZING, false);
2179        mHost = null;
2180        mContainer = null;
2181        mParent = null;
2182    }
2183
2184    public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) {
2185        if (mAdded == null) {
2186            return;
2187        }
2188        for (int i = mAdded.size() - 1; i >= 0; --i) {
2189            final android.support.v4.app.Fragment f = mAdded.get(i);
2190            if (f != null) {
2191                f.performMultiWindowModeChanged(isInMultiWindowMode);
2192            }
2193        }
2194    }
2195
2196    public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
2197        if (mAdded == null) {
2198            return;
2199        }
2200        for (int i = mAdded.size() - 1; i >= 0; --i) {
2201            final android.support.v4.app.Fragment f = mAdded.get(i);
2202            if (f != null) {
2203                f.performPictureInPictureModeChanged(isInPictureInPictureMode);
2204            }
2205        }
2206    }
2207
2208    public void dispatchConfigurationChanged(Configuration newConfig) {
2209        if (mAdded != null) {
2210            for (int i=0; i<mAdded.size(); i++) {
2211                Fragment f = mAdded.get(i);
2212                if (f != null) {
2213                    f.performConfigurationChanged(newConfig);
2214                }
2215            }
2216        }
2217    }
2218
2219    public void dispatchLowMemory() {
2220        if (mAdded != null) {
2221            for (int i=0; i<mAdded.size(); i++) {
2222                Fragment f = mAdded.get(i);
2223                if (f != null) {
2224                    f.performLowMemory();
2225                }
2226            }
2227        }
2228    }
2229
2230    public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2231        boolean show = false;
2232        ArrayList<Fragment> newMenus = null;
2233        if (mAdded != null) {
2234            for (int i=0; i<mAdded.size(); i++) {
2235                Fragment f = mAdded.get(i);
2236                if (f != null) {
2237                    if (f.performCreateOptionsMenu(menu, inflater)) {
2238                        show = true;
2239                        if (newMenus == null) {
2240                            newMenus = new ArrayList<Fragment>();
2241                        }
2242                        newMenus.add(f);
2243                    }
2244                }
2245            }
2246        }
2247
2248        if (mCreatedMenus != null) {
2249            for (int i=0; i<mCreatedMenus.size(); i++) {
2250                Fragment f = mCreatedMenus.get(i);
2251                if (newMenus == null || !newMenus.contains(f)) {
2252                    f.onDestroyOptionsMenu();
2253                }
2254            }
2255        }
2256
2257        mCreatedMenus = newMenus;
2258
2259        return show;
2260    }
2261
2262    public boolean dispatchPrepareOptionsMenu(Menu menu) {
2263        boolean show = false;
2264        if (mAdded != null) {
2265            for (int i=0; i<mAdded.size(); i++) {
2266                Fragment f = mAdded.get(i);
2267                if (f != null) {
2268                    if (f.performPrepareOptionsMenu(menu)) {
2269                        show = true;
2270                    }
2271                }
2272            }
2273        }
2274        return show;
2275    }
2276
2277    public boolean dispatchOptionsItemSelected(MenuItem item) {
2278        if (mAdded != null) {
2279            for (int i=0; i<mAdded.size(); i++) {
2280                Fragment f = mAdded.get(i);
2281                if (f != null) {
2282                    if (f.performOptionsItemSelected(item)) {
2283                        return true;
2284                    }
2285                }
2286            }
2287        }
2288        return false;
2289    }
2290
2291    public boolean dispatchContextItemSelected(MenuItem item) {
2292        if (mAdded != null) {
2293            for (int i=0; i<mAdded.size(); i++) {
2294                Fragment f = mAdded.get(i);
2295                if (f != null) {
2296                    if (f.performContextItemSelected(item)) {
2297                        return true;
2298                    }
2299                }
2300            }
2301        }
2302        return false;
2303    }
2304
2305    public void dispatchOptionsMenuClosed(Menu menu) {
2306        if (mAdded != null) {
2307            for (int i=0; i<mAdded.size(); i++) {
2308                Fragment f = mAdded.get(i);
2309                if (f != null) {
2310                    f.performOptionsMenuClosed(menu);
2311                }
2312            }
2313        }
2314    }
2315
2316    public static int reverseTransit(int transit) {
2317        int rev = 0;
2318        switch (transit) {
2319            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2320                rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
2321                break;
2322            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2323                rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
2324                break;
2325            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2326                rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
2327                break;
2328        }
2329        return rev;
2330
2331    }
2332
2333    public static final int ANIM_STYLE_OPEN_ENTER = 1;
2334    public static final int ANIM_STYLE_OPEN_EXIT = 2;
2335    public static final int ANIM_STYLE_CLOSE_ENTER = 3;
2336    public static final int ANIM_STYLE_CLOSE_EXIT = 4;
2337    public static final int ANIM_STYLE_FADE_ENTER = 5;
2338    public static final int ANIM_STYLE_FADE_EXIT = 6;
2339
2340    public static int transitToStyleIndex(int transit, boolean enter) {
2341        int animAttr = -1;
2342        switch (transit) {
2343            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2344                animAttr = enter ? ANIM_STYLE_OPEN_ENTER : ANIM_STYLE_OPEN_EXIT;
2345                break;
2346            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2347                animAttr = enter ? ANIM_STYLE_CLOSE_ENTER : ANIM_STYLE_CLOSE_EXIT;
2348                break;
2349            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2350                animAttr = enter ? ANIM_STYLE_FADE_ENTER : ANIM_STYLE_FADE_EXIT;
2351                break;
2352        }
2353        return animAttr;
2354    }
2355
2356    @Override
2357    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
2358        if (!"fragment".equals(name)) {
2359            return null;
2360        }
2361
2362        String fname = attrs.getAttributeValue(null, "class");
2363        TypedArray a =  context.obtainStyledAttributes(attrs, FragmentTag.Fragment);
2364        if (fname == null) {
2365            fname = a.getString(FragmentTag.Fragment_name);
2366        }
2367        int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID);
2368        String tag = a.getString(FragmentTag.Fragment_tag);
2369        a.recycle();
2370
2371        if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) {
2372            // Invalid support lib fragment; let the device's framework handle it.
2373            // This will allow android.app.Fragments to do the right thing.
2374            return null;
2375        }
2376
2377        int containerId = parent != null ? parent.getId() : 0;
2378        if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
2379            throw new IllegalArgumentException(attrs.getPositionDescription()
2380                    + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
2381        }
2382
2383        // If we restored from a previous state, we may already have
2384        // instantiated this fragment from the state and should use
2385        // that instance instead of making a new one.
2386        Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
2387        if (fragment == null && tag != null) {
2388            fragment = findFragmentByTag(tag);
2389        }
2390        if (fragment == null && containerId != View.NO_ID) {
2391            fragment = findFragmentById(containerId);
2392        }
2393
2394        if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
2395                + Integer.toHexString(id) + " fname=" + fname
2396                + " existing=" + fragment);
2397        if (fragment == null) {
2398            fragment = Fragment.instantiate(context, fname);
2399            fragment.mFromLayout = true;
2400            fragment.mFragmentId = id != 0 ? id : containerId;
2401            fragment.mContainerId = containerId;
2402            fragment.mTag = tag;
2403            fragment.mInLayout = true;
2404            fragment.mFragmentManager = this;
2405            fragment.mHost = mHost;
2406            fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
2407            addFragment(fragment, true);
2408
2409        } else if (fragment.mInLayout) {
2410            // A fragment already exists and it is not one we restored from
2411            // previous state.
2412            throw new IllegalArgumentException(attrs.getPositionDescription()
2413                    + ": Duplicate id 0x" + Integer.toHexString(id)
2414                    + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
2415                    + " with another fragment for " + fname);
2416        } else {
2417            // This fragment was retained from a previous instance; get it
2418            // going now.
2419            fragment.mInLayout = true;
2420            fragment.mHost = mHost;
2421            // If this fragment is newly instantiated (either right now, or
2422            // from last saved state), then give it the attributes to
2423            // initialize itself.
2424            if (!fragment.mRetaining) {
2425                fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
2426            }
2427        }
2428
2429        // If we haven't finished entering the CREATED state ourselves yet,
2430        // push the inflated child fragment along.
2431        if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
2432            moveToState(fragment, Fragment.CREATED, 0, 0, false);
2433        } else {
2434            moveToState(fragment);
2435        }
2436
2437        if (fragment.mView == null) {
2438            throw new IllegalStateException("Fragment " + fname
2439                    + " did not create a view.");
2440        }
2441        if (id != 0) {
2442            fragment.mView.setId(id);
2443        }
2444        if (fragment.mView.getTag() == null) {
2445            fragment.mView.setTag(tag);
2446        }
2447        return fragment.mView;
2448    }
2449
2450    LayoutInflaterFactory getLayoutInflaterFactory() {
2451        return this;
2452    }
2453
2454    static class FragmentTag {
2455        public static final int[] Fragment = {
2456                0x01010003, 0x010100d0, 0x010100d1
2457        };
2458        public static final int Fragment_id = 1;
2459        public static final int Fragment_name = 0;
2460        public static final int Fragment_tag = 2;
2461    }
2462}
2463