1/*
2 * Copyright (C) 2010 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.app;
18
19import android.animation.Animator;
20import android.animation.AnimatorInflater;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.AnimatorSet;
23import android.animation.PropertyValuesHolder;
24import android.animation.ValueAnimator;
25import android.content.Context;
26import android.content.pm.ApplicationInfo;
27import android.content.res.Configuration;
28import android.content.res.Resources.NotFoundException;
29import android.content.res.TypedArray;
30import android.os.Build;
31import android.os.Bundle;
32import android.os.Debug;
33import android.os.Looper;
34import android.os.Parcel;
35import android.os.Parcelable;
36import android.util.ArraySet;
37import android.util.AttributeSet;
38import android.util.DebugUtils;
39import android.util.Log;
40import android.util.LogWriter;
41import android.util.Pair;
42import android.util.SparseArray;
43import android.util.SuperNotCalledException;
44import android.view.LayoutInflater;
45import android.view.Menu;
46import android.view.MenuInflater;
47import android.view.MenuItem;
48import android.view.View;
49import android.view.ViewGroup;
50
51import com.android.internal.util.FastPrintWriter;
52
53import java.io.FileDescriptor;
54import java.io.PrintWriter;
55import java.util.ArrayList;
56import java.util.Arrays;
57import java.util.Collections;
58import java.util.List;
59import java.util.concurrent.CopyOnWriteArrayList;
60
61/**
62 * Interface for interacting with {@link Fragment} objects inside of an
63 * {@link Activity}
64 *
65 * <div class="special reference">
66 * <h3>Developer Guides</h3>
67 * <p>For more information about using fragments, read the
68 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
69 * </div>
70 *
71 * While the FragmentManager API was introduced in
72 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
73 * at is also available for use on older platforms through
74 * {@link android.support.v4.app.FragmentActivity}.  See the blog post
75 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
76 * Fragments For All</a> for more details.
77 *
78 * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">Support Library</a>
79 *      {@link android.support.v4.app.FragmentManager} for consistent behavior across all devices
80 *      and access to <a href="{@docRoot}topic/libraries/architecture/lifecycle.html">Lifecycle</a>.
81 */
82@Deprecated
83public abstract class FragmentManager {
84    /**
85     * Representation of an entry on the fragment back stack, as created
86     * with {@link FragmentTransaction#addToBackStack(String)
87     * FragmentTransaction.addToBackStack()}.  Entries can later be
88     * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
89     * FragmentManager.getBackStackEntryAt()}.
90     *
91     * <p>Note that you should never hold on to a BackStackEntry object;
92     * the identifier as returned by {@link #getId} is the only thing that
93     * will be persisted across activity instances.
94     *
95     * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">
96     *      Support Library</a> {@link android.support.v4.app.FragmentManager.BackStackEntry}
97     */
98    @Deprecated
99    public interface BackStackEntry {
100        /**
101         * Return the unique identifier for the entry.  This is the only
102         * representation of the entry that will persist across activity
103         * instances.
104         */
105        public int getId();
106
107        /**
108         * Get the name that was supplied to
109         * {@link FragmentTransaction#addToBackStack(String)
110         * FragmentTransaction.addToBackStack(String)} when creating this entry.
111         */
112        public String getName();
113
114        /**
115         * Return the full bread crumb title resource identifier for the entry,
116         * or 0 if it does not have one.
117         */
118        public int getBreadCrumbTitleRes();
119
120        /**
121         * Return the short bread crumb title resource identifier for the entry,
122         * or 0 if it does not have one.
123         */
124        public int getBreadCrumbShortTitleRes();
125
126        /**
127         * Return the full bread crumb title for the entry, or null if it
128         * does not have one.
129         */
130        public CharSequence getBreadCrumbTitle();
131
132        /**
133         * Return the short bread crumb title for the entry, or null if it
134         * does not have one.
135         */
136        public CharSequence getBreadCrumbShortTitle();
137    }
138
139    /**
140     * Interface to watch for changes to the back stack.
141     *
142     * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">
143     *      Support Library</a>
144     *      {@link android.support.v4.app.FragmentManager.OnBackStackChangedListener}
145     */
146    @Deprecated
147    public interface OnBackStackChangedListener {
148        /**
149         * Called whenever the contents of the back stack change.
150         */
151        public void onBackStackChanged();
152    }
153
154    /**
155     * Start a series of edit operations on the Fragments associated with
156     * this FragmentManager.
157     *
158     * <p>Note: A fragment transaction can only be created/committed prior
159     * to an activity saving its state.  If you try to commit a transaction
160     * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()}
161     * (and prior to a following {@link Activity#onStart Activity.onStart}
162     * or {@link Activity#onResume Activity.onResume()}, you will get an error.
163     * This is because the framework takes care of saving your current fragments
164     * in the state, and if changes are made after the state is saved then they
165     * will be lost.</p>
166     */
167    public abstract FragmentTransaction beginTransaction();
168
169    /** @hide -- remove once prebuilts are in. */
170    @Deprecated
171    public FragmentTransaction openTransaction() {
172        return beginTransaction();
173    }
174
175    /**
176     * After a {@link FragmentTransaction} is committed with
177     * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
178     * is scheduled to be executed asynchronously on the process's main thread.
179     * If you want to immediately executing any such pending operations, you
180     * can call this function (only from the main thread) to do so.  Note that
181     * all callbacks and other related behavior will be done from within this
182     * call, so be careful about where this is called from.
183     * <p>
184     * This also forces the start of any postponed Transactions where
185     * {@link Fragment#postponeEnterTransition()} has been called.
186     *
187     * @return Returns true if there were any pending transactions to be
188     * executed.
189     */
190    public abstract boolean executePendingTransactions();
191
192    /**
193     * Finds a fragment that was identified by the given id either when inflated
194     * from XML or as the container ID when added in a transaction.  This first
195     * searches through fragments that are currently added to the manager's
196     * activity; if no such fragment is found, then all fragments currently
197     * on the back stack associated with this ID are searched.
198     * @return The fragment if found or null otherwise.
199     */
200    public abstract Fragment findFragmentById(int id);
201
202    /**
203     * Finds a fragment that was identified by the given tag either when inflated
204     * from XML or as supplied when added in a transaction.  This first
205     * searches through fragments that are currently added to the manager's
206     * activity; if no such fragment is found, then all fragments currently
207     * on the back stack are searched.
208     * @return The fragment if found or null otherwise.
209     */
210    public abstract Fragment findFragmentByTag(String tag);
211
212    /**
213     * Flag for {@link #popBackStack(String, int)}
214     * and {@link #popBackStack(int, int)}: If set, and the name or ID of
215     * a back stack entry has been supplied, then all matching entries will
216     * be consumed until one that doesn't match is found or the bottom of
217     * the stack is reached.  Otherwise, all entries up to but not including that entry
218     * will be removed.
219     */
220    public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
221
222    /**
223     * Pop the top state off the back stack.  This function is asynchronous -- it
224     * enqueues the request to pop, but the action will not be performed until the
225     * application returns to its event loop.
226     */
227    public abstract void popBackStack();
228
229    /**
230     * Like {@link #popBackStack()}, but performs the operation immediately
231     * inside of the call.  This is like calling {@link #executePendingTransactions()}
232     * afterwards without forcing the start of postponed Transactions.
233     * @return Returns true if there was something popped, else false.
234     */
235    public abstract boolean popBackStackImmediate();
236
237    /**
238     * Pop the last fragment transition from the manager's fragment
239     * back stack.  If there is nothing to pop, false is returned.
240     * This function is asynchronous -- it enqueues the
241     * request to pop, but the action will not be performed until the application
242     * returns to its event loop.
243     *
244     * @param name If non-null, this is the name of a previous back state
245     * to look for; if found, all states up to that state will be popped.  The
246     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
247     * the named state itself is popped. If null, only the top state is popped.
248     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
249     */
250    public abstract void popBackStack(String name, int flags);
251
252    /**
253     * Like {@link #popBackStack(String, int)}, but performs the operation immediately
254     * inside of the call.  This is like calling {@link #executePendingTransactions()}
255     * afterwards without forcing the start of postponed Transactions.
256     * @return Returns true if there was something popped, else false.
257     */
258    public abstract boolean popBackStackImmediate(String name, int flags);
259
260    /**
261     * Pop all back stack states up to the one with the given identifier.
262     * This function is asynchronous -- it enqueues the
263     * request to pop, but the action will not be performed until the application
264     * returns to its event loop.
265     *
266     * @param id Identifier of the stated to be popped. If no identifier exists,
267     * false is returned.
268     * The identifier is the number returned by
269     * {@link FragmentTransaction#commit() FragmentTransaction.commit()}.  The
270     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
271     * the named state itself is popped.
272     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
273     */
274    public abstract void popBackStack(int id, int flags);
275
276    /**
277     * Like {@link #popBackStack(int, int)}, but performs the operation immediately
278     * inside of the call.  This is like calling {@link #executePendingTransactions()}
279     * afterwards without forcing the start of postponed Transactions.
280     * @return Returns true if there was something popped, else false.
281     */
282    public abstract boolean popBackStackImmediate(int id, int flags);
283
284    /**
285     * Return the number of entries currently in the back stack.
286     */
287    public abstract int getBackStackEntryCount();
288
289    /**
290     * Return the BackStackEntry at index <var>index</var> in the back stack;
291     * where the item on the bottom of the stack has index 0.
292     */
293    public abstract BackStackEntry getBackStackEntryAt(int index);
294
295    /**
296     * Add a new listener for changes to the fragment back stack.
297     */
298    public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
299
300    /**
301     * Remove a listener that was previously added with
302     * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
303     */
304    public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
305
306    /**
307     * Put a reference to a fragment in a Bundle.  This Bundle can be
308     * persisted as saved state, and when later restoring
309     * {@link #getFragment(Bundle, String)} will return the current
310     * instance of the same fragment.
311     *
312     * @param bundle The bundle in which to put the fragment reference.
313     * @param key The name of the entry in the bundle.
314     * @param fragment The Fragment whose reference is to be stored.
315     */
316    public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
317
318    /**
319     * Retrieve the current Fragment instance for a reference previously
320     * placed with {@link #putFragment(Bundle, String, Fragment)}.
321     *
322     * @param bundle The bundle from which to retrieve the fragment reference.
323     * @param key The name of the entry in the bundle.
324     * @return Returns the current Fragment instance that is associated with
325     * the given reference.
326     */
327    public abstract Fragment getFragment(Bundle bundle, String key);
328
329    /**
330     * Get a list of all fragments that are currently added to the FragmentManager.
331     * This may include those that are hidden as well as those that are shown.
332     * This will not include any fragments only in the back stack, or fragments that
333     * are detached or removed.
334     * <p>
335     * The order of the fragments in the list is the order in which they were
336     * added or attached.
337     *
338     * @return A list of all fragments that are added to the FragmentManager.
339     */
340    public abstract List<Fragment> getFragments();
341
342    /**
343     * Save the current instance state of the given Fragment.  This can be
344     * used later when creating a new instance of the Fragment and adding
345     * it to the fragment manager, to have it create itself to match the
346     * current state returned here.  Note that there are limits on how
347     * this can be used:
348     *
349     * <ul>
350     * <li>The Fragment must currently be attached to the FragmentManager.
351     * <li>A new Fragment created using this saved state must be the same class
352     * type as the Fragment it was created from.
353     * <li>The saved state can not contain dependencies on other fragments --
354     * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
355     * store a fragment reference because that reference may not be valid when
356     * this saved state is later used.  Likewise the Fragment's target and
357     * result code are not included in this state.
358     * </ul>
359     *
360     * @param f The Fragment whose state is to be saved.
361     * @return The generated state.  This will be null if there was no
362     * interesting state created by the fragment.
363     */
364    public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
365
366    /**
367     * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()}
368     * call has been made on the FragmentManager's Activity, so this instance is now dead.
369     */
370    public abstract boolean isDestroyed();
371
372    /**
373     * Registers a {@link FragmentLifecycleCallbacks} to listen to fragment lifecycle events
374     * happening in this FragmentManager. All registered callbacks will be automatically
375     * unregistered when this FragmentManager is destroyed.
376     *
377     * @param cb Callbacks to register
378     * @param recursive true to automatically register this callback for all child FragmentManagers
379     */
380    public abstract void registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb,
381            boolean recursive);
382
383    /**
384     * Unregisters a previously registered {@link FragmentLifecycleCallbacks}. If the callback
385     * was not previously registered this call has no effect. All registered callbacks will be
386     * automatically unregistered when this FragmentManager is destroyed.
387     *
388     * @param cb Callbacks to unregister
389     */
390    public abstract void unregisterFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb);
391
392    /**
393     * Return the currently active primary navigation fragment for this FragmentManager.
394     *
395     * <p>The primary navigation fragment's
396     * {@link Fragment#getChildFragmentManager() child FragmentManager} will be called first
397     * to process delegated navigation actions such as {@link #popBackStack()} if no ID
398     * or transaction name is provided to pop to.</p>
399     *
400     * @return the fragment designated as the primary navigation fragment
401     */
402    public abstract Fragment getPrimaryNavigationFragment();
403
404    /**
405     * Print the FragmentManager's state into the given stream.
406     *
407     * @param prefix Text to print at the front of each line.
408     * @param fd The raw file descriptor that the dump is being sent to.
409     * @param writer A PrintWriter to which the dump is to be set.
410     * @param args Additional arguments to the dump request.
411     */
412    public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
413
414    /**
415     * Control whether the framework's internal fragment manager debugging
416     * logs are turned on.  If enabled, you will see output in logcat as
417     * the framework performs fragment operations.
418     */
419    public static void enableDebugLogging(boolean enabled) {
420        FragmentManagerImpl.DEBUG = enabled;
421    }
422
423    /**
424     * Invalidate the attached activity's options menu as necessary.
425     * This may end up being deferred until we move to the resumed state.
426     */
427    public void invalidateOptionsMenu() { }
428
429    /**
430     * Returns {@code true} if the FragmentManager's state has already been saved
431     * by its host. Any operations that would change saved state should not be performed
432     * if this method returns true. For example, any popBackStack() method, such as
433     * {@link #popBackStackImmediate()} or any FragmentTransaction using
434     * {@link FragmentTransaction#commit()} instead of
435     * {@link FragmentTransaction#commitAllowingStateLoss()} will change
436     * the state and will result in an error.
437     *
438     * @return true if this FragmentManager's state has already been saved by its host
439     */
440    public abstract boolean isStateSaved();
441
442    /**
443     * Callback interface for listening to fragment state changes that happen
444     * within a given FragmentManager.
445     *
446     * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">
447     *      Support Library</a>
448     *      {@link android.support.v4.app.FragmentManager.FragmentLifecycleCallbacks}
449     */
450    @Deprecated
451    public abstract static class FragmentLifecycleCallbacks {
452        /**
453         * Called right before the fragment's {@link Fragment#onAttach(Context)} method is called.
454         * This is a good time to inject any required dependencies for the fragment before any of
455         * the fragment's lifecycle methods are invoked.
456         *
457         * @param fm Host FragmentManager
458         * @param f Fragment changing state
459         * @param context Context that the Fragment is being attached to
460         */
461        public void onFragmentPreAttached(FragmentManager fm, Fragment f, Context context) {}
462
463        /**
464         * Called after the fragment has been attached to its host. Its host will have had
465         * <code>onAttachFragment</code> called before this call happens.
466         *
467         * @param fm Host FragmentManager
468         * @param f Fragment changing state
469         * @param context Context that the Fragment was attached to
470         */
471        public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {}
472
473        /**
474         * Called right before the fragment's {@link Fragment#onCreate(Bundle)} method is called.
475         * This is a good time to inject any required dependencies or perform other configuration
476         * for the fragment.
477         *
478         * @param fm Host FragmentManager
479         * @param f Fragment changing state
480         * @param savedInstanceState Saved instance bundle from a previous instance
481         */
482        public void onFragmentPreCreated(FragmentManager fm, Fragment f,
483                Bundle savedInstanceState) {}
484
485        /**
486         * Called after the fragment has returned from the FragmentManager's call to
487         * {@link Fragment#onCreate(Bundle)}. This will only happen once for any given
488         * fragment instance, though the fragment may be attached and detached multiple times.
489         *
490         * @param fm Host FragmentManager
491         * @param f Fragment changing state
492         * @param savedInstanceState Saved instance bundle from a previous instance
493         */
494        public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {}
495
496        /**
497         * Called after the fragment has returned from the FragmentManager's call to
498         * {@link Fragment#onActivityCreated(Bundle)}. This will only happen once for any given
499         * fragment instance, though the fragment may be attached and detached multiple times.
500         *
501         * @param fm Host FragmentManager
502         * @param f Fragment changing state
503         * @param savedInstanceState Saved instance bundle from a previous instance
504         */
505        public void onFragmentActivityCreated(FragmentManager fm, Fragment f,
506                Bundle savedInstanceState) {}
507
508        /**
509         * Called after the fragment has returned a non-null view from the FragmentManager's
510         * request to {@link Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)}.
511         *
512         * @param fm Host FragmentManager
513         * @param f Fragment that created and owns the view
514         * @param v View returned by the fragment
515         * @param savedInstanceState Saved instance bundle from a previous instance
516         */
517        public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v,
518                Bundle savedInstanceState) {}
519
520        /**
521         * Called after the fragment has returned from the FragmentManager's call to
522         * {@link Fragment#onStart()}.
523         *
524         * @param fm Host FragmentManager
525         * @param f Fragment changing state
526         */
527        public void onFragmentStarted(FragmentManager fm, Fragment f) {}
528
529        /**
530         * Called after the fragment has returned from the FragmentManager's call to
531         * {@link Fragment#onResume()}.
532         *
533         * @param fm Host FragmentManager
534         * @param f Fragment changing state
535         */
536        public void onFragmentResumed(FragmentManager fm, Fragment f) {}
537
538        /**
539         * Called after the fragment has returned from the FragmentManager's call to
540         * {@link Fragment#onPause()}.
541         *
542         * @param fm Host FragmentManager
543         * @param f Fragment changing state
544         */
545        public void onFragmentPaused(FragmentManager fm, Fragment f) {}
546
547        /**
548         * Called after the fragment has returned from the FragmentManager's call to
549         * {@link Fragment#onStop()}.
550         *
551         * @param fm Host FragmentManager
552         * @param f Fragment changing state
553         */
554        public void onFragmentStopped(FragmentManager fm, Fragment f) {}
555
556        /**
557         * Called after the fragment has returned from the FragmentManager's call to
558         * {@link Fragment#onSaveInstanceState(Bundle)}.
559         *
560         * @param fm Host FragmentManager
561         * @param f Fragment changing state
562         * @param outState Saved state bundle for the fragment
563         */
564        public void onFragmentSaveInstanceState(FragmentManager fm, Fragment f, Bundle outState) {}
565
566        /**
567         * Called after the fragment has returned from the FragmentManager's call to
568         * {@link Fragment#onDestroyView()}.
569         *
570         * @param fm Host FragmentManager
571         * @param f Fragment changing state
572         */
573        public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {}
574
575        /**
576         * Called after the fragment has returned from the FragmentManager's call to
577         * {@link Fragment#onDestroy()}.
578         *
579         * @param fm Host FragmentManager
580         * @param f Fragment changing state
581         */
582        public void onFragmentDestroyed(FragmentManager fm, Fragment f) {}
583
584        /**
585         * Called after the fragment has returned from the FragmentManager's call to
586         * {@link Fragment#onDetach()}.
587         *
588         * @param fm Host FragmentManager
589         * @param f Fragment changing state
590         */
591        public void onFragmentDetached(FragmentManager fm, Fragment f) {}
592    }
593}
594
595final class FragmentManagerState implements Parcelable {
596    FragmentState[] mActive;
597    int[] mAdded;
598    BackStackState[] mBackStack;
599    int mPrimaryNavActiveIndex = -1;
600    int mNextFragmentIndex;
601
602    public FragmentManagerState() {
603    }
604
605    public FragmentManagerState(Parcel in) {
606        mActive = in.createTypedArray(FragmentState.CREATOR);
607        mAdded = in.createIntArray();
608        mBackStack = in.createTypedArray(BackStackState.CREATOR);
609        mPrimaryNavActiveIndex = in.readInt();
610        mNextFragmentIndex = in.readInt();
611    }
612
613    public int describeContents() {
614        return 0;
615    }
616
617    public void writeToParcel(Parcel dest, int flags) {
618        dest.writeTypedArray(mActive, flags);
619        dest.writeIntArray(mAdded);
620        dest.writeTypedArray(mBackStack, flags);
621        dest.writeInt(mPrimaryNavActiveIndex);
622        dest.writeInt(mNextFragmentIndex);
623    }
624
625    public static final Parcelable.Creator<FragmentManagerState> CREATOR
626            = new Parcelable.Creator<FragmentManagerState>() {
627        public FragmentManagerState createFromParcel(Parcel in) {
628            return new FragmentManagerState(in);
629        }
630
631        public FragmentManagerState[] newArray(int size) {
632            return new FragmentManagerState[size];
633        }
634    };
635}
636
637/**
638 * Container for fragments associated with an activity.
639 */
640final class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 {
641    static boolean DEBUG = false;
642    static final String TAG = "FragmentManager";
643
644    static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
645    static final String TARGET_STATE_TAG = "android:target_state";
646    static final String VIEW_STATE_TAG = "android:view_state";
647    static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
648
649    static class AnimateOnHWLayerIfNeededListener implements Animator.AnimatorListener {
650        private boolean mShouldRunOnHWLayer = false;
651        private View mView;
652        public AnimateOnHWLayerIfNeededListener(final View v) {
653            if (v == null) {
654                return;
655            }
656            mView = v;
657        }
658
659        @Override
660        public void onAnimationStart(Animator animation) {
661            mShouldRunOnHWLayer = shouldRunOnHWLayer(mView, animation);
662            if (mShouldRunOnHWLayer) {
663                mView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
664            }
665        }
666
667        @Override
668        public void onAnimationEnd(Animator animation) {
669            if (mShouldRunOnHWLayer) {
670                mView.setLayerType(View.LAYER_TYPE_NONE, null);
671            }
672            mView = null;
673            animation.removeListener(this);
674        }
675
676        @Override
677        public void onAnimationCancel(Animator animation) {
678
679        }
680
681        @Override
682        public void onAnimationRepeat(Animator animation) {
683
684        }
685    }
686
687    ArrayList<OpGenerator> mPendingActions;
688    boolean mExecutingActions;
689
690    int mNextFragmentIndex = 0;
691    SparseArray<Fragment> mActive;
692    final ArrayList<Fragment> mAdded = new ArrayList<>();
693    ArrayList<BackStackRecord> mBackStack;
694    ArrayList<Fragment> mCreatedMenus;
695
696    // Must be accessed while locked.
697    ArrayList<BackStackRecord> mBackStackIndices;
698    ArrayList<Integer> mAvailBackStackIndices;
699
700    ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
701    final CopyOnWriteArrayList<Pair<FragmentLifecycleCallbacks, Boolean>>
702            mLifecycleCallbacks = new CopyOnWriteArrayList<>();
703
704    int mCurState = Fragment.INITIALIZING;
705    FragmentHostCallback<?> mHost;
706    FragmentContainer mContainer;
707    Fragment mParent;
708    Fragment mPrimaryNav;
709
710    boolean mNeedMenuInvalidate;
711    boolean mStateSaved;
712    boolean mDestroyed;
713    String mNoTransactionsBecause;
714    boolean mHavePendingDeferredStart;
715
716    // Temporary vars for removing redundant operations in BackStackRecords:
717    ArrayList<BackStackRecord> mTmpRecords;
718    ArrayList<Boolean> mTmpIsPop;
719    ArrayList<Fragment> mTmpAddedFragments;
720
721    // Temporary vars for state save and restore.
722    Bundle mStateBundle = null;
723    SparseArray<Parcelable> mStateArray = null;
724
725    // Postponed transactions.
726    ArrayList<StartEnterTransitionListener> mPostponedTransactions;
727
728    // Prior to O, we allowed executing transactions during fragment manager state changes.
729    // This is dangerous, but we want to keep from breaking old applications.
730    boolean mAllowOldReentrantBehavior;
731
732    // Saved FragmentManagerNonConfig during saveAllState() and cleared in noteStateNotSaved()
733    FragmentManagerNonConfig mSavedNonConfig;
734
735    Runnable mExecCommit = new Runnable() {
736        @Override
737        public void run() {
738            execPendingActions();
739        }
740    };
741
742    private void throwException(RuntimeException ex) {
743        Log.e(TAG, ex.getMessage());
744        LogWriter logw = new LogWriter(Log.ERROR, TAG);
745        PrintWriter pw = new FastPrintWriter(logw, false, 1024);
746        if (mHost != null) {
747            Log.e(TAG, "Activity state:");
748            try {
749                mHost.onDump("  ", null, pw, new String[] { });
750            } catch (Exception e) {
751                pw.flush();
752                Log.e(TAG, "Failed dumping state", e);
753            }
754        } else {
755            Log.e(TAG, "Fragment manager state:");
756            try {
757                dump("  ", null, pw, new String[] { });
758            } catch (Exception e) {
759                pw.flush();
760                Log.e(TAG, "Failed dumping state", e);
761            }
762        }
763        pw.flush();
764        throw ex;
765    }
766
767    static boolean modifiesAlpha(Animator anim) {
768        if (anim == null) {
769            return false;
770        }
771        if (anim instanceof ValueAnimator) {
772            ValueAnimator valueAnim = (ValueAnimator) anim;
773            PropertyValuesHolder[] values = valueAnim.getValues();
774            for (int i = 0; i < values.length; i++) {
775                if (("alpha").equals(values[i].getPropertyName())) {
776                    return true;
777                }
778            }
779        } else if (anim instanceof AnimatorSet) {
780            List<Animator> animList = ((AnimatorSet) anim).getChildAnimations();
781            for (int i = 0; i < animList.size(); i++) {
782                if (modifiesAlpha(animList.get(i))) {
783                    return true;
784                }
785            }
786        }
787        return false;
788    }
789
790    static boolean shouldRunOnHWLayer(View v, Animator anim) {
791        if (v == null || anim == null) {
792            return false;
793        }
794        return v.getLayerType() == View.LAYER_TYPE_NONE
795                && v.hasOverlappingRendering()
796                && modifiesAlpha(anim);
797    }
798
799    /**
800     * Sets the to be animated view on hardware layer during the animation.
801     */
802    private void setHWLayerAnimListenerIfAlpha(final View v, Animator anim) {
803        if (v == null || anim == null) {
804            return;
805        }
806        if (shouldRunOnHWLayer(v, anim)) {
807            anim.addListener(new AnimateOnHWLayerIfNeededListener(v));
808        }
809    }
810
811    @Override
812    public FragmentTransaction beginTransaction() {
813        return new BackStackRecord(this);
814    }
815
816    @Override
817    public boolean executePendingTransactions() {
818        boolean updates = execPendingActions();
819        forcePostponedTransactions();
820        return updates;
821    }
822
823    @Override
824    public void popBackStack() {
825        enqueueAction(new PopBackStackState(null, -1, 0), false);
826    }
827
828    @Override
829    public boolean popBackStackImmediate() {
830        checkStateLoss();
831        return popBackStackImmediate(null, -1, 0);
832    }
833
834    @Override
835    public void popBackStack(String name, int flags) {
836        enqueueAction(new PopBackStackState(name, -1, flags), false);
837    }
838
839    @Override
840    public boolean popBackStackImmediate(String name, int flags) {
841        checkStateLoss();
842        return popBackStackImmediate(name, -1, flags);
843    }
844
845    @Override
846    public void popBackStack(int id, int flags) {
847        if (id < 0) {
848            throw new IllegalArgumentException("Bad id: " + id);
849        }
850        enqueueAction(new PopBackStackState(null, id, flags), false);
851    }
852
853    @Override
854    public boolean popBackStackImmediate(int id, int flags) {
855        checkStateLoss();
856        if (id < 0) {
857            throw new IllegalArgumentException("Bad id: " + id);
858        }
859        return popBackStackImmediate(null, id, flags);
860    }
861
862    /**
863     * Used by all public popBackStackImmediate methods, this executes pending transactions and
864     * returns true if the pop action did anything, regardless of what other pending
865     * transactions did.
866     *
867     * @return true if the pop operation did anything or false otherwise.
868     */
869    private boolean popBackStackImmediate(String name, int id, int flags) {
870        execPendingActions();
871        ensureExecReady(true);
872
873        if (mPrimaryNav != null // We have a primary nav fragment
874                && id < 0 // No valid id (since they're local)
875                && name == null) { // no name to pop to (since they're local)
876            final FragmentManager childManager = mPrimaryNav.mChildFragmentManager;
877            if (childManager != null && childManager.popBackStackImmediate()) {
878                // We did something, just not to this specific FragmentManager. Return true.
879                return true;
880            }
881        }
882
883        boolean executePop = popBackStackState(mTmpRecords, mTmpIsPop, name, id, flags);
884        if (executePop) {
885            mExecutingActions = true;
886            try {
887                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
888            } finally {
889                cleanupExec();
890            }
891        }
892
893        doPendingDeferredStart();
894        burpActive();
895        return executePop;
896    }
897
898    @Override
899    public int getBackStackEntryCount() {
900        return mBackStack != null ? mBackStack.size() : 0;
901    }
902
903    @Override
904    public BackStackEntry getBackStackEntryAt(int index) {
905        return mBackStack.get(index);
906    }
907
908    @Override
909    public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
910        if (mBackStackChangeListeners == null) {
911            mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
912        }
913        mBackStackChangeListeners.add(listener);
914    }
915
916    @Override
917    public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
918        if (mBackStackChangeListeners != null) {
919            mBackStackChangeListeners.remove(listener);
920        }
921    }
922
923    @Override
924    public void putFragment(Bundle bundle, String key, Fragment fragment) {
925        if (fragment.mIndex < 0) {
926            throwException(new IllegalStateException("Fragment " + fragment
927                    + " is not currently in the FragmentManager"));
928        }
929        bundle.putInt(key, fragment.mIndex);
930    }
931
932    @Override
933    public Fragment getFragment(Bundle bundle, String key) {
934        int index = bundle.getInt(key, -1);
935        if (index == -1) {
936            return null;
937        }
938        Fragment f = mActive.get(index);
939        if (f == null) {
940            throwException(new IllegalStateException("Fragment no longer exists for key "
941                    + key + ": index " + index));
942        }
943        return f;
944    }
945
946    @Override
947    public List<Fragment> getFragments() {
948        if (mAdded.isEmpty()) {
949            return Collections.EMPTY_LIST;
950        }
951        synchronized (mAdded) {
952            return (List<Fragment>) mAdded.clone();
953        }
954    }
955
956    @Override
957    public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
958        if (fragment.mIndex < 0) {
959            throwException(new IllegalStateException("Fragment " + fragment
960                    + " is not currently in the FragmentManager"));
961        }
962        if (fragment.mState > Fragment.INITIALIZING) {
963            Bundle result = saveFragmentBasicState(fragment);
964            return result != null ? new Fragment.SavedState(result) : null;
965        }
966        return null;
967    }
968
969    @Override
970    public boolean isDestroyed() {
971        return mDestroyed;
972    }
973
974    @Override
975    public String toString() {
976        StringBuilder sb = new StringBuilder(128);
977        sb.append("FragmentManager{");
978        sb.append(Integer.toHexString(System.identityHashCode(this)));
979        sb.append(" in ");
980        if (mParent != null) {
981            DebugUtils.buildShortClassTag(mParent, sb);
982        } else {
983            DebugUtils.buildShortClassTag(mHost, sb);
984        }
985        sb.append("}}");
986        return sb.toString();
987    }
988
989    @Override
990    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
991        String innerPrefix = prefix + "    ";
992
993        int N;
994        if (mActive != null) {
995            N = mActive.size();
996            if (N > 0) {
997                writer.print(prefix); writer.print("Active Fragments in ");
998                        writer.print(Integer.toHexString(System.identityHashCode(this)));
999                        writer.println(":");
1000                for (int i=0; i<N; i++) {
1001                    Fragment f = mActive.valueAt(i);
1002                    writer.print(prefix); writer.print("  #"); writer.print(i);
1003                            writer.print(": "); writer.println(f);
1004                    if (f != null) {
1005                        f.dump(innerPrefix, fd, writer, args);
1006                    }
1007                }
1008            }
1009        }
1010
1011        N = mAdded.size();
1012        if (N > 0) {
1013            writer.print(prefix);
1014            writer.println("Added Fragments:");
1015            for (int i = 0; i < N; i++) {
1016                Fragment f = mAdded.get(i);
1017                writer.print(prefix);
1018                writer.print("  #");
1019                writer.print(i);
1020                writer.print(": ");
1021                writer.println(f.toString());
1022            }
1023        }
1024
1025        if (mCreatedMenus != null) {
1026            N = mCreatedMenus.size();
1027            if (N > 0) {
1028                writer.print(prefix); writer.println("Fragments Created Menus:");
1029                for (int i=0; i<N; i++) {
1030                    Fragment f = mCreatedMenus.get(i);
1031                    writer.print(prefix); writer.print("  #"); writer.print(i);
1032                            writer.print(": "); writer.println(f.toString());
1033                }
1034            }
1035        }
1036
1037        if (mBackStack != null) {
1038            N = mBackStack.size();
1039            if (N > 0) {
1040                writer.print(prefix); writer.println("Back Stack:");
1041                for (int i=0; i<N; i++) {
1042                    BackStackRecord bs = mBackStack.get(i);
1043                    writer.print(prefix); writer.print("  #"); writer.print(i);
1044                            writer.print(": "); writer.println(bs.toString());
1045                    bs.dump(innerPrefix, fd, writer, args);
1046                }
1047            }
1048        }
1049
1050        synchronized (this) {
1051            if (mBackStackIndices != null) {
1052                N = mBackStackIndices.size();
1053                if (N > 0) {
1054                    writer.print(prefix); writer.println("Back Stack Indices:");
1055                    for (int i=0; i<N; i++) {
1056                        BackStackRecord bs = mBackStackIndices.get(i);
1057                        writer.print(prefix); writer.print("  #"); writer.print(i);
1058                                writer.print(": "); writer.println(bs);
1059                    }
1060                }
1061            }
1062
1063            if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
1064                writer.print(prefix); writer.print("mAvailBackStackIndices: ");
1065                        writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
1066            }
1067        }
1068
1069        if (mPendingActions != null) {
1070            N = mPendingActions.size();
1071            if (N > 0) {
1072                writer.print(prefix); writer.println("Pending Actions:");
1073                for (int i=0; i<N; i++) {
1074                    OpGenerator r = mPendingActions.get(i);
1075                    writer.print(prefix); writer.print("  #"); writer.print(i);
1076                            writer.print(": "); writer.println(r);
1077                }
1078            }
1079        }
1080
1081        writer.print(prefix); writer.println("FragmentManager misc state:");
1082        writer.print(prefix); writer.print("  mHost="); writer.println(mHost);
1083        writer.print(prefix); writer.print("  mContainer="); writer.println(mContainer);
1084        if (mParent != null) {
1085            writer.print(prefix); writer.print("  mParent="); writer.println(mParent);
1086        }
1087        writer.print(prefix); writer.print("  mCurState="); writer.print(mCurState);
1088                writer.print(" mStateSaved="); writer.print(mStateSaved);
1089                writer.print(" mDestroyed="); writer.println(mDestroyed);
1090        if (mNeedMenuInvalidate) {
1091            writer.print(prefix); writer.print("  mNeedMenuInvalidate=");
1092                    writer.println(mNeedMenuInvalidate);
1093        }
1094        if (mNoTransactionsBecause != null) {
1095            writer.print(prefix); writer.print("  mNoTransactionsBecause=");
1096                    writer.println(mNoTransactionsBecause);
1097        }
1098    }
1099
1100    Animator loadAnimator(Fragment fragment, int transit, boolean enter,
1101            int transitionStyle) {
1102        Animator animObj = fragment.onCreateAnimator(transit, enter, fragment.getNextAnim());
1103        if (animObj != null) {
1104            return animObj;
1105        }
1106
1107        if (fragment.getNextAnim() != 0) {
1108            Animator anim = AnimatorInflater.loadAnimator(mHost.getContext(),
1109                    fragment.getNextAnim());
1110            if (anim != null) {
1111                return anim;
1112            }
1113        }
1114
1115        if (transit == 0) {
1116            return null;
1117        }
1118
1119        int styleIndex = transitToStyleIndex(transit, enter);
1120        if (styleIndex < 0) {
1121            return null;
1122        }
1123
1124        if (transitionStyle == 0 && mHost.onHasWindowAnimations()) {
1125            transitionStyle = mHost.onGetWindowAnimations();
1126        }
1127        if (transitionStyle == 0) {
1128            return null;
1129        }
1130
1131        TypedArray attrs = mHost.getContext().obtainStyledAttributes(transitionStyle,
1132                com.android.internal.R.styleable.FragmentAnimation);
1133        int anim = attrs.getResourceId(styleIndex, 0);
1134        attrs.recycle();
1135
1136        if (anim == 0) {
1137            return null;
1138        }
1139
1140        return AnimatorInflater.loadAnimator(mHost.getContext(), anim);
1141    }
1142
1143    public void performPendingDeferredStart(Fragment f) {
1144        if (f.mDeferStart) {
1145            if (mExecutingActions) {
1146                // Wait until we're done executing our pending transactions
1147                mHavePendingDeferredStart = true;
1148                return;
1149            }
1150            f.mDeferStart = false;
1151            moveToState(f, mCurState, 0, 0, false);
1152        }
1153    }
1154
1155    boolean isStateAtLeast(int state) {
1156        return mCurState >= state;
1157    }
1158
1159    @SuppressWarnings("ReferenceEquality")
1160    void moveToState(Fragment f, int newState, int transit, int transitionStyle,
1161            boolean keepActive) {
1162        if (DEBUG && false) Log.v(TAG, "moveToState: " + f
1163            + " oldState=" + f.mState + " newState=" + newState
1164            + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
1165
1166        // Fragments that are not currently added will sit in the onCreate() state.
1167        if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
1168            newState = Fragment.CREATED;
1169        }
1170        if (f.mRemoving && newState > f.mState) {
1171            if (f.mState == Fragment.INITIALIZING && f.isInBackStack()) {
1172                // Allow the fragment to be created so that it can be saved later.
1173                newState = Fragment.CREATED;
1174            } else {
1175                // While removing a fragment, we can't change it to a higher state.
1176                newState = f.mState;
1177            }
1178        }
1179        // Defer start if requested; don't allow it to move to STARTED or higher
1180        // if it's not already started.
1181        if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
1182            newState = Fragment.STOPPED;
1183        }
1184        if (f.mState <= newState) {
1185            // For fragments that are created from a layout, when restoring from
1186            // state we don't want to allow them to be created until they are
1187            // being reloaded from the layout.
1188            if (f.mFromLayout && !f.mInLayout) {
1189                return;
1190            }
1191            if (f.getAnimatingAway() != null) {
1192                // The fragment is currently being animated...  but!  Now we
1193                // want to move our state back up.  Give up on waiting for the
1194                // animation, move to whatever the final state should be once
1195                // the animation is done, and then we can proceed from there.
1196                f.setAnimatingAway(null);
1197                moveToState(f, f.getStateAfterAnimating(), 0, 0, true);
1198            }
1199            switch (f.mState) {
1200                case Fragment.INITIALIZING:
1201                    if (newState > Fragment.INITIALIZING) {
1202                        if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
1203                        if (f.mSavedFragmentState != null) {
1204                            f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
1205                                    FragmentManagerImpl.VIEW_STATE_TAG);
1206                            f.mTarget = getFragment(f.mSavedFragmentState,
1207                                    FragmentManagerImpl.TARGET_STATE_TAG);
1208                            if (f.mTarget != null) {
1209                                f.mTargetRequestCode = f.mSavedFragmentState.getInt(
1210                                        FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
1211                            }
1212                            f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
1213                                    FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
1214                            if (!f.mUserVisibleHint) {
1215                                f.mDeferStart = true;
1216                                if (newState > Fragment.STOPPED) {
1217                                    newState = Fragment.STOPPED;
1218                                }
1219                            }
1220                        }
1221
1222                        f.mHost = mHost;
1223                        f.mParentFragment = mParent;
1224                        f.mFragmentManager = mParent != null
1225                                ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl();
1226
1227                        // If we have a target fragment, push it along to at least CREATED
1228                        // so that this one can rely on it as an initialized dependency.
1229                        if (f.mTarget != null) {
1230                            if (mActive.get(f.mTarget.mIndex) != f.mTarget) {
1231                                throw new IllegalStateException("Fragment " + f
1232                                        + " declared target fragment " + f.mTarget
1233                                        + " that does not belong to this FragmentManager!");
1234                            }
1235                            if (f.mTarget.mState < Fragment.CREATED) {
1236                                moveToState(f.mTarget, Fragment.CREATED, 0, 0, true);
1237                            }
1238                        }
1239
1240                        dispatchOnFragmentPreAttached(f, mHost.getContext(), false);
1241                        f.mCalled = false;
1242                        f.onAttach(mHost.getContext());
1243                        if (!f.mCalled) {
1244                            throw new SuperNotCalledException("Fragment " + f
1245                                    + " did not call through to super.onAttach()");
1246                        }
1247                        if (f.mParentFragment == null) {
1248                            mHost.onAttachFragment(f);
1249                        } else {
1250                            f.mParentFragment.onAttachFragment(f);
1251                        }
1252                        dispatchOnFragmentAttached(f, mHost.getContext(), false);
1253
1254                        if (!f.mIsCreated) {
1255                            dispatchOnFragmentPreCreated(f, f.mSavedFragmentState, false);
1256                            f.performCreate(f.mSavedFragmentState);
1257                            dispatchOnFragmentCreated(f, f.mSavedFragmentState, false);
1258                        } else {
1259                            f.restoreChildFragmentState(f.mSavedFragmentState, true);
1260                            f.mState = Fragment.CREATED;
1261                        }
1262                        f.mRetaining = false;
1263                    }
1264                    // fall through
1265                case Fragment.CREATED:
1266                    // This is outside the if statement below on purpose; we want this to run
1267                    // even if we do a moveToState from CREATED => *, CREATED => CREATED, and
1268                    // * => CREATED as part of the case fallthrough above.
1269                    ensureInflatedFragmentView(f);
1270
1271                    if (newState > Fragment.CREATED) {
1272                        if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
1273                        if (!f.mFromLayout) {
1274                            ViewGroup container = null;
1275                            if (f.mContainerId != 0) {
1276                                if (f.mContainerId == View.NO_ID) {
1277                                    throwException(new IllegalArgumentException(
1278                                            "Cannot create fragment "
1279                                                    + f
1280                                                    + " for a container view with no id"));
1281                                }
1282                                container = mContainer.onFindViewById(f.mContainerId);
1283                                if (container == null && !f.mRestored) {
1284                                    String resName;
1285                                    try {
1286                                        resName = f.getResources().getResourceName(f.mContainerId);
1287                                    } catch (NotFoundException e) {
1288                                        resName = "unknown";
1289                                    }
1290                                    throwException(new IllegalArgumentException(
1291                                            "No view found for id 0x"
1292                                            + Integer.toHexString(f.mContainerId) + " ("
1293                                            + resName
1294                                            + ") for fragment " + f));
1295                                }
1296                            }
1297                            f.mContainer = container;
1298                            f.mView = f.performCreateView(f.performGetLayoutInflater(
1299                                    f.mSavedFragmentState), container, f.mSavedFragmentState);
1300                            if (f.mView != null) {
1301                                f.mView.setSaveFromParentEnabled(false);
1302                                if (container != null) {
1303                                    container.addView(f.mView);
1304                                }
1305                                if (f.mHidden) {
1306                                    f.mView.setVisibility(View.GONE);
1307                                }
1308                                f.onViewCreated(f.mView, f.mSavedFragmentState);
1309                                dispatchOnFragmentViewCreated(f, f.mView, f.mSavedFragmentState,
1310                                        false);
1311                                // Only animate the view if it is visible. This is done after
1312                                // dispatchOnFragmentViewCreated in case visibility is changed
1313                                f.mIsNewlyAdded = (f.mView.getVisibility() == View.VISIBLE)
1314                                        && f.mContainer != null;
1315                            }
1316                        }
1317
1318                        f.performActivityCreated(f.mSavedFragmentState);
1319                        dispatchOnFragmentActivityCreated(f, f.mSavedFragmentState, false);
1320                        if (f.mView != null) {
1321                            f.restoreViewState(f.mSavedFragmentState);
1322                        }
1323                        f.mSavedFragmentState = null;
1324                    }
1325                    // fall through
1326                case Fragment.ACTIVITY_CREATED:
1327                    if (newState > Fragment.ACTIVITY_CREATED) {
1328                        f.mState = Fragment.STOPPED;
1329                    }
1330                    // fall through
1331                case Fragment.STOPPED:
1332                    if (newState > Fragment.STOPPED) {
1333                        if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
1334                        f.performStart();
1335                        dispatchOnFragmentStarted(f, false);
1336                    }
1337                    // fall through
1338                case Fragment.STARTED:
1339                    if (newState > Fragment.STARTED) {
1340                        if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
1341                        f.performResume();
1342                        dispatchOnFragmentResumed(f, false);
1343                        // Get rid of this in case we saved it and never needed it.
1344                        f.mSavedFragmentState = null;
1345                        f.mSavedViewState = null;
1346                    }
1347            }
1348        } else if (f.mState > newState) {
1349            switch (f.mState) {
1350                case Fragment.RESUMED:
1351                    if (newState < Fragment.RESUMED) {
1352                        if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
1353                        f.performPause();
1354                        dispatchOnFragmentPaused(f, false);
1355                    }
1356                    // fall through
1357                case Fragment.STARTED:
1358                    if (newState < Fragment.STARTED) {
1359                        if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
1360                        f.performStop();
1361                        dispatchOnFragmentStopped(f, false);
1362                    }
1363                    // fall through
1364                case Fragment.STOPPED:
1365                case Fragment.ACTIVITY_CREATED:
1366                    if (newState < Fragment.ACTIVITY_CREATED) {
1367                        if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
1368                        if (f.mView != null) {
1369                            // Need to save the current view state if not
1370                            // done already.
1371                            if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) {
1372                                saveFragmentViewState(f);
1373                            }
1374                        }
1375                        f.performDestroyView();
1376                        dispatchOnFragmentViewDestroyed(f, false);
1377                        if (f.mView != null && f.mContainer != null) {
1378                            if (getTargetSdk() >= Build.VERSION_CODES.O) {
1379                                // Stop any current animations:
1380                                f.mView.clearAnimation();
1381                                f.mContainer.endViewTransition(f.mView);
1382                            }
1383                            Animator anim = null;
1384                            if (mCurState > Fragment.INITIALIZING && !mDestroyed
1385                                    && f.mView.getVisibility() == View.VISIBLE
1386                                    && f.mView.getTransitionAlpha() > 0) {
1387                                anim = loadAnimator(f, transit, false,
1388                                        transitionStyle);
1389                            }
1390                            f.mView.setTransitionAlpha(1f);
1391                            if (anim != null) {
1392                                final ViewGroup container = f.mContainer;
1393                                final View view = f.mView;
1394                                final Fragment fragment = f;
1395                                container.startViewTransition(view);
1396                                f.setAnimatingAway(anim);
1397                                f.setStateAfterAnimating(newState);
1398                                anim.addListener(new AnimatorListenerAdapter() {
1399                                    @Override
1400                                    public void onAnimationEnd(Animator anim) {
1401                                        container.endViewTransition(view);
1402                                        Animator animator = f.getAnimatingAway();
1403                                        f.setAnimatingAway(null);
1404                                        // If the animation finished immediately, the fragment's
1405                                        // view will still be there. If so, we can just pretend
1406                                        // there was no animation and skip the moveToState()
1407                                        if (container.indexOfChild(view) == -1
1408                                                && animator != null) {
1409                                            moveToState(fragment, fragment.getStateAfterAnimating(),
1410                                                    0, 0, false);
1411                                        }
1412                                    }
1413                                });
1414                                anim.setTarget(f.mView);
1415                                setHWLayerAnimListenerIfAlpha(f.mView, anim);
1416                                anim.start();
1417
1418                            }
1419                            f.mContainer.removeView(f.mView);
1420                        }
1421                        f.mContainer = null;
1422                        f.mView = null;
1423                        f.mInLayout = false;
1424                    }
1425                    // fall through
1426                case Fragment.CREATED:
1427                    if (newState < Fragment.CREATED) {
1428                        if (mDestroyed) {
1429                            if (f.getAnimatingAway() != null) {
1430                                // The fragment's containing activity is
1431                                // being destroyed, but this fragment is
1432                                // currently animating away.  Stop the
1433                                // animation right now -- it is not needed,
1434                                // and we can't wait any more on destroying
1435                                // the fragment.
1436                                Animator anim = f.getAnimatingAway();
1437                                f.setAnimatingAway(null);
1438                                anim.cancel();
1439                            }
1440                        }
1441                        if (f.getAnimatingAway() != null) {
1442                            // We are waiting for the fragment's view to finish
1443                            // animating away.  Just make a note of the state
1444                            // the fragment now should move to once the animation
1445                            // is done.
1446                            f.setStateAfterAnimating(newState);
1447                            newState = Fragment.CREATED;
1448                        } else {
1449                            if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1450                            if (!f.mRetaining) {
1451                                f.performDestroy();
1452                                dispatchOnFragmentDestroyed(f, false);
1453                            } else {
1454                                f.mState = Fragment.INITIALIZING;
1455                            }
1456
1457                            f.performDetach();
1458                            dispatchOnFragmentDetached(f, false);
1459                            if (!keepActive) {
1460                                if (!f.mRetaining) {
1461                                    makeInactive(f);
1462                                } else {
1463                                    f.mHost = null;
1464                                    f.mParentFragment = null;
1465                                    f.mFragmentManager = null;
1466                                }
1467                            }
1468                        }
1469                    }
1470            }
1471        }
1472
1473        if (f.mState != newState) {
1474            Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; "
1475                    + "expected state " + newState + " found " + f.mState);
1476            f.mState = newState;
1477        }
1478    }
1479
1480    void moveToState(Fragment f) {
1481        moveToState(f, mCurState, 0, 0, false);
1482    }
1483
1484    void ensureInflatedFragmentView(Fragment f) {
1485        if (f.mFromLayout && !f.mPerformedCreateView) {
1486            f.mView = f.performCreateView(f.performGetLayoutInflater(
1487                    f.mSavedFragmentState), null, f.mSavedFragmentState);
1488            if (f.mView != null) {
1489                f.mView.setSaveFromParentEnabled(false);
1490                if (f.mHidden) f.mView.setVisibility(View.GONE);
1491                f.onViewCreated(f.mView, f.mSavedFragmentState);
1492                dispatchOnFragmentViewCreated(f, f.mView, f.mSavedFragmentState, false);
1493            }
1494        }
1495    }
1496
1497    /**
1498     * Fragments that have been shown or hidden don't have their visibility changed or
1499     * animations run during the {@link #showFragment(Fragment)} or {@link #hideFragment(Fragment)}
1500     * calls. After fragments are brought to their final state in
1501     * {@link #moveFragmentToExpectedState(Fragment)} the fragments that have been shown or
1502     * hidden must have their visibility changed and their animations started here.
1503     *
1504     * @param fragment The fragment with mHiddenChanged = true that should change its View's
1505     *                 visibility and start the show or hide animation.
1506     */
1507    void completeShowHideFragment(final Fragment fragment) {
1508        if (fragment.mView != null) {
1509            Animator anim = loadAnimator(fragment, fragment.getNextTransition(), !fragment.mHidden,
1510                    fragment.getNextTransitionStyle());
1511            if (anim != null) {
1512                anim.setTarget(fragment.mView);
1513                if (fragment.mHidden) {
1514                    if (fragment.isHideReplaced()) {
1515                        fragment.setHideReplaced(false);
1516                    } else {
1517                        final ViewGroup container = fragment.mContainer;
1518                        final View animatingView = fragment.mView;
1519                        if (container != null) {
1520                            container.startViewTransition(animatingView);
1521                        }
1522                        // Delay the actual hide operation until the animation finishes, otherwise
1523                        // the fragment will just immediately disappear
1524                        anim.addListener(new AnimatorListenerAdapter() {
1525                            @Override
1526                            public void onAnimationEnd(Animator animation) {
1527                                if (container != null) {
1528                                    container.endViewTransition(animatingView);
1529                                }
1530                                animation.removeListener(this);
1531                                animatingView.setVisibility(View.GONE);
1532                            }
1533                        });
1534                    }
1535                } else {
1536                    fragment.mView.setVisibility(View.VISIBLE);
1537                }
1538                setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1539                anim.start();
1540            } else {
1541                final int visibility = fragment.mHidden && !fragment.isHideReplaced()
1542                        ? View.GONE
1543                        : View.VISIBLE;
1544                fragment.mView.setVisibility(visibility);
1545                if (fragment.isHideReplaced()) {
1546                    fragment.setHideReplaced(false);
1547                }
1548            }
1549        }
1550        if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1551            mNeedMenuInvalidate = true;
1552        }
1553        fragment.mHiddenChanged = false;
1554        fragment.onHiddenChanged(fragment.mHidden);
1555    }
1556
1557    /**
1558     * Moves a fragment to its expected final state or the fragment manager's state, depending
1559     * on whether the fragment manager's state is raised properly.
1560     *
1561     * @param f The fragment to change.
1562     */
1563    void moveFragmentToExpectedState(final Fragment f) {
1564        if (f == null) {
1565            return;
1566        }
1567        int nextState = mCurState;
1568        if (f.mRemoving) {
1569            if (f.isInBackStack()) {
1570                nextState = Math.min(nextState, Fragment.CREATED);
1571            } else {
1572                nextState = Math.min(nextState, Fragment.INITIALIZING);
1573            }
1574        }
1575
1576        moveToState(f, nextState, f.getNextTransition(), f.getNextTransitionStyle(), false);
1577
1578        if (f.mView != null) {
1579            // Move the view if it is out of order
1580            Fragment underFragment = findFragmentUnder(f);
1581            if (underFragment != null) {
1582                final View underView = underFragment.mView;
1583                // make sure this fragment is in the right order.
1584                final ViewGroup container = f.mContainer;
1585                int underIndex = container.indexOfChild(underView);
1586                int viewIndex = container.indexOfChild(f.mView);
1587                if (viewIndex < underIndex) {
1588                    container.removeViewAt(viewIndex);
1589                    container.addView(f.mView, underIndex);
1590                }
1591            }
1592            if (f.mIsNewlyAdded && f.mContainer != null) {
1593                // Make it visible and run the animations
1594                f.mView.setTransitionAlpha(1f);
1595                f.mIsNewlyAdded = false;
1596                // run animations:
1597                Animator anim = loadAnimator(f, f.getNextTransition(), true, f.getNextTransitionStyle());
1598                if (anim != null) {
1599                    anim.setTarget(f.mView);
1600                    setHWLayerAnimListenerIfAlpha(f.mView, anim);
1601                    anim.start();
1602                }
1603            }
1604        }
1605        if (f.mHiddenChanged) {
1606            completeShowHideFragment(f);
1607        }
1608    }
1609
1610    /**
1611     * Changes the state of the fragment manager to {@code newState}. If the fragment manager
1612     * changes state or {@code always} is {@code true}, any fragments within it have their
1613     * states updated as well.
1614     *
1615     * @param newState The new state for the fragment manager
1616     * @param always If {@code true}, all fragments update their state, even
1617     *               if {@code newState} matches the current fragment manager's state.
1618     */
1619    void moveToState(int newState, boolean always) {
1620        if (mHost == null && newState != Fragment.INITIALIZING) {
1621            throw new IllegalStateException("No activity");
1622        }
1623
1624        if (!always && mCurState == newState) {
1625            return;
1626        }
1627
1628        mCurState = newState;
1629
1630        if (mActive != null) {
1631            boolean loadersRunning = false;
1632
1633            // Must add them in the proper order. mActive fragments may be out of order
1634            final int numAdded = mAdded.size();
1635            for (int i = 0; i < numAdded; i++) {
1636                Fragment f = mAdded.get(i);
1637                moveFragmentToExpectedState(f);
1638                if (f.mLoaderManager != null) {
1639                    loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1640                }
1641            }
1642
1643            // Now iterate through all active fragments. These will include those that are removed
1644            // and detached.
1645            final int numActive = mActive.size();
1646            for (int i = 0; i < numActive; i++) {
1647                Fragment f = mActive.valueAt(i);
1648                if (f != null && (f.mRemoving || f.mDetached) && !f.mIsNewlyAdded) {
1649                    moveFragmentToExpectedState(f);
1650                    if (f.mLoaderManager != null) {
1651                        loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1652                    }
1653                }
1654            }
1655
1656            if (!loadersRunning) {
1657                startPendingDeferredFragments();
1658            }
1659
1660            if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) {
1661                mHost.onInvalidateOptionsMenu();
1662                mNeedMenuInvalidate = false;
1663            }
1664        }
1665    }
1666
1667    void startPendingDeferredFragments() {
1668        if (mActive == null) return;
1669
1670        for (int i=0; i<mActive.size(); i++) {
1671            Fragment f = mActive.valueAt(i);
1672            if (f != null) {
1673                performPendingDeferredStart(f);
1674            }
1675        }
1676    }
1677
1678    void makeActive(Fragment f) {
1679        if (f.mIndex >= 0) {
1680            return;
1681        }
1682
1683        f.setIndex(mNextFragmentIndex++, mParent);
1684        if (mActive == null) {
1685            mActive = new SparseArray<>();
1686        }
1687        mActive.put(f.mIndex, f);
1688        if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
1689    }
1690
1691    void makeInactive(Fragment f) {
1692        if (f.mIndex < 0) {
1693            return;
1694        }
1695
1696        if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
1697        // Don't remove yet. That happens in burpActive(). This prevents
1698        // concurrent modification while iterating over mActive
1699        mActive.put(f.mIndex, null);
1700        mHost.inactivateFragment(f.mWho);
1701        f.initState();
1702    }
1703
1704    public void addFragment(Fragment fragment, boolean moveToStateNow) {
1705        if (DEBUG) Log.v(TAG, "add: " + fragment);
1706        makeActive(fragment);
1707        if (!fragment.mDetached) {
1708            if (mAdded.contains(fragment)) {
1709                throw new IllegalStateException("Fragment already added: " + fragment);
1710            }
1711            synchronized (mAdded) {
1712                mAdded.add(fragment);
1713            }
1714            fragment.mAdded = true;
1715            fragment.mRemoving = false;
1716            if (fragment.mView == null) {
1717                fragment.mHiddenChanged = false;
1718            }
1719            if (fragment.mHasMenu && fragment.mMenuVisible) {
1720                mNeedMenuInvalidate = true;
1721            }
1722            if (moveToStateNow) {
1723                moveToState(fragment);
1724            }
1725        }
1726    }
1727
1728    public void removeFragment(Fragment fragment) {
1729        if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
1730        final boolean inactive = !fragment.isInBackStack();
1731        if (!fragment.mDetached || inactive) {
1732            if (false) {
1733                // Would be nice to catch a bad remove here, but we need
1734                // time to test this to make sure we aren't crashes cases
1735                // where it is not a problem.
1736                if (!mAdded.contains(fragment)) {
1737                    throw new IllegalStateException("Fragment not added: " + fragment);
1738                }
1739            }
1740            synchronized (mAdded) {
1741                mAdded.remove(fragment);
1742            }
1743            if (fragment.mHasMenu && fragment.mMenuVisible) {
1744                mNeedMenuInvalidate = true;
1745            }
1746            fragment.mAdded = false;
1747            fragment.mRemoving = true;
1748        }
1749    }
1750
1751    /**
1752     * Marks a fragment as hidden to be later animated in with
1753     * {@link #completeShowHideFragment(Fragment)}.
1754     *
1755     * @param fragment The fragment to be shown.
1756     */
1757    public void hideFragment(Fragment fragment) {
1758        if (DEBUG) Log.v(TAG, "hide: " + fragment);
1759        if (!fragment.mHidden) {
1760            fragment.mHidden = true;
1761            // Toggle hidden changed so that if a fragment goes through show/hide/show
1762            // it doesn't go through the animation.
1763            fragment.mHiddenChanged = !fragment.mHiddenChanged;
1764        }
1765    }
1766
1767    /**
1768     * Marks a fragment as shown to be later animated in with
1769     * {@link #completeShowHideFragment(Fragment)}.
1770     *
1771     * @param fragment The fragment to be shown.
1772     */
1773    public void showFragment(Fragment fragment) {
1774        if (DEBUG) Log.v(TAG, "show: " + fragment);
1775        if (fragment.mHidden) {
1776            fragment.mHidden = false;
1777            // Toggle hidden changed so that if a fragment goes through show/hide/show
1778            // it doesn't go through the animation.
1779            fragment.mHiddenChanged = !fragment.mHiddenChanged;
1780        }
1781    }
1782
1783    public void detachFragment(Fragment fragment) {
1784        if (DEBUG) Log.v(TAG, "detach: " + fragment);
1785        if (!fragment.mDetached) {
1786            fragment.mDetached = true;
1787            if (fragment.mAdded) {
1788                // We are not already in back stack, so need to remove the fragment.
1789                if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
1790                synchronized (mAdded) {
1791                    mAdded.remove(fragment);
1792                }
1793                if (fragment.mHasMenu && fragment.mMenuVisible) {
1794                    mNeedMenuInvalidate = true;
1795                }
1796                fragment.mAdded = false;
1797            }
1798        }
1799    }
1800
1801    public void attachFragment(Fragment fragment) {
1802        if (DEBUG) Log.v(TAG, "attach: " + fragment);
1803        if (fragment.mDetached) {
1804            fragment.mDetached = false;
1805            if (!fragment.mAdded) {
1806                if (mAdded.contains(fragment)) {
1807                    throw new IllegalStateException("Fragment already added: " + fragment);
1808                }
1809                if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
1810                synchronized (mAdded) {
1811                    mAdded.add(fragment);
1812                }
1813                fragment.mAdded = true;
1814                if (fragment.mHasMenu && fragment.mMenuVisible) {
1815                    mNeedMenuInvalidate = true;
1816                }
1817            }
1818        }
1819    }
1820
1821    public Fragment findFragmentById(int id) {
1822        // First look through added fragments.
1823        for (int i = mAdded.size() - 1; i >= 0; i--) {
1824            Fragment f = mAdded.get(i);
1825            if (f != null && f.mFragmentId == id) {
1826                return f;
1827            }
1828        }
1829        if (mActive != null) {
1830            // Now for any known fragment.
1831            for (int i=mActive.size()-1; i>=0; i--) {
1832                Fragment f = mActive.valueAt(i);
1833                if (f != null && f.mFragmentId == id) {
1834                    return f;
1835                }
1836            }
1837        }
1838        return null;
1839    }
1840
1841    public Fragment findFragmentByTag(String tag) {
1842        if (tag != null) {
1843            // First look through added fragments.
1844            for (int i=mAdded.size()-1; i>=0; i--) {
1845                Fragment f = mAdded.get(i);
1846                if (f != null && tag.equals(f.mTag)) {
1847                    return f;
1848                }
1849            }
1850        }
1851        if (mActive != null && tag != null) {
1852            // Now for any known fragment.
1853            for (int i=mActive.size()-1; i>=0; i--) {
1854                Fragment f = mActive.valueAt(i);
1855                if (f != null && tag.equals(f.mTag)) {
1856                    return f;
1857                }
1858            }
1859        }
1860        return null;
1861    }
1862
1863    public Fragment findFragmentByWho(String who) {
1864        if (mActive != null && who != null) {
1865            for (int i=mActive.size()-1; i>=0; i--) {
1866                Fragment f = mActive.valueAt(i);
1867                if (f != null && (f=f.findFragmentByWho(who)) != null) {
1868                    return f;
1869                }
1870            }
1871        }
1872        return null;
1873    }
1874
1875    private void checkStateLoss() {
1876        if (mStateSaved) {
1877            throw new IllegalStateException(
1878                    "Can not perform this action after onSaveInstanceState");
1879        }
1880        if (mNoTransactionsBecause != null) {
1881            throw new IllegalStateException(
1882                    "Can not perform this action inside of " + mNoTransactionsBecause);
1883        }
1884    }
1885
1886    @Override
1887    public boolean isStateSaved() {
1888        return mStateSaved;
1889    }
1890
1891    /**
1892     * Adds an action to the queue of pending actions.
1893     *
1894     * @param action the action to add
1895     * @param allowStateLoss whether to allow loss of state information
1896     * @throws IllegalStateException if the activity has been destroyed
1897     */
1898    public void enqueueAction(OpGenerator action, boolean allowStateLoss) {
1899        if (!allowStateLoss) {
1900            checkStateLoss();
1901        }
1902        synchronized (this) {
1903            if (mDestroyed || mHost == null) {
1904                if (allowStateLoss) {
1905                    // This FragmentManager isn't attached, so drop the entire transaction.
1906                    return;
1907                }
1908                throw new IllegalStateException("Activity has been destroyed");
1909            }
1910            if (mPendingActions == null) {
1911                mPendingActions = new ArrayList<>();
1912            }
1913            mPendingActions.add(action);
1914            scheduleCommit();
1915        }
1916    }
1917
1918    /**
1919     * Schedules the execution when one hasn't been scheduled already. This should happen
1920     * the first time {@link #enqueueAction(OpGenerator, boolean)} is called or when
1921     * a postponed transaction has been started with
1922     * {@link Fragment#startPostponedEnterTransition()}
1923     */
1924    private void scheduleCommit() {
1925        synchronized (this) {
1926            boolean postponeReady =
1927                    mPostponedTransactions != null && !mPostponedTransactions.isEmpty();
1928            boolean pendingReady = mPendingActions != null && mPendingActions.size() == 1;
1929            if (postponeReady || pendingReady) {
1930                mHost.getHandler().removeCallbacks(mExecCommit);
1931                mHost.getHandler().post(mExecCommit);
1932            }
1933        }
1934    }
1935
1936    public int allocBackStackIndex(BackStackRecord bse) {
1937        synchronized (this) {
1938            if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1939                if (mBackStackIndices == null) {
1940                    mBackStackIndices = new ArrayList<BackStackRecord>();
1941                }
1942                int index = mBackStackIndices.size();
1943                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1944                mBackStackIndices.add(bse);
1945                return index;
1946
1947            } else {
1948                int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1949                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1950                mBackStackIndices.set(index, bse);
1951                return index;
1952            }
1953        }
1954    }
1955
1956    public void setBackStackIndex(int index, BackStackRecord bse) {
1957        synchronized (this) {
1958            if (mBackStackIndices == null) {
1959                mBackStackIndices = new ArrayList<BackStackRecord>();
1960            }
1961            int N = mBackStackIndices.size();
1962            if (index < N) {
1963                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1964                mBackStackIndices.set(index, bse);
1965            } else {
1966                while (N < index) {
1967                    mBackStackIndices.add(null);
1968                    if (mAvailBackStackIndices == null) {
1969                        mAvailBackStackIndices = new ArrayList<Integer>();
1970                    }
1971                    if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1972                    mAvailBackStackIndices.add(N);
1973                    N++;
1974                }
1975                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1976                mBackStackIndices.add(bse);
1977            }
1978        }
1979    }
1980
1981    public void freeBackStackIndex(int index) {
1982        synchronized (this) {
1983            mBackStackIndices.set(index, null);
1984            if (mAvailBackStackIndices == null) {
1985                mAvailBackStackIndices = new ArrayList<Integer>();
1986            }
1987            if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1988            mAvailBackStackIndices.add(index);
1989        }
1990    }
1991
1992    /**
1993     * Broken out from exec*, this prepares for gathering and executing operations.
1994     *
1995     * @param allowStateLoss true if state loss should be ignored or false if it should be
1996     *                       checked.
1997     */
1998    private void ensureExecReady(boolean allowStateLoss) {
1999        if (mExecutingActions) {
2000            throw new IllegalStateException("FragmentManager is already executing transactions");
2001        }
2002
2003        if (Looper.myLooper() != mHost.getHandler().getLooper()) {
2004            throw new IllegalStateException("Must be called from main thread of fragment host");
2005        }
2006
2007        if (!allowStateLoss) {
2008            checkStateLoss();
2009        }
2010
2011        if (mTmpRecords == null) {
2012            mTmpRecords = new ArrayList<>();
2013            mTmpIsPop = new ArrayList<>();
2014        }
2015        mExecutingActions = true;
2016        try {
2017            executePostponedTransaction(null, null);
2018        } finally {
2019            mExecutingActions = false;
2020        }
2021    }
2022
2023    public void execSingleAction(OpGenerator action, boolean allowStateLoss) {
2024        if (allowStateLoss && (mHost == null || mDestroyed)) {
2025            // This FragmentManager isn't attached, so drop the entire transaction.
2026            return;
2027        }
2028        ensureExecReady(allowStateLoss);
2029        if (action.generateOps(mTmpRecords, mTmpIsPop)) {
2030            mExecutingActions = true;
2031            try {
2032                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
2033            } finally {
2034                cleanupExec();
2035            }
2036        }
2037
2038        doPendingDeferredStart();
2039        burpActive();
2040    }
2041
2042    /**
2043     * Broken out of exec*, this cleans up the mExecutingActions and the temporary structures
2044     * used in executing operations.
2045     */
2046    private void cleanupExec() {
2047        mExecutingActions = false;
2048        mTmpIsPop.clear();
2049        mTmpRecords.clear();
2050    }
2051
2052    /**
2053     * Only call from main thread!
2054     */
2055    public boolean execPendingActions() {
2056        ensureExecReady(true);
2057
2058        boolean didSomething = false;
2059        while (generateOpsForPendingActions(mTmpRecords, mTmpIsPop)) {
2060            mExecutingActions = true;
2061            try {
2062                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
2063            } finally {
2064                cleanupExec();
2065            }
2066            didSomething = true;
2067        }
2068
2069        doPendingDeferredStart();
2070        burpActive();
2071
2072        return didSomething;
2073    }
2074
2075    /**
2076     * Complete the execution of transactions that have previously been postponed, but are
2077     * now ready.
2078     */
2079    private void executePostponedTransaction(ArrayList<BackStackRecord> records,
2080            ArrayList<Boolean> isRecordPop) {
2081        int numPostponed = mPostponedTransactions == null ? 0 : mPostponedTransactions.size();
2082        for (int i = 0; i < numPostponed; i++) {
2083            StartEnterTransitionListener listener = mPostponedTransactions.get(i);
2084            if (records != null && !listener.mIsBack) {
2085                int index = records.indexOf(listener.mRecord);
2086                if (index != -1 && isRecordPop.get(index)) {
2087                    listener.cancelTransaction();
2088                    continue;
2089                }
2090            }
2091            if (listener.isReady() || (records != null &&
2092                    listener.mRecord.interactsWith(records, 0, records.size()))) {
2093                mPostponedTransactions.remove(i);
2094                i--;
2095                numPostponed--;
2096                int index;
2097                if (records != null && !listener.mIsBack &&
2098                        (index = records.indexOf(listener.mRecord)) != -1 &&
2099                        isRecordPop.get(index)) {
2100                    // This is popping a postponed transaction
2101                    listener.cancelTransaction();
2102                } else {
2103                    listener.completeTransaction();
2104                }
2105            }
2106        }
2107    }
2108
2109    /**
2110     * Remove redundant BackStackRecord operations and executes them. This method merges operations
2111     * of proximate records that allow reordering. See
2112     * {@link FragmentTransaction#setReorderingAllowed(boolean)}.
2113     * <p>
2114     * For example, a transaction that adds to the back stack and then another that pops that
2115     * back stack record will be optimized to remove the unnecessary operation.
2116     * <p>
2117     * Likewise, two transactions committed that are executed at the same time will be optimized
2118     * to remove the redundant operations as well as two pop operations executed together.
2119     *
2120     * @param records The records pending execution
2121     * @param isRecordPop The direction that these records are being run.
2122     */
2123    private void removeRedundantOperationsAndExecute(ArrayList<BackStackRecord> records,
2124            ArrayList<Boolean> isRecordPop) {
2125        if (records == null || records.isEmpty()) {
2126            return;
2127        }
2128
2129        if (isRecordPop == null || records.size() != isRecordPop.size()) {
2130            throw new IllegalStateException("Internal error with the back stack records");
2131        }
2132
2133        // Force start of any postponed transactions that interact with scheduled transactions:
2134        executePostponedTransaction(records, isRecordPop);
2135
2136        final int numRecords = records.size();
2137        int startIndex = 0;
2138        for (int recordNum = 0; recordNum < numRecords; recordNum++) {
2139            final boolean canReorder = records.get(recordNum).mReorderingAllowed;
2140            if (!canReorder) {
2141                // execute all previous transactions
2142                if (startIndex != recordNum) {
2143                    executeOpsTogether(records, isRecordPop, startIndex, recordNum);
2144                }
2145                // execute all pop operations that don't allow reordering together or
2146                // one add operation
2147                int reorderingEnd = recordNum + 1;
2148                if (isRecordPop.get(recordNum)) {
2149                    while (reorderingEnd < numRecords
2150                            && isRecordPop.get(reorderingEnd)
2151                            && !records.get(reorderingEnd).mReorderingAllowed) {
2152                        reorderingEnd++;
2153                    }
2154                }
2155                executeOpsTogether(records, isRecordPop, recordNum, reorderingEnd);
2156                startIndex = reorderingEnd;
2157                recordNum = reorderingEnd - 1;
2158            }
2159        }
2160        if (startIndex != numRecords) {
2161            executeOpsTogether(records, isRecordPop, startIndex, numRecords);
2162        }
2163    }
2164
2165    /**
2166     * Executes a subset of a list of BackStackRecords, all of which either allow reordering or
2167     * do not allow ordering.
2168     * @param records A list of BackStackRecords that are to be executed together
2169     * @param isRecordPop The direction that these records are being run.
2170     * @param startIndex The index of the first record in <code>records</code> to be executed
2171     * @param endIndex One more than the final record index in <code>records</code> to executed.
2172     */
2173    private void executeOpsTogether(ArrayList<BackStackRecord> records,
2174            ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) {
2175        final boolean allowReordering = records.get(startIndex).mReorderingAllowed;
2176        boolean addToBackStack = false;
2177        if (mTmpAddedFragments == null) {
2178            mTmpAddedFragments = new ArrayList<>();
2179        } else {
2180            mTmpAddedFragments.clear();
2181        }
2182        mTmpAddedFragments.addAll(mAdded);
2183        Fragment oldPrimaryNav = getPrimaryNavigationFragment();
2184        for (int recordNum = startIndex; recordNum < endIndex; recordNum++) {
2185            final BackStackRecord record = records.get(recordNum);
2186            final boolean isPop = isRecordPop.get(recordNum);
2187            if (!isPop) {
2188                oldPrimaryNav = record.expandOps(mTmpAddedFragments, oldPrimaryNav);
2189            } else {
2190                record.trackAddedFragmentsInPop(mTmpAddedFragments);
2191            }
2192            addToBackStack = addToBackStack || record.mAddToBackStack;
2193        }
2194        mTmpAddedFragments.clear();
2195
2196        if (!allowReordering) {
2197            FragmentTransition.startTransitions(this, records, isRecordPop, startIndex, endIndex,
2198                    false);
2199        }
2200        executeOps(records, isRecordPop, startIndex, endIndex);
2201
2202        int postponeIndex = endIndex;
2203        if (allowReordering) {
2204            ArraySet<Fragment> addedFragments = new ArraySet<>();
2205            addAddedFragments(addedFragments);
2206            postponeIndex = postponePostponableTransactions(records, isRecordPop,
2207                    startIndex, endIndex, addedFragments);
2208            makeRemovedFragmentsInvisible(addedFragments);
2209        }
2210
2211        if (postponeIndex != startIndex && allowReordering) {
2212            // need to run something now
2213            FragmentTransition.startTransitions(this, records, isRecordPop, startIndex,
2214                    postponeIndex, true);
2215            moveToState(mCurState, true);
2216        }
2217
2218        for (int recordNum = startIndex; recordNum < endIndex; recordNum++) {
2219            final BackStackRecord record = records.get(recordNum);
2220            final boolean isPop = isRecordPop.get(recordNum);
2221            if (isPop && record.mIndex >= 0) {
2222                freeBackStackIndex(record.mIndex);
2223                record.mIndex = -1;
2224            }
2225            record.runOnCommitRunnables();
2226        }
2227
2228        if (addToBackStack) {
2229            reportBackStackChanged();
2230        }
2231    }
2232
2233    /**
2234     * Any fragments that were removed because they have been postponed should have their views
2235     * made invisible by setting their transition alpha to 0.
2236     *
2237     * @param fragments The fragments that were added during operation execution. Only the ones
2238     *                  that are no longer added will have their transition alpha changed.
2239     */
2240    private void makeRemovedFragmentsInvisible(ArraySet<Fragment> fragments) {
2241        final int numAdded = fragments.size();
2242        for (int i = 0; i < numAdded; i++) {
2243            final Fragment fragment = fragments.valueAt(i);
2244            if (!fragment.mAdded) {
2245                final View view = fragment.getView();
2246                view.setTransitionAlpha(0f);
2247            }
2248        }
2249    }
2250
2251    /**
2252     * Examine all transactions and determine which ones are marked as postponed. Those will
2253     * have their operations rolled back and moved to the end of the record list (up to endIndex).
2254     * It will also add the postponed transaction to the queue.
2255     *
2256     * @param records A list of BackStackRecords that should be checked.
2257     * @param isRecordPop The direction that these records are being run.
2258     * @param startIndex The index of the first record in <code>records</code> to be checked
2259     * @param endIndex One more than the final record index in <code>records</code> to be checked.
2260     * @return The index of the first postponed transaction or endIndex if no transaction was
2261     * postponed.
2262     */
2263    private int postponePostponableTransactions(ArrayList<BackStackRecord> records,
2264            ArrayList<Boolean> isRecordPop, int startIndex, int endIndex,
2265            ArraySet<Fragment> added) {
2266        int postponeIndex = endIndex;
2267        for (int i = endIndex - 1; i >= startIndex; i--) {
2268            final BackStackRecord record = records.get(i);
2269            final boolean isPop = isRecordPop.get(i);
2270            boolean isPostponed = record.isPostponed() &&
2271                    !record.interactsWith(records, i + 1, endIndex);
2272            if (isPostponed) {
2273                if (mPostponedTransactions == null) {
2274                    mPostponedTransactions = new ArrayList<>();
2275                }
2276                StartEnterTransitionListener listener =
2277                        new StartEnterTransitionListener(record, isPop);
2278                mPostponedTransactions.add(listener);
2279                record.setOnStartPostponedListener(listener);
2280
2281                // roll back the transaction
2282                if (isPop) {
2283                    record.executeOps();
2284                } else {
2285                    record.executePopOps(false);
2286                }
2287
2288                // move to the end
2289                postponeIndex--;
2290                if (i != postponeIndex) {
2291                    records.remove(i);
2292                    records.add(postponeIndex, record);
2293                }
2294
2295                // different views may be visible now
2296                addAddedFragments(added);
2297            }
2298        }
2299        return postponeIndex;
2300    }
2301
2302    /**
2303     * When a postponed transaction is ready to be started, this completes the transaction,
2304     * removing, hiding, or showing views as well as starting the animations and transitions.
2305     * <p>
2306     * {@code runtransitions} is set to false when the transaction postponement was interrupted
2307     * abnormally -- normally by a new transaction being started that affects the postponed
2308     * transaction.
2309     *
2310     * @param record The transaction to run
2311     * @param isPop true if record is popping or false if it is adding
2312     * @param runTransitions true if the fragment transition should be run or false otherwise.
2313     * @param moveToState true if the state should be changed after executing the operations.
2314     *                    This is false when the transaction is canceled when a postponed
2315     *                    transaction is popped.
2316     */
2317    private void completeExecute(BackStackRecord record, boolean isPop, boolean runTransitions,
2318            boolean moveToState) {
2319        if (isPop) {
2320            record.executePopOps(moveToState);
2321        } else {
2322            record.executeOps();
2323        }
2324        ArrayList<BackStackRecord> records = new ArrayList<>(1);
2325        ArrayList<Boolean> isRecordPop = new ArrayList<>(1);
2326        records.add(record);
2327        isRecordPop.add(isPop);
2328        if (runTransitions) {
2329            FragmentTransition.startTransitions(this, records, isRecordPop, 0, 1, true);
2330        }
2331        if (moveToState) {
2332            moveToState(mCurState, true);
2333        }
2334
2335        if (mActive != null) {
2336            final int numActive = mActive.size();
2337            for (int i = 0; i < numActive; i++) {
2338                // Allow added fragments to be removed during the pop since we aren't going
2339                // to move them to the final state with moveToState(mCurState).
2340                Fragment fragment = mActive.valueAt(i);
2341                if (fragment != null && fragment.mView != null && fragment.mIsNewlyAdded
2342                        && record.interactsWith(fragment.mContainerId)) {
2343                    fragment.mIsNewlyAdded = false;
2344                }
2345            }
2346        }
2347    }
2348
2349    /**
2350     * Find a fragment within the fragment's container whose View should be below the passed
2351     * fragment. {@code null} is returned when the fragment has no View or if there should be
2352     * no fragment with a View below the given fragment.
2353     *
2354     * As an example, if mAdded has two Fragments with Views sharing the same container:
2355     * FragmentA
2356     * FragmentB
2357     *
2358     * Then, when processing FragmentB, FragmentA will be returned. If, however, FragmentA
2359     * had no View, null would be returned.
2360     *
2361     * @param f The fragment that may be on top of another fragment.
2362     * @return The fragment with a View under f, if one exists or null if f has no View or
2363     * there are no fragments with Views in the same container.
2364     */
2365    private Fragment findFragmentUnder(Fragment f) {
2366        final ViewGroup container = f.mContainer;
2367        final View view = f.mView;
2368
2369        if (container == null || view == null) {
2370            return null;
2371        }
2372
2373        final int fragmentIndex = mAdded.indexOf(f);
2374        for (int i = fragmentIndex - 1; i >= 0; i--) {
2375            Fragment underFragment = mAdded.get(i);
2376            if (underFragment.mContainer == container && underFragment.mView != null) {
2377                // Found the fragment under this one
2378                return underFragment;
2379            }
2380        }
2381        return null;
2382    }
2383
2384    /**
2385     * Run the operations in the BackStackRecords, either to push or pop.
2386     *
2387     * @param records The list of records whose operations should be run.
2388     * @param isRecordPop The direction that these records are being run.
2389     * @param startIndex The index of the first entry in records to run.
2390     * @param endIndex One past the index of the final entry in records to run.
2391     */
2392    private static void executeOps(ArrayList<BackStackRecord> records,
2393            ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) {
2394        for (int i = startIndex; i < endIndex; i++) {
2395            final BackStackRecord record = records.get(i);
2396            final boolean isPop = isRecordPop.get(i);
2397            if (isPop) {
2398                record.bumpBackStackNesting(-1);
2399                // Only execute the add operations at the end of
2400                // all transactions.
2401                boolean moveToState = i == (endIndex - 1);
2402                record.executePopOps(moveToState);
2403            } else {
2404                record.bumpBackStackNesting(1);
2405                record.executeOps();
2406            }
2407        }
2408    }
2409
2410    /**
2411     * Ensure that fragments that are added are moved to at least the CREATED state.
2412     * Any newly-added Views are inserted into {@code added} so that the Transaction can be
2413     * postponed with {@link Fragment#postponeEnterTransition()}. They will later be made
2414     * invisible by changing their transitionAlpha to 0 if they have been removed when postponed.
2415     */
2416    private void addAddedFragments(ArraySet<Fragment> added) {
2417        if (mCurState < Fragment.CREATED) {
2418            return;
2419        }
2420        // We want to leave the fragment in the started state
2421        final int state = Math.min(mCurState, Fragment.STARTED);
2422        final int numAdded = mAdded.size();
2423        for (int i = 0; i < numAdded; i++) {
2424            Fragment fragment = mAdded.get(i);
2425            if (fragment.mState < state) {
2426                moveToState(fragment, state, fragment.getNextAnim(), fragment.getNextTransition(), false);
2427                if (fragment.mView != null && !fragment.mHidden && fragment.mIsNewlyAdded) {
2428                    added.add(fragment);
2429                }
2430            }
2431        }
2432    }
2433
2434    /**
2435     * Starts all postponed transactions regardless of whether they are ready or not.
2436     */
2437    private void forcePostponedTransactions() {
2438        if (mPostponedTransactions != null) {
2439            while (!mPostponedTransactions.isEmpty()) {
2440                mPostponedTransactions.remove(0).completeTransaction();
2441            }
2442        }
2443    }
2444
2445    /**
2446     * Ends the animations of fragments so that they immediately reach the end state.
2447     * This is used prior to saving the state so that the correct state is saved.
2448     */
2449    private void endAnimatingAwayFragments() {
2450        final int numFragments = mActive == null ? 0 : mActive.size();
2451        for (int i = 0; i < numFragments; i++) {
2452            Fragment fragment = mActive.valueAt(i);
2453            if (fragment != null && fragment.getAnimatingAway() != null) {
2454                // Give up waiting for the animation and just end it.
2455                fragment.getAnimatingAway().end();
2456            }
2457        }
2458    }
2459
2460    /**
2461     * Adds all records in the pending actions to records and whether they are add or pop
2462     * operations to isPop. After executing, the pending actions will be empty.
2463     *
2464     * @param records All pending actions will generate BackStackRecords added to this.
2465     *                This contains the transactions, in order, to execute.
2466     * @param isPop All pending actions will generate booleans to add to this. This contains
2467     *              an entry for each entry in records to indicate whether or not it is a
2468     *              pop action.
2469     */
2470    private boolean generateOpsForPendingActions(ArrayList<BackStackRecord> records,
2471            ArrayList<Boolean> isPop) {
2472        boolean didSomething = false;
2473        synchronized (this) {
2474            if (mPendingActions == null || mPendingActions.size() == 0) {
2475                return false;
2476            }
2477
2478            final int numActions = mPendingActions.size();
2479            for (int i = 0; i < numActions; i++) {
2480                didSomething |= mPendingActions.get(i).generateOps(records, isPop);
2481            }
2482            mPendingActions.clear();
2483            mHost.getHandler().removeCallbacks(mExecCommit);
2484        }
2485        return didSomething;
2486    }
2487
2488    void doPendingDeferredStart() {
2489        if (mHavePendingDeferredStart) {
2490            boolean loadersRunning = false;
2491            for (int i=0; i<mActive.size(); i++) {
2492                Fragment f = mActive.valueAt(i);
2493                if (f != null && f.mLoaderManager != null) {
2494                    loadersRunning |= f.mLoaderManager.hasRunningLoaders();
2495                }
2496            }
2497            if (!loadersRunning) {
2498                mHavePendingDeferredStart = false;
2499                startPendingDeferredFragments();
2500            }
2501        }
2502    }
2503
2504    void reportBackStackChanged() {
2505        if (mBackStackChangeListeners != null) {
2506            for (int i=0; i<mBackStackChangeListeners.size(); i++) {
2507                mBackStackChangeListeners.get(i).onBackStackChanged();
2508            }
2509        }
2510    }
2511
2512    void addBackStackState(BackStackRecord state) {
2513        if (mBackStack == null) {
2514            mBackStack = new ArrayList<BackStackRecord>();
2515        }
2516        mBackStack.add(state);
2517    }
2518
2519    boolean popBackStackState(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop,
2520            String name, int id, int flags) {
2521        if (mBackStack == null) {
2522            return false;
2523        }
2524        if (name == null && id < 0 && (flags & POP_BACK_STACK_INCLUSIVE) == 0) {
2525            int last = mBackStack.size() - 1;
2526            if (last < 0) {
2527                return false;
2528            }
2529            records.add(mBackStack.remove(last));
2530            isRecordPop.add(true);
2531        } else {
2532            int index = -1;
2533            if (name != null || id >= 0) {
2534                // If a name or ID is specified, look for that place in
2535                // the stack.
2536                index = mBackStack.size()-1;
2537                while (index >= 0) {
2538                    BackStackRecord bss = mBackStack.get(index);
2539                    if (name != null && name.equals(bss.getName())) {
2540                        break;
2541                    }
2542                    if (id >= 0 && id == bss.mIndex) {
2543                        break;
2544                    }
2545                    index--;
2546                }
2547                if (index < 0) {
2548                    return false;
2549                }
2550                if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
2551                    index--;
2552                    // Consume all following entries that match.
2553                    while (index >= 0) {
2554                        BackStackRecord bss = mBackStack.get(index);
2555                        if ((name != null && name.equals(bss.getName()))
2556                                || (id >= 0 && id == bss.mIndex)) {
2557                            index--;
2558                            continue;
2559                        }
2560                        break;
2561                    }
2562                }
2563            }
2564            if (index == mBackStack.size()-1) {
2565                return false;
2566            }
2567            for (int i = mBackStack.size() - 1; i > index; i--) {
2568                records.add(mBackStack.remove(i));
2569                isRecordPop.add(true);
2570            }
2571        }
2572        return true;
2573    }
2574
2575    FragmentManagerNonConfig retainNonConfig() {
2576        setRetaining(mSavedNonConfig);
2577        return mSavedNonConfig;
2578    }
2579
2580    /**
2581     * Recurse the FragmentManagerNonConfig fragments and set the mRetaining to true. This
2582     * was previously done while saving the non-config state, but that has been moved to
2583     * {@link #saveNonConfig()} called from {@link #saveAllState()}. If mRetaining is set too
2584     * early, the fragment won't be destroyed when the FragmentManager is destroyed.
2585     */
2586    private static void setRetaining(FragmentManagerNonConfig nonConfig) {
2587        if (nonConfig == null) {
2588            return;
2589        }
2590        List<Fragment> fragments = nonConfig.getFragments();
2591        if (fragments != null) {
2592            for (Fragment fragment : fragments) {
2593                fragment.mRetaining = true;
2594            }
2595        }
2596        List<FragmentManagerNonConfig> children = nonConfig.getChildNonConfigs();
2597        if (children != null) {
2598            for (FragmentManagerNonConfig child : children) {
2599                setRetaining(child);
2600            }
2601        }
2602    }
2603
2604    void saveNonConfig() {
2605        ArrayList<Fragment> fragments = null;
2606        ArrayList<FragmentManagerNonConfig> childFragments = null;
2607        if (mActive != null) {
2608            for (int i=0; i<mActive.size(); i++) {
2609                Fragment f = mActive.valueAt(i);
2610                if (f != null) {
2611                    if (f.mRetainInstance) {
2612                        if (fragments == null) {
2613                            fragments = new ArrayList<>();
2614                        }
2615                        fragments.add(f);
2616                        f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
2617                        if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
2618                    }
2619                    FragmentManagerNonConfig child;
2620                    if (f.mChildFragmentManager != null) {
2621                        f.mChildFragmentManager.saveNonConfig();
2622                        child = f.mChildFragmentManager.mSavedNonConfig;
2623                    } else {
2624                        // f.mChildNonConfig may be not null, when the parent fragment is
2625                        // in the backstack.
2626                        child = f.mChildNonConfig;
2627                    }
2628
2629                    if (childFragments == null && child != null) {
2630                        childFragments = new ArrayList<>(mActive.size());
2631                        for (int j = 0; j < i; j++) {
2632                            childFragments.add(null);
2633                        }
2634                    }
2635
2636                    if (childFragments != null) {
2637                        childFragments.add(child);
2638                    }
2639                }
2640            }
2641        }
2642        if (fragments == null && childFragments == null) {
2643            mSavedNonConfig = null;
2644        } else {
2645            mSavedNonConfig = new FragmentManagerNonConfig(fragments, childFragments);
2646        }
2647    }
2648
2649    void saveFragmentViewState(Fragment f) {
2650        if (f.mView == null) {
2651            return;
2652        }
2653        if (mStateArray == null) {
2654            mStateArray = new SparseArray<Parcelable>();
2655        } else {
2656            mStateArray.clear();
2657        }
2658        f.mView.saveHierarchyState(mStateArray);
2659        if (mStateArray.size() > 0) {
2660            f.mSavedViewState = mStateArray;
2661            mStateArray = null;
2662        }
2663    }
2664
2665    Bundle saveFragmentBasicState(Fragment f) {
2666        Bundle result = null;
2667
2668        if (mStateBundle == null) {
2669            mStateBundle = new Bundle();
2670        }
2671        f.performSaveInstanceState(mStateBundle);
2672        dispatchOnFragmentSaveInstanceState(f, mStateBundle, false);
2673        if (!mStateBundle.isEmpty()) {
2674            result = mStateBundle;
2675            mStateBundle = null;
2676        }
2677
2678        if (f.mView != null) {
2679            saveFragmentViewState(f);
2680        }
2681        if (f.mSavedViewState != null) {
2682            if (result == null) {
2683                result = new Bundle();
2684            }
2685            result.putSparseParcelableArray(
2686                    FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
2687        }
2688        if (!f.mUserVisibleHint) {
2689            if (result == null) {
2690                result = new Bundle();
2691            }
2692            // Only add this if it's not the default value
2693            result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
2694        }
2695
2696        return result;
2697    }
2698
2699    Parcelable saveAllState() {
2700        // Make sure all pending operations have now been executed to get
2701        // our state update-to-date.
2702        forcePostponedTransactions();
2703        endAnimatingAwayFragments();
2704        execPendingActions();
2705
2706        mStateSaved = true;
2707        mSavedNonConfig = null;
2708
2709        if (mActive == null || mActive.size() <= 0) {
2710            return null;
2711        }
2712
2713        // First collect all active fragments.
2714        int N = mActive.size();
2715        FragmentState[] active = new FragmentState[N];
2716        boolean haveFragments = false;
2717        for (int i=0; i<N; i++) {
2718            Fragment f = mActive.valueAt(i);
2719            if (f != null) {
2720                if (f.mIndex < 0) {
2721                    throwException(new IllegalStateException(
2722                            "Failure saving state: active " + f
2723                            + " has cleared index: " + f.mIndex));
2724                }
2725
2726                haveFragments = true;
2727
2728                FragmentState fs = new FragmentState(f);
2729                active[i] = fs;
2730
2731                if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
2732                    fs.mSavedFragmentState = saveFragmentBasicState(f);
2733
2734                    if (f.mTarget != null) {
2735                        if (f.mTarget.mIndex < 0) {
2736                            throwException(new IllegalStateException(
2737                                    "Failure saving state: " + f
2738                                    + " has target not in fragment manager: " + f.mTarget));
2739                        }
2740                        if (fs.mSavedFragmentState == null) {
2741                            fs.mSavedFragmentState = new Bundle();
2742                        }
2743                        putFragment(fs.mSavedFragmentState,
2744                                FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
2745                        if (f.mTargetRequestCode != 0) {
2746                            fs.mSavedFragmentState.putInt(
2747                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
2748                                    f.mTargetRequestCode);
2749                        }
2750                    }
2751
2752                } else {
2753                    fs.mSavedFragmentState = f.mSavedFragmentState;
2754                }
2755
2756                if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
2757                        + fs.mSavedFragmentState);
2758            }
2759        }
2760
2761        if (!haveFragments) {
2762            if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
2763            return null;
2764        }
2765
2766        int[] added = null;
2767        BackStackState[] backStack = null;
2768
2769        // Build list of currently added fragments.
2770        N = mAdded.size();
2771        if (N > 0) {
2772            added = new int[N];
2773            for (int i=0; i<N; i++) {
2774                added[i] = mAdded.get(i).mIndex;
2775                if (added[i] < 0) {
2776                    throwException(new IllegalStateException(
2777                            "Failure saving state: active " + mAdded.get(i)
2778                            + " has cleared index: " + added[i]));
2779                }
2780                if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
2781                        + ": " + mAdded.get(i));
2782            }
2783        }
2784
2785        // Now save back stack.
2786        if (mBackStack != null) {
2787            N = mBackStack.size();
2788            if (N > 0) {
2789                backStack = new BackStackState[N];
2790                for (int i=0; i<N; i++) {
2791                    backStack[i] = new BackStackState(this, mBackStack.get(i));
2792                    if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
2793                            + ": " + mBackStack.get(i));
2794                }
2795            }
2796        }
2797
2798        FragmentManagerState fms = new FragmentManagerState();
2799        fms.mActive = active;
2800        fms.mAdded = added;
2801        fms.mBackStack = backStack;
2802        fms.mNextFragmentIndex = mNextFragmentIndex;
2803        if (mPrimaryNav != null) {
2804            fms.mPrimaryNavActiveIndex = mPrimaryNav.mIndex;
2805        }
2806        saveNonConfig();
2807        return fms;
2808    }
2809
2810    void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) {
2811        // If there is no saved state at all, then there can not be
2812        // any nonConfig fragments either, so that is that.
2813        if (state == null) return;
2814        FragmentManagerState fms = (FragmentManagerState)state;
2815        if (fms.mActive == null) return;
2816
2817        List<FragmentManagerNonConfig> childNonConfigs = null;
2818
2819        // First re-attach any non-config instances we are retaining back
2820        // to their saved state, so we don't try to instantiate them again.
2821        if (nonConfig != null) {
2822            List<Fragment> nonConfigFragments = nonConfig.getFragments();
2823            childNonConfigs = nonConfig.getChildNonConfigs();
2824            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2825            for (int i = 0; i < count; i++) {
2826                Fragment f = nonConfigFragments.get(i);
2827                if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
2828                int index = 0; // index of f in fms.mActive
2829                while (index < fms.mActive.length && fms.mActive[index].mIndex != f.mIndex) {
2830                    index++;
2831                }
2832                if (index == fms.mActive.length) {
2833                    throwException(new IllegalStateException("Could not find active fragment "
2834                            + "with index " + f.mIndex));
2835                }
2836                FragmentState fs = fms.mActive[index];
2837                fs.mInstance = f;
2838                f.mSavedViewState = null;
2839                f.mBackStackNesting = 0;
2840                f.mInLayout = false;
2841                f.mAdded = false;
2842                f.mTarget = null;
2843                if (fs.mSavedFragmentState != null) {
2844                    fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader());
2845                    f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
2846                            FragmentManagerImpl.VIEW_STATE_TAG);
2847                    f.mSavedFragmentState = fs.mSavedFragmentState;
2848                }
2849            }
2850        }
2851
2852        // Build the full list of active fragments, instantiating them from
2853        // their saved state.
2854        mActive = new SparseArray<>(fms.mActive.length);
2855        for (int i=0; i<fms.mActive.length; i++) {
2856            FragmentState fs = fms.mActive[i];
2857            if (fs != null) {
2858                FragmentManagerNonConfig childNonConfig = null;
2859                if (childNonConfigs != null && i < childNonConfigs.size()) {
2860                    childNonConfig = childNonConfigs.get(i);
2861                }
2862                Fragment f = fs.instantiate(mHost, mContainer, mParent, childNonConfig);
2863                if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
2864                mActive.put(f.mIndex, f);
2865                // Now that the fragment is instantiated (or came from being
2866                // retained above), clear mInstance in case we end up re-restoring
2867                // from this FragmentState again.
2868                fs.mInstance = null;
2869            }
2870        }
2871
2872        // Update the target of all retained fragments.
2873        if (nonConfig != null) {
2874            List<Fragment> nonConfigFragments = nonConfig.getFragments();
2875            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2876            for (int i = 0; i < count; i++) {
2877                Fragment f = nonConfigFragments.get(i);
2878                if (f.mTargetIndex >= 0) {
2879                    f.mTarget = mActive.get(f.mTargetIndex);
2880                    if (f.mTarget == null) {
2881                        Log.w(TAG, "Re-attaching retained fragment " + f
2882                                + " target no longer exists: " + f.mTargetIndex);
2883                        f.mTarget = null;
2884                    }
2885                }
2886            }
2887        }
2888
2889        // Build the list of currently added fragments.
2890        mAdded.clear();
2891        if (fms.mAdded != null) {
2892            for (int i=0; i<fms.mAdded.length; i++) {
2893                Fragment f = mActive.get(fms.mAdded[i]);
2894                if (f == null) {
2895                    throwException(new IllegalStateException(
2896                            "No instantiated fragment for index #" + fms.mAdded[i]));
2897                }
2898                f.mAdded = true;
2899                if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
2900                if (mAdded.contains(f)) {
2901                    throw new IllegalStateException("Already added!");
2902                }
2903                synchronized (mAdded) {
2904                    mAdded.add(f);
2905                }
2906            }
2907        }
2908
2909        // Build the back stack.
2910        if (fms.mBackStack != null) {
2911            mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
2912            for (int i=0; i<fms.mBackStack.length; i++) {
2913                BackStackRecord bse = fms.mBackStack[i].instantiate(this);
2914                if (DEBUG) {
2915                    Log.v(TAG, "restoreAllState: back stack #" + i
2916                        + " (index " + bse.mIndex + "): " + bse);
2917                    LogWriter logw = new LogWriter(Log.VERBOSE, TAG);
2918                    PrintWriter pw = new FastPrintWriter(logw, false, 1024);
2919                    bse.dump("  ", pw, false);
2920                    pw.flush();
2921                }
2922                mBackStack.add(bse);
2923                if (bse.mIndex >= 0) {
2924                    setBackStackIndex(bse.mIndex, bse);
2925                }
2926            }
2927        } else {
2928            mBackStack = null;
2929        }
2930
2931        if (fms.mPrimaryNavActiveIndex >= 0) {
2932            mPrimaryNav = mActive.get(fms.mPrimaryNavActiveIndex);
2933        }
2934
2935        mNextFragmentIndex = fms.mNextFragmentIndex;
2936    }
2937
2938    /**
2939     * To prevent list modification errors, mActive sets values to null instead of
2940     * removing them when the Fragment becomes inactive. This cleans up the list at the
2941     * end of executing the transactions.
2942     */
2943    private void burpActive() {
2944        if (mActive != null) {
2945            for (int i = mActive.size() - 1; i >= 0; i--) {
2946                if (mActive.valueAt(i) == null) {
2947                    mActive.delete(mActive.keyAt(i));
2948                }
2949            }
2950        }
2951    }
2952
2953    public void attachController(FragmentHostCallback<?> host, FragmentContainer container,
2954            Fragment parent) {
2955        if (mHost != null) throw new IllegalStateException("Already attached");
2956        mHost = host;
2957        mContainer = container;
2958        mParent = parent;
2959        mAllowOldReentrantBehavior = getTargetSdk() <= Build.VERSION_CODES.N_MR1;
2960    }
2961
2962    /**
2963     * @return the target SDK of the FragmentManager's application info. If the
2964     * FragmentManager has been torn down, then 0 is returned.
2965     */
2966    int getTargetSdk() {
2967        if (mHost != null) {
2968            Context context = mHost.getContext();
2969            if (context != null) {
2970                ApplicationInfo info = context.getApplicationInfo();
2971                if (info != null) {
2972                    return info.targetSdkVersion;
2973                }
2974            }
2975        }
2976        return 0;
2977    }
2978
2979    public void noteStateNotSaved() {
2980        mSavedNonConfig = null;
2981        mStateSaved = false;
2982        final int addedCount = mAdded.size();
2983        for (int i = 0; i < addedCount; i++) {
2984            Fragment fragment = mAdded.get(i);
2985            if (fragment != null) {
2986                fragment.noteStateNotSaved();
2987            }
2988        }
2989    }
2990
2991    public void dispatchCreate() {
2992        mStateSaved = false;
2993        dispatchMoveToState(Fragment.CREATED);
2994    }
2995
2996    public void dispatchActivityCreated() {
2997        mStateSaved = false;
2998        dispatchMoveToState(Fragment.ACTIVITY_CREATED);
2999    }
3000
3001    public void dispatchStart() {
3002        mStateSaved = false;
3003        dispatchMoveToState(Fragment.STARTED);
3004    }
3005
3006    public void dispatchResume() {
3007        mStateSaved = false;
3008        dispatchMoveToState(Fragment.RESUMED);
3009    }
3010
3011    public void dispatchPause() {
3012        dispatchMoveToState(Fragment.STARTED);
3013    }
3014
3015    public void dispatchStop() {
3016        dispatchMoveToState(Fragment.STOPPED);
3017    }
3018
3019    public void dispatchDestroyView() {
3020        dispatchMoveToState(Fragment.CREATED);
3021    }
3022
3023    public void dispatchDestroy() {
3024        mDestroyed = true;
3025        execPendingActions();
3026        dispatchMoveToState(Fragment.INITIALIZING);
3027        mHost = null;
3028        mContainer = null;
3029        mParent = null;
3030    }
3031
3032    /**
3033     * This method is called by dispatch* methods to change the FragmentManager's state.
3034     * It calls moveToState directly if the target SDK is older than O. Otherwise, it sets and
3035     * clears mExecutingActions to ensure that there is no reentrancy while the
3036     * FragmentManager is changing state.
3037     *
3038     * @param state The new state of the FragmentManager.
3039     */
3040    private void dispatchMoveToState(int state) {
3041        if (mAllowOldReentrantBehavior) {
3042            moveToState(state, false);
3043        } else {
3044            try {
3045                mExecutingActions = true;
3046                moveToState(state, false);
3047            } finally {
3048                mExecutingActions = false;
3049            }
3050        }
3051        execPendingActions();
3052    }
3053
3054    /**
3055     * @deprecated use {@link #dispatchMultiWindowModeChanged(boolean, Configuration)}
3056     */
3057    @Deprecated
3058    public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) {
3059        for (int i = mAdded.size() - 1; i >= 0; --i) {
3060            final Fragment f = mAdded.get(i);
3061            if (f != null) {
3062                f.performMultiWindowModeChanged(isInMultiWindowMode);
3063            }
3064        }
3065    }
3066
3067    public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode,
3068            Configuration newConfig) {
3069        for (int i = mAdded.size() - 1; i >= 0; --i) {
3070            final Fragment f = mAdded.get(i);
3071            if (f != null) {
3072                f.performMultiWindowModeChanged(isInMultiWindowMode, newConfig);
3073            }
3074        }
3075    }
3076
3077    /**
3078     * @deprecated use {@link #dispatchPictureInPictureModeChanged(boolean, Configuration)}
3079     */
3080    @Deprecated
3081    public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
3082        for (int i = mAdded.size() - 1; i >= 0; --i) {
3083            final Fragment f = mAdded.get(i);
3084            if (f != null) {
3085                f.performPictureInPictureModeChanged(isInPictureInPictureMode);
3086            }
3087        }
3088    }
3089
3090    public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode,
3091            Configuration newConfig) {
3092        for (int i = mAdded.size() - 1; i >= 0; --i) {
3093            final Fragment f = mAdded.get(i);
3094            if (f != null) {
3095                f.performPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
3096            }
3097        }
3098    }
3099
3100    public void dispatchConfigurationChanged(Configuration newConfig) {
3101        for (int i = 0; i < mAdded.size(); i++) {
3102            Fragment f = mAdded.get(i);
3103            if (f != null) {
3104                f.performConfigurationChanged(newConfig);
3105            }
3106        }
3107    }
3108
3109    public void dispatchLowMemory() {
3110        for (int i = 0; i < mAdded.size(); i++) {
3111            Fragment f = mAdded.get(i);
3112            if (f != null) {
3113                f.performLowMemory();
3114            }
3115        }
3116    }
3117
3118    public void dispatchTrimMemory(int level) {
3119        for (int i = 0; i < mAdded.size(); i++) {
3120            Fragment f = mAdded.get(i);
3121            if (f != null) {
3122                f.performTrimMemory(level);
3123            }
3124        }
3125    }
3126
3127    public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
3128        if (mCurState < Fragment.CREATED) {
3129            return false;
3130        }
3131        boolean show = false;
3132        ArrayList<Fragment> newMenus = null;
3133        for (int i = 0; i < mAdded.size(); i++) {
3134            Fragment f = mAdded.get(i);
3135            if (f != null) {
3136                if (f.performCreateOptionsMenu(menu, inflater)) {
3137                    show = true;
3138                    if (newMenus == null) {
3139                        newMenus = new ArrayList<Fragment>();
3140                    }
3141                    newMenus.add(f);
3142                }
3143            }
3144        }
3145
3146        if (mCreatedMenus != null) {
3147            for (int i=0; i<mCreatedMenus.size(); i++) {
3148                Fragment f = mCreatedMenus.get(i);
3149                if (newMenus == null || !newMenus.contains(f)) {
3150                    f.onDestroyOptionsMenu();
3151                }
3152            }
3153        }
3154
3155        mCreatedMenus = newMenus;
3156
3157        return show;
3158    }
3159
3160    public boolean dispatchPrepareOptionsMenu(Menu menu) {
3161        if (mCurState < Fragment.CREATED) {
3162            return false;
3163        }
3164        boolean show = false;
3165        for (int i = 0; i < mAdded.size(); i++) {
3166            Fragment f = mAdded.get(i);
3167            if (f != null) {
3168                if (f.performPrepareOptionsMenu(menu)) {
3169                    show = true;
3170                }
3171            }
3172        }
3173        return show;
3174    }
3175
3176    public boolean dispatchOptionsItemSelected(MenuItem item) {
3177        if (mCurState < Fragment.CREATED) {
3178            return false;
3179        }
3180        for (int i = 0; i < mAdded.size(); i++) {
3181            Fragment f = mAdded.get(i);
3182            if (f != null) {
3183                if (f.performOptionsItemSelected(item)) {
3184                    return true;
3185                }
3186            }
3187        }
3188        return false;
3189    }
3190
3191    public boolean dispatchContextItemSelected(MenuItem item) {
3192        if (mCurState < Fragment.CREATED) {
3193            return false;
3194        }
3195        for (int i = 0; i < mAdded.size(); i++) {
3196            Fragment f = mAdded.get(i);
3197            if (f != null) {
3198                if (f.performContextItemSelected(item)) {
3199                    return true;
3200                }
3201            }
3202        }
3203        return false;
3204    }
3205
3206    public void dispatchOptionsMenuClosed(Menu menu) {
3207        if (mCurState < Fragment.CREATED) {
3208            return;
3209        }
3210        for (int i = 0; i < mAdded.size(); i++) {
3211            Fragment f = mAdded.get(i);
3212            if (f != null) {
3213                f.performOptionsMenuClosed(menu);
3214            }
3215        }
3216    }
3217
3218    @SuppressWarnings("ReferenceEquality")
3219    public void setPrimaryNavigationFragment(Fragment f) {
3220        if (f != null && (mActive.get(f.mIndex) != f
3221                || (f.mHost != null && f.getFragmentManager() != this))) {
3222            throw new IllegalArgumentException("Fragment " + f
3223                    + " is not an active fragment of FragmentManager " + this);
3224        }
3225        mPrimaryNav = f;
3226    }
3227
3228    public Fragment getPrimaryNavigationFragment() {
3229        return mPrimaryNav;
3230    }
3231
3232    public void registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb,
3233            boolean recursive) {
3234        mLifecycleCallbacks.add(new Pair<>(cb, recursive));
3235    }
3236
3237    public void unregisterFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb) {
3238        synchronized (mLifecycleCallbacks) {
3239            for (int i = 0, N = mLifecycleCallbacks.size(); i < N; i++) {
3240                if (mLifecycleCallbacks.get(i).first == cb) {
3241                    mLifecycleCallbacks.remove(i);
3242                    break;
3243                }
3244            }
3245        }
3246    }
3247
3248    void dispatchOnFragmentPreAttached(Fragment f, Context context, boolean onlyRecursive) {
3249        if (mParent != null) {
3250            FragmentManager parentManager = mParent.getFragmentManager();
3251            if (parentManager instanceof FragmentManagerImpl) {
3252                ((FragmentManagerImpl) parentManager)
3253                        .dispatchOnFragmentPreAttached(f, context, true);
3254            }
3255        }
3256        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3257            if (!onlyRecursive || p.second) {
3258                p.first.onFragmentPreAttached(this, f, context);
3259            }
3260        }
3261    }
3262
3263    void dispatchOnFragmentAttached(Fragment f, Context context, boolean onlyRecursive) {
3264        if (mParent != null) {
3265            FragmentManager parentManager = mParent.getFragmentManager();
3266            if (parentManager instanceof FragmentManagerImpl) {
3267                ((FragmentManagerImpl) parentManager)
3268                        .dispatchOnFragmentAttached(f, context, true);
3269            }
3270        }
3271        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3272            if (!onlyRecursive || p.second) {
3273                p.first.onFragmentAttached(this, f, context);
3274            }
3275        }
3276    }
3277
3278    void dispatchOnFragmentPreCreated(Fragment f, Bundle savedInstanceState,
3279            boolean onlyRecursive) {
3280        if (mParent != null) {
3281            FragmentManager parentManager = mParent.getFragmentManager();
3282            if (parentManager instanceof FragmentManagerImpl) {
3283                ((FragmentManagerImpl) parentManager)
3284                        .dispatchOnFragmentPreCreated(f, savedInstanceState, true);
3285            }
3286        }
3287        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3288            if (!onlyRecursive || p.second) {
3289                p.first.onFragmentPreCreated(this, f, savedInstanceState);
3290            }
3291        }
3292    }
3293
3294    void dispatchOnFragmentCreated(Fragment f, Bundle savedInstanceState, boolean onlyRecursive) {
3295        if (mParent != null) {
3296            FragmentManager parentManager = mParent.getFragmentManager();
3297            if (parentManager instanceof FragmentManagerImpl) {
3298                ((FragmentManagerImpl) parentManager)
3299                        .dispatchOnFragmentCreated(f, savedInstanceState, true);
3300            }
3301        }
3302        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3303            if (!onlyRecursive || p.second) {
3304                p.first.onFragmentCreated(this, f, savedInstanceState);
3305            }
3306        }
3307    }
3308
3309    void dispatchOnFragmentActivityCreated(Fragment f, Bundle savedInstanceState,
3310            boolean onlyRecursive) {
3311        if (mParent != null) {
3312            FragmentManager parentManager = mParent.getFragmentManager();
3313            if (parentManager instanceof FragmentManagerImpl) {
3314                ((FragmentManagerImpl) parentManager)
3315                        .dispatchOnFragmentActivityCreated(f, savedInstanceState, true);
3316            }
3317        }
3318        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3319            if (!onlyRecursive || p.second) {
3320                p.first.onFragmentActivityCreated(this, f, savedInstanceState);
3321            }
3322        }
3323    }
3324
3325    void dispatchOnFragmentViewCreated(Fragment f, View v, Bundle savedInstanceState,
3326            boolean onlyRecursive) {
3327        if (mParent != null) {
3328            FragmentManager parentManager = mParent.getFragmentManager();
3329            if (parentManager instanceof FragmentManagerImpl) {
3330                ((FragmentManagerImpl) parentManager)
3331                        .dispatchOnFragmentViewCreated(f, v, savedInstanceState, true);
3332            }
3333        }
3334        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3335            if (!onlyRecursive || p.second) {
3336                p.first.onFragmentViewCreated(this, f, v, savedInstanceState);
3337            }
3338        }
3339    }
3340
3341    void dispatchOnFragmentStarted(Fragment f, boolean onlyRecursive) {
3342        if (mParent != null) {
3343            FragmentManager parentManager = mParent.getFragmentManager();
3344            if (parentManager instanceof FragmentManagerImpl) {
3345                ((FragmentManagerImpl) parentManager)
3346                        .dispatchOnFragmentStarted(f, true);
3347            }
3348        }
3349        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3350            if (!onlyRecursive || p.second) {
3351                p.first.onFragmentStarted(this, f);
3352            }
3353        }
3354    }
3355
3356    void dispatchOnFragmentResumed(Fragment f, boolean onlyRecursive) {
3357        if (mParent != null) {
3358            FragmentManager parentManager = mParent.getFragmentManager();
3359            if (parentManager instanceof FragmentManagerImpl) {
3360                ((FragmentManagerImpl) parentManager)
3361                        .dispatchOnFragmentResumed(f, true);
3362            }
3363        }
3364        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3365            if (!onlyRecursive || p.second) {
3366                p.first.onFragmentResumed(this, f);
3367            }
3368        }
3369    }
3370
3371    void dispatchOnFragmentPaused(Fragment f, boolean onlyRecursive) {
3372        if (mParent != null) {
3373            FragmentManager parentManager = mParent.getFragmentManager();
3374            if (parentManager instanceof FragmentManagerImpl) {
3375                ((FragmentManagerImpl) parentManager)
3376                        .dispatchOnFragmentPaused(f, true);
3377            }
3378        }
3379        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3380            if (!onlyRecursive || p.second) {
3381                p.first.onFragmentPaused(this, f);
3382            }
3383        }
3384    }
3385
3386    void dispatchOnFragmentStopped(Fragment f, boolean onlyRecursive) {
3387        if (mParent != null) {
3388            FragmentManager parentManager = mParent.getFragmentManager();
3389            if (parentManager instanceof FragmentManagerImpl) {
3390                ((FragmentManagerImpl) parentManager)
3391                        .dispatchOnFragmentStopped(f, true);
3392            }
3393        }
3394        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3395            if (!onlyRecursive || p.second) {
3396                p.first.onFragmentStopped(this, f);
3397            }
3398        }
3399    }
3400
3401    void dispatchOnFragmentSaveInstanceState(Fragment f, Bundle outState, boolean onlyRecursive) {
3402        if (mParent != null) {
3403            FragmentManager parentManager = mParent.getFragmentManager();
3404            if (parentManager instanceof FragmentManagerImpl) {
3405                ((FragmentManagerImpl) parentManager)
3406                        .dispatchOnFragmentSaveInstanceState(f, outState, true);
3407            }
3408        }
3409        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3410            if (!onlyRecursive || p.second) {
3411                p.first.onFragmentSaveInstanceState(this, f, outState);
3412            }
3413        }
3414    }
3415
3416    void dispatchOnFragmentViewDestroyed(Fragment f, boolean onlyRecursive) {
3417        if (mParent != null) {
3418            FragmentManager parentManager = mParent.getFragmentManager();
3419            if (parentManager instanceof FragmentManagerImpl) {
3420                ((FragmentManagerImpl) parentManager)
3421                        .dispatchOnFragmentViewDestroyed(f, true);
3422            }
3423        }
3424        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3425            if (!onlyRecursive || p.second) {
3426                p.first.onFragmentViewDestroyed(this, f);
3427            }
3428        }
3429    }
3430
3431    void dispatchOnFragmentDestroyed(Fragment f, boolean onlyRecursive) {
3432        if (mParent != null) {
3433            FragmentManager parentManager = mParent.getFragmentManager();
3434            if (parentManager instanceof FragmentManagerImpl) {
3435                ((FragmentManagerImpl) parentManager)
3436                        .dispatchOnFragmentDestroyed(f, true);
3437            }
3438        }
3439        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3440            if (!onlyRecursive || p.second) {
3441                p.first.onFragmentDestroyed(this, f);
3442            }
3443        }
3444    }
3445
3446    void dispatchOnFragmentDetached(Fragment f, boolean onlyRecursive) {
3447        if (mParent != null) {
3448            FragmentManager parentManager = mParent.getFragmentManager();
3449            if (parentManager instanceof FragmentManagerImpl) {
3450                ((FragmentManagerImpl) parentManager)
3451                        .dispatchOnFragmentDetached(f, true);
3452            }
3453        }
3454        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3455            if (!onlyRecursive || p.second) {
3456                p.first.onFragmentDetached(this, f);
3457            }
3458        }
3459    }
3460
3461    @Override
3462    public void invalidateOptionsMenu() {
3463        if (mHost != null && mCurState == Fragment.RESUMED) {
3464            mHost.onInvalidateOptionsMenu();
3465        } else {
3466            mNeedMenuInvalidate = true;
3467        }
3468    }
3469
3470    public static int reverseTransit(int transit) {
3471        int rev = 0;
3472        switch (transit) {
3473            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
3474                rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
3475                break;
3476            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
3477                rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
3478                break;
3479            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
3480                rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
3481                break;
3482        }
3483        return rev;
3484
3485    }
3486
3487    public static int transitToStyleIndex(int transit, boolean enter) {
3488        int animAttr = -1;
3489        switch (transit) {
3490            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
3491                animAttr = enter
3492                    ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation
3493                    : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation;
3494                break;
3495            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
3496                animAttr = enter
3497                    ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation
3498                    : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation;
3499                break;
3500            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
3501                animAttr = enter
3502                    ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation
3503                    : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation;
3504                break;
3505        }
3506        return animAttr;
3507    }
3508
3509    @Override
3510    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
3511        if (!"fragment".equals(name)) {
3512            return null;
3513        }
3514
3515        String fname = attrs.getAttributeValue(null, "class");
3516        TypedArray a =
3517                context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
3518        if (fname == null) {
3519            fname = a.getString(com.android.internal.R.styleable.Fragment_name);
3520        }
3521        int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID);
3522        String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
3523        a.recycle();
3524
3525        int containerId = parent != null ? parent.getId() : 0;
3526        if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
3527            throw new IllegalArgumentException(attrs.getPositionDescription()
3528                    + ": Must specify unique android:id, android:tag, or have a parent with"
3529                    + " an id for " + fname);
3530        }
3531
3532        // If we restored from a previous state, we may already have
3533        // instantiated this fragment from the state and should use
3534        // that instance instead of making a new one.
3535        Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
3536        if (fragment == null && tag != null) {
3537            fragment = findFragmentByTag(tag);
3538        }
3539        if (fragment == null && containerId != View.NO_ID) {
3540            fragment = findFragmentById(containerId);
3541        }
3542
3543        if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
3544                + Integer.toHexString(id) + " fname=" + fname
3545                + " existing=" + fragment);
3546        if (fragment == null) {
3547            fragment = mContainer.instantiate(context, fname, null);
3548            fragment.mFromLayout = true;
3549            fragment.mFragmentId = id != 0 ? id : containerId;
3550            fragment.mContainerId = containerId;
3551            fragment.mTag = tag;
3552            fragment.mInLayout = true;
3553            fragment.mFragmentManager = this;
3554            fragment.mHost = mHost;
3555            fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
3556            addFragment(fragment, true);
3557        } else if (fragment.mInLayout) {
3558            // A fragment already exists and it is not one we restored from
3559            // previous state.
3560            throw new IllegalArgumentException(attrs.getPositionDescription()
3561                    + ": Duplicate id 0x" + Integer.toHexString(id)
3562                    + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
3563                    + " with another fragment for " + fname);
3564        } else {
3565            // This fragment was retained from a previous instance; get it
3566            // going now.
3567            fragment.mInLayout = true;
3568            fragment.mHost = mHost;
3569            // If this fragment is newly instantiated (either right now, or
3570            // from last saved state), then give it the attributes to
3571            // initialize itself.
3572            if (!fragment.mRetaining) {
3573                fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
3574            }
3575        }
3576
3577        // If we haven't finished entering the CREATED state ourselves yet,
3578        // push the inflated child fragment along. This will ensureInflatedFragmentView
3579        // at the right phase of the lifecycle so that we will have mView populated
3580        // for compliant fragments below.
3581        if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
3582            moveToState(fragment, Fragment.CREATED, 0, 0, false);
3583        } else {
3584            moveToState(fragment);
3585        }
3586
3587        if (fragment.mView == null) {
3588            throw new IllegalStateException("Fragment " + fname
3589                    + " did not create a view.");
3590        }
3591        if (id != 0) {
3592            fragment.mView.setId(id);
3593        }
3594        if (fragment.mView.getTag() == null) {
3595            fragment.mView.setTag(tag);
3596        }
3597        return fragment.mView;
3598    }
3599
3600    @Override
3601    public View onCreateView(String name, Context context, AttributeSet attrs) {
3602        return null;
3603    }
3604
3605    LayoutInflater.Factory2 getLayoutInflaterFactory() {
3606        return this;
3607    }
3608
3609    /**
3610     * An add or pop transaction to be scheduled for the UI thread.
3611     */
3612    interface OpGenerator {
3613        /**
3614         * Generate transactions to add to {@code records} and whether or not the transaction is
3615         * an add or pop to {@code isRecordPop}.
3616         *
3617         * records and isRecordPop must be added equally so that each transaction in records
3618         * matches the boolean for whether or not it is a pop in isRecordPop.
3619         *
3620         * @param records A list to add transactions to.
3621         * @param isRecordPop A list to add whether or not the transactions added to records is
3622         *                    a pop transaction.
3623         * @return true if something was added or false otherwise.
3624         */
3625        boolean generateOps(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop);
3626    }
3627
3628    /**
3629     * A pop operation OpGenerator. This will be run on the UI thread and will generate the
3630     * transactions that will be popped if anything can be popped.
3631     */
3632    private class PopBackStackState implements OpGenerator {
3633        final String mName;
3634        final int mId;
3635        final int mFlags;
3636
3637        public PopBackStackState(String name, int id, int flags) {
3638            mName = name;
3639            mId = id;
3640            mFlags = flags;
3641        }
3642
3643        @Override
3644        public boolean generateOps(ArrayList<BackStackRecord> records,
3645                ArrayList<Boolean> isRecordPop) {
3646            if (mPrimaryNav != null // We have a primary nav fragment
3647                    && mId < 0 // No valid id (since they're local)
3648                    && mName == null) { // no name to pop to (since they're local)
3649                final FragmentManager childManager = mPrimaryNav.mChildFragmentManager;
3650                if (childManager != null && childManager.popBackStackImmediate()) {
3651                    // We didn't add any operations for this FragmentManager even though
3652                    // a child did do work.
3653                    return false;
3654                }
3655            }
3656            return popBackStackState(records, isRecordPop, mName, mId, mFlags);
3657        }
3658    }
3659
3660    /**
3661     * A listener for a postponed transaction. This waits until
3662     * {@link Fragment#startPostponedEnterTransition()} is called or a transaction is started
3663     * that interacts with this one, based on interactions with the fragment container.
3664     */
3665    static class StartEnterTransitionListener
3666            implements Fragment.OnStartEnterTransitionListener {
3667        private final boolean mIsBack;
3668        private final BackStackRecord mRecord;
3669        private int mNumPostponed;
3670
3671        public StartEnterTransitionListener(BackStackRecord record, boolean isBack) {
3672            mIsBack = isBack;
3673            mRecord = record;
3674        }
3675
3676        /**
3677         * Called from {@link Fragment#startPostponedEnterTransition()}, this decreases the
3678         * number of Fragments that are postponed. This may cause the transaction to schedule
3679         * to finish running and run transitions and animations.
3680         */
3681        @Override
3682        public void onStartEnterTransition() {
3683            mNumPostponed--;
3684            if (mNumPostponed != 0) {
3685                return;
3686            }
3687            mRecord.mManager.scheduleCommit();
3688        }
3689
3690        /**
3691         * Called from {@link Fragment#
3692         * setOnStartEnterTransitionListener(Fragment.OnStartEnterTransitionListener)}, this
3693         * increases the number of fragments that are postponed as part of this transaction.
3694         */
3695        @Override
3696        public void startListening() {
3697            mNumPostponed++;
3698        }
3699
3700        /**
3701         * @return true if there are no more postponed fragments as part of the transaction.
3702         */
3703        public boolean isReady() {
3704            return mNumPostponed == 0;
3705        }
3706
3707        /**
3708         * Completes the transaction and start the animations and transitions. This may skip
3709         * the transitions if this is called before all fragments have called
3710         * {@link Fragment#startPostponedEnterTransition()}.
3711         */
3712        public void completeTransaction() {
3713            final boolean canceled;
3714            canceled = mNumPostponed > 0;
3715            FragmentManagerImpl manager = mRecord.mManager;
3716            final int numAdded = manager.mAdded.size();
3717            for (int i = 0; i < numAdded; i++) {
3718                final Fragment fragment = manager.mAdded.get(i);
3719                fragment.setOnStartEnterTransitionListener(null);
3720                if (canceled && fragment.isPostponed()) {
3721                    fragment.startPostponedEnterTransition();
3722                }
3723            }
3724            mRecord.mManager.completeExecute(mRecord, mIsBack, !canceled, true);
3725        }
3726
3727        /**
3728         * Cancels this transaction instead of completing it. That means that the state isn't
3729         * changed, so the pop results in no change to the state.
3730         */
3731        public void cancelTransaction() {
3732            mRecord.mManager.completeExecute(mRecord, mIsBack, false, false);
3733        }
3734    }
3735}
3736