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