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