FragmentManager.java revision d15baded7e3e904ef00ccda50449c2adf8374f11
166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne/*
266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * Copyright (C) 2011 The Android Open Source Project
366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne *
466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * Licensed under the Apache License, Version 2.0 (the "License");
566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * you may not use this file except in compliance with the License.
666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * You may obtain a copy of the License at
766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne *
866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne *      http://www.apache.org/licenses/LICENSE-2.0
966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne *
1066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * Unless required by applicable law or agreed to in writing, software
1166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * distributed under the License is distributed on an "AS IS" BASIS,
1266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * See the License for the specific language governing permissions and
1466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * limitations under the License.
1566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne */
1666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
1766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhnepackage android.support.v4.app;
1866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
19c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
2066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
21f50550926a7bddc24adf822876f35812d7d8c7beRoshan Piusimport android.animation.Animator;
2266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.animation.AnimatorInflater;
23f50550926a7bddc24adf822876f35812d7d8c7beRoshan Piusimport android.animation.AnimatorListenerAdapter;
24c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport android.animation.AnimatorSet;
2566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.animation.PropertyValuesHolder;
2666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.animation.ValueAnimator;
2766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.content.Context;
28c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Piusimport android.content.res.Configuration;
29727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Piusimport android.content.res.Resources.NotFoundException;
3066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.content.res.TypedArray;
31c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport android.os.Build;
32c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport android.os.Bundle;
33c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport android.os.Looper;
34c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport android.os.Parcel;
35c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport android.os.Parcelable;
3666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.support.annotation.CallSuper;
3766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.support.annotation.IdRes;
3866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.support.annotation.NonNull;
3964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Piusimport android.support.annotation.RestrictTo;
4066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.support.annotation.StringRes;
4164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Piusimport android.support.v4.util.ArraySet;
4266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.support.v4.util.DebugUtils;
43c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Piusimport android.support.v4.util.LogWriter;
442104cc5eaf4ed7b9047ca1de460838a8ca6d0fe1Roshan Piusimport android.support.v4.util.Pair;
452104cc5eaf4ed7b9047ca1de460838a8ca6d0fe1Roshan Piusimport android.support.v4.view.ViewCompat;
4666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.util.AttributeSet;
4766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.util.Log;
4866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.util.SparseArray;
4966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.LayoutInflater;
5066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.Menu;
5166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.MenuInflater;
5266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.MenuItem;
5366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.View;
54c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Piusimport android.view.ViewGroup;
5566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.animation.AccelerateInterpolator;
5666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.animation.AlphaAnimation;
5766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.animation.Animation;
5866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.animation.Animation.AnimationListener;
5966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport android.view.animation.AnimationSet;
6003fea88ccab149c07391d38f3c406bb04ab0a3a9Roshan Piusimport android.view.animation.AnimationUtils;
61b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Piusimport android.view.animation.DecelerateInterpolator;
62b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Piusimport android.view.animation.Interpolator;
63b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Piusimport android.view.animation.ScaleAnimation;
64b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius
65b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Piusimport java.io.FileDescriptor;
66b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Piusimport java.io.PrintWriter;
67b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Piusimport java.lang.reflect.Field;
68a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wangimport java.util.ArrayList;
6966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport java.util.Arrays;
7066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport java.util.Collections;
7166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport java.util.List;
7266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhneimport java.util.concurrent.CopyOnWriteArrayList;
7364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
744e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar/**
754e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar * Static library support version of the framework's {@link android.app.FragmentManager}.
76c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius * Used to write apps that run on platforms prior to Android 3.0.  When running
77c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius * on Android 3.0 or above, this implementation is still used; it does not try
784e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar * to switch to the framework's implementation.  See the framework {@link FragmentManager}
7966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * documentation for a class overview.
80c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius *
81c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius * <p>Your activity must derive from {@link FragmentActivity} to use this. From such an activity,
8266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne * you can acquire the {@link FragmentManager} by calling
834e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar * {@link FragmentActivity#getSupportFragmentManager}.
844e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar */
854e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwarpublic abstract class FragmentManager {
864e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    /**
874e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * Representation of an entry on the fragment back stack, as created
884e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * with {@link FragmentTransaction#addToBackStack(String)
894e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * FragmentTransaction.addToBackStack()}.  Entries can later be
904e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
914e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * FragmentManager.getBackStackEntryAt()}.
924e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     *
934e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * <p>Note that you should never hold on to a BackStackEntry object;
944e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * the identifier as returned by {@link #getId} is the only thing that
954e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     * will be persisted across activity instances.
964e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar     */
974e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    public interface BackStackEntry {
984e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar        /**
994e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar         * Return the unique identifier for the entry.  This is the only
10066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * representation of the entry that will persist across activity
10166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * instances.
10266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         */
10366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        public int getId();
10466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
10566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        /**
10666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * Get the name that was supplied to
10766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * {@link FragmentTransaction#addToBackStack(String)
108c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius         * FragmentTransaction.addToBackStack(String)} when creating this entry.
109c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius         */
110c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius        public String getName();
11166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
11266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        /**
11366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * Return the full bread crumb title resource identifier for the entry,
11466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * or 0 if it does not have one.
11566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         */
11666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        @StringRes
11766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        public int getBreadCrumbTitleRes();
11866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
11966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        /**
12066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * Return the short bread crumb title resource identifier for the entry,
12166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * or 0 if it does not have one.
12266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         */
12366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        @StringRes
124f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        public int getBreadCrumbShortTitleRes();
12566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
12666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        /**
127f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius         * Return the full bread crumb title for the entry, or null if it
12866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         * does not have one.
12966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne         */
13066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        public CharSequence getBreadCrumbTitle();
131f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius
132f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        /**
133f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius         * Return the short bread crumb title for the entry, or null if it
134f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius         * does not have one.
135f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius         */
136f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        public CharSequence getBreadCrumbShortTitle();
137f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    }
138f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius
139f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    /**
140f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     * Interface to watch for changes to the back stack.
141f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     */
142f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    public interface OnBackStackChangedListener {
143f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        /**
144f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius         * Called whenever the contents of the back stack change.
145f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius         */
146f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        public void onBackStackChanged();
147f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    }
148f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius
149f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    /**
150f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     * Start a series of edit operations on the Fragments associated with
151f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     * this FragmentManager.
152f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     *
15366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * <p>Note: A fragment transaction can only be created/committed prior
15466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * to an activity saving its state.  If you try to commit a transaction
15566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * after {@link FragmentActivity#onSaveInstanceState FragmentActivity.onSaveInstanceState()}
15667bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     * (and prior to a following {@link FragmentActivity#onStart FragmentActivity.onStart}
15767bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     * or {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will get an error.
15867bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     * This is because the framework takes care of saving your current fragments
15967bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     * in the state, and if changes are made after the state is saved then they
16067bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     * will be lost.</p>
16167bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     */
16267bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius    public abstract FragmentTransaction beginTransaction();
16367bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius
16467bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius    /**
16567bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     * @hide -- remove once prebuilts are in.
16667bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     * @deprecated
16767bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius     */
16867bbcdc2c81824ff671a5a1d8475a598199e342eRoshan Pius    @RestrictTo(LIBRARY_GROUP)
16966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Deprecated
17066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public FragmentTransaction openTransaction() {
17166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return beginTransaction();
17266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
17366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
17466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
17566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * After a {@link FragmentTransaction} is committed with
17666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
17766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * is scheduled to be executed asynchronously on the process's main thread.
17866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * If you want to immediately executing any such pending operations, you
17964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * can call this function (only from the main thread) to do so.  Note that
18064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * all callbacks and other related behavior will be done from within this
18164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * call, so be careful about where this is called from.
18264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
18364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * <p>If you are committing a single transaction that does not modify the
18464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * fragment back stack, strongly consider using
18564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * {@link FragmentTransaction#commitNow()} instead. This can help avoid
18664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * unwanted side effects when other code in your app has pending committed
18764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * transactions that expect different timing.</p>
18864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * <p>
18964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * This also forces the start of any postponed Transactions where
19064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * {@link Fragment#postponeEnterTransition()} has been called.
19164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
19264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * @return Returns true if there were any pending transactions to be
19364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * executed.
19464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
19564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    public abstract boolean executePendingTransactions();
19664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
19764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
19864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Finds a fragment that was identified by the given id either when inflated
19964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * from XML or as the container ID when added in a transaction.  This first
20064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * searches through fragments that are currently added to the manager's
20164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * activity; if no such fragment is found, then all fragments currently
20264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * on the back stack associated with this ID are searched.
20364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * @return The fragment if found or null otherwise.
20464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
20564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    public abstract Fragment findFragmentById(@IdRes int id);
20664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
20764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
20864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Finds a fragment that was identified by the given tag either when inflated
20964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * from XML or as supplied when added in a transaction.  This first
21064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * searches through fragments that are currently added to the manager's
21164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * activity; if no such fragment is found, then all fragments currently
21264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * on the back stack are searched.
213cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     * @return The fragment if found or null otherwise.
214cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     */
215cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius    public abstract Fragment findFragmentByTag(String tag);
216cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius
217cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius    /**
218cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     * Flag for {@link #popBackStack(String, int)}
219cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     * and {@link #popBackStack(int, int)}: If set, and the name or ID of
220cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     * a back stack entry has been supplied, then all matching entries will
221cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     * be consumed until one that doesn't match is found or the bottom of
222cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     * the stack is reached.  Otherwise, all entries up to but not including that entry
223cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3Roshan Pius     * will be removed.
224727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     */
225727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius    public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
226727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius
227727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius    /**
228727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * Pop the top state off the back stack.  Returns true if there was one
229727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * to pop, else false.  This function is asynchronous -- it enqueues the
230727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * request to pop, but the action will not be performed until the application
231727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * returns to its event loop.
232727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     */
233727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius    public abstract void popBackStack();
234727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius
235727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius    /**
236727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * Like {@link #popBackStack()}, but performs the operation immediately
237727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * inside of the call.  This is like calling {@link #executePendingTransactions()}
238727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * afterwards without forcing the start of postponed Transactions.
239727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius     * @return Returns true if there was something popped, else false.
24066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
24166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract boolean popBackStackImmediate();
24266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
24366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
24466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Pop the last fragment transition from the manager's fragment
24566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * back stack.  If there is nothing to pop, false is returned.
24666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * This function is asynchronous -- it enqueues the
24766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * request to pop, but the action will not be performed until the application
24866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * returns to its event loop.
24966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     *
25066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param name If non-null, this is the name of a previous back state
25166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * to look for; if found, all states up to that state will be popped.  The
25266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
25366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * the named state itself is popped. If null, only the top state is popped.
25466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
25566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
25666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract void popBackStack(String name, int flags);
25766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
25866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
25966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Like {@link #popBackStack(String, int)}, but performs the operation immediately
26066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * inside of the call.  This is like calling {@link #executePendingTransactions()}
26166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * afterwards without forcing the start of postponed Transactions.
26266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @return Returns true if there was something popped, else false.
26366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
26466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract boolean popBackStackImmediate(String name, int flags);
26566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
26666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
26766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Pop all back stack states up to the one with the given identifier.
26866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * This function is asynchronous -- it enqueues the
26966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * request to pop, but the action will not be performed until the application
27066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * returns to its event loop.
27166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     *
27266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param id Identifier of the stated to be popped. If no identifier exists,
27366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * false is returned.
27466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * The identifier is the number returned by
27566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * {@link FragmentTransaction#commit() FragmentTransaction.commit()}.  The
27666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
27766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * the named state itself is popped.
27866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
27966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
28066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract void popBackStack(int id, int flags);
28166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
28266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
28366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Like {@link #popBackStack(int, int)}, but performs the operation immediately
28466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * inside of the call.  This is like calling {@link #executePendingTransactions()}
28566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * afterwards without forcing the start of postponed Transactions.
28666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @return Returns true if there was something popped, else false.
28766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
28866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract boolean popBackStackImmediate(int id, int flags);
28966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
29066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
29166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Return the number of entries currently in the back stack.
29266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
29366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract int getBackStackEntryCount();
29466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
29566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
29666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Return the BackStackEntry at index <var>index</var> in the back stack;
29766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * entries start index 0 being the bottom of the stack.
29866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
29966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract BackStackEntry getBackStackEntryAt(int index);
30066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
30166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
30266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Add a new listener for changes to the fragment back stack.
30366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
30466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
30566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
30666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
30766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Remove a listener that was previously added with
30866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
30966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
31066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
31166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
31266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
31366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Put a reference to a fragment in a Bundle.  This Bundle can be
31466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * persisted as saved state, and when later restoring
31566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * {@link #getFragment(Bundle, String)} will return the current
31666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * instance of the same fragment.
31766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     *
31866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param bundle The bundle in which to put the fragment reference.
31966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param key The name of the entry in the bundle.
32066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param fragment The Fragment whose reference is to be stored.
32166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
32266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
32366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
32466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
32566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Retrieve the current Fragment instance for a reference previously
32666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * placed with {@link #putFragment(Bundle, String, Fragment)}.
32764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
32866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param bundle The bundle from which to retrieve the fragment reference.
32966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @param key The name of the entry in the bundle.
33066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @return Returns the current Fragment instance that is associated with
33166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * the given reference.
33266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
33366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract Fragment getFragment(Bundle bundle, String key);
33466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
33566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
33666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * Get a list of all fragments that are currently added to the FragmentManager.
33766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * This may include those that are hidden as well as those that are shown.
33866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * This will not include any fragments only in the back stack, or fragments that
33966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * are detached or removed.
34066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * <p>
34166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * The order of the fragments in the list is the order in which they were
34266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * added or attached.
34366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     *
34466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * @return A list of all fragments that are added to the FragmentManager.
34566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
34666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public abstract List<Fragment> getFragments();
34766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
34866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
34964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Save the current instance state of the given Fragment.  This can be
35064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * used later when creating a new instance of the Fragment and adding
35164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * it to the fragment manager, to have it create itself to match the
35264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * current state returned here.  Note that there are limits on how
35364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * this can be used:
35464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
35564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * <ul>
35664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * <li>The Fragment must currently be attached to the FragmentManager.
35764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * <li>A new Fragment created using this saved state must be the same class
35864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * type as the Fragment it was created from.
35964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * <li>The saved state can not contain dependencies on other fragments --
36064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
36164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * store a fragment reference because that reference may not be valid when
36264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * this saved state is later used.  Likewise the Fragment's target and
36364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * result code are not included in this state.
36464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * </ul>
36564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
36664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * @param f The Fragment whose state is to be saved.
36764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * @return The generated state.  This will be null if there was no
36864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * interesting state created by the fragment.
36964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
37064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
37164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
37264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
37364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Returns true if the final {@link android.app.Activity#onDestroy() Activity.onDestroy()}
37464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * call has been made on the FragmentManager's Activity, so this instance is now dead.
37564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
37664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    public abstract boolean isDestroyed();
37764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
37864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
37964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Registers a {@link FragmentLifecycleCallbacks} to listen to fragment lifecycle events
38064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * happening in this FragmentManager. All registered callbacks will be automatically
38164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * unregistered when this FragmentManager is destroyed.
38264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
38364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * @param cb Callbacks to register
38464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * @param recursive true to automatically register this callback for all child FragmentManagers
38564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
38664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    public abstract void registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb,
38764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            boolean recursive);
38864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
38964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
39064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Unregisters a previously registered {@link FragmentLifecycleCallbacks}. If the callback
39164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * was not previously registered this call has no effect. All registered callbacks will be
39264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * automatically unregistered when this FragmentManager is destroyed.
39364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
39464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * @param cb Callbacks to unregister
39564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
39664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    public abstract void unregisterFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb);
39764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
39864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
39964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Return the currently active primary navigation fragment for this FragmentManager.
40064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * The primary navigation fragment is set by fragment transactions using
40164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * {@link FragmentTransaction#setPrimaryNavigationFragment(Fragment)}.
40264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     *
403445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * <p>The primary navigation fragment's
404445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * {@link Fragment#getChildFragmentManager() child FragmentManager} will be called first
405445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * to process delegated navigation actions such as {@link #popBackStack()} if no ID
406445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * or transaction name is provided to pop to.</p>
407445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     *
408445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * @return the fragment designated as the primary navigation fragment
409445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     */
410445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public abstract Fragment getPrimaryNavigationFragment();
411dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius
412dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius    /**
413dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius     * Print the FragmentManager's state into the given stream.
414dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius     *
415445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * @param prefix Text to print at the front of each line.
416445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * @param fd The raw file descriptor that the dump is being sent to.
417445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * @param writer A PrintWriter to which the dump is to be set.
418445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * @param args Additional arguments to the dump request.
419445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     */
420445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
421445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
422445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    /**
423445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * Control whether the framework's internal fragment manager debugging
424445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * logs are turned on.  If enabled, you will see output in logcat as
425445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * the framework performs fragment operations.
426445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     */
427445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public static void enableDebugLogging(boolean enabled) {
428445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        FragmentManagerImpl.DEBUG = enabled;
429445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    }
430445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
431445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    /**
432445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * Returns {@code true} if the FragmentManager's state has already been saved
433445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * by its host. Any operations that would change saved state should not be performed
434445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * if this method returns true. For example, any popBackStack() method, such as
435445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * {@link #popBackStackImmediate()} or any FragmentTransaction using
436445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * {@link FragmentTransaction#commit()} instead of
437445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * {@link FragmentTransaction#commitAllowingStateLoss()} will change
438445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * the state and will result in an error.
439445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     *
440445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     * @return true if this FragmentManager's state has already been saved by its host
441445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius     */
442445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public abstract boolean isStateSaved();
443445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
444dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius    /**
445dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius     * Callback interface for listening to fragment state changes that happen
446dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius     * within a given FragmentManager.
447dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius     */
448dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius    public abstract static class FragmentLifecycleCallbacks {
449dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius        /**
450445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called right before the fragment's {@link Fragment#onAttach(Context)} method is called.
451445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * This is a good time to inject any required dependencies or perform other configuration
452445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * for the fragment before any of the fragment's lifecycle methods are invoked.
453445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
454445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
455445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
456445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param context Context that the Fragment is being attached to
457445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
458445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentPreAttached(FragmentManager fm, Fragment f, Context context) {}
459445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
460445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
461445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has been attached to its host. Its host will have had
462445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * <code>onAttachFragment</code> called before this call happens.
463445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
464445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
465445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
466445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param context Context that the Fragment was attached to
467445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
468445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {}
469445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
470445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
471445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called right before the fragment's {@link Fragment#onCreate(Bundle)} method is called.
472445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * This is a good time to inject any required dependencies or perform other configuration
473445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * for the fragment.
474445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
475445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
476445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
477445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param savedInstanceState Saved instance bundle from a previous instance
478445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
479dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius        public void onFragmentPreCreated(FragmentManager fm, Fragment f,
480dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius                Bundle savedInstanceState) {}
481dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius
482dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius        /**
483445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
484445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onCreate(Bundle)}. This will only happen once for any given
485445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * fragment instance, though the fragment may be attached and detached multiple times.
486445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
487445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
488445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
489445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param savedInstanceState Saved instance bundle from a previous instance
490445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
491445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {}
492445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
493445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
494445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
495445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onActivityCreated(Bundle)}. This will only happen once for any given
496445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * fragment instance, though the fragment may be attached and detached multiple times.
497445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
498445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
499445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
500445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param savedInstanceState Saved instance bundle from a previous instance
501445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
502445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentActivityCreated(FragmentManager fm, Fragment f,
503dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius                Bundle savedInstanceState) {}
504dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius
505445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
506445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned a non-null view from the FragmentManager's
507445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * request to {@link Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)}.
508445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
509445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
510445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment that created and owns the view
511445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param v View returned by the fragment
512445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param savedInstanceState Saved instance bundle from a previous instance
513445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
514445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v,
515445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius                Bundle savedInstanceState) {}
516445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
517445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
518445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
519445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onStart()}.
520445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
521445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
522445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
523445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
524445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentStarted(FragmentManager fm, Fragment f) {}
525445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
526445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
527dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius         * Called after the fragment has returned from the FragmentManager's call to
528dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius         * {@link Fragment#onResume()}.
529dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius         *
530445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
531445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
532445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
533445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentResumed(FragmentManager fm, Fragment f) {}
534445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
535445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
536445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
537445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onPause()}.
538445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
539445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
540445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
541445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
542445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentPaused(FragmentManager fm, Fragment f) {}
543445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
544445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
545445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
546445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onStop()}.
547445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
548445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
549445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
550445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
551445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentStopped(FragmentManager fm, Fragment f) {}
552445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
553445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
554445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
555445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onSaveInstanceState(Bundle)}.
556445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
557445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
558445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
559445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param outState Saved state bundle for the fragment
560dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius         */
561dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius        public void onFragmentSaveInstanceState(FragmentManager fm, Fragment f, Bundle outState) {}
562dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius
563445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
564445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
565445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onDestroyView()}.
566445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
567445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
568445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
569445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
570445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {}
571445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
572445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
573445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
574445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * {@link Fragment#onDestroy()}.
575445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
576445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
577445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
578445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
579445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentDestroyed(FragmentManager fm, Fragment f) {}
580445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
581445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        /**
582445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * Called after the fragment has returned from the FragmentManager's call to
5832104cc5eaf4ed7b9047ca1de460838a8ca6d0fe1Roshan Pius         * {@link Fragment#onDetach()}.
584445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         *
585445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param fm Host FragmentManager
586445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         * @param f Fragment changing state
587445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius         */
588445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public void onFragmentDetached(FragmentManager fm, Fragment f) {}
589445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    }
590445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius}
591445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
592445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Piusfinal class FragmentManagerState implements Parcelable {
593445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    FragmentState[] mActive;
594445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    int[] mAdded;
595445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    BackStackState[] mBackStack;
596445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    int mPrimaryNavActiveIndex = -1;
597445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    int mNextFragmentIndex;
598445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
599445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public FragmentManagerState() {
600445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    }
601445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
6022104cc5eaf4ed7b9047ca1de460838a8ca6d0fe1Roshan Pius    public FragmentManagerState(Parcel in) {
603445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        mActive = in.createTypedArray(FragmentState.CREATOR);
604445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        mAdded = in.createIntArray();
605445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        mBackStack = in.createTypedArray(BackStackState.CREATOR);
606445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        mPrimaryNavActiveIndex = in.readInt();
607445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        mNextFragmentIndex = in.readInt();
608445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    }
609445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
610445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    @Override
611445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public int describeContents() {
612445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        return 0;
613445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    }
614445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
615445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    @Override
616445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public void writeToParcel(Parcel dest, int flags) {
617445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        dest.writeTypedArray(mActive, flags);
618445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        dest.writeIntArray(mAdded);
619445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        dest.writeTypedArray(mBackStack, flags);
620445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        dest.writeInt(mPrimaryNavActiveIndex);
6214e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar        dest.writeInt(mNextFragmentIndex);
622445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    }
623445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
624445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    public static final Parcelable.Creator<FragmentManagerState> CREATOR
6252104cc5eaf4ed7b9047ca1de460838a8ca6d0fe1Roshan Pius            = new Parcelable.Creator<FragmentManagerState>() {
626445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        @Override
627445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        public FragmentManagerState createFromParcel(Parcel in) {
628445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius            return new FragmentManagerState(in);
629445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius        }
6304e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar
6314e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar        @Override
6324e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar        public FragmentManagerState[] newArray(int size) {
6334e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar            return new FragmentManagerState[size];
6344e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar        }
6354e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    };
6364e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar}
6374e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar
6384e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar/**
6394e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar * Container for fragments associated with an activity.
6404e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar */
6414e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwarfinal class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 {
6424e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    static boolean DEBUG = false;
6434e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    static final String TAG = "FragmentManager";
6444e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar
6454e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
6464e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    static final String TARGET_STATE_TAG = "android:target_state";
6474e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    static final String VIEW_STATE_TAG = "android:view_state";
6484e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
6494e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar
6504e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar    ArrayList<OpGenerator> mPendingActions;
651445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius    boolean mExecutingActions;
652445b5f46264af0a2e9c043c9badb2b4f38044f11Roshan Pius
653c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    int mNextFragmentIndex = 0;
654c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
655c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<Fragment> mAdded;
656c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    SparseArray<Fragment> mActive;
657c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<BackStackRecord> mBackStack;
658c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<Fragment> mCreatedMenus;
659c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
660c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    // Must be accessed while locked.
661c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<BackStackRecord> mBackStackIndices;
662c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<Integer> mAvailBackStackIndices;
663c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
664c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
665c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    private CopyOnWriteArrayList<Pair<FragmentLifecycleCallbacks, Boolean>> mLifecycleCallbacks;
666c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
667c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    int mCurState = Fragment.INITIALIZING;
668c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    FragmentHostCallback mHost;
669c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    FragmentContainer mContainer;
670c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    Fragment mParent;
671c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    Fragment mPrimaryNav;
672c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
673c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    static Field sAnimationListenerField = null;
674c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
675c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    boolean mNeedMenuInvalidate;
676c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    boolean mStateSaved;
677c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    boolean mDestroyed;
678c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    String mNoTransactionsBecause;
679c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    boolean mHavePendingDeferredStart;
680c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
681c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    // Temporary vars for removing redundant operations in BackStackRecords:
682c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<BackStackRecord> mTmpRecords;
683c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<Boolean> mTmpIsPop;
684c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<Fragment> mTmpAddedFragments;
685c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
686c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    // Temporary vars for state save and restore.
687c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    Bundle mStateBundle = null;
688c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    SparseArray<Parcelable> mStateArray = null;
689c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
690c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    // Postponed transactions.
691c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    ArrayList<StartEnterTransitionListener> mPostponedTransactions;
692c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
693c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    // Saved FragmentManagerNonConfig during saveAllState() and cleared in noteStateNotSaved()
694c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    FragmentManagerNonConfig mSavedNonConfig;
695c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius
696c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius    Runnable mExecCommit = new Runnable() {
697c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        @Override
698c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        public void run() {
699c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            execPendingActions();
700c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        }
701c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    };
702c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
703c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    static boolean modifiesAlpha(AnimationOrAnimator anim) {
704c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        if (anim.animation instanceof AlphaAnimation) {
705c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            return true;
706c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        } else if (anim.animation instanceof AnimationSet) {
707c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            List<Animation> anims = ((AnimationSet) anim.animation).getAnimations();
708c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            for (int i = 0; i < anims.size(); i++) {
709c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                if (anims.get(i) instanceof AlphaAnimation) {
710c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                    return true;
711c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                }
712c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            }
713c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            return false;
714c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        } else {
715c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            return modifiesAlpha(anim.animator);
716c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        }
717c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
718c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
719c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    static boolean modifiesAlpha(Animator anim) {
720c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        if (anim == null) {
721c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            return false;
722c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        }
723c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        if (anim instanceof ValueAnimator) {
724c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            ValueAnimator valueAnim = (ValueAnimator) anim;
725c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            PropertyValuesHolder[] values = valueAnim.getValues();
726c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            for (int i = 0; i < values.length; i++) {
727c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                if (("alpha").equals(values[i].getPropertyName())) {
728c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                    return true;
729c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                }
730c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            }
731c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        } else if (anim instanceof AnimatorSet) {
732c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            List<Animator> animList = ((AnimatorSet) anim).getChildAnimations();
733c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            for (int i = 0; i < animList.size(); i++) {
734c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                if (modifiesAlpha(animList.get(i))) {
735c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                    return true;
736c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                }
737c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            }
738c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        }
739c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        return false;
740c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
741c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
742c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    static boolean shouldRunOnHWLayer(View v, AnimationOrAnimator anim) {
743c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        if (v == null || anim == null) {
744c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            return false;
745c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        }
746c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        return Build.VERSION.SDK_INT >= 19
747c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                && v.getLayerType() == View.LAYER_TYPE_NONE
748c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                && ViewCompat.hasOverlappingRendering(v)
749c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                && modifiesAlpha(anim);
750c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
751c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
752c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    private void throwException(RuntimeException ex) {
753c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        Log.e(TAG, ex.getMessage());
754c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        Log.e(TAG, "Activity state:");
755c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        LogWriter logw = new LogWriter(TAG);
756c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        PrintWriter pw = new PrintWriter(logw);
757c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        if (mHost != null) {
758c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            try {
759c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                mHost.onDump("  ", null, pw, new String[] { });
760c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            } catch (Exception e) {
761c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                Log.e(TAG, "Failed dumping state", e);
762c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            }
763c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        } else {
764c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            try {
765c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                dump("  ", null, pw, new String[] { });
766c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            } catch (Exception e) {
767c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                Log.e(TAG, "Failed dumping state", e);
768c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            }
769c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        }
770c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        throw ex;
771c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
772c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
773c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    @Override
774c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    public FragmentTransaction beginTransaction() {
775c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        return new BackStackRecord(this);
776c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
777dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius
778dd685b34596b790c76e2cc03fc825c1249c9174fRoshan Pius    @Override
779c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    public boolean executePendingTransactions() {
780c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        boolean updates = execPendingActions();
781c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        forcePostponedTransactions();
782c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        return updates;
783c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
784c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
785c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    @Override
786c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    public void popBackStack() {
787c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        enqueueAction(new PopBackStackState(null, -1, 0), false);
788c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
789c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
790c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    @Override
791c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    public boolean popBackStackImmediate() {
792c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        checkStateLoss();
793c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        return popBackStackImmediate(null, -1, 0);
794c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
795c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
796c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    @Override
797c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    public void popBackStack(final String name, final int flags) {
798c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius        enqueueAction(new PopBackStackState(name, -1, flags), false);
799c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    }
800c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius
801c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius    @Override
80266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public boolean popBackStackImmediate(String name, int flags) {
80366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        checkStateLoss();
80466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return popBackStackImmediate(name, -1, flags);
80566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
80666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
80766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
80866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public void popBackStack(final int id, final int flags) {
80966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (id < 0) {
81066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            throw new IllegalArgumentException("Bad id: " + id);
81166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
81266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        enqueueAction(new PopBackStackState(null, id, flags), false);
81366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
81466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
81564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    @Override
81666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public boolean popBackStackImmediate(int id, int flags) {
81766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        checkStateLoss();
81866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        execPendingActions();
81964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (id < 0) {
82064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            throw new IllegalArgumentException("Bad id: " + id);
82164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
82264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        return popBackStackImmediate(null, id, flags);
82364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
82464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
82564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
82664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Used by all public popBackStackImmediate methods, this executes pending transactions and
82766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * returns true if the pop action did anything, regardless of what other pending
82866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * transactions did.
82966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     *
830b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius     * @return true if the pop operation did anything or false otherwise.
831b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius     */
832b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius    private boolean popBackStackImmediate(String name, int id, int flags) {
833b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius        execPendingActions();
834b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius        ensureExecReady(true);
835b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius
836b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius        if (mPrimaryNav != null // We have a primary nav fragment
837b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius                && id < 0 // No valid id (since they're local)
838b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius                && name == null) { // no name to pop to (since they're local)
839b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius            final FragmentManager childManager = mPrimaryNav.peekChildFragmentManager();
840b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius            if (childManager != null && childManager.popBackStackImmediate()) {
841b8b3fb8228a1f90106bad8c59ce006b81ef7921cRoshan Pius                // We did something, just not to this specific FragmentManager. Return true.
842a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang                return true;
843a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang            }
844a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang        }
845a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang
846a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang        boolean executePop = popBackStackState(mTmpRecords, mTmpIsPop, name, id, flags);
847a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang        if (executePop) {
848a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang            mExecutingActions = true;
849a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang            try {
850a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
851a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang            } finally {
852a1da73ea4926ce8a5689594ff3685b0fe033d99fNingyuan Wang                cleanupExec();
85366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
85466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
85566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
85666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        doPendingDeferredStart();
85766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        burpActive();
85866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return executePop;
85966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
86066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
86166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
86266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public int getBackStackEntryCount() {
86366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return mBackStack != null ? mBackStack.size() : 0;
86466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
86566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
86666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
86766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public BackStackEntry getBackStackEntryAt(int index) {
86866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return mBackStack.get(index);
86966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
87066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
871727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius    @Override
872727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius    public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
873727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius        if (mBackStackChangeListeners == null) {
874727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius            mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
875727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius        }
876727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius        mBackStackChangeListeners.add(listener);
877727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius    }
87866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
87966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
88066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
88166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mBackStackChangeListeners != null) {
88266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            mBackStackChangeListeners.remove(listener);
88366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
88466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
88566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
88666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
88766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public void putFragment(Bundle bundle, String key, Fragment fragment) {
88866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (fragment.mIndex < 0) {
88966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            throwException(new IllegalStateException("Fragment " + fragment
89066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    + " is not currently in the FragmentManager"));
89166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
89266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        bundle.putInt(key, fragment.mIndex);
89366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
89466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
89566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
89666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public Fragment getFragment(Bundle bundle, String key) {
89766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        int index = bundle.getInt(key, -1);
89866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (index == -1) {
89966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            return null;
90066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
90166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        Fragment f = mActive.get(index);
90266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (f == null) {
90366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            throwException(new IllegalStateException("Fragment no longer exists for key "
90466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    + key + ": index " + index));
90566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
90666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return f;
90766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
90866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
90966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
91066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public List<Fragment> getFragments() {
91166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mAdded == null) {
91266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            return Collections.EMPTY_LIST;
91366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
91466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        synchronized (mAdded) {
91566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            return (List<Fragment>) mAdded.clone();
91666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
91766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
91866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
91966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    /**
92066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * This is used by FragmentController to get the Active fragments.
92166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     *
922f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     * @return A list of active fragments in the fragment manager, including those that are in the
92366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     * back stack.
92466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne     */
92566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    List<Fragment> getActiveFragments() {
92666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mActive == null) {
92766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            return null;
92866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
92966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        final int count = mActive.size();
93066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        ArrayList<Fragment> fragments = new ArrayList<>(count);
93166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        for (int i = 0; i < count; i++) {
93266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            fragments.add(mActive.valueAt(i));
93366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
93466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return fragments;
93566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
93666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
937f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    /**
938f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     * Used by FragmentController to get the number of Active Fragments.
939f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     *
940f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     * @return The number of active fragments.
941f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius     */
942f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    int getActiveFragmentCount() {
943f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        if (mActive == null) {
944f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius            return 0;
945f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        }
946f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        return mActive.size();
947f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    }
948f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius
949f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    @Override
950f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius    public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
951f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius        if (fragment.mIndex < 0) {
95266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            throwException( new IllegalStateException("Fragment " + fragment
95366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    + " is not currently in the FragmentManager"));
95466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
95566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (fragment.mState > Fragment.INITIALIZING) {
95666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            Bundle result = saveFragmentBasicState(fragment);
95766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            return result != null ? new Fragment.SavedState(result) : null;
95866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
95966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return null;
96066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
96166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
96266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
96366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public boolean isDestroyed() {
96466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return mDestroyed;
96566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
96666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
96766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
96866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public String toString() {
96966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        StringBuilder sb = new StringBuilder(128);
97066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        sb.append("FragmentManager{");
97166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        sb.append(Integer.toHexString(System.identityHashCode(this)));
97266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        sb.append(" in ");
97366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mParent != null) {
97466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            DebugUtils.buildShortClassTag(mParent, sb);
97566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        } else {
97666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            DebugUtils.buildShortClassTag(mHost, sb);
97766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
97866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        sb.append("}}");
97966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        return sb.toString();
98066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    }
98166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
98266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    @Override
98366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
98466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        String innerPrefix = prefix + "    ";
98566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
98666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        int N;
98766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mActive != null) {
98866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            N = mActive.size();
98966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            if (N > 0) {
99066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                writer.print(prefix); writer.print("Active Fragments in ");
99166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        writer.print(Integer.toHexString(System.identityHashCode(this)));
99266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        writer.println(":");
99366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                for (int i=0; i<N; i++) {
99466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    Fragment f = mActive.valueAt(i);
99566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    writer.print(prefix); writer.print("  #"); writer.print(i);
99666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            writer.print(": "); writer.println(f);
99766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    if (f != null) {
99866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        f.dump(innerPrefix, fd, writer, args);
99966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    }
100066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                }
100166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
100266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
100366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
100466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mAdded != null) {
100566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            N = mAdded.size();
100666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            if (N > 0) {
100766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                writer.print(prefix); writer.println("Added Fragments:");
100866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                for (int i=0; i<N; i++) {
100966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    Fragment f = mAdded.get(i);
101066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    writer.print(prefix); writer.print("  #"); writer.print(i);
101166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            writer.print(": "); writer.println(f.toString());
101266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                }
101366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
101466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
101566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
101666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mCreatedMenus != null) {
101766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            N = mCreatedMenus.size();
101866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            if (N > 0) {
101966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                writer.print(prefix); writer.println("Fragments Created Menus:");
102066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                for (int i=0; i<N; i++) {
102166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    Fragment f = mCreatedMenus.get(i);
102266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    writer.print(prefix); writer.print("  #"); writer.print(i);
102366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            writer.print(": "); writer.println(f.toString());
102466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                }
102566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
102666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
102766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
102866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mBackStack != null) {
102966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            N = mBackStack.size();
103066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            if (N > 0) {
103166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                writer.print(prefix); writer.println("Back Stack:");
103266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                for (int i=0; i<N; i++) {
103366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    BackStackRecord bs = mBackStack.get(i);
103466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    writer.print(prefix); writer.print("  #"); writer.print(i);
103566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            writer.print(": "); writer.println(bs.toString());
103666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    bs.dump(innerPrefix, fd, writer, args);
103766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                }
103866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
103966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
104066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
104166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        synchronized (this) {
104266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            if (mBackStackIndices != null) {
104366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                N = mBackStackIndices.size();
104466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                if (N > 0) {
104566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    writer.print(prefix); writer.println("Back Stack Indices:");
104666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    for (int i=0; i<N; i++) {
104766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        BackStackRecord bs = mBackStackIndices.get(i);
104866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        writer.print(prefix); writer.print("  #"); writer.print(i);
104966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                                writer.print(": "); writer.println(bs);
105066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    }
105166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                }
105266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
105366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
105466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
105566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                writer.print(prefix); writer.print("mAvailBackStackIndices: ");
105666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
105766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
105866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
105966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
106066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        if (mPendingActions != null) {
106166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            N = mPendingActions.size();
106266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            if (N > 0) {
106366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                writer.print(prefix); writer.println("Pending Actions:");
106466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                for (int i=0; i<N; i++) {
106566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    OpGenerator r = mPendingActions.get(i);
106666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    writer.print(prefix); writer.print("  #"); writer.print(i);
106766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            writer.print(": "); writer.println(r);
106866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                }
106966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
107066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        }
107166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
107266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        writer.print(prefix); writer.println("FragmentManager misc state:");
107366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne        writer.print(prefix); writer.print("  mHost="); writer.println(mHost);
107464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        writer.print(prefix); writer.print("  mContainer="); writer.println(mContainer);
107564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (mParent != null) {
107664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            writer.print(prefix); writer.print("  mParent="); writer.println(mParent);
107764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
107864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        writer.print(prefix); writer.print("  mCurState="); writer.print(mCurState);
107964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                writer.print(" mStateSaved="); writer.print(mStateSaved);
108064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                writer.print(" mDestroyed="); writer.println(mDestroyed);
108164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (mNeedMenuInvalidate) {
108264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            writer.print(prefix); writer.print("  mNeedMenuInvalidate=");
108364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    writer.println(mNeedMenuInvalidate);
108464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
108564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (mNoTransactionsBecause != null) {
108664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            writer.print(prefix); writer.print("  mNoTransactionsBecause=");
108764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    writer.println(mNoTransactionsBecause);
108864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
108964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
109064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
109164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
109264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    static final Interpolator DECELERATE_CUBIC = new DecelerateInterpolator(1.5f);
109364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    static final Interpolator ACCELERATE_QUINT = new AccelerateInterpolator(2.5f);
109464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    static final Interpolator ACCELERATE_CUBIC = new AccelerateInterpolator(1.5f);
109564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
109664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    static final int ANIM_DUR = 220;
109764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
109864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    static AnimationOrAnimator makeOpenCloseAnimation(Context context, float startScale,
109964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            float endScale, float startAlpha, float endAlpha) {
110064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        AnimationSet set = new AnimationSet(false);
110164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale,
110264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
110364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        scale.setInterpolator(DECELERATE_QUINT);
110464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        scale.setDuration(ANIM_DUR);
110564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        set.addAnimation(scale);
110664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha);
110764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        alpha.setInterpolator(DECELERATE_CUBIC);
110864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        alpha.setDuration(ANIM_DUR);
110964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        set.addAnimation(alpha);
111064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        return new AnimationOrAnimator(set);
111164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
111264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
111364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    static AnimationOrAnimator makeFadeAnimation(Context context, float start, float end) {
111464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        AlphaAnimation anim = new AlphaAnimation(start, end);
111564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        anim.setInterpolator(DECELERATE_CUBIC);
111664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        anim.setDuration(ANIM_DUR);
111764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        return new AnimationOrAnimator(anim);
111864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
111964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
112064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    AnimationOrAnimator loadAnimation(Fragment fragment, int transit, boolean enter,
112164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            int transitionStyle) {
112264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        int nextAnim = fragment.getNextAnim();
112364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        Animation animation = fragment.onCreateAnimation(transit, enter, nextAnim);
112464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (animation != null) {
112564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            return new AnimationOrAnimator(animation);
112664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
112764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
112864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        Animator animator = fragment.onCreateAnimator(transit, enter, nextAnim);
112964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (animator != null) {
113064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            return new AnimationOrAnimator(animator);
113164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
113264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
113364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (nextAnim != 0) {
113464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            String dir = mHost.getContext().getResources().getResourceTypeName(nextAnim);
113564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            boolean isAnim = "anim".equals(dir);
113664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            boolean successfulLoad = false;
113764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            if (isAnim) {
113864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                // try AnimationUtils first
113964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                try {
114064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    animation = AnimationUtils.loadAnimation(mHost.getContext(), nextAnim);
114164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    if (animation != null) {
114264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        return new AnimationOrAnimator(animation);
114364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    }
114464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    // A null animation may be returned and that is acceptable
114564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    successfulLoad = true; // succeeded in loading animation, but it is null
114664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                } catch (NotFoundException e) {
114764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    throw e; // Rethrow it -- the resource should be found if it is provided.
114864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                } catch (RuntimeException e) {
114964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    // Other exceptions can occur when loading an Animator from AnimationUtils.
115064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                }
115164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            }
115264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            if (!successfulLoad) {
115364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                // try Animator
115464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                try {
115564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    animator = AnimatorInflater.loadAnimator(mHost.getContext(), nextAnim);
115664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    if (animator != null) {
115764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        return new AnimationOrAnimator(animator);
115864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    }
115964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                } catch (RuntimeException e) {
116064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    if (isAnim) {
116164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        // Rethrow it -- we already tried AnimationUtils and it failed.
116264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        throw e;
116364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    }
116464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    // Otherwise, it is probably an animation resource
116564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    animation = AnimationUtils.loadAnimation(mHost.getContext(), nextAnim);
116664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    if (animation != null) {
116764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        return new AnimationOrAnimator(animation);
116864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                    }
116964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                }
117064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            }
117164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
117264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
117364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (transit == 0) {
117464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            return null;
117564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
117664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
117764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        int styleIndex = transitToStyleIndex(transit, enter);
117864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (styleIndex < 0) {
117964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            return null;
118064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
118164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
118264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        switch (styleIndex) {
118364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            case ANIM_STYLE_OPEN_ENTER:
118464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                return makeOpenCloseAnimation(mHost.getContext(), 1.125f, 1.0f, 0, 1);
118564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            case ANIM_STYLE_OPEN_EXIT:
118664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                return makeOpenCloseAnimation(mHost.getContext(), 1.0f, .975f, 1, 0);
118764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            case ANIM_STYLE_CLOSE_ENTER:
118864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                return makeOpenCloseAnimation(mHost.getContext(), .975f, 1.0f, 0, 1);
118964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            case ANIM_STYLE_CLOSE_EXIT:
119064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                return makeOpenCloseAnimation(mHost.getContext(), 1.0f, 1.075f, 1, 0);
119164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            case ANIM_STYLE_FADE_ENTER:
119264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                return makeFadeAnimation(mHost.getContext(), 0, 1);
119364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            case ANIM_STYLE_FADE_EXIT:
119464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                return makeFadeAnimation(mHost.getContext(), 1, 0);
119564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
119664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
119764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        // TODO: remove or fix transitionStyle -- it apparently never worked.
119864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (transitionStyle == 0 && mHost.onHasWindowAnimations()) {
119964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            transitionStyle = mHost.onGetWindowAnimations();
120064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
120164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (transitionStyle == 0) {
120264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            return null;
120364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
120464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
120564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
120664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //        com.android.internal.R.styleable.FragmentAnimation);
120764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //int anim = attrs.getResourceId(styleIndex, 0);
120864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //attrs.recycle();
120964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
121064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //if (anim == 0) {
121164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //    return null;
121264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //}
121364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
121464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        //return AnimatorInflater.loadAnimator(mActivity, anim);
121564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        return null;
121664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
121764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
121864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    public void performPendingDeferredStart(Fragment f) {
121964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (f.mDeferStart) {
122064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            if (mExecutingActions) {
122164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                // Wait until we're done executing our pending transactions
122264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                mHavePendingDeferredStart = true;
122364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                return;
122464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            }
122564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            f.mDeferStart = false;
122664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            moveToState(f, mCurState, 0, 0, false);
122764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
122864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
122964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
123064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
123164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Sets the to be animated view on hardware layer during the animation. Note
123264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * that calling this will replace any existing animation listener on the animation
123364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * with a new one, as animations do not support more than one listeners. Therefore,
123464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * animations that already have listeners should do the layer change operations
123564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * in their existing listeners, rather than calling this function.
123664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
123764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    private static void setHWLayerAnimListenerIfAlpha(final View v, AnimationOrAnimator anim) {
123864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (v == null || anim == null) {
12391f5dee32716a2709ca2e1e491cbc2f09ca35a2faRoshan Pius            return;
124064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
124164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (shouldRunOnHWLayer(v, anim)) {
12421f5dee32716a2709ca2e1e491cbc2f09ca35a2faRoshan Pius            if (anim.animator != null) {
124364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                anim.animator.addListener(new AnimatorOnHWLayerIfNeededListener(v));
12441f5dee32716a2709ca2e1e491cbc2f09ca35a2faRoshan Pius            } else {
124564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                AnimationListener originalListener = getAnimationListener(anim.animation);
12461f5dee32716a2709ca2e1e491cbc2f09ca35a2faRoshan Pius                // If there's already a listener set on the animation, we need wrap the new listener
124764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                // around the existing listener, so that they will both get animation listener
124864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                // callbacks.
12491f5dee32716a2709ca2e1e491cbc2f09ca35a2faRoshan Pius                v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
125064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                anim.animation.setAnimationListener(new AnimateOnHWLayerIfNeededListener(v,
125164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        originalListener));
125264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            }
125364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
125464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
125564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
125664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    /**
125764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     * Returns an existing AnimationListener on an Animation or {@code null} if none exists.
125864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius     */
125964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    private static AnimationListener getAnimationListener(Animation animation) {
126064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        AnimationListener originalListener = null;
126164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        try {
126264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            if (sAnimationListenerField == null) {
126364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                sAnimationListenerField = Animation.class.getDeclaredField("mListener");
126464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                sAnimationListenerField.setAccessible(true);
126564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            }
126664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            originalListener = (AnimationListener) sAnimationListenerField.get(animation);
126764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        } catch (NoSuchFieldException e) {
126864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            Log.e(TAG, "No field with the name mListener is found in Animation class", e);
126964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        } catch (IllegalAccessException e) {
127064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            Log.e(TAG, "Cannot access Animation's mListener field", e);
127164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
127264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        return originalListener;
127364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
127464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
127564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    boolean isStateAtLeast(int state) {
127664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        return mCurState >= state;
127764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    }
127864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius
127964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    @SuppressWarnings("ReferenceEquality")
128064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius    void moveToState(Fragment f, int newState, int transit, int transitionStyle,
128164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            boolean keepActive) {
128264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        // Fragments that are not currently added will sit in the onCreate() state.
128364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
128464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            newState = Fragment.CREATED;
128564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
128664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (f.mRemoving && newState > f.mState) {
128764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            if (f.mState == Fragment.INITIALIZING && f.isInBackStack()) {
128864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                // Allow the fragment to be created so that it can be saved later.
128964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                newState = Fragment.CREATED;
129064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            } else {
129164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                // While removing a fragment, we can't change it to a higher state.
129264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                newState = f.mState;
129364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            }
129464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
129564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        // Defer start if requested; don't allow it to move to STARTED or higher
129664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        // if it's not already started.
129764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
129864950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            newState = Fragment.STOPPED;
129964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        }
130064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius        if (f.mState <= newState) {
130164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            // For fragments that are created from a layout, when restoring from
130264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            // state we don't want to allow them to be created until they are
130364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            // being reloaded from the layout.
130464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius            if (f.mFromLayout && !f.mInLayout) {
1305c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                return;
1306c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            }
1307c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius            if (f.getAnimatingAway() != null || f.getAnimator() != null) {
1308c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                // The fragment is currently being animated...  but!  Now we
1309c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                // want to move our state back up.  Give up on waiting for the
1310c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                // animation, move to whatever the final state should be once
1311c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                // the animation is done, and then we can proceed from there.
1312c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                f.setAnimatingAway(null);
1313c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                f.setAnimator(null);
1314c3d81dbf035c205ca1baf16b84a41b67eda9ae4aRoshan Pius                moveToState(f, f.getStateAfterAnimating(), 0, 0, true);
131566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            }
131666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne            switch (f.mState) {
131766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                case Fragment.INITIALIZING:
131866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                    if (newState > Fragment.INITIALIZING) {
131966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
132066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        if (f.mSavedFragmentState != null) {
132166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            f.mSavedFragmentState.setClassLoader(mHost.getContext()
132266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                                    .getClassLoader());
1323c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius                            f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
1324c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius                                    FragmentManagerImpl.VIEW_STATE_TAG);
1325c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius                            f.mTarget = getFragment(f.mSavedFragmentState,
1326c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius                                    FragmentManagerImpl.TARGET_STATE_TAG);
1327c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius                            if (f.mTarget != null) {
13284e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar                                f.mTargetRequestCode = f.mSavedFragmentState.getInt(
13294e45fd9302b4ed943732ad85c0a88c7d7830be1dpkanwar                                        FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
1330c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius                            }
1331c7a4b6706fa09042bb36a64036d86d88eb6e4126Roshan Pius                            f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
133266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                                    FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
133366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            if (!f.mUserVisibleHint) {
133466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                                f.mDeferStart = true;
1335727ba04029935d4faee3c7fce8a5a0ba6ed0a4eaRoshan Pius                                if (newState > Fragment.STOPPED) {
133666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                                    newState = Fragment.STOPPED;
133766e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                                }
133866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            }
133966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        }
134066e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
134166e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        f.mHost = mHost;
134266e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        f.mParentFragment = mParent;
134366e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        f.mFragmentManager = mParent != null
134466e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                                ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl();
134566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne
134666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        // If we have a target fragment, push it along to at least CREATED
1347f50550926a7bddc24adf822876f35812d7d8c7beRoshan Pius                        // so that this one can rely on it as an initialized dependency.
134866e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        if (f.mTarget != null) {
134966e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                            if (mActive.get(f.mTarget.mIndex) != f.mTarget) {
135064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                                throw new IllegalStateException("Fragment " + f
135164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                                        + " declared target fragment " + f.mTarget
135264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                                        + " that does not belong to this FragmentManager!");
135364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                            }
135464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                            if (f.mTarget.mState < Fragment.CREATED) {
135564950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                                moveToState(f.mTarget, Fragment.CREATED, 0, 0, true);
135664950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                            }
135764950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        }
13581f5dee32716a2709ca2e1e491cbc2f09ca35a2faRoshan Pius
135964950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        dispatchOnFragmentPreAttached(f, mHost.getContext(), false);
136064950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        f.mCalled = false;
136164950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        f.onAttach(mHost.getContext());
136264950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                        if (!f.mCalled) {
136364950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                            throw new SuperNotCalledException("Fragment " + f
136464950d46f5469e5e6ce32ccc81a1f751c40f4202Roshan Pius                                    + " did not call through to super.onAttach()");
136566e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        }
136666e9f4ab597136cbf4accadb8e009fc68ff071a7Glen Kuhne                        if (f.mParentFragment == null) {
1367                            mHost.onAttachFragment(f);
1368                        } else {
1369                            f.mParentFragment.onAttachFragment(f);
1370                        }
1371                        dispatchOnFragmentAttached(f, mHost.getContext(), false);
1372
1373                        if (!f.mRetaining) {
1374                            dispatchOnFragmentPreCreated(f, f.mSavedFragmentState, false);
1375                            f.performCreate(f.mSavedFragmentState);
1376                            dispatchOnFragmentCreated(f, f.mSavedFragmentState, false);
1377                        } else {
1378                            f.restoreChildFragmentState(f.mSavedFragmentState);
1379                            f.mState = Fragment.CREATED;
1380                        }
1381                        f.mRetaining = false;
1382                    }
1383                    // fall through
1384                case Fragment.CREATED:
1385                    // This is outside the if statement below on purpose; we want this to run
1386                    // even if we do a moveToState from CREATED => *, CREATED => CREATED, and
1387                    // * => CREATED as part of the case fallthrough above.
1388                    ensureInflatedFragmentView(f);
1389
1390                    if (newState > Fragment.CREATED) {
1391                        if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
1392                        if (!f.mFromLayout) {
1393                            ViewGroup container = null;
1394                            if (f.mContainerId != 0) {
1395                                if (f.mContainerId == View.NO_ID) {
1396                                    throwException(new IllegalArgumentException(
1397                                            "Cannot create fragment "
1398                                                    + f
1399                                                    + " for a container view with no id"));
1400                                }
1401                                container = (ViewGroup) mContainer.onFindViewById(f.mContainerId);
1402                                if (container == null && !f.mRestored) {
1403                                    String resName;
1404                                    try {
1405                                        resName = f.getResources().getResourceName(f.mContainerId);
1406                                    } catch (NotFoundException e) {
1407                                        resName = "unknown";
1408                                    }
1409                                    throwException(new IllegalArgumentException(
1410                                            "No view found for id 0x"
1411                                            + Integer.toHexString(f.mContainerId) + " ("
1412                                            + resName
1413                                            + ") for fragment " + f));
1414                                }
1415                            }
1416                            f.mContainer = container;
1417                            f.mView = f.performCreateView(f.performGetLayoutInflater(
1418                                    f.mSavedFragmentState), container, f.mSavedFragmentState);
1419                            if (f.mView != null) {
1420                                f.mInnerView = f.mView;
1421                                f.mView.setSaveFromParentEnabled(false);
1422                                if (container != null) {
1423                                    container.addView(f.mView);
1424                                }
1425                                if (f.mHidden) {
1426                                    f.mView.setVisibility(View.GONE);
1427                                }
1428                                f.onViewCreated(f.mView, f.mSavedFragmentState);
1429                                dispatchOnFragmentViewCreated(f, f.mView, f.mSavedFragmentState,
1430                                        false);
1431                                // Only animate the view if it is visible. This is done after
1432                                // dispatchOnFragmentViewCreated in case visibility is changed
1433                                f.mIsNewlyAdded = (f.mView.getVisibility() == View.VISIBLE)
1434                                        && f.mContainer != null;
1435                            } else {
1436                                f.mInnerView = null;
1437                            }
1438                        }
1439
1440                        f.performActivityCreated(f.mSavedFragmentState);
1441                        dispatchOnFragmentActivityCreated(f, f.mSavedFragmentState, false);
1442                        if (f.mView != null) {
1443                            f.restoreViewState(f.mSavedFragmentState);
1444                        }
1445                        f.mSavedFragmentState = null;
1446                    }
1447                    // fall through
1448                case Fragment.ACTIVITY_CREATED:
1449                    if (newState > Fragment.ACTIVITY_CREATED) {
1450                        f.mState = Fragment.STOPPED;
1451                    }
1452                    // fall through
1453                case Fragment.STOPPED:
1454                    if (newState > Fragment.STOPPED) {
1455                        if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
1456                        f.performStart();
1457                        dispatchOnFragmentStarted(f, false);
1458                    }
1459                    // fall through
1460                case Fragment.STARTED:
1461                    if (newState > Fragment.STARTED) {
1462                        if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
1463                        f.performResume();
1464                        dispatchOnFragmentResumed(f, false);
1465                        f.mSavedFragmentState = null;
1466                        f.mSavedViewState = null;
1467                    }
1468            }
1469        } else if (f.mState > newState) {
1470            switch (f.mState) {
1471                case Fragment.RESUMED:
1472                    if (newState < Fragment.RESUMED) {
1473                        if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
1474                        f.performPause();
1475                        dispatchOnFragmentPaused(f, false);
1476                    }
1477                    // fall through
1478                case Fragment.STARTED:
1479                    if (newState < Fragment.STARTED) {
1480                        if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
1481                        f.performStop();
1482                        dispatchOnFragmentStopped(f, false);
1483                    }
1484                    // fall through
1485                case Fragment.STOPPED:
1486                    if (newState < Fragment.STOPPED) {
1487                        if (DEBUG) Log.v(TAG, "movefrom STOPPED: " + f);
1488                        f.performReallyStop();
1489                    }
1490                    // fall through
1491                case Fragment.ACTIVITY_CREATED:
1492                    if (newState < Fragment.ACTIVITY_CREATED) {
1493                        if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
1494                        if (f.mView != null) {
1495                            // Need to save the current view state if not
1496                            // done already.
1497                            if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) {
1498                                saveFragmentViewState(f);
1499                            }
1500                        }
1501                        f.performDestroyView();
1502                        dispatchOnFragmentViewDestroyed(f, false);
1503                        if (f.mView != null && f.mContainer != null) {
1504                            // Stop any current animations:
1505                            f.mView.clearAnimation();
1506                            f.mContainer.endViewTransition(f.mView);
1507                            AnimationOrAnimator anim = null;
1508                            if (mCurState > Fragment.INITIALIZING && !mDestroyed
1509                                    && f.mView.getVisibility() == View.VISIBLE
1510                                    && f.mPostponedAlpha >= 0) {
1511                                anim = loadAnimation(f, transit, false,
1512                                        transitionStyle);
1513                            }
1514                            f.mPostponedAlpha = 0;
1515                            if (anim != null) {
1516                                animateRemoveFragment(f, anim, newState);
1517                            }
1518                            f.mContainer.removeView(f.mView);
1519                        }
1520                        f.mContainer = null;
1521                        f.mView = null;
1522                        f.mInnerView = null;
1523                        f.mInLayout = false;
1524                    }
1525                    // fall through
1526                case Fragment.CREATED:
1527                    if (newState < Fragment.CREATED) {
1528                        if (mDestroyed) {
1529                            // The fragment's containing activity is
1530                            // being destroyed, but this fragment is
1531                            // currently animating away.  Stop the
1532                            // animation right now -- it is not needed,
1533                            // and we can't wait any more on destroying
1534                            // the fragment.
1535                            if (f.getAnimatingAway() != null) {
1536                                View v = f.getAnimatingAway();
1537                                f.setAnimatingAway(null);
1538                                v.clearAnimation();
1539                            } else if (f.getAnimator() != null) {
1540                                Animator animator = f.getAnimator();
1541                                f.setAnimator(null);
1542                                animator.cancel();
1543                            }
1544                        }
1545                        if (f.getAnimatingAway() != null || f.getAnimator() != null) {
1546                            // We are waiting for the fragment's view to finish
1547                            // animating away.  Just make a note of the state
1548                            // the fragment now should move to once the animation
1549                            // is done.
1550                            f.setStateAfterAnimating(newState);
1551                            newState = Fragment.CREATED;
1552                        } else {
1553                            if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1554                            if (!f.mRetaining) {
1555                                f.performDestroy();
1556                                dispatchOnFragmentDestroyed(f, false);
1557                            } else {
1558                                f.mState = Fragment.INITIALIZING;
1559                            }
1560
1561                            f.performDetach();
1562                            dispatchOnFragmentDetached(f, false);
1563                            if (!keepActive) {
1564                                if (!f.mRetaining) {
1565                                    makeInactive(f);
1566                                } else {
1567                                    f.mHost = null;
1568                                    f.mParentFragment = null;
1569                                    f.mFragmentManager = null;
1570                                }
1571                            }
1572                        }
1573                    }
1574            }
1575        }
1576
1577        if (f.mState != newState) {
1578            Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; "
1579                    + "expected state " + newState + " found " + f.mState);
1580            f.mState = newState;
1581        }
1582    }
1583
1584    /**
1585     * Animates the removal of a fragment with the given animator or animation. After animating,
1586     * the fragment's view will be removed from the hierarchy.
1587     *
1588     * @param fragment The fragment to animate out
1589     * @param anim The animator or animation to run on the fragment's view
1590     * @param newState The final state after animating.
1591     */
1592    private void animateRemoveFragment(@NonNull final Fragment fragment,
1593            @NonNull AnimationOrAnimator anim, final int newState) {
1594        final View viewToAnimate = fragment.mView;
1595        fragment.setStateAfterAnimating(newState);
1596        if (anim.animation != null) {
1597            Animation animation = anim.animation;
1598            fragment.setAnimatingAway(fragment.mView);
1599            AnimationListener listener = getAnimationListener(animation);
1600            animation.setAnimationListener(new AnimationListenerWrapper(listener) {
1601                @Override
1602                public void onAnimationEnd(Animation animation) {
1603                    super.onAnimationEnd(animation);
1604                    if (fragment.getAnimatingAway() != null) {
1605                        fragment.setAnimatingAway(null);
1606                        moveToState(fragment, fragment.getStateAfterAnimating(), 0, 0, false);
1607                    }
1608                }
1609            });
1610            setHWLayerAnimListenerIfAlpha(viewToAnimate, anim);
1611            fragment.mView.startAnimation(animation);
1612        } else {
1613            final Animator animator = anim.animator;
1614            fragment.setAnimator(anim.animator);
1615            final ViewGroup container = fragment.mContainer;
1616            if (container != null) {
1617                container.startViewTransition(viewToAnimate);
1618            }
1619            animator.addListener(new AnimatorListenerAdapter() {
1620                @Override
1621                public void onAnimationEnd(Animator anim) {
1622                    if (container != null) {
1623                        container.endViewTransition(viewToAnimate);
1624                    }
1625                    if (fragment.getAnimator() != null) {
1626                        fragment.setAnimator(null);
1627                        moveToState(fragment, fragment.getStateAfterAnimating(), 0, 0, false);
1628                    }
1629                }
1630            });
1631            animator.setTarget(fragment.mView);
1632            setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1633            animator.start();
1634        }
1635    }
1636
1637    void moveToState(Fragment f) {
1638        moveToState(f, mCurState, 0, 0, false);
1639    }
1640
1641    void ensureInflatedFragmentView(Fragment f) {
1642        if (f.mFromLayout && !f.mPerformedCreateView) {
1643            f.mView = f.performCreateView(f.performGetLayoutInflater(
1644                    f.mSavedFragmentState), null, f.mSavedFragmentState);
1645            if (f.mView != null) {
1646                f.mInnerView = f.mView;
1647                f.mView.setSaveFromParentEnabled(false);
1648                if (f.mHidden) f.mView.setVisibility(View.GONE);
1649                f.onViewCreated(f.mView, f.mSavedFragmentState);
1650                dispatchOnFragmentViewCreated(f, f.mView, f.mSavedFragmentState, false);
1651            } else {
1652                f.mInnerView = null;
1653            }
1654        }
1655    }
1656
1657    /**
1658     * Fragments that have been shown or hidden don't have their visibility changed or
1659     * animations run during the {@link #showFragment(Fragment)} or {@link #hideFragment(Fragment)}
1660     * calls. After fragments are brought to their final state in
1661     * {@link #moveFragmentToExpectedState(Fragment)} the fragments that have been shown or
1662     * hidden must have their visibility changed and their animations started here.
1663     *
1664     * @param fragment The fragment with mHiddenChanged = true that should change its View's
1665     *                 visibility and start the show or hide animation.
1666     */
1667    void completeShowHideFragment(final Fragment fragment) {
1668        if (fragment.mView != null) {
1669            AnimationOrAnimator anim = loadAnimation(fragment, fragment.getNextTransition(),
1670                    !fragment.mHidden, fragment.getNextTransitionStyle());
1671            if (anim != null && anim.animator != null) {
1672                anim.animator.setTarget(fragment.mView);
1673                if (fragment.mHidden) {
1674                    if (fragment.isHideReplaced()) {
1675                        fragment.setHideReplaced(false);
1676                    } else {
1677                        final ViewGroup container = fragment.mContainer;
1678                        final View animatingView = fragment.mView;
1679                        container.startViewTransition(animatingView);
1680                        // Delay the actual hide operation until the animation finishes,
1681                        // otherwise the fragment will just immediately disappear
1682                        anim.animator.addListener(new AnimatorListenerAdapter() {
1683                            @Override
1684                            public void onAnimationEnd(Animator animation) {
1685                                container.endViewTransition(animatingView);
1686                                animation.removeListener(this);
1687                                if (fragment.mView != null) {
1688                                    fragment.mView.setVisibility(View.GONE);
1689                                }
1690                            }
1691                        });
1692                    }
1693                } else {
1694                    fragment.mView.setVisibility(View.VISIBLE);
1695                }
1696                setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1697                anim.animator.start();
1698            } else {
1699                if (anim != null) {
1700                    setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1701                    fragment.mView.startAnimation(anim.animation);
1702                    anim.animation.start();
1703                }
1704                final int visibility = fragment.mHidden && !fragment.isHideReplaced()
1705                        ? View.GONE
1706                        : View.VISIBLE;
1707                fragment.mView.setVisibility(visibility);
1708                if (fragment.isHideReplaced()) {
1709                    fragment.setHideReplaced(false);
1710                }
1711            }
1712        }
1713        if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1714            mNeedMenuInvalidate = true;
1715        }
1716        fragment.mHiddenChanged = false;
1717        fragment.onHiddenChanged(fragment.mHidden);
1718    }
1719
1720    /**
1721     * Moves a fragment to its expected final state or the fragment manager's state, depending
1722     * on whether the fragment manager's state is raised properly.
1723     *
1724     * @param f The fragment to change.
1725     */
1726    void moveFragmentToExpectedState(Fragment f) {
1727        if (f == null) {
1728            return;
1729        }
1730        int nextState = mCurState;
1731        if (f.mRemoving) {
1732            if (f.isInBackStack()) {
1733                nextState = Math.min(nextState, Fragment.CREATED);
1734            } else {
1735                nextState = Math.min(nextState, Fragment.INITIALIZING);
1736            }
1737        }
1738        moveToState(f, nextState, f.getNextTransition(), f.getNextTransitionStyle(), false);
1739
1740        if (f.mView != null) {
1741            // Move the view if it is out of order
1742            Fragment underFragment = findFragmentUnder(f);
1743            if (underFragment != null) {
1744                final View underView = underFragment.mView;
1745                // make sure this fragment is in the right order.
1746                final ViewGroup container = f.mContainer;
1747                int underIndex = container.indexOfChild(underView);
1748                int viewIndex = container.indexOfChild(f.mView);
1749                if (viewIndex < underIndex) {
1750                    container.removeViewAt(viewIndex);
1751                    container.addView(f.mView, underIndex);
1752                }
1753            }
1754            if (f.mIsNewlyAdded && f.mContainer != null) {
1755                // Make it visible and run the animations
1756                if (f.mPostponedAlpha > 0f) {
1757                    f.mView.setAlpha(f.mPostponedAlpha);
1758                }
1759                f.mPostponedAlpha = 0f;
1760                f.mIsNewlyAdded = false;
1761                // run animations:
1762                AnimationOrAnimator anim = loadAnimation(f, f.getNextTransition(), true,
1763                        f.getNextTransitionStyle());
1764                if (anim != null) {
1765                    setHWLayerAnimListenerIfAlpha(f.mView, anim);
1766                    if (anim.animation != null) {
1767                        f.mView.startAnimation(anim.animation);
1768                    } else {
1769                        anim.animator.setTarget(f.mView);
1770                        anim.animator.start();
1771                    }
1772                }
1773            }
1774        }
1775        if (f.mHiddenChanged) {
1776            completeShowHideFragment(f);
1777        }
1778    }
1779
1780    /**
1781     * Changes the state of the fragment manager to {@code newState}. If the fragment manager
1782     * changes state or {@code always} is {@code true}, any fragments within it have their
1783     * states updated as well.
1784     *
1785     * @param newState The new state for the fragment manager
1786     * @param always If {@code true}, all fragments update their state, even
1787     *               if {@code newState} matches the current fragment manager's state.
1788     */
1789    void moveToState(int newState, boolean always) {
1790        if (mHost == null && newState != Fragment.INITIALIZING) {
1791            throw new IllegalStateException("No activity");
1792        }
1793
1794        if (!always && newState == mCurState) {
1795            return;
1796        }
1797
1798        mCurState = newState;
1799
1800        if (mActive != null) {
1801            boolean loadersRunning = false;
1802
1803            // Must add them in the proper order. mActive fragments may be out of order
1804            if (mAdded != null) {
1805                final int numAdded = mAdded.size();
1806                for (int i = 0; i < numAdded; i++) {
1807                    Fragment f = mAdded.get(i);
1808                    moveFragmentToExpectedState(f);
1809                    if (f.mLoaderManager != null) {
1810                        loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1811                    }
1812                }
1813            }
1814
1815            // Now iterate through all active fragments. These will include those that are removed
1816            // and detached.
1817            final int numActive = mActive.size();
1818            for (int i = 0; i < numActive; i++) {
1819                Fragment f = mActive.valueAt(i);
1820                if (f != null && (f.mRemoving || f.mDetached) && !f.mIsNewlyAdded) {
1821                    moveFragmentToExpectedState(f);
1822                    if (f.mLoaderManager != null) {
1823                        loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1824                    }
1825                }
1826            }
1827
1828            if (!loadersRunning) {
1829                startPendingDeferredFragments();
1830            }
1831
1832            if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) {
1833                mHost.onSupportInvalidateOptionsMenu();
1834                mNeedMenuInvalidate = false;
1835            }
1836        }
1837    }
1838
1839    void startPendingDeferredFragments() {
1840        if (mActive == null) return;
1841
1842        for (int i=0; i<mActive.size(); i++) {
1843            Fragment f = mActive.valueAt(i);
1844            if (f != null) {
1845                performPendingDeferredStart(f);
1846            }
1847        }
1848    }
1849
1850    void makeActive(Fragment f) {
1851        if (f.mIndex >= 0) {
1852            return;
1853        }
1854
1855        f.setIndex(mNextFragmentIndex++, mParent);
1856        if (mActive == null) {
1857            mActive = new SparseArray<>();
1858        }
1859        mActive.put(f.mIndex, f);
1860        if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
1861    }
1862
1863    void makeInactive(Fragment f) {
1864        if (f.mIndex < 0) {
1865            return;
1866        }
1867
1868        if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
1869        // Don't remove yet. That happens in burpActive(). This prevents
1870        // concurrent modification while iterating over mActive
1871        mActive.put(f.mIndex, null);
1872
1873        mHost.inactivateFragment(f.mWho);
1874        f.initState();
1875    }
1876
1877    public void addFragment(Fragment fragment, boolean moveToStateNow) {
1878        if (mAdded == null) {
1879            mAdded = new ArrayList<Fragment>();
1880        }
1881        if (DEBUG) Log.v(TAG, "add: " + fragment);
1882        makeActive(fragment);
1883        if (!fragment.mDetached) {
1884            if (mAdded.contains(fragment)) {
1885                throw new IllegalStateException("Fragment already added: " + fragment);
1886            }
1887            synchronized (mAdded) {
1888                mAdded.add(fragment);
1889            }
1890            fragment.mAdded = true;
1891            fragment.mRemoving = false;
1892            if (fragment.mView == null) {
1893                fragment.mHiddenChanged = false;
1894            }
1895            if (fragment.mHasMenu && fragment.mMenuVisible) {
1896                mNeedMenuInvalidate = true;
1897            }
1898            if (moveToStateNow) {
1899                moveToState(fragment);
1900            }
1901        }
1902    }
1903
1904    public void removeFragment(Fragment fragment) {
1905        if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
1906        final boolean inactive = !fragment.isInBackStack();
1907        if (!fragment.mDetached || inactive) {
1908            if (mAdded != null) {
1909                synchronized (mAdded) {
1910                    mAdded.remove(fragment);
1911                }
1912            }
1913            if (fragment.mHasMenu && fragment.mMenuVisible) {
1914                mNeedMenuInvalidate = true;
1915            }
1916            fragment.mAdded = false;
1917            fragment.mRemoving = true;
1918        }
1919    }
1920
1921    /**
1922     * Marks a fragment as hidden to be later animated in with
1923     * {@link #completeShowHideFragment(Fragment)}.
1924     *
1925     * @param fragment The fragment to be shown.
1926     */
1927    public void hideFragment(Fragment fragment) {
1928        if (DEBUG) Log.v(TAG, "hide: " + fragment);
1929        if (!fragment.mHidden) {
1930            fragment.mHidden = true;
1931            // Toggle hidden changed so that if a fragment goes through show/hide/show
1932            // it doesn't go through the animation.
1933            fragment.mHiddenChanged = !fragment.mHiddenChanged;
1934        }
1935    }
1936
1937    /**
1938     * Marks a fragment as shown to be later animated in with
1939     * {@link #completeShowHideFragment(Fragment)}.
1940     *
1941     * @param fragment The fragment to be shown.
1942     */
1943    public void showFragment(Fragment fragment) {
1944        if (DEBUG) Log.v(TAG, "show: " + fragment);
1945        if (fragment.mHidden) {
1946            fragment.mHidden = false;
1947            // Toggle hidden changed so that if a fragment goes through show/hide/show
1948            // it doesn't go through the animation.
1949            fragment.mHiddenChanged = !fragment.mHiddenChanged;
1950        }
1951    }
1952
1953    public void detachFragment(Fragment fragment) {
1954        if (DEBUG) Log.v(TAG, "detach: " + fragment);
1955        if (!fragment.mDetached) {
1956            fragment.mDetached = true;
1957            if (fragment.mAdded) {
1958                // We are not already in back stack, so need to remove the fragment.
1959                if (mAdded != null) {
1960                    if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
1961                    synchronized (mAdded) {
1962                        mAdded.remove(fragment);
1963                    }
1964                }
1965                if (fragment.mHasMenu && fragment.mMenuVisible) {
1966                    mNeedMenuInvalidate = true;
1967                }
1968                fragment.mAdded = false;
1969            }
1970        }
1971    }
1972
1973    public void attachFragment(Fragment fragment) {
1974        if (DEBUG) Log.v(TAG, "attach: " + fragment);
1975        if (fragment.mDetached) {
1976            fragment.mDetached = false;
1977            if (!fragment.mAdded) {
1978                if (mAdded == null) {
1979                    mAdded = new ArrayList<Fragment>();
1980                }
1981                if (mAdded.contains(fragment)) {
1982                    throw new IllegalStateException("Fragment already added: " + fragment);
1983                }
1984                if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
1985                synchronized (mAdded) {
1986                    mAdded.add(fragment);
1987                }
1988                fragment.mAdded = true;
1989                if (fragment.mHasMenu && fragment.mMenuVisible) {
1990                    mNeedMenuInvalidate = true;
1991                }
1992            }
1993        }
1994    }
1995
1996    @Override
1997    public Fragment findFragmentById(int id) {
1998        if (mAdded != null) {
1999            // First look through added fragments.
2000            for (int i=mAdded.size()-1; i>=0; i--) {
2001                Fragment f = mAdded.get(i);
2002                if (f != null && f.mFragmentId == id) {
2003                    return f;
2004                }
2005            }
2006        }
2007        if (mActive != null) {
2008            // Now for any known fragment.
2009            for (int i=mActive.size()-1; i>=0; i--) {
2010                Fragment f = mActive.valueAt(i);
2011                if (f != null && f.mFragmentId == id) {
2012                    return f;
2013                }
2014            }
2015        }
2016        return null;
2017    }
2018
2019    @Override
2020    public Fragment findFragmentByTag(String tag) {
2021        if (mAdded != null && tag != null) {
2022            // First look through added fragments.
2023            for (int i=mAdded.size()-1; i>=0; i--) {
2024                Fragment f = mAdded.get(i);
2025                if (f != null && tag.equals(f.mTag)) {
2026                    return f;
2027                }
2028            }
2029        }
2030        if (mActive != null && tag != null) {
2031            // Now for any known fragment.
2032            for (int i=mActive.size()-1; i>=0; i--) {
2033                Fragment f = mActive.valueAt(i);
2034                if (f != null && tag.equals(f.mTag)) {
2035                    return f;
2036                }
2037            }
2038        }
2039        return null;
2040    }
2041
2042    public Fragment findFragmentByWho(String who) {
2043        if (mActive != null && who != null) {
2044            for (int i=mActive.size()-1; i>=0; i--) {
2045                Fragment f = mActive.valueAt(i);
2046                if (f != null && (f=f.findFragmentByWho(who)) != null) {
2047                    return f;
2048                }
2049            }
2050        }
2051        return null;
2052    }
2053
2054    private void checkStateLoss() {
2055        if (mStateSaved) {
2056            throw new IllegalStateException(
2057                    "Can not perform this action after onSaveInstanceState");
2058        }
2059        if (mNoTransactionsBecause != null) {
2060            throw new IllegalStateException(
2061                    "Can not perform this action inside of " + mNoTransactionsBecause);
2062        }
2063    }
2064
2065    @Override
2066    public boolean isStateSaved() {
2067        return mStateSaved;
2068    }
2069
2070    /**
2071     * Adds an action to the queue of pending actions.
2072     *
2073     * @param action the action to add
2074     * @param allowStateLoss whether to allow loss of state information
2075     * @throws IllegalStateException if the activity has been destroyed
2076     */
2077    public void enqueueAction(OpGenerator action, boolean allowStateLoss) {
2078        if (!allowStateLoss) {
2079            checkStateLoss();
2080        }
2081        synchronized (this) {
2082            if (mDestroyed || mHost == null) {
2083                if (allowStateLoss) {
2084                    // This FragmentManager isn't attached, so drop the entire transaction.
2085                    return;
2086                }
2087                throw new IllegalStateException("Activity has been destroyed");
2088            }
2089            if (mPendingActions == null) {
2090                mPendingActions = new ArrayList<>();
2091            }
2092            mPendingActions.add(action);
2093            scheduleCommit();
2094        }
2095    }
2096
2097    /**
2098     * Schedules the execution when one hasn't been scheduled already. This should happen
2099     * the first time {@link #enqueueAction(OpGenerator, boolean)} is called or when
2100     * a postponed transaction has been started with
2101     * {@link Fragment#startPostponedEnterTransition()}
2102     */
2103    private void scheduleCommit() {
2104        synchronized (this) {
2105            boolean postponeReady =
2106                    mPostponedTransactions != null && !mPostponedTransactions.isEmpty();
2107            boolean pendingReady = mPendingActions != null && mPendingActions.size() == 1;
2108            if (postponeReady || pendingReady) {
2109                mHost.getHandler().removeCallbacks(mExecCommit);
2110                mHost.getHandler().post(mExecCommit);
2111            }
2112        }
2113    }
2114
2115    public int allocBackStackIndex(BackStackRecord bse) {
2116        synchronized (this) {
2117            if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
2118                if (mBackStackIndices == null) {
2119                    mBackStackIndices = new ArrayList<BackStackRecord>();
2120                }
2121                int index = mBackStackIndices.size();
2122                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
2123                mBackStackIndices.add(bse);
2124                return index;
2125
2126            } else {
2127                int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
2128                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
2129                mBackStackIndices.set(index, bse);
2130                return index;
2131            }
2132        }
2133    }
2134
2135    public void setBackStackIndex(int index, BackStackRecord bse) {
2136        synchronized (this) {
2137            if (mBackStackIndices == null) {
2138                mBackStackIndices = new ArrayList<BackStackRecord>();
2139            }
2140            int N = mBackStackIndices.size();
2141            if (index < N) {
2142                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
2143                mBackStackIndices.set(index, bse);
2144            } else {
2145                while (N < index) {
2146                    mBackStackIndices.add(null);
2147                    if (mAvailBackStackIndices == null) {
2148                        mAvailBackStackIndices = new ArrayList<Integer>();
2149                    }
2150                    if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
2151                    mAvailBackStackIndices.add(N);
2152                    N++;
2153                }
2154                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
2155                mBackStackIndices.add(bse);
2156            }
2157        }
2158    }
2159
2160    public void freeBackStackIndex(int index) {
2161        synchronized (this) {
2162            mBackStackIndices.set(index, null);
2163            if (mAvailBackStackIndices == null) {
2164                mAvailBackStackIndices = new ArrayList<Integer>();
2165            }
2166            if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
2167            mAvailBackStackIndices.add(index);
2168        }
2169    }
2170
2171    /**
2172     * Broken out from exec*, this prepares for gathering and executing operations.
2173     *
2174     * @param allowStateLoss true if state loss should be ignored or false if it should be
2175     *                       checked.
2176     */
2177    private void ensureExecReady(boolean allowStateLoss) {
2178        if (mExecutingActions) {
2179            throw new IllegalStateException("FragmentManager is already executing transactions");
2180        }
2181
2182        if (Looper.myLooper() != mHost.getHandler().getLooper()) {
2183            throw new IllegalStateException("Must be called from main thread of fragment host");
2184        }
2185
2186        if (!allowStateLoss) {
2187            checkStateLoss();
2188        }
2189
2190        if (mTmpRecords == null) {
2191            mTmpRecords = new ArrayList<>();
2192            mTmpIsPop = new ArrayList<>();
2193        }
2194        mExecutingActions = true;
2195        try {
2196            executePostponedTransaction(null, null);
2197        } finally {
2198            mExecutingActions = false;
2199        }
2200    }
2201
2202    public void execSingleAction(OpGenerator action, boolean allowStateLoss) {
2203        if (allowStateLoss && (mHost == null || mDestroyed)) {
2204            // This FragmentManager isn't attached, so drop the entire transaction.
2205            return;
2206        }
2207        ensureExecReady(allowStateLoss);
2208        if (action.generateOps(mTmpRecords, mTmpIsPop)) {
2209            mExecutingActions = true;
2210            try {
2211                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
2212            } finally {
2213                cleanupExec();
2214            }
2215        }
2216
2217        doPendingDeferredStart();
2218        burpActive();
2219    }
2220
2221    /**
2222     * Broken out of exec*, this cleans up the mExecutingActions and the temporary structures
2223     * used in executing operations.
2224     */
2225    private void cleanupExec() {
2226        mExecutingActions = false;
2227        mTmpIsPop.clear();
2228        mTmpRecords.clear();
2229    }
2230
2231    /**
2232     * Only call from main thread!
2233     */
2234    public boolean execPendingActions() {
2235        ensureExecReady(true);
2236
2237        boolean didSomething = false;
2238        while (generateOpsForPendingActions(mTmpRecords, mTmpIsPop)) {
2239            mExecutingActions = true;
2240            try {
2241                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
2242            } finally {
2243                cleanupExec();
2244            }
2245            didSomething = true;
2246        }
2247
2248        doPendingDeferredStart();
2249        burpActive();
2250
2251        return didSomething;
2252    }
2253
2254    /**
2255     * Complete the execution of transactions that have previously been postponed, but are
2256     * now ready.
2257     */
2258    private void executePostponedTransaction(ArrayList<BackStackRecord> records,
2259            ArrayList<Boolean> isRecordPop) {
2260        int numPostponed = mPostponedTransactions == null ? 0 : mPostponedTransactions.size();
2261        for (int i = 0; i < numPostponed; i++) {
2262            StartEnterTransitionListener listener = mPostponedTransactions.get(i);
2263            if (records != null && !listener.mIsBack) {
2264                int index = records.indexOf(listener.mRecord);
2265                if (index != -1 && isRecordPop.get(index)) {
2266                    listener.cancelTransaction();
2267                    continue;
2268                }
2269            }
2270            if (listener.isReady() || (records != null
2271                    && listener.mRecord.interactsWith(records, 0, records.size()))) {
2272                mPostponedTransactions.remove(i);
2273                i--;
2274                numPostponed--;
2275                int index;
2276                if (records != null && !listener.mIsBack
2277                        && (index = records.indexOf(listener.mRecord)) != -1
2278                        && isRecordPop.get(index)) {
2279                    // This is popping a postponed transaction
2280                    listener.cancelTransaction();
2281                } else {
2282                    listener.completeTransaction();
2283                }
2284            }
2285        }
2286    }
2287
2288    /**
2289     * Remove redundant BackStackRecord operations and executes them. This method merges operations
2290     * of proximate records that allow reordering. See
2291     * {@link FragmentTransaction#setReorderingAllowed(boolean)}.
2292     * <p>
2293     * For example, a transaction that adds to the back stack and then another that pops that
2294     * back stack record will be optimized to remove the unnecessary operation.
2295     * <p>
2296     * Likewise, two transactions committed that are executed at the same time will be optimized
2297     * to remove the redundant operations as well as two pop operations executed together.
2298     *
2299     * @param records The records pending execution
2300     * @param isRecordPop The direction that these records are being run.
2301     */
2302    private void removeRedundantOperationsAndExecute(ArrayList<BackStackRecord> records,
2303            ArrayList<Boolean> isRecordPop) {
2304        if (records == null || records.isEmpty()) {
2305            return;
2306        }
2307
2308        if (isRecordPop == null || records.size() != isRecordPop.size()) {
2309            throw new IllegalStateException("Internal error with the back stack records");
2310        }
2311
2312        // Force start of any postponed transactions that interact with scheduled transactions:
2313        executePostponedTransaction(records, isRecordPop);
2314
2315        final int numRecords = records.size();
2316        int startIndex = 0;
2317        for (int recordNum = 0; recordNum < numRecords; recordNum++) {
2318            final boolean canReorder = records.get(recordNum).mReorderingAllowed;
2319            if (!canReorder) {
2320                // execute all previous transactions
2321                if (startIndex != recordNum) {
2322                    executeOpsTogether(records, isRecordPop, startIndex, recordNum);
2323                }
2324                // execute all pop operations that don't allow reordering together or
2325                // one add operation
2326                int reorderingEnd = recordNum + 1;
2327                if (isRecordPop.get(recordNum)) {
2328                    while (reorderingEnd < numRecords
2329                            && isRecordPop.get(reorderingEnd)
2330                            && !records.get(reorderingEnd).mReorderingAllowed) {
2331                        reorderingEnd++;
2332                    }
2333                }
2334                executeOpsTogether(records, isRecordPop, recordNum, reorderingEnd);
2335                startIndex = reorderingEnd;
2336                recordNum = reorderingEnd - 1;
2337            }
2338        }
2339        if (startIndex != numRecords) {
2340            executeOpsTogether(records, isRecordPop, startIndex, numRecords);
2341        }
2342    }
2343
2344    /**
2345     * Executes a subset of a list of BackStackRecords, all of which either allow reordering or
2346     * do not allow ordering.
2347     * @param records A list of BackStackRecords that are to be executed
2348     * @param isRecordPop The direction that these records are being run.
2349     * @param startIndex The index of the first record in <code>records</code> to be executed
2350     * @param endIndex One more than the final record index in <code>records</code> to executed.
2351     */
2352    private void executeOpsTogether(ArrayList<BackStackRecord> records,
2353            ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) {
2354        final boolean allowReordering = records.get(startIndex).mReorderingAllowed;
2355        boolean addToBackStack = false;
2356        if (mTmpAddedFragments == null) {
2357            mTmpAddedFragments = new ArrayList<>();
2358        } else {
2359            mTmpAddedFragments.clear();
2360        }
2361        if (mAdded != null) {
2362            mTmpAddedFragments.addAll(mAdded);
2363        }
2364        Fragment oldPrimaryNav = getPrimaryNavigationFragment();
2365        for (int recordNum = startIndex; recordNum < endIndex; recordNum++) {
2366            final BackStackRecord record = records.get(recordNum);
2367            final boolean isPop = isRecordPop.get(recordNum);
2368            if (!isPop) {
2369                oldPrimaryNav = record.expandOps(mTmpAddedFragments, oldPrimaryNav);
2370            } else {
2371                oldPrimaryNav = record.trackAddedFragmentsInPop(mTmpAddedFragments, oldPrimaryNav);
2372            }
2373            addToBackStack = addToBackStack || record.mAddToBackStack;
2374        }
2375        mTmpAddedFragments.clear();
2376
2377        if (!allowReordering) {
2378            FragmentTransition.startTransitions(this, records, isRecordPop, startIndex, endIndex,
2379                    false);
2380        }
2381        executeOps(records, isRecordPop, startIndex, endIndex);
2382
2383        int postponeIndex = endIndex;
2384        if (allowReordering) {
2385            ArraySet<Fragment> addedFragments = new ArraySet<>();
2386            addAddedFragments(addedFragments);
2387            postponeIndex = postponePostponableTransactions(records, isRecordPop,
2388                    startIndex, endIndex, addedFragments);
2389            makeRemovedFragmentsInvisible(addedFragments);
2390        }
2391
2392        if (postponeIndex != startIndex && allowReordering) {
2393            // need to run something now
2394            FragmentTransition.startTransitions(this, records, isRecordPop, startIndex,
2395                    postponeIndex, true);
2396            moveToState(mCurState, true);
2397        }
2398
2399        for (int recordNum = startIndex; recordNum < endIndex; recordNum++) {
2400            final BackStackRecord record = records.get(recordNum);
2401            final boolean isPop = isRecordPop.get(recordNum);
2402            if (isPop && record.mIndex >= 0) {
2403                freeBackStackIndex(record.mIndex);
2404                record.mIndex = -1;
2405            }
2406            record.runOnCommitRunnables();
2407        }
2408        if (addToBackStack) {
2409            reportBackStackChanged();
2410        }
2411    }
2412
2413    /**
2414     * Any fragments that were removed because they have been postponed should have their views
2415     * made invisible by setting their alpha to 0.
2416     *
2417     * @param fragments The fragments that were added during operation execution. Only the ones
2418     *                  that are no longer added will have their alpha changed.
2419     */
2420    private void makeRemovedFragmentsInvisible(ArraySet<Fragment> fragments) {
2421        final int numAdded = fragments.size();
2422        for (int i = 0; i < numAdded; i++) {
2423            final Fragment fragment = fragments.valueAt(i);
2424            if (!fragment.mAdded) {
2425                final View view = fragment.getView();
2426                fragment.mPostponedAlpha = view.getAlpha();
2427                view.setAlpha(0f);
2428            }
2429        }
2430    }
2431
2432    /**
2433     * Examine all transactions and determine which ones are marked as postponed. Those will
2434     * have their operations rolled back and moved to the end of the record list (up to endIndex).
2435     * It will also add the postponed transaction to the queue.
2436     *
2437     * @param records A list of BackStackRecords that should be checked.
2438     * @param isRecordPop The direction that these records are being run.
2439     * @param startIndex The index of the first record in <code>records</code> to be checked
2440     * @param endIndex One more than the final record index in <code>records</code> to be checked.
2441     * @return The index of the first postponed transaction or endIndex if no transaction was
2442     * postponed.
2443     */
2444    private int postponePostponableTransactions(ArrayList<BackStackRecord> records,
2445            ArrayList<Boolean> isRecordPop, int startIndex, int endIndex,
2446            ArraySet<Fragment> added) {
2447        int postponeIndex = endIndex;
2448        for (int i = endIndex - 1; i >= startIndex; i--) {
2449            final BackStackRecord record = records.get(i);
2450            final boolean isPop = isRecordPop.get(i);
2451            boolean isPostponed = record.isPostponed()
2452                    && !record.interactsWith(records, i + 1, endIndex);
2453            if (isPostponed) {
2454                if (mPostponedTransactions == null) {
2455                    mPostponedTransactions = new ArrayList<>();
2456                }
2457                StartEnterTransitionListener listener =
2458                        new StartEnterTransitionListener(record, isPop);
2459                mPostponedTransactions.add(listener);
2460                record.setOnStartPostponedListener(listener);
2461
2462                // roll back the transaction
2463                if (isPop) {
2464                    record.executeOps();
2465                } else {
2466                    record.executePopOps(false);
2467                }
2468
2469                // move to the end
2470                postponeIndex--;
2471                if (i != postponeIndex) {
2472                    records.remove(i);
2473                    records.add(postponeIndex, record);
2474                }
2475
2476                // different views may be visible now
2477                addAddedFragments(added);
2478            }
2479        }
2480        return postponeIndex;
2481    }
2482
2483    /**
2484     * When a postponed transaction is ready to be started, this completes the transaction,
2485     * removing, hiding, or showing views as well as starting the animations and transitions.
2486     * <p>
2487     * {@code runtransitions} is set to false when the transaction postponement was interrupted
2488     * abnormally -- normally by a new transaction being started that affects the postponed
2489     * transaction.
2490     *
2491     * @param record The transaction to run
2492     * @param isPop true if record is popping or false if it is adding
2493     * @param runTransitions true if the fragment transition should be run or false otherwise.
2494     * @param moveToState true if the state should be changed after executing the operations.
2495     *                    This is false when the transaction is canceled when a postponed
2496     *                    transaction is popped.
2497     */
2498    private void completeExecute(BackStackRecord record, boolean isPop, boolean runTransitions,
2499            boolean moveToState) {
2500        if (isPop) {
2501            record.executePopOps(moveToState);
2502        } else {
2503            record.executeOps();
2504        }
2505        ArrayList<BackStackRecord> records = new ArrayList<>(1);
2506        ArrayList<Boolean> isRecordPop = new ArrayList<>(1);
2507        records.add(record);
2508        isRecordPop.add(isPop);
2509        if (runTransitions) {
2510            FragmentTransition.startTransitions(this, records, isRecordPop, 0, 1, true);
2511        }
2512        if (moveToState) {
2513            moveToState(mCurState, true);
2514        }
2515
2516        if (mActive != null) {
2517            final int numActive = mActive.size();
2518            for (int i = 0; i < numActive; i++) {
2519                // Allow added fragments to be removed during the pop since we aren't going
2520                // to move them to the final state with moveToState(mCurState).
2521                Fragment fragment = mActive.valueAt(i);
2522                if (fragment != null && fragment.mView != null && fragment.mIsNewlyAdded
2523                        && record.interactsWith(fragment.mContainerId)) {
2524                    if (fragment.mPostponedAlpha > 0) {
2525                        fragment.mView.setAlpha(fragment.mPostponedAlpha);
2526                    }
2527                    if (moveToState) {
2528                        fragment.mPostponedAlpha = 0;
2529                    } else {
2530                        fragment.mPostponedAlpha = -1;
2531                        fragment.mIsNewlyAdded = false;
2532                    }
2533                }
2534            }
2535        }
2536    }
2537
2538    /**
2539     * Find a fragment within the fragment's container whose View should be below the passed
2540     * fragment. {@code null} is returned when the fragment has no View or if there should be
2541     * no fragment with a View below the given fragment.
2542     *
2543     * As an example, if mAdded has two Fragments with Views sharing the same container:
2544     * FragmentA
2545     * FragmentB
2546     *
2547     * Then, when processing FragmentB, FragmentA will be returned. If, however, FragmentA
2548     * had no View, null would be returned.
2549     *
2550     * @param f The fragment that may be on top of another fragment.
2551     * @return The fragment with a View under f, if one exists or null if f has no View or
2552     * there are no fragments with Views in the same container.
2553     */
2554    private Fragment findFragmentUnder(Fragment f) {
2555        final ViewGroup container = f.mContainer;
2556        final View view = f.mView;
2557
2558        if (container == null || view == null) {
2559            return null;
2560        }
2561
2562        final int fragmentIndex = mAdded.indexOf(f);
2563        for (int i = fragmentIndex - 1; i >= 0; i--) {
2564            Fragment underFragment = mAdded.get(i);
2565            if (underFragment.mContainer == container && underFragment.mView != null) {
2566                // Found the fragment under this one
2567                return underFragment;
2568            }
2569        }
2570        return null;
2571    }
2572
2573    /**
2574     * Run the operations in the BackStackRecords, either to push or pop.
2575     *
2576     * @param records The list of records whose operations should be run.
2577     * @param isRecordPop The direction that these records are being run.
2578     * @param startIndex The index of the first entry in records to run.
2579     * @param endIndex One past the index of the final entry in records to run.
2580     */
2581    private static void executeOps(ArrayList<BackStackRecord> records,
2582            ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) {
2583        for (int i = startIndex; i < endIndex; i++) {
2584            final BackStackRecord record = records.get(i);
2585            final boolean isPop = isRecordPop.get(i);
2586            if (isPop) {
2587                record.bumpBackStackNesting(-1);
2588                // Only execute the add operations at the end of
2589                // all transactions.
2590                boolean moveToState = i == (endIndex - 1);
2591                record.executePopOps(moveToState);
2592            } else {
2593                record.bumpBackStackNesting(1);
2594                record.executeOps();
2595            }
2596        }
2597    }
2598
2599    /**
2600     * Ensure that fragments that are added are moved to at least the CREATED state.
2601     * Any newly-added Views are inserted into {@code added} so that the Transaction can be
2602     * postponed with {@link Fragment#postponeEnterTransition()}. They will later be made
2603     * invisible (by setting their alpha to 0) if they have been removed when postponed.
2604     */
2605    private void addAddedFragments(ArraySet<Fragment> added) {
2606        if (mCurState < Fragment.CREATED) {
2607            return;
2608        }
2609        // We want to leave the fragment in the started state
2610        final int state = Math.min(mCurState, Fragment.STARTED);
2611        final int numAdded = mAdded == null ? 0 : mAdded.size();
2612        for (int i = 0; i < numAdded; i++) {
2613            Fragment fragment = mAdded.get(i);
2614            if (fragment.mState < state) {
2615                moveToState(fragment, state, fragment.getNextAnim(), fragment.getNextTransition(),
2616                        false);
2617                if (fragment.mView != null && !fragment.mHidden && fragment.mIsNewlyAdded) {
2618                    added.add(fragment);
2619                }
2620            }
2621        }
2622    }
2623
2624    /**
2625     * Starts all postponed transactions regardless of whether they are ready or not.
2626     */
2627    private void forcePostponedTransactions() {
2628        if (mPostponedTransactions != null) {
2629            while (!mPostponedTransactions.isEmpty()) {
2630                mPostponedTransactions.remove(0).completeTransaction();
2631            }
2632        }
2633    }
2634
2635    /**
2636     * Ends the animations of fragments so that they immediately reach the end state.
2637     * This is used prior to saving the state so that the correct state is saved.
2638     */
2639    private void endAnimatingAwayFragments() {
2640        final int numFragments = mActive == null ? 0 : mActive.size();
2641        for (int i = 0; i < numFragments; i++) {
2642            Fragment fragment = mActive.valueAt(i);
2643            if (fragment != null) {
2644                if (fragment.getAnimatingAway() != null) {
2645                    // Give up waiting for the animation and just end it.
2646                    final int stateAfterAnimating = fragment.getStateAfterAnimating();
2647                    final View animatingAway = fragment.getAnimatingAway();
2648                    fragment.setAnimatingAway(null);
2649                    Animation animation = animatingAway.getAnimation();
2650                    if (animation != null) {
2651                        animation.cancel();
2652                        // force-clear the animation, as Animation#cancel() doesn't work prior to N,
2653                        // and will instead cause the animation to infinitely loop
2654                        animatingAway.clearAnimation();
2655                    }
2656                    moveToState(fragment, stateAfterAnimating, 0, 0, false);
2657                } else if (fragment.getAnimator() != null) {
2658                    fragment.getAnimator().end();
2659                }
2660            }
2661        }
2662    }
2663
2664    /**
2665     * Adds all records in the pending actions to records and whether they are add or pop
2666     * operations to isPop. After executing, the pending actions will be empty.
2667     *
2668     * @param records All pending actions will generate BackStackRecords added to this.
2669     *                This contains the transactions, in order, to execute.
2670     * @param isPop All pending actions will generate booleans to add to this. This contains
2671     *              an entry for each entry in records to indicate whether or not it is a
2672     *              pop action.
2673     */
2674    private boolean generateOpsForPendingActions(ArrayList<BackStackRecord> records,
2675            ArrayList<Boolean> isPop) {
2676        boolean didSomething = false;
2677        synchronized (this) {
2678            if (mPendingActions == null || mPendingActions.size() == 0) {
2679                return false;
2680            }
2681
2682            final int numActions = mPendingActions.size();
2683            for (int i = 0; i < numActions; i++) {
2684                didSomething |= mPendingActions.get(i).generateOps(records, isPop);
2685            }
2686            mPendingActions.clear();
2687            mHost.getHandler().removeCallbacks(mExecCommit);
2688        }
2689        return didSomething;
2690    }
2691
2692    void doPendingDeferredStart() {
2693        if (mHavePendingDeferredStart) {
2694            boolean loadersRunning = false;
2695            for (int i = 0; i < mActive.size(); i++) {
2696                Fragment f = mActive.valueAt(i);
2697                if (f != null && f.mLoaderManager != null) {
2698                    loadersRunning |= f.mLoaderManager.hasRunningLoaders();
2699                }
2700            }
2701            if (!loadersRunning) {
2702                mHavePendingDeferredStart = false;
2703                startPendingDeferredFragments();
2704            }
2705        }
2706    }
2707
2708    void reportBackStackChanged() {
2709        if (mBackStackChangeListeners != null) {
2710            for (int i=0; i<mBackStackChangeListeners.size(); i++) {
2711                mBackStackChangeListeners.get(i).onBackStackChanged();
2712            }
2713        }
2714    }
2715
2716    void addBackStackState(BackStackRecord state) {
2717        if (mBackStack == null) {
2718            mBackStack = new ArrayList<BackStackRecord>();
2719        }
2720        mBackStack.add(state);
2721    }
2722
2723    @SuppressWarnings("unused")
2724    boolean popBackStackState(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop,
2725            String name, int id, int flags) {
2726        if (mBackStack == null) {
2727            return false;
2728        }
2729        if (name == null && id < 0 && (flags & POP_BACK_STACK_INCLUSIVE) == 0) {
2730            int last = mBackStack.size() - 1;
2731            if (last < 0) {
2732                return false;
2733            }
2734            records.add(mBackStack.remove(last));
2735            isRecordPop.add(true);
2736        } else {
2737            int index = -1;
2738            if (name != null || id >= 0) {
2739                // If a name or ID is specified, look for that place in
2740                // the stack.
2741                index = mBackStack.size()-1;
2742                while (index >= 0) {
2743                    BackStackRecord bss = mBackStack.get(index);
2744                    if (name != null && name.equals(bss.getName())) {
2745                        break;
2746                    }
2747                    if (id >= 0 && id == bss.mIndex) {
2748                        break;
2749                    }
2750                    index--;
2751                }
2752                if (index < 0) {
2753                    return false;
2754                }
2755                if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
2756                    index--;
2757                    // Consume all following entries that match.
2758                    while (index >= 0) {
2759                        BackStackRecord bss = mBackStack.get(index);
2760                        if ((name != null && name.equals(bss.getName()))
2761                                || (id >= 0 && id == bss.mIndex)) {
2762                            index--;
2763                            continue;
2764                        }
2765                        break;
2766                    }
2767                }
2768            }
2769            if (index == mBackStack.size()-1) {
2770                return false;
2771            }
2772            for (int i = mBackStack.size() - 1; i > index; i--) {
2773                records.add(mBackStack.remove(i));
2774                isRecordPop.add(true);
2775            }
2776        }
2777        return true;
2778    }
2779
2780    FragmentManagerNonConfig retainNonConfig() {
2781        setRetaining(mSavedNonConfig);
2782        return mSavedNonConfig;
2783    }
2784
2785    /**
2786     * Recurse the FragmentManagerNonConfig fragments and set the mRetaining to true. This
2787     * was previously done while saving the non-config state, but that has been moved to
2788     * {@link #saveNonConfig()} called from {@link #saveAllState()}. If mRetaining is set too
2789     * early, the fragment won't be destroyed when the FragmentManager is destroyed.
2790     */
2791    private static void setRetaining(FragmentManagerNonConfig nonConfig) {
2792        if (nonConfig == null) {
2793            return;
2794        }
2795        List<Fragment> fragments = nonConfig.getFragments();
2796        if (fragments != null) {
2797            for (Fragment fragment : fragments) {
2798                fragment.mRetaining = true;
2799            }
2800        }
2801        List<FragmentManagerNonConfig> children = nonConfig.getChildNonConfigs();
2802        if (children != null) {
2803            for (FragmentManagerNonConfig child : children) {
2804                setRetaining(child);
2805            }
2806        }
2807    }
2808
2809    void saveNonConfig() {
2810        ArrayList<Fragment> fragments = null;
2811        ArrayList<FragmentManagerNonConfig> childFragments = null;
2812        if (mActive != null) {
2813            for (int i=0; i<mActive.size(); i++) {
2814                Fragment f = mActive.valueAt(i);
2815                if (f != null) {
2816                    if (f.mRetainInstance) {
2817                        if (fragments == null) {
2818                            fragments = new ArrayList<Fragment>();
2819                        }
2820                        fragments.add(f);
2821                        f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
2822                        if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
2823                    }
2824                    FragmentManagerNonConfig child;
2825                    if (f.mChildFragmentManager != null) {
2826                        f.mChildFragmentManager.saveNonConfig();
2827                        child = f.mChildFragmentManager.mSavedNonConfig;
2828                    } else {
2829                        // f.mChildNonConfig may be not null, when the parent fragment is
2830                        // in the backstack.
2831                        child = f.mChildNonConfig;
2832                    }
2833
2834                    if (childFragments == null && child != null) {
2835                        childFragments = new ArrayList<>(mActive.size());
2836                        for (int j = 0; j < i; j++) {
2837                            childFragments.add(null);
2838                        }
2839                    }
2840
2841                    if (childFragments != null) {
2842                        childFragments.add(child);
2843                    }
2844                }
2845            }
2846        }
2847        if (fragments == null && childFragments == null) {
2848            mSavedNonConfig = null;
2849        } else {
2850            mSavedNonConfig = new FragmentManagerNonConfig(fragments, childFragments);
2851        }
2852    }
2853
2854    void saveFragmentViewState(Fragment f) {
2855        if (f.mInnerView == null) {
2856            return;
2857        }
2858        if (mStateArray == null) {
2859            mStateArray = new SparseArray<Parcelable>();
2860        } else {
2861            mStateArray.clear();
2862        }
2863        f.mInnerView.saveHierarchyState(mStateArray);
2864        if (mStateArray.size() > 0) {
2865            f.mSavedViewState = mStateArray;
2866            mStateArray = null;
2867        }
2868    }
2869
2870    Bundle saveFragmentBasicState(Fragment f) {
2871        Bundle result = null;
2872
2873        if (mStateBundle == null) {
2874            mStateBundle = new Bundle();
2875        }
2876        f.performSaveInstanceState(mStateBundle);
2877        dispatchOnFragmentSaveInstanceState(f, mStateBundle, false);
2878        if (!mStateBundle.isEmpty()) {
2879            result = mStateBundle;
2880            mStateBundle = null;
2881        }
2882
2883        if (f.mView != null) {
2884            saveFragmentViewState(f);
2885        }
2886        if (f.mSavedViewState != null) {
2887            if (result == null) {
2888                result = new Bundle();
2889            }
2890            result.putSparseParcelableArray(
2891                    FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
2892        }
2893        if (!f.mUserVisibleHint) {
2894            if (result == null) {
2895                result = new Bundle();
2896            }
2897            // Only add this if it's not the default value
2898            result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
2899        }
2900
2901        return result;
2902    }
2903
2904    Parcelable saveAllState() {
2905        // Make sure all pending operations have now been executed to get
2906        // our state update-to-date.
2907        forcePostponedTransactions();
2908        endAnimatingAwayFragments();
2909        execPendingActions();
2910
2911        mStateSaved = true;
2912        mSavedNonConfig = null;
2913
2914        if (mActive == null || mActive.size() <= 0) {
2915            return null;
2916        }
2917
2918        // First collect all active fragments.
2919        int N = mActive.size();
2920        FragmentState[] active = new FragmentState[N];
2921        boolean haveFragments = false;
2922        for (int i=0; i<N; i++) {
2923            Fragment f = mActive.valueAt(i);
2924            if (f != null) {
2925                if (f.mIndex < 0) {
2926                    throwException(new IllegalStateException(
2927                            "Failure saving state: active " + f
2928                            + " has cleared index: " + f.mIndex));
2929                }
2930
2931                haveFragments = true;
2932
2933                FragmentState fs = new FragmentState(f);
2934                active[i] = fs;
2935
2936                if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
2937                    fs.mSavedFragmentState = saveFragmentBasicState(f);
2938
2939                    if (f.mTarget != null) {
2940                        if (f.mTarget.mIndex < 0) {
2941                            throwException(new IllegalStateException(
2942                                    "Failure saving state: " + f
2943                                    + " has target not in fragment manager: " + f.mTarget));
2944                        }
2945                        if (fs.mSavedFragmentState == null) {
2946                            fs.mSavedFragmentState = new Bundle();
2947                        }
2948                        putFragment(fs.mSavedFragmentState,
2949                                FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
2950                        if (f.mTargetRequestCode != 0) {
2951                            fs.mSavedFragmentState.putInt(
2952                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
2953                                    f.mTargetRequestCode);
2954                        }
2955                    }
2956
2957                } else {
2958                    fs.mSavedFragmentState = f.mSavedFragmentState;
2959                }
2960
2961                if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
2962                        + fs.mSavedFragmentState);
2963            }
2964        }
2965
2966        if (!haveFragments) {
2967            if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
2968            return null;
2969        }
2970
2971        int[] added = null;
2972        BackStackState[] backStack = null;
2973
2974        // Build list of currently added fragments.
2975        if (mAdded != null) {
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) Log.v(TAG, "saveAllState: adding fragment #" + i
2987                            + ": " + mAdded.get(i));
2988                }
2989            }
2990        }
2991
2992        // Now save back stack.
2993        if (mBackStack != null) {
2994            N = mBackStack.size();
2995            if (N > 0) {
2996                backStack = new BackStackState[N];
2997                for (int i=0; i<N; i++) {
2998                    backStack[i] = new BackStackState(mBackStack.get(i));
2999                    if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
3000                            + ": " + mBackStack.get(i));
3001                }
3002            }
3003        }
3004
3005        FragmentManagerState fms = new FragmentManagerState();
3006        fms.mActive = active;
3007        fms.mAdded = added;
3008        fms.mBackStack = backStack;
3009        if (mPrimaryNav != null) {
3010            fms.mPrimaryNavActiveIndex = mPrimaryNav.mIndex;
3011        }
3012        fms.mNextFragmentIndex = mNextFragmentIndex;
3013        saveNonConfig();
3014        return fms;
3015    }
3016
3017    void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) {
3018        // If there is no saved state at all, then there can not be
3019        // any nonConfig fragments either, so that is that.
3020        if (state == null) return;
3021        FragmentManagerState fms = (FragmentManagerState)state;
3022        if (fms.mActive == null) return;
3023
3024        List<FragmentManagerNonConfig> childNonConfigs = null;
3025
3026        // First re-attach any non-config instances we are retaining back
3027        // to their saved state, so we don't try to instantiate them again.
3028        if (nonConfig != null) {
3029            List<Fragment> nonConfigFragments = nonConfig.getFragments();
3030            childNonConfigs = nonConfig.getChildNonConfigs();
3031            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
3032            for (int i = 0; i < count; i++) {
3033                Fragment f = nonConfigFragments.get(i);
3034                if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
3035                int index = 0; // index into fms.mActive
3036                while (index < fms.mActive.length && fms.mActive[index].mIndex != f.mIndex) {
3037                    index++;
3038                }
3039                if (index == fms.mActive.length) {
3040                    throwException(new IllegalStateException("Could not find active fragment "
3041                            + "with index " + f.mIndex));
3042                }
3043                FragmentState fs = fms.mActive[index];
3044                fs.mInstance = f;
3045                f.mSavedViewState = null;
3046                f.mBackStackNesting = 0;
3047                f.mInLayout = false;
3048                f.mAdded = false;
3049                f.mTarget = null;
3050                if (fs.mSavedFragmentState != null) {
3051                    fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader());
3052                    f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
3053                            FragmentManagerImpl.VIEW_STATE_TAG);
3054                    f.mSavedFragmentState = fs.mSavedFragmentState;
3055                }
3056            }
3057        }
3058
3059        // Build the full list of active fragments, instantiating them from
3060        // their saved state.
3061        mActive = new SparseArray<>(fms.mActive.length);
3062        for (int i=0; i<fms.mActive.length; i++) {
3063            FragmentState fs = fms.mActive[i];
3064            if (fs != null) {
3065                FragmentManagerNonConfig childNonConfig = null;
3066                if (childNonConfigs != null && i < childNonConfigs.size()) {
3067                    childNonConfig = childNonConfigs.get(i);
3068                }
3069                Fragment f = fs.instantiate(mHost, mContainer, mParent, childNonConfig);
3070                if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
3071                mActive.put(f.mIndex, f);
3072                // Now that the fragment is instantiated (or came from being
3073                // retained above), clear mInstance in case we end up re-restoring
3074                // from this FragmentState again.
3075                fs.mInstance = null;
3076            }
3077        }
3078
3079        // Update the target of all retained fragments.
3080        if (nonConfig != null) {
3081            List<Fragment> nonConfigFragments = nonConfig.getFragments();
3082            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
3083            for (int i = 0; i < count; i++) {
3084                Fragment f = nonConfigFragments.get(i);
3085                if (f.mTargetIndex >= 0) {
3086                    f.mTarget = mActive.get(f.mTargetIndex);
3087                    if (f.mTarget == null) {
3088                        Log.w(TAG, "Re-attaching retained fragment " + f
3089                                + " target no longer exists: " + f.mTargetIndex);
3090                    }
3091                }
3092            }
3093        }
3094
3095        // Build the list of currently added fragments.
3096        if (fms.mAdded != null) {
3097            mAdded = new ArrayList<Fragment>(fms.mAdded.length);
3098            for (int i=0; i<fms.mAdded.length; i++) {
3099                Fragment f = mActive.get(fms.mAdded[i]);
3100                if (f == null) {
3101                    throwException(new IllegalStateException(
3102                            "No instantiated fragment for index #" + fms.mAdded[i]));
3103                }
3104                f.mAdded = true;
3105                if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
3106                if (mAdded.contains(f)) {
3107                    throw new IllegalStateException("Already added!");
3108                }
3109                synchronized (mAdded) {
3110                    mAdded.add(f);
3111                }
3112            }
3113        } else {
3114            mAdded = null;
3115        }
3116
3117        // Build the back stack.
3118        if (fms.mBackStack != null) {
3119            mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
3120            for (int i=0; i<fms.mBackStack.length; i++) {
3121                BackStackRecord bse = fms.mBackStack[i].instantiate(this);
3122                if (DEBUG) {
3123                    Log.v(TAG, "restoreAllState: back stack #" + i
3124                        + " (index " + bse.mIndex + "): " + bse);
3125                    LogWriter logw = new LogWriter(TAG);
3126                    PrintWriter pw = new PrintWriter(logw);
3127                    bse.dump("  ", pw, false);
3128                    pw.close();
3129                }
3130                mBackStack.add(bse);
3131                if (bse.mIndex >= 0) {
3132                    setBackStackIndex(bse.mIndex, bse);
3133                }
3134            }
3135        } else {
3136            mBackStack = null;
3137        }
3138
3139        if (fms.mPrimaryNavActiveIndex >= 0) {
3140            mPrimaryNav = mActive.get(fms.mPrimaryNavActiveIndex);
3141        }
3142        this.mNextFragmentIndex = fms.mNextFragmentIndex;
3143    }
3144
3145    /**
3146     * To prevent list modification errors, mActive sets values to null instead of
3147     * removing them when the Fragment becomes inactive. This cleans up the list at the
3148     * end of executing the transactions.
3149     */
3150    private void burpActive() {
3151        if (mActive != null) {
3152            for (int i = mActive.size() - 1; i >= 0; i--) {
3153                if (mActive.valueAt(i) == null) {
3154                    mActive.delete(mActive.keyAt(i));
3155                }
3156            }
3157        }
3158    }
3159
3160    public void attachController(FragmentHostCallback host,
3161            FragmentContainer container, Fragment parent) {
3162        if (mHost != null) throw new IllegalStateException("Already attached");
3163        mHost = host;
3164        mContainer = container;
3165        mParent = parent;
3166    }
3167
3168    public void noteStateNotSaved() {
3169        mSavedNonConfig = null;
3170        mStateSaved = false;
3171        final int addedCount = mAdded == null ? 0 : mAdded.size();
3172        for (int i = 0; i < addedCount; i++) {
3173            Fragment fragment = mAdded.get(i);
3174            if (fragment != null) {
3175                fragment.noteStateNotSaved();
3176            }
3177        }
3178    }
3179
3180    public void dispatchCreate() {
3181        mStateSaved = false;
3182        dispatchStateChange(Fragment.CREATED);
3183    }
3184
3185    public void dispatchActivityCreated() {
3186        mStateSaved = false;
3187        dispatchStateChange(Fragment.ACTIVITY_CREATED);
3188    }
3189
3190    public void dispatchStart() {
3191        mStateSaved = false;
3192        dispatchStateChange(Fragment.STARTED);
3193    }
3194
3195    public void dispatchResume() {
3196        mStateSaved = false;
3197        dispatchStateChange(Fragment.RESUMED);
3198    }
3199
3200    public void dispatchPause() {
3201        dispatchStateChange(Fragment.STARTED);
3202    }
3203
3204    public void dispatchStop() {
3205        // See saveAllState() for the explanation of this.  We do this for
3206        // all platform versions, to keep our behavior more consistent between
3207        // them.
3208        mStateSaved = true;
3209
3210        dispatchStateChange(Fragment.STOPPED);
3211    }
3212
3213    public void dispatchReallyStop() {
3214        dispatchStateChange(Fragment.ACTIVITY_CREATED);
3215    }
3216
3217    public void dispatchDestroyView() {
3218        dispatchStateChange(Fragment.CREATED);
3219    }
3220
3221    public void dispatchDestroy() {
3222        mDestroyed = true;
3223        execPendingActions();
3224        dispatchStateChange(Fragment.INITIALIZING);
3225        mHost = null;
3226        mContainer = null;
3227        mParent = null;
3228    }
3229
3230    private void dispatchStateChange(int nextState) {
3231        try {
3232            mExecutingActions = true;
3233            moveToState(nextState, false);
3234        } finally {
3235            mExecutingActions = false;
3236        }
3237        execPendingActions();
3238    }
3239
3240    public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) {
3241        if (mAdded == null) {
3242            return;
3243        }
3244        for (int i = mAdded.size() - 1; i >= 0; --i) {
3245            final android.support.v4.app.Fragment f = mAdded.get(i);
3246            if (f != null) {
3247                f.performMultiWindowModeChanged(isInMultiWindowMode);
3248            }
3249        }
3250    }
3251
3252    public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
3253        if (mAdded == null) {
3254            return;
3255        }
3256        for (int i = mAdded.size() - 1; i >= 0; --i) {
3257            final android.support.v4.app.Fragment f = mAdded.get(i);
3258            if (f != null) {
3259                f.performPictureInPictureModeChanged(isInPictureInPictureMode);
3260            }
3261        }
3262    }
3263
3264    public void dispatchConfigurationChanged(Configuration newConfig) {
3265        if (mAdded != null) {
3266            for (int i=0; i<mAdded.size(); i++) {
3267                Fragment f = mAdded.get(i);
3268                if (f != null) {
3269                    f.performConfigurationChanged(newConfig);
3270                }
3271            }
3272        }
3273    }
3274
3275    public void dispatchLowMemory() {
3276        if (mAdded != null) {
3277            for (int i=0; i<mAdded.size(); i++) {
3278                Fragment f = mAdded.get(i);
3279                if (f != null) {
3280                    f.performLowMemory();
3281                }
3282            }
3283        }
3284    }
3285
3286    public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
3287        boolean show = false;
3288        ArrayList<Fragment> newMenus = null;
3289        if (mAdded != null) {
3290            for (int i=0; i<mAdded.size(); i++) {
3291                Fragment f = mAdded.get(i);
3292                if (f != null) {
3293                    if (f.performCreateOptionsMenu(menu, inflater)) {
3294                        show = true;
3295                        if (newMenus == null) {
3296                            newMenus = new ArrayList<Fragment>();
3297                        }
3298                        newMenus.add(f);
3299                    }
3300                }
3301            }
3302        }
3303
3304        if (mCreatedMenus != null) {
3305            for (int i=0; i<mCreatedMenus.size(); i++) {
3306                Fragment f = mCreatedMenus.get(i);
3307                if (newMenus == null || !newMenus.contains(f)) {
3308                    f.onDestroyOptionsMenu();
3309                }
3310            }
3311        }
3312
3313        mCreatedMenus = newMenus;
3314
3315        return show;
3316    }
3317
3318    public boolean dispatchPrepareOptionsMenu(Menu menu) {
3319        boolean show = false;
3320        if (mAdded != null) {
3321            for (int i=0; i<mAdded.size(); i++) {
3322                Fragment f = mAdded.get(i);
3323                if (f != null) {
3324                    if (f.performPrepareOptionsMenu(menu)) {
3325                        show = true;
3326                    }
3327                }
3328            }
3329        }
3330        return show;
3331    }
3332
3333    public boolean dispatchOptionsItemSelected(MenuItem item) {
3334        if (mAdded != null) {
3335            for (int i=0; i<mAdded.size(); i++) {
3336                Fragment f = mAdded.get(i);
3337                if (f != null) {
3338                    if (f.performOptionsItemSelected(item)) {
3339                        return true;
3340                    }
3341                }
3342            }
3343        }
3344        return false;
3345    }
3346
3347    public boolean dispatchContextItemSelected(MenuItem item) {
3348        if (mAdded != null) {
3349            for (int i=0; i<mAdded.size(); i++) {
3350                Fragment f = mAdded.get(i);
3351                if (f != null) {
3352                    if (f.performContextItemSelected(item)) {
3353                        return true;
3354                    }
3355                }
3356            }
3357        }
3358        return false;
3359    }
3360
3361    public void dispatchOptionsMenuClosed(Menu menu) {
3362        if (mAdded != null) {
3363            for (int i=0; i<mAdded.size(); i++) {
3364                Fragment f = mAdded.get(i);
3365                if (f != null) {
3366                    f.performOptionsMenuClosed(menu);
3367                }
3368            }
3369        }
3370    }
3371
3372    @SuppressWarnings("ReferenceEquality")
3373    public void setPrimaryNavigationFragment(Fragment f) {
3374        if (f != null && (mActive.get(f.mIndex) != f
3375            || (f.mHost != null && f.getFragmentManager() != this))) {
3376            throw new IllegalArgumentException("Fragment " + f
3377                    + " is not an active fragment of FragmentManager " + this);
3378        }
3379        mPrimaryNav = f;
3380    }
3381
3382    @Override
3383    public Fragment getPrimaryNavigationFragment() {
3384        return mPrimaryNav;
3385    }
3386
3387    @Override
3388    public void registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb,
3389            boolean recursive) {
3390        if (mLifecycleCallbacks == null) {
3391            mLifecycleCallbacks = new CopyOnWriteArrayList<>();
3392        }
3393        mLifecycleCallbacks.add(new Pair<>(cb, recursive));
3394    }
3395
3396    @Override
3397    public void unregisterFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb) {
3398        if (mLifecycleCallbacks == null) {
3399            return;
3400        }
3401
3402        synchronized (mLifecycleCallbacks) {
3403            for (int i = 0, N = mLifecycleCallbacks.size(); i < N; i++) {
3404                if (mLifecycleCallbacks.get(i).first == cb) {
3405                    mLifecycleCallbacks.remove(i);
3406                    break;
3407                }
3408            }
3409        }
3410    }
3411
3412    void dispatchOnFragmentPreAttached(Fragment f, Context context, boolean onlyRecursive) {
3413        if (mParent != null) {
3414            FragmentManager parentManager = mParent.getFragmentManager();
3415            if (parentManager instanceof FragmentManagerImpl) {
3416                ((FragmentManagerImpl) parentManager)
3417                        .dispatchOnFragmentPreAttached(f, context, true);
3418            }
3419        }
3420        if (mLifecycleCallbacks == null) {
3421            return;
3422        }
3423        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3424            if (!onlyRecursive || p.second) {
3425                p.first.onFragmentPreAttached(this, f, context);
3426            }
3427        }
3428    }
3429
3430    void dispatchOnFragmentAttached(Fragment f, Context context, boolean onlyRecursive) {
3431        if (mParent != null) {
3432            FragmentManager parentManager = mParent.getFragmentManager();
3433            if (parentManager instanceof FragmentManagerImpl) {
3434                ((FragmentManagerImpl) parentManager)
3435                        .dispatchOnFragmentAttached(f, context, true);
3436            }
3437        }
3438        if (mLifecycleCallbacks == null) {
3439            return;
3440        }
3441        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3442            if (!onlyRecursive || p.second) {
3443                p.first.onFragmentAttached(this, f, context);
3444            }
3445        }
3446    }
3447
3448    void dispatchOnFragmentPreCreated(Fragment f, Bundle savedInstanceState,
3449            boolean onlyRecursive) {
3450        if (mParent != null) {
3451            FragmentManager parentManager = mParent.getFragmentManager();
3452            if (parentManager instanceof FragmentManagerImpl) {
3453                ((FragmentManagerImpl) parentManager)
3454                        .dispatchOnFragmentPreCreated(f, savedInstanceState, true);
3455            }
3456        }
3457        if (mLifecycleCallbacks == null) {
3458            return;
3459        }
3460        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3461            if (!onlyRecursive || p.second) {
3462                p.first.onFragmentPreCreated(this, f, savedInstanceState);
3463            }
3464        }
3465    }
3466
3467    void dispatchOnFragmentCreated(Fragment f, Bundle savedInstanceState, boolean onlyRecursive) {
3468        if (mParent != null) {
3469            FragmentManager parentManager = mParent.getFragmentManager();
3470            if (parentManager instanceof FragmentManagerImpl) {
3471                ((FragmentManagerImpl) parentManager)
3472                        .dispatchOnFragmentCreated(f, savedInstanceState, true);
3473            }
3474        }
3475        if (mLifecycleCallbacks == null) {
3476            return;
3477        }
3478        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3479            if (!onlyRecursive || p.second) {
3480                p.first.onFragmentCreated(this, f, savedInstanceState);
3481            }
3482        }
3483    }
3484
3485    void dispatchOnFragmentActivityCreated(Fragment f, Bundle savedInstanceState,
3486            boolean onlyRecursive) {
3487        if (mParent != null) {
3488            FragmentManager parentManager = mParent.getFragmentManager();
3489            if (parentManager instanceof FragmentManagerImpl) {
3490                ((FragmentManagerImpl) parentManager)
3491                        .dispatchOnFragmentActivityCreated(f, savedInstanceState, true);
3492            }
3493        }
3494        if (mLifecycleCallbacks == null) {
3495            return;
3496        }
3497        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3498            if (!onlyRecursive || p.second) {
3499                p.first.onFragmentActivityCreated(this, f, savedInstanceState);
3500            }
3501        }
3502    }
3503
3504    void dispatchOnFragmentViewCreated(Fragment f, View v, Bundle savedInstanceState,
3505            boolean onlyRecursive) {
3506        if (mParent != null) {
3507            FragmentManager parentManager = mParent.getFragmentManager();
3508            if (parentManager instanceof FragmentManagerImpl) {
3509                ((FragmentManagerImpl) parentManager)
3510                        .dispatchOnFragmentViewCreated(f, v, savedInstanceState, true);
3511            }
3512        }
3513        if (mLifecycleCallbacks == null) {
3514            return;
3515        }
3516        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3517            if (!onlyRecursive || p.second) {
3518                p.first.onFragmentViewCreated(this, f, v, savedInstanceState);
3519            }
3520        }
3521    }
3522
3523    void dispatchOnFragmentStarted(Fragment f, boolean onlyRecursive) {
3524        if (mParent != null) {
3525            FragmentManager parentManager = mParent.getFragmentManager();
3526            if (parentManager instanceof FragmentManagerImpl) {
3527                ((FragmentManagerImpl) parentManager)
3528                        .dispatchOnFragmentStarted(f, true);
3529            }
3530        }
3531        if (mLifecycleCallbacks == null) {
3532            return;
3533        }
3534        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3535            if (!onlyRecursive || p.second) {
3536                p.first.onFragmentStarted(this, f);
3537            }
3538        }
3539    }
3540
3541    void dispatchOnFragmentResumed(Fragment f, boolean onlyRecursive) {
3542        if (mParent != null) {
3543            FragmentManager parentManager = mParent.getFragmentManager();
3544            if (parentManager instanceof FragmentManagerImpl) {
3545                ((FragmentManagerImpl) parentManager)
3546                        .dispatchOnFragmentResumed(f, true);
3547            }
3548        }
3549        if (mLifecycleCallbacks == null) {
3550            return;
3551        }
3552        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3553            if (!onlyRecursive || p.second) {
3554                p.first.onFragmentResumed(this, f);
3555            }
3556        }
3557    }
3558
3559    void dispatchOnFragmentPaused(Fragment f, boolean onlyRecursive) {
3560        if (mParent != null) {
3561            FragmentManager parentManager = mParent.getFragmentManager();
3562            if (parentManager instanceof FragmentManagerImpl) {
3563                ((FragmentManagerImpl) parentManager)
3564                        .dispatchOnFragmentPaused(f, true);
3565            }
3566        }
3567        if (mLifecycleCallbacks == null) {
3568            return;
3569        }
3570        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3571            if (!onlyRecursive || p.second) {
3572                p.first.onFragmentPaused(this, f);
3573            }
3574        }
3575    }
3576
3577    void dispatchOnFragmentStopped(Fragment f, boolean onlyRecursive) {
3578        if (mParent != null) {
3579            FragmentManager parentManager = mParent.getFragmentManager();
3580            if (parentManager instanceof FragmentManagerImpl) {
3581                ((FragmentManagerImpl) parentManager)
3582                        .dispatchOnFragmentStopped(f, true);
3583            }
3584        }
3585        if (mLifecycleCallbacks == null) {
3586            return;
3587        }
3588        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3589            if (!onlyRecursive || p.second) {
3590                p.first.onFragmentStopped(this, f);
3591            }
3592        }
3593    }
3594
3595    void dispatchOnFragmentSaveInstanceState(Fragment f, Bundle outState, boolean onlyRecursive) {
3596        if (mParent != null) {
3597            FragmentManager parentManager = mParent.getFragmentManager();
3598            if (parentManager instanceof FragmentManagerImpl) {
3599                ((FragmentManagerImpl) parentManager)
3600                        .dispatchOnFragmentSaveInstanceState(f, outState, true);
3601            }
3602        }
3603        if (mLifecycleCallbacks == null) {
3604            return;
3605        }
3606        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3607            if (!onlyRecursive || p.second) {
3608                p.first.onFragmentSaveInstanceState(this, f, outState);
3609            }
3610        }
3611    }
3612
3613    void dispatchOnFragmentViewDestroyed(Fragment f, boolean onlyRecursive) {
3614        if (mParent != null) {
3615            FragmentManager parentManager = mParent.getFragmentManager();
3616            if (parentManager instanceof FragmentManagerImpl) {
3617                ((FragmentManagerImpl) parentManager)
3618                        .dispatchOnFragmentViewDestroyed(f, true);
3619            }
3620        }
3621        if (mLifecycleCallbacks == null) {
3622            return;
3623        }
3624        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3625            if (!onlyRecursive || p.second) {
3626                p.first.onFragmentViewDestroyed(this, f);
3627            }
3628        }
3629    }
3630
3631    void dispatchOnFragmentDestroyed(Fragment f, boolean onlyRecursive) {
3632        if (mParent != null) {
3633            FragmentManager parentManager = mParent.getFragmentManager();
3634            if (parentManager instanceof FragmentManagerImpl) {
3635                ((FragmentManagerImpl) parentManager)
3636                        .dispatchOnFragmentDestroyed(f, true);
3637            }
3638        }
3639        if (mLifecycleCallbacks == null) {
3640            return;
3641        }
3642        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3643            if (!onlyRecursive || p.second) {
3644                p.first.onFragmentDestroyed(this, f);
3645            }
3646        }
3647    }
3648
3649    void dispatchOnFragmentDetached(Fragment f, boolean onlyRecursive) {
3650        if (mParent != null) {
3651            FragmentManager parentManager = mParent.getFragmentManager();
3652            if (parentManager instanceof FragmentManagerImpl) {
3653                ((FragmentManagerImpl) parentManager)
3654                        .dispatchOnFragmentDetached(f, true);
3655            }
3656        }
3657        if (mLifecycleCallbacks == null) {
3658            return;
3659        }
3660        for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) {
3661            if (!onlyRecursive || p.second) {
3662                p.first.onFragmentDetached(this, f);
3663            }
3664        }
3665    }
3666
3667    public static int reverseTransit(int transit) {
3668        int rev = 0;
3669        switch (transit) {
3670            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
3671                rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
3672                break;
3673            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
3674                rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
3675                break;
3676            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
3677                rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
3678                break;
3679        }
3680        return rev;
3681
3682    }
3683
3684    public static final int ANIM_STYLE_OPEN_ENTER = 1;
3685    public static final int ANIM_STYLE_OPEN_EXIT = 2;
3686    public static final int ANIM_STYLE_CLOSE_ENTER = 3;
3687    public static final int ANIM_STYLE_CLOSE_EXIT = 4;
3688    public static final int ANIM_STYLE_FADE_ENTER = 5;
3689    public static final int ANIM_STYLE_FADE_EXIT = 6;
3690
3691    public static int transitToStyleIndex(int transit, boolean enter) {
3692        int animAttr = -1;
3693        switch (transit) {
3694            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
3695                animAttr = enter ? ANIM_STYLE_OPEN_ENTER : ANIM_STYLE_OPEN_EXIT;
3696                break;
3697            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
3698                animAttr = enter ? ANIM_STYLE_CLOSE_ENTER : ANIM_STYLE_CLOSE_EXIT;
3699                break;
3700            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
3701                animAttr = enter ? ANIM_STYLE_FADE_ENTER : ANIM_STYLE_FADE_EXIT;
3702                break;
3703        }
3704        return animAttr;
3705    }
3706
3707    @Override
3708    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
3709        if (!"fragment".equals(name)) {
3710            return null;
3711        }
3712
3713        String fname = attrs.getAttributeValue(null, "class");
3714        TypedArray a =  context.obtainStyledAttributes(attrs, FragmentTag.Fragment);
3715        if (fname == null) {
3716            fname = a.getString(FragmentTag.Fragment_name);
3717        }
3718        int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID);
3719        String tag = a.getString(FragmentTag.Fragment_tag);
3720        a.recycle();
3721
3722        if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) {
3723            // Invalid support lib fragment; let the device's framework handle it.
3724            // This will allow android.app.Fragments to do the right thing.
3725            return null;
3726        }
3727
3728        int containerId = parent != null ? parent.getId() : 0;
3729        if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
3730            throw new IllegalArgumentException(attrs.getPositionDescription()
3731                    + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
3732        }
3733
3734        // If we restored from a previous state, we may already have
3735        // instantiated this fragment from the state and should use
3736        // that instance instead of making a new one.
3737        Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
3738        if (fragment == null && tag != null) {
3739            fragment = findFragmentByTag(tag);
3740        }
3741        if (fragment == null && containerId != View.NO_ID) {
3742            fragment = findFragmentById(containerId);
3743        }
3744
3745        if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
3746                + Integer.toHexString(id) + " fname=" + fname
3747                + " existing=" + fragment);
3748        if (fragment == null) {
3749            fragment = mContainer.instantiate(context, fname, null);
3750            fragment.mFromLayout = true;
3751            fragment.mFragmentId = id != 0 ? id : containerId;
3752            fragment.mContainerId = containerId;
3753            fragment.mTag = tag;
3754            fragment.mInLayout = true;
3755            fragment.mFragmentManager = this;
3756            fragment.mHost = mHost;
3757            fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
3758            addFragment(fragment, true);
3759
3760        } else if (fragment.mInLayout) {
3761            // A fragment already exists and it is not one we restored from
3762            // previous state.
3763            throw new IllegalArgumentException(attrs.getPositionDescription()
3764                    + ": Duplicate id 0x" + Integer.toHexString(id)
3765                    + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
3766                    + " with another fragment for " + fname);
3767        } else {
3768            // This fragment was retained from a previous instance; get it
3769            // going now.
3770            fragment.mInLayout = true;
3771            fragment.mHost = mHost;
3772            // If this fragment is newly instantiated (either right now, or
3773            // from last saved state), then give it the attributes to
3774            // initialize itself.
3775            if (!fragment.mRetaining) {
3776                fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
3777            }
3778        }
3779
3780        // If we haven't finished entering the CREATED state ourselves yet,
3781        // push the inflated child fragment along. This will ensureInflatedFragmentView
3782        // at the right phase of the lifecycle so that we will have mView populated
3783        // for compliant fragments below.
3784        if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
3785            moveToState(fragment, Fragment.CREATED, 0, 0, false);
3786        } else {
3787            moveToState(fragment);
3788        }
3789
3790        if (fragment.mView == null) {
3791            throw new IllegalStateException("Fragment " + fname
3792                    + " did not create a view.");
3793        }
3794        if (id != 0) {
3795            fragment.mView.setId(id);
3796        }
3797        if (fragment.mView.getTag() == null) {
3798            fragment.mView.setTag(tag);
3799        }
3800        return fragment.mView;
3801    }
3802
3803    @Override
3804    public View onCreateView(String name, Context context, AttributeSet attrs) {
3805        return onCreateView(null, name, context, attrs);
3806    }
3807
3808    LayoutInflater.Factory2 getLayoutInflaterFactory() {
3809        return this;
3810    }
3811
3812    static class FragmentTag {
3813        public static final int[] Fragment = {
3814                0x01010003, 0x010100d0, 0x010100d1
3815        };
3816        public static final int Fragment_id = 1;
3817        public static final int Fragment_name = 0;
3818        public static final int Fragment_tag = 2;
3819    }
3820
3821    /**
3822     * An add or pop transaction to be scheduled for the UI thread.
3823     */
3824    interface OpGenerator {
3825        /**
3826         * Generate transactions to add to {@code records} and whether or not the transaction is
3827         * an add or pop to {@code isRecordPop}.
3828         *
3829         * records and isRecordPop must be added equally so that each transaction in records
3830         * matches the boolean for whether or not it is a pop in isRecordPop.
3831         *
3832         * @param records A list to add transactions to.
3833         * @param isRecordPop A list to add whether or not the transactions added to records is
3834         *                    a pop transaction.
3835         * @return true if something was added or false otherwise.
3836         */
3837        boolean generateOps(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop);
3838    }
3839
3840    /**
3841     * A pop operation OpGenerator. This will be run on the UI thread and will generate the
3842     * transactions that will be popped if anything can be popped.
3843     */
3844    private class PopBackStackState implements OpGenerator {
3845        final String mName;
3846        final int mId;
3847        final int mFlags;
3848
3849        PopBackStackState(String name, int id, int flags) {
3850            mName = name;
3851            mId = id;
3852            mFlags = flags;
3853        }
3854
3855        @Override
3856        public boolean generateOps(ArrayList<BackStackRecord> records,
3857                ArrayList<Boolean> isRecordPop) {
3858            if (mPrimaryNav != null // We have a primary nav fragment
3859                    && mId < 0 // No valid id (since they're local)
3860                    && mName == null) { // no name to pop to (since they're local)
3861                final FragmentManager childManager = mPrimaryNav.peekChildFragmentManager();
3862                if (childManager != null && childManager.popBackStackImmediate()) {
3863                    // We didn't add any operations for this FragmentManager even though
3864                    // a child did do work.
3865                    return false;
3866                }
3867            }
3868            return popBackStackState(records, isRecordPop, mName, mId, mFlags);
3869        }
3870    }
3871
3872    /**
3873     * A listener for a postponed transaction. This waits until
3874     * {@link Fragment#startPostponedEnterTransition()} is called or a transaction is started
3875     * that interacts with this one, based on interactions with the fragment container.
3876     */
3877    static class StartEnterTransitionListener
3878            implements Fragment.OnStartEnterTransitionListener {
3879        private final boolean mIsBack;
3880        private final BackStackRecord mRecord;
3881        private int mNumPostponed;
3882
3883        StartEnterTransitionListener(BackStackRecord record, boolean isBack) {
3884            mIsBack = isBack;
3885            mRecord = record;
3886        }
3887
3888        /**
3889         * Called from {@link Fragment#startPostponedEnterTransition()}, this decreases the
3890         * number of Fragments that are postponed. This may cause the transaction to schedule
3891         * to finish running and run transitions and animations.
3892         */
3893        @Override
3894        public void onStartEnterTransition() {
3895            mNumPostponed--;
3896            if (mNumPostponed != 0) {
3897                return;
3898            }
3899            mRecord.mManager.scheduleCommit();
3900        }
3901
3902        /**
3903         * Called from {@link Fragment#
3904         * setOnStartEnterTransitionListener(Fragment.OnStartEnterTransitionListener)}, this
3905         * increases the number of fragments that are postponed as part of this transaction.
3906         */
3907        @Override
3908        public void startListening() {
3909            mNumPostponed++;
3910        }
3911
3912        /**
3913         * @return true if there are no more postponed fragments as part of the transaction.
3914         */
3915        public boolean isReady() {
3916            return mNumPostponed == 0;
3917        }
3918
3919        /**
3920         * Completes the transaction and start the animations and transitions. This may skip
3921         * the transitions if this is called before all fragments have called
3922         * {@link Fragment#startPostponedEnterTransition()}.
3923         */
3924        public void completeTransaction() {
3925            final boolean canceled;
3926            canceled = mNumPostponed > 0;
3927            FragmentManagerImpl manager = mRecord.mManager;
3928            final int numAdded = manager.mAdded.size();
3929            for (int i = 0; i < numAdded; i++) {
3930                final Fragment fragment = manager.mAdded.get(i);
3931                fragment.setOnStartEnterTransitionListener(null);
3932                if (canceled && fragment.isPostponed()) {
3933                    fragment.startPostponedEnterTransition();
3934                }
3935            }
3936            mRecord.mManager.completeExecute(mRecord, mIsBack, !canceled, true);
3937        }
3938
3939        /**
3940         * Cancels this transaction instead of completing it. That means that the state isn't
3941         * changed, so the pop results in no change to the state.
3942         */
3943        public void cancelTransaction() {
3944            mRecord.mManager.completeExecute(mRecord, mIsBack, false, false);
3945        }
3946    }
3947
3948    /**
3949     * Contains either an animator or animation. One of these should be null.
3950     */
3951    private static class AnimationOrAnimator {
3952        public final Animation animation;
3953        public final Animator animator;
3954
3955        private AnimationOrAnimator(Animation animation) {
3956            this.animation = animation;
3957            this.animator = null;
3958            if (animation == null) {
3959                throw new IllegalStateException("Animation cannot be null");
3960            }
3961        }
3962
3963        private AnimationOrAnimator(Animator animator) {
3964            this.animation = null;
3965            this.animator = animator;
3966            if (animator == null) {
3967                throw new IllegalStateException("Animator cannot be null");
3968            }
3969        }
3970    }
3971
3972    /**
3973     * Wrap an AnimationListener that can be null. This allows us to chain animation listeners.
3974     */
3975    private static class AnimationListenerWrapper implements AnimationListener {
3976        private final AnimationListener mWrapped;
3977
3978        private AnimationListenerWrapper(AnimationListener wrapped) {
3979            mWrapped = wrapped;
3980        }
3981
3982        @CallSuper
3983        @Override
3984        public void onAnimationStart(Animation animation) {
3985            if (mWrapped != null) {
3986                mWrapped.onAnimationStart(animation);
3987            }
3988        }
3989
3990        @CallSuper
3991        @Override
3992        public void onAnimationEnd(Animation animation) {
3993            if (mWrapped != null) {
3994                mWrapped.onAnimationEnd(animation);
3995            }
3996        }
3997
3998        @CallSuper
3999        @Override
4000        public void onAnimationRepeat(Animation animation) {
4001            if (mWrapped != null) {
4002                mWrapped.onAnimationRepeat(animation);
4003            }
4004        }
4005    }
4006
4007    /**
4008     * Reset the layer type to LAYER_TYPE_NONE at the end of an animation.
4009     */
4010    private static class AnimateOnHWLayerIfNeededListener extends AnimationListenerWrapper  {
4011        View mView;
4012
4013        AnimateOnHWLayerIfNeededListener(final View v, AnimationListener listener) {
4014            super(listener);
4015            mView = v;
4016        }
4017
4018        @Override
4019        @CallSuper
4020        public void onAnimationEnd(Animation animation) {
4021            // If we're attached to a window, assume we're in the normal performTraversals
4022            // drawing path for Animations running. It's not safe to change the layer type
4023            // during drawing, so post it to the View to run later. If we're not attached
4024            // or we're running on N and above, post it to the view. If we're not on N and
4025            // not attached, do it right now since existing platform versions don't run the
4026            // hwui renderer for detached views off the UI thread making changing layer type
4027            // safe, but posting may not be.
4028            // Prior to N posting to a detached view from a non-Looper thread could cause
4029            // leaks, since the thread-local run queue on a non-Looper thread would never
4030            // be flushed.
4031            if (ViewCompat.isAttachedToWindow(mView) || Build.VERSION.SDK_INT >= 24) {
4032                mView.post(new Runnable() {
4033                    @Override
4034                    public void run() {
4035                        mView.setLayerType(View.LAYER_TYPE_NONE, null);
4036                    }
4037                });
4038            } else {
4039                mView.setLayerType(View.LAYER_TYPE_NONE, null);
4040            }
4041            super.onAnimationEnd(animation);
4042        }
4043    }
4044
4045    /**
4046     * Set the layer type to LAYER_TYPE_HARDWARE while an animator is running.
4047     */
4048    private static class AnimatorOnHWLayerIfNeededListener extends AnimatorListenerAdapter  {
4049        View mView;
4050
4051        AnimatorOnHWLayerIfNeededListener(final View v) {
4052            mView = v;
4053        }
4054
4055        @Override
4056        public void onAnimationStart(Animator animation) {
4057            mView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4058        }
4059
4060        @Override
4061        public void onAnimationEnd(Animator animation) {
4062            mView.setLayerType(View.LAYER_TYPE_NONE, null);
4063            animation.removeListener(this);
4064        }
4065    }
4066}
4067