1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/*
2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project
3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License.
6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at
7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and
14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License.
15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.app;
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
199277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.content.Context;
20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.res.Configuration;
2136bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powellimport android.content.res.Resources.NotFoundException;
220f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powellimport android.content.res.TypedArray;
235e63ab9505a3a4d11374cbbec418c1aba921409dChris Banesimport android.os.Build;
24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Bundle;
25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler;
26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Looper;
27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Parcel;
28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Parcelable;
2903526560f132021f8fd7290259762ab362d4d567Doris Liuimport android.support.annotation.CallSuper;
30a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.IdRes;
31a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbyeimport android.support.annotation.StringRes;
321802bf381380809e224c72008a400715e2e375b4Adam Powellimport android.support.v4.os.BuildCompat;
33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.support.v4.util.DebugUtils;
34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.support.v4.util.LogWriter;
35bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banesimport android.support.v4.view.LayoutInflaterFactory;
365e63ab9505a3a4d11374cbbec418c1aba921409dChris Banesimport android.support.v4.view.ViewCompat;
370f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powellimport android.util.AttributeSet;
38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.Log;
39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.SparseArray;
409277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AccelerateInterpolator;
419277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AlphaAnimation;
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.Animation;
439277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.AnimationSet;
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.AnimationUtils;
459277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.DecelerateInterpolator;
469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.Interpolator;
479277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackbornimport android.view.animation.ScaleAnimation;
48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.animation.Animation.AnimationListener;
49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.Menu;
50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.MenuInflater;
51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.MenuItem;
52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View;
53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.ViewGroup;
54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.FileDescriptor;
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.PrintWriter;
57d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liuimport java.lang.reflect.Field;
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.util.ArrayList;
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.util.Arrays;
603a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brownimport java.util.List;
61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.app.FragmentManager}.
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0.  When running
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try
669dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * to switch to the framework's implementation.  See the framework {@link FragmentManager}
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview.
689dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main *
699dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * <p>Your activity must derive from {@link FragmentActivity} to use this. From such an activity,
709dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * you can acquire the {@link FragmentManager} by calling
719dcd2e58138ca4eb4b18f80b50e8979329e859d6Scott Main * {@link FragmentActivity#getSupportFragmentManager}.
72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic abstract class FragmentManager {
74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Representation of an entry on the fragment back stack, as created
76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * with {@link FragmentTransaction#addToBackStack(String)
77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * FragmentTransaction.addToBackStack()}.  Entries can later be
78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * FragmentManager.getBackStackEntry()}.
80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Note that you should never hold on to a BackStackEntry object;
82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the identifier as returned by {@link #getId} is the only thing that
83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * will be persisted across activity instances.
84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public interface BackStackEntry {
86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Return the unique identifier for the entry.  This is the only
88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * representation of the entry that will persist across activity
89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * instances.
90cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public int getId();
92cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
93cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
942a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn         * Get the name that was supplied to
952a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn         * {@link FragmentTransaction#addToBackStack(String)
962a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn         * FragmentTransaction.addToBackStack(String)} when creating this entry.
972a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn         */
982a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        public String getName();
992a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn
1002a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn        /**
101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Return the full bread crumb title resource identifier for the entry,
102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * or 0 if it does not have one.
103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
104a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye        @StringRes
105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public int getBreadCrumbTitleRes();
106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Return the short bread crumb title resource identifier for the entry,
109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * or 0 if it does not have one.
110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
111a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye        @StringRes
112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public int getBreadCrumbShortTitleRes();
113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Return the full bread crumb title for the entry, or null if it
116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * does not have one.
117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public CharSequence getBreadCrumbTitle();
119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Return the short bread crumb title for the entry, or null if it
122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * does not have one.
123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public CharSequence getBreadCrumbShortTitle();
125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Interface to watch for changes to the back stack.
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public interface OnBackStackChangedListener {
131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Called whenever the contents of the back stack change.
133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void onBackStackChanged();
135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Start a series of edit operations on the Fragments associated with
139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * this FragmentManager.
140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Note: A fragment transaction can only be created/committed prior
142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to an activity saving its state.  If you try to commit a transaction
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * after {@link FragmentActivity#onSaveInstanceState FragmentActivity.onSaveInstanceState()}
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * (and prior to a following {@link FragmentActivity#onStart FragmentActivity.onStart}
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * or {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will get an error.
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This is because the framework takes care of saving your current fragments
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * in the state, and if changes are made after the state is saved then they
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * will be lost.</p>
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract FragmentTransaction beginTransaction();
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
152d805095048f6be52cddbd572ee343c4639ba8187Alan Viverette    /**
153d805095048f6be52cddbd572ee343c4639ba8187Alan Viverette     * @hide -- remove once prebuilts are in.
154d805095048f6be52cddbd572ee343c4639ba8187Alan Viverette     * @deprecated
155d805095048f6be52cddbd572ee343c4639ba8187Alan Viverette     */
156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Deprecated
157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public FragmentTransaction openTransaction() {
158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return beginTransaction();
159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * After a {@link FragmentTransaction} is committed with
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is scheduled to be executed asynchronously on the process's main thread.
165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If you want to immediately executing any such pending operations, you
166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * can call this function (only from the main thread) to do so.  Note that
167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * all callbacks and other related behavior will be done from within this
168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * call, so be careful about where this is called from.
169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
1701500716b2257e9f442606fe5bd992ab4e0198dcbAdam Powell     * <p>If you are committing a single transaction that does not modify the
1711500716b2257e9f442606fe5bd992ab4e0198dcbAdam Powell     * fragment back stack, strongly consider using
1721500716b2257e9f442606fe5bd992ab4e0198dcbAdam Powell     * {@link FragmentTransaction#commitNow()} instead. This can help avoid
1731500716b2257e9f442606fe5bd992ab4e0198dcbAdam Powell     * unwanted side effects when other code in your app has pending committed
1741500716b2257e9f442606fe5bd992ab4e0198dcbAdam Powell     * transactions that expect different timing.</p>
1751500716b2257e9f442606fe5bd992ab4e0198dcbAdam Powell     *
176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return Returns true if there were any pending transactions to be
177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * executed.
178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract boolean executePendingTransactions();
180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Finds a fragment that was identified by the given id either when inflated
183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * from XML or as the container ID when added in a transaction.  This first
184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * searches through fragments that are currently added to the manager's
185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * activity; if no such fragment is found, then all fragments currently
186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * on the back stack associated with this ID are searched.
187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return The fragment if found or null otherwise.
188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
189a3ff3273e976adf19770651dcf473fa67b38eb22Tor Norbye    public abstract Fragment findFragmentById(@IdRes int id);
190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Finds a fragment that was identified by the given tag either when inflated
193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * from XML or as supplied when added in a transaction.  This first
194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * searches through fragments that are currently added to the manager's
195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * activity; if no such fragment is found, then all fragments currently
196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * on the back stack are searched.
197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return The fragment if found or null otherwise.
198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract Fragment findFragmentByTag(String tag);
200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Flag for {@link #popBackStack(String, int)}
203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * and {@link #popBackStack(int, int)}: If set, and the name or ID of
204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * a back stack entry has been supplied, then all matching entries will
205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * be consumed until one that doesn't match is found or the bottom of
206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the stack is reached.  Otherwise, all entries up to but not including that entry
207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * will be removed.
208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Pop the top state off the back stack.  Returns true if there was one
213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to pop, else false.  This function is asynchronous -- it enqueues the
214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * request to pop, but the action will not be performed until the application
215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * returns to its event loop.
216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void popBackStack();
218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Like {@link #popBackStack()}, but performs the operation immediately
221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * inside of the call.  This is like calling {@link #executePendingTransactions()}
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * afterwards.
223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return Returns true if there was something popped, else false.
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract boolean popBackStackImmediate();
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Pop the last fragment transition from the manager's fragment
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * back stack.  If there is nothing to pop, false is returned.
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This function is asynchronous -- it enqueues the
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * request to pop, but the action will not be performed until the application
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * returns to its event loop.
233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param name If non-null, this is the name of a previous back state
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to look for; if found, all states up to that state will be popped.  The
236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the named state itself is popped. If null, only the top state is popped.
238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void popBackStack(String name, int flags);
241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Like {@link #popBackStack(String, int)}, but performs the operation immediately
244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * inside of the call.  This is like calling {@link #executePendingTransactions()}
245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * afterwards.
246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return Returns true if there was something popped, else false.
247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract boolean popBackStackImmediate(String name, int flags);
249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Pop all back stack states up to the one with the given identifier.
252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This function is asynchronous -- it enqueues the
253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * request to pop, but the action will not be performed until the application
254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * returns to its event loop.
255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param id Identifier of the stated to be popped. If no identifier exists,
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * false is returned.
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The identifier is the number returned by
259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link FragmentTransaction#commit() FragmentTransaction.commit()}.  The
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the named state itself is popped.
262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void popBackStack(int id, int flags);
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Like {@link #popBackStack(int, int)}, but performs the operation immediately
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * inside of the call.  This is like calling {@link #executePendingTransactions()}
269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * afterwards.
270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return Returns true if there was something popped, else false.
271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract boolean popBackStackImmediate(int id, int flags);
273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return the number of entries currently in the back stack.
276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract int getBackStackEntryCount();
278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return the BackStackEntry at index <var>index</var> in the back stack;
281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * entries start index 0 being the bottom of the stack.
282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract BackStackEntry getBackStackEntryAt(int index);
284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Add a new listener for changes to the fragment back stack.
287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Remove a listener that was previously added with
292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Put a reference to a fragment in a Bundle.  This Bundle can be
298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * persisted as saved state, and when later restoring
299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #getFragment(Bundle, String)} will return the current
300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * instance of the same fragment.
301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param bundle The bundle in which to put the fragment reference.
303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param key The name of the entry in the bundle.
304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param fragment The Fragment whose reference is to be stored.
305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Retrieve the current Fragment instance for a reference previously
310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * placed with {@link #putFragment(Bundle, String, Fragment)}.
311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param bundle The bundle from which to retrieve the fragment reference.
313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param key The name of the entry in the bundle.
314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return Returns the current Fragment instance that is associated with
315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the given reference.
316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract Fragment getFragment(Bundle bundle, String key);
318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
3203a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown     * Get a list of all fragments that have been added to the fragment manager.
3213a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown     *
3223a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown     * @return The list of all fragments or null if none.
3233a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown     * @hide
3243a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown     */
3253a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    public abstract List<Fragment> getFragments();
3263a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown
3273a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    /**
3285c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * Save the current instance state of the given Fragment.  This can be
3295c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * used later when creating a new instance of the Fragment and adding
3305c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * it to the fragment manager, to have it create itself to match the
3315c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * current state returned here.  Note that there are limits on how
3325c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * this can be used:
3335c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     *
3345c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * <ul>
3355c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * <li>The Fragment must currently be attached to the FragmentManager.
3365c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * <li>A new Fragment created using this saved state must be the same class
3375c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * type as the Fragment it was created from.
3385c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * <li>The saved state can not contain dependencies on other fragments --
3395c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
3405c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * store a fragment reference because that reference may not be valid when
3415c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * this saved state is later used.  Likewise the Fragment's target and
3425c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * result code are not included in this state.
3435c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * </ul>
3445c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     *
3455c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @param f The Fragment whose state is to be saved.
3465c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * @return The generated state.  This will be null if there was no
3475c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     * interesting state created by the fragment.
3485c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn     */
3495c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
3505c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
3515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    /**
35201df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler     * Returns true if the final {@link android.app.Activity#onDestroy() Activity.onDestroy()}
35301df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler     * call has been made on the FragmentManager's Activity, so this instance is now dead.
35401df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler     */
35501df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler    public abstract boolean isDestroyed();
35601df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler
35701df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler    /**
358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Print the FragmentManager's state into the given stream.
359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param prefix Text to print at the front of each line.
361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param fd The raw file descriptor that the dump is being sent to.
362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param writer A PrintWriter to which the dump is to be set.
363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param args Additional arguments to the dump request.
364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Control whether the framework's internal fragment manager debugging
369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * logs are turned on.  If enabled, you will see output in logcat as
370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the framework performs fragment operations.
371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static void enableDebugLogging(boolean enabled) {
373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        FragmentManagerImpl.DEBUG = enabled;
374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornfinal class FragmentManagerState implements Parcelable {
378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    FragmentState[] mActive;
379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int[] mAdded;
380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    BackStackState[] mBackStack;
381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public FragmentManagerState() {
383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public FragmentManagerState(Parcel in) {
386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mActive = in.createTypedArray(FragmentState.CREATOR);
387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mAdded = in.createIntArray();
388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mBackStack = in.createTypedArray(BackStackState.CREATOR);
389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int describeContents() {
392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return 0;
393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void writeToParcel(Parcel dest, int flags) {
396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        dest.writeTypedArray(mActive, flags);
397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        dest.writeIntArray(mAdded);
398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        dest.writeTypedArray(mBackStack, flags);
399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final Parcelable.Creator<FragmentManagerState> CREATOR
402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            = new Parcelable.Creator<FragmentManagerState>() {
403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public FragmentManagerState createFromParcel(Parcel in) {
404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return new FragmentManagerState(in);
405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public FragmentManagerState[] newArray(int size) {
408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return new FragmentManagerState[size];
409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    };
411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
413cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Container for fragments associated with an activity.
415cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
416bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banesfinal class FragmentManagerImpl extends FragmentManager implements LayoutInflaterFactory {
417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static boolean DEBUG = false;
418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static final String TAG = "FragmentManager";
419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
420681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn    static final boolean HONEYCOMB = android.os.Build.VERSION.SDK_INT >= 11;
421681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn
422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
423cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static final String TARGET_STATE_TAG = "android:target_state";
424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    static final String VIEW_STATE_TAG = "android:view_state";
42579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell    static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
42703526560f132021f8fd7290259762ab362d4d567Doris Liu    static class AnimateOnHWLayerIfNeededListener implements AnimationListener {
4281802bf381380809e224c72008a400715e2e375b4Adam Powell        private AnimationListener mOrignalListener;
4291802bf381380809e224c72008a400715e2e375b4Adam Powell        private boolean mShouldRunOnHWLayer;
4301802bf381380809e224c72008a400715e2e375b4Adam Powell        private View mView;
4311802bf381380809e224c72008a400715e2e375b4Adam Powell
43203526560f132021f8fd7290259762ab362d4d567Doris Liu        public AnimateOnHWLayerIfNeededListener(final View v, Animation anim) {
43303526560f132021f8fd7290259762ab362d4d567Doris Liu            if (v == null || anim == null) {
43403526560f132021f8fd7290259762ab362d4d567Doris Liu                return;
43503526560f132021f8fd7290259762ab362d4d567Doris Liu            }
43603526560f132021f8fd7290259762ab362d4d567Doris Liu            mView = v;
43703526560f132021f8fd7290259762ab362d4d567Doris Liu        }
43803526560f132021f8fd7290259762ab362d4d567Doris Liu
439d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu        public AnimateOnHWLayerIfNeededListener(final View v, Animation anim,
440d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                AnimationListener listener) {
441d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            if (v == null || anim == null) {
442d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                return;
443d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            }
444d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            mOrignalListener = listener;
445d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            mView = v;
4461802bf381380809e224c72008a400715e2e375b4Adam Powell            mShouldRunOnHWLayer = true;
447d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu        }
448d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu
44903526560f132021f8fd7290259762ab362d4d567Doris Liu        @Override
45003526560f132021f8fd7290259762ab362d4d567Doris Liu        @CallSuper
45103526560f132021f8fd7290259762ab362d4d567Doris Liu        public void onAnimationStart(Animation animation) {
452d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            if (mOrignalListener != null) {
453d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                mOrignalListener.onAnimationStart(animation);
45403526560f132021f8fd7290259762ab362d4d567Doris Liu            }
45503526560f132021f8fd7290259762ab362d4d567Doris Liu        }
45603526560f132021f8fd7290259762ab362d4d567Doris Liu
45703526560f132021f8fd7290259762ab362d4d567Doris Liu        @Override
45803526560f132021f8fd7290259762ab362d4d567Doris Liu        @CallSuper
45903526560f132021f8fd7290259762ab362d4d567Doris Liu        public void onAnimationEnd(Animation animation) {
460d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            if (mView != null && mShouldRunOnHWLayer) {
4611802bf381380809e224c72008a400715e2e375b4Adam Powell                // If we're attached to a window, assume we're in the normal performTraversals
4621802bf381380809e224c72008a400715e2e375b4Adam Powell                // drawing path for Animations running. It's not safe to change the layer type
4631802bf381380809e224c72008a400715e2e375b4Adam Powell                // during drawing, so post it to the View to run later. If we're not attached
4641802bf381380809e224c72008a400715e2e375b4Adam Powell                // or we're running on N and above, post it to the view. If we're not on N and
4651802bf381380809e224c72008a400715e2e375b4Adam Powell                // not attached, do it right now since existing platform versions don't run the
4661802bf381380809e224c72008a400715e2e375b4Adam Powell                // hwui renderer for detached views off the UI thread making changing layer type
4671802bf381380809e224c72008a400715e2e375b4Adam Powell                // safe, but posting may not be.
4681802bf381380809e224c72008a400715e2e375b4Adam Powell                // Prior to N posting to a detached view from a non-Looper thread could cause
4691802bf381380809e224c72008a400715e2e375b4Adam Powell                // leaks, since the thread-local run queue on a non-Looper thread would never
4701802bf381380809e224c72008a400715e2e375b4Adam Powell                // be flushed.
4711802bf381380809e224c72008a400715e2e375b4Adam Powell                if (ViewCompat.isAttachedToWindow(mView) || BuildCompat.isAtLeastN()) {
4721802bf381380809e224c72008a400715e2e375b4Adam Powell                    mView.post(new Runnable() {
4731802bf381380809e224c72008a400715e2e375b4Adam Powell                        @Override
4741802bf381380809e224c72008a400715e2e375b4Adam Powell                        public void run() {
4751802bf381380809e224c72008a400715e2e375b4Adam Powell                            ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null);
4761802bf381380809e224c72008a400715e2e375b4Adam Powell                        }
4771802bf381380809e224c72008a400715e2e375b4Adam Powell                    });
4781802bf381380809e224c72008a400715e2e375b4Adam Powell                } else {
4791802bf381380809e224c72008a400715e2e375b4Adam Powell                    ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null);
4801802bf381380809e224c72008a400715e2e375b4Adam Powell                }
48103526560f132021f8fd7290259762ab362d4d567Doris Liu            }
482d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            if (mOrignalListener != null) {
483d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                mOrignalListener.onAnimationEnd(animation);
484d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            }
48503526560f132021f8fd7290259762ab362d4d567Doris Liu        }
48603526560f132021f8fd7290259762ab362d4d567Doris Liu
48703526560f132021f8fd7290259762ab362d4d567Doris Liu        @Override
48803526560f132021f8fd7290259762ab362d4d567Doris Liu        public void onAnimationRepeat(Animation animation) {
489d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            if (mOrignalListener != null) {
490d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                mOrignalListener.onAnimationRepeat(animation);
491d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            }
49203526560f132021f8fd7290259762ab362d4d567Doris Liu        }
49303526560f132021f8fd7290259762ab362d4d567Doris Liu    }
49403526560f132021f8fd7290259762ab362d4d567Doris Liu
495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<Runnable> mPendingActions;
496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    Runnable[] mTmpActions;
497cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mExecutingActions;
498cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
499cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<Fragment> mActive;
500cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<Fragment> mAdded;
501cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<Integer> mAvailIndices;
502cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<BackStackRecord> mBackStack;
503cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<Fragment> mCreatedMenus;
504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // Must be accessed while locked.
506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<BackStackRecord> mBackStackIndices;
507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<Integer> mAvailBackStackIndices;
508cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
509cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
510cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int mCurState = Fragment.INITIALIZING;
5128491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy    FragmentHostCallback mHost;
513d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    FragmentController mController;
5140adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    FragmentContainer mContainer;
5150adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    Fragment mParent;
516d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu
517d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu    static Field sAnimationListenerField = null;
518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mNeedMenuInvalidate;
520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mStateSaved;
521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mDestroyed;
522cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    String mNoTransactionsBecause;
52379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell    boolean mHavePendingDeferredStart;
524cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
525cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    // Temporary vars for state save and restore.
526cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    Bundle mStateBundle = null;
527cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    SparseArray<Parcelable> mStateArray = null;
528cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
529cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    Runnable mExecCommit = new Runnable() {
530cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        @Override
531cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void run() {
532cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            execPendingActions();
533cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
534cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    };
535cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
53603526560f132021f8fd7290259762ab362d4d567Doris Liu    static boolean modifiesAlpha(Animation anim) {
53703526560f132021f8fd7290259762ab362d4d567Doris Liu        if (anim instanceof AlphaAnimation) {
53803526560f132021f8fd7290259762ab362d4d567Doris Liu            return true;
53903526560f132021f8fd7290259762ab362d4d567Doris Liu        } else if (anim instanceof AnimationSet) {
54003526560f132021f8fd7290259762ab362d4d567Doris Liu            List<Animation> anims = ((AnimationSet) anim).getAnimations();
54103526560f132021f8fd7290259762ab362d4d567Doris Liu            for (int i = 0; i < anims.size(); i++) {
54203526560f132021f8fd7290259762ab362d4d567Doris Liu                if (anims.get(i) instanceof AlphaAnimation) {
54303526560f132021f8fd7290259762ab362d4d567Doris Liu                    return true;
54403526560f132021f8fd7290259762ab362d4d567Doris Liu                }
54503526560f132021f8fd7290259762ab362d4d567Doris Liu            }
54603526560f132021f8fd7290259762ab362d4d567Doris Liu        }
54703526560f132021f8fd7290259762ab362d4d567Doris Liu        return false;
54803526560f132021f8fd7290259762ab362d4d567Doris Liu    }
54903526560f132021f8fd7290259762ab362d4d567Doris Liu
55003526560f132021f8fd7290259762ab362d4d567Doris Liu    static boolean shouldRunOnHWLayer(View v, Animation anim) {
551b8d65fef161c7cd4bb06dc97685cf3fb3d6c3e1aChris Banes        return Build.VERSION.SDK_INT >= 19
552410d77a6fbe42b0784cccd046bb8958caa723035Chris Banes                && ViewCompat.getLayerType(v) == ViewCompat.LAYER_TYPE_NONE
55303526560f132021f8fd7290259762ab362d4d567Doris Liu                && ViewCompat.hasOverlappingRendering(v)
55403526560f132021f8fd7290259762ab362d4d567Doris Liu                && modifiesAlpha(anim);
55503526560f132021f8fd7290259762ab362d4d567Doris Liu    }
55603526560f132021f8fd7290259762ab362d4d567Doris Liu
55713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn    private void throwException(RuntimeException ex) {
55813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn        Log.e(TAG, ex.getMessage());
559ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn        Log.e(TAG, "Activity state:");
56013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn        LogWriter logw = new LogWriter(TAG);
56113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn        PrintWriter pw = new PrintWriter(logw);
562d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        if (mHost != null) {
563ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn            try {
5648491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy                mHost.onDump("  ", null, pw, new String[] { });
565ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn            } catch (Exception e) {
566ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn                Log.e(TAG, "Failed dumping state", e);
567ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn            }
568ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn        } else {
569ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn            try {
57013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                dump("  ", null, pw, new String[] { });
571ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn            } catch (Exception e) {
57213fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                Log.e(TAG, "Failed dumping state", e);
573ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn            }
574ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn        }
57513fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn        throw ex;
576ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn    }
577ec9fb2522d42d1ff73ddffa12b318d925c10ab18Dianne Hackborn
578cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
579cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public FragmentTransaction beginTransaction() {
580cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return new BackStackRecord(this);
581cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
582cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
583cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
584cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean executePendingTransactions() {
585cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return execPendingActions();
586cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
587cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
588cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
589cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void popBackStack() {
590cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        enqueueAction(new Runnable() {
591cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            @Override public void run() {
592d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                popBackStackState(mHost.getHandler(), null, -1, 0);
593cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
594cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }, false);
595cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
596cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
597cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
598cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean popBackStackImmediate() {
599cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        checkStateLoss();
600cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        executePendingTransactions();
601d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        return popBackStackState(mHost.getHandler(), null, -1, 0);
602cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
603cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
604cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
605cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void popBackStack(final String name, final int flags) {
606cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        enqueueAction(new Runnable() {
607cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            @Override public void run() {
608d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                popBackStackState(mHost.getHandler(), name, -1, flags);
609cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
610cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }, false);
611cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
612cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
613cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
614cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean popBackStackImmediate(String name, int flags) {
615cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        checkStateLoss();
616cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        executePendingTransactions();
617d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        return popBackStackState(mHost.getHandler(), name, -1, flags);
618cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
619cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
620cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
621cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void popBackStack(final int id, final int flags) {
622cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (id < 0) {
623cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalArgumentException("Bad id: " + id);
624cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
625cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        enqueueAction(new Runnable() {
626cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            @Override public void run() {
627d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                popBackStackState(mHost.getHandler(), null, id, flags);
628cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
629cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }, false);
630cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
631cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
632cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
633cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean popBackStackImmediate(int id, int flags) {
634cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        checkStateLoss();
635cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        executePendingTransactions();
636cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (id < 0) {
637cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalArgumentException("Bad id: " + id);
638cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
639d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        return popBackStackState(mHost.getHandler(), null, id, flags);
640cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
641cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
642cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
643cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int getBackStackEntryCount() {
644cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mBackStack != null ? mBackStack.size() : 0;
645cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
646cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
647cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
648cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public BackStackEntry getBackStackEntryAt(int index) {
649cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mBackStack.get(index);
650cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
651cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
652cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
653cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
654cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStackChangeListeners == null) {
655cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
656cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
657cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mBackStackChangeListeners.add(listener);
658cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
659cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
660cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
661cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
662cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStackChangeListeners != null) {
663cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStackChangeListeners.remove(listener);
664cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
665cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
666cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
667cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
668cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void putFragment(Bundle bundle, String key, Fragment fragment) {
669cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (fragment.mIndex < 0) {
67013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn            throwException(new IllegalStateException("Fragment " + fragment
67113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                    + " is not currently in the FragmentManager"));
672cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
673cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        bundle.putInt(key, fragment.mIndex);
674cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
675cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
676cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
677cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Fragment getFragment(Bundle bundle, String key) {
678cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        int index = bundle.getInt(key, -1);
679cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (index == -1) {
680cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
681cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
682cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (index >= mActive.size()) {
6832b336307cf98ca5142db6736812178293d47c500Cyril Mottier            throwException(new IllegalStateException("Fragment no longer exists for key "
68413fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                    + key + ": index " + index));
685cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
686cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        Fragment f = mActive.get(index);
687cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (f == null) {
6882b336307cf98ca5142db6736812178293d47c500Cyril Mottier            throwException(new IllegalStateException("Fragment no longer exists for key "
68913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                    + key + ": index " + index));
690cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
691cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return f;
692cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
693cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
694cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
6953a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    public List<Fragment> getFragments() {
6963a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown        return mActive;
6973a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    }
6983a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown
6993a1a7fff9873abbf8097c96f7654a459bf34f223Jeff Brown    @Override
7005c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
7015c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (fragment.mIndex < 0) {
70213fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn            throwException( new IllegalStateException("Fragment " + fragment
70313fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                    + " is not currently in the FragmentManager"));
7045c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
7055c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (fragment.mState > Fragment.INITIALIZING) {
7065c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            Bundle result = saveFragmentBasicState(fragment);
7075c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            return result != null ? new Fragment.SavedState(result) : null;
7085c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
7095c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return null;
7105c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
7115c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
7125c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    @Override
71301df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler    public boolean isDestroyed() {
71401df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler        return mDestroyed;
71501df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler    }
71601df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler
71701df737703cceecc6b3a319ca0ee0bd64415f1cfTony Mantler    @Override
718cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public String toString() {
719cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        StringBuilder sb = new StringBuilder(128);
720cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append("FragmentManager{");
721cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append(Integer.toHexString(System.identityHashCode(this)));
722cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append(" in ");
7230adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        if (mParent != null) {
7240adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            DebugUtils.buildShortClassTag(mParent, sb);
7250adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        } else {
726d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy            DebugUtils.buildShortClassTag(mHost, sb);
7270adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        }
728cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append("}}");
729cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return sb.toString();
730cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
731cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
732cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
733cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
734cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        String innerPrefix = prefix + "    ";
735cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
736cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        int N;
737cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mActive != null) {
738cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            N = mActive.size();
739cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (N > 0) {
740cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(prefix); writer.print("Active Fragments in ");
741cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        writer.print(Integer.toHexString(System.identityHashCode(this)));
742cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        writer.println(":");
743cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                for (int i=0; i<N; i++) {
744cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    Fragment f = mActive.get(i);
745cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
746cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            writer.print(": "); writer.println(f);
747cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (f != null) {
748cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.dump(innerPrefix, fd, writer, args);
749cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
750cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
751cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
752cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
753cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
754cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAdded != null) {
755cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            N = mAdded.size();
756cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (N > 0) {
757cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(prefix); writer.println("Added Fragments:");
758cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                for (int i=0; i<N; i++) {
759cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    Fragment f = mAdded.get(i);
760cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
761cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            writer.print(": "); writer.println(f.toString());
762cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
763cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
764cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
765cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
766cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mCreatedMenus != null) {
767cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            N = mCreatedMenus.size();
768cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (N > 0) {
769cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(prefix); writer.println("Fragments Created Menus:");
770cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                for (int i=0; i<N; i++) {
771cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    Fragment f = mCreatedMenus.get(i);
772cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
773cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            writer.print(": "); writer.println(f.toString());
774cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
775cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
776cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
777cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
778cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStack != null) {
779cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            N = mBackStack.size();
780cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (N > 0) {
781cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(prefix); writer.println("Back Stack:");
782cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                for (int i=0; i<N; i++) {
783cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    BackStackRecord bs = mBackStack.get(i);
784cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
785cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            writer.print(": "); writer.println(bs.toString());
786cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    bs.dump(innerPrefix, fd, writer, args);
787cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
788cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
789cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
790cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
791cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        synchronized (this) {
792cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mBackStackIndices != null) {
793cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                N = mBackStackIndices.size();
794cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (N > 0) {
795cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.print(prefix); writer.println("Back Stack Indices:");
796cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    for (int i=0; i<N; i++) {
797cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        BackStackRecord bs = mBackStackIndices.get(i);
798cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        writer.print(prefix); writer.print("  #"); writer.print(i);
799cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                writer.print(": "); writer.println(bs);
800cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
801cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
802cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
803cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
804cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
805cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(prefix); writer.print("mAvailBackStackIndices: ");
806cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
807cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
808cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
809cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
810cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mPendingActions != null) {
811cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            N = mPendingActions.size();
812cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (N > 0) {
813cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(prefix); writer.println("Pending Actions:");
814cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                for (int i=0; i<N; i++) {
815cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    Runnable r = mPendingActions.get(i);
816cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
817cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            writer.print(": "); writer.println(r);
818cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
819cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
820cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
821cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
822cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        writer.print(prefix); writer.println("FragmentManager misc state:");
823d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        writer.print(prefix); writer.print("  mHost="); writer.println(mHost);
8240adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        writer.print(prefix); writer.print("  mContainer="); writer.println(mContainer);
8250adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        if (mParent != null) {
8260adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            writer.print(prefix); writer.print("  mParent="); writer.println(mParent);
8270adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        }
828cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        writer.print(prefix); writer.print("  mCurState="); writer.print(mCurState);
829cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(" mStateSaved="); writer.print(mStateSaved);
830cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(" mDestroyed="); writer.println(mDestroyed);
831cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mNeedMenuInvalidate) {
832cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            writer.print(prefix); writer.print("  mNeedMenuInvalidate=");
833cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.println(mNeedMenuInvalidate);
834cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
835cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mNoTransactionsBecause != null) {
836cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            writer.print(prefix); writer.print("  mNoTransactionsBecause=");
837cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.println(mNoTransactionsBecause);
838cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
839cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAvailIndices != null && mAvailIndices.size() > 0) {
840cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            writer.print(prefix); writer.print("  mAvailIndices: ");
841cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    writer.println(Arrays.toString(mAvailIndices.toArray()));
842cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
843cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
844cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
8459277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
8469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    static final Interpolator DECELERATE_CUBIC = new DecelerateInterpolator(1.5f);
8479277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    static final Interpolator ACCELERATE_QUINT = new AccelerateInterpolator(2.5f);
8489277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    static final Interpolator ACCELERATE_CUBIC = new AccelerateInterpolator(1.5f);
8499277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
8509277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    static final int ANIM_DUR = 220;
8519277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
8529277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    static Animation makeOpenCloseAnimation(Context context, float startScale,
8539277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            float endScale, float startAlpha, float endAlpha) {
8549277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        AnimationSet set = new AnimationSet(false);
8559277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale,
8569277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
8579277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        scale.setInterpolator(DECELERATE_QUINT);
8589277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        scale.setDuration(ANIM_DUR);
8599277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        set.addAnimation(scale);
8609277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha);
8619277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        alpha.setInterpolator(DECELERATE_CUBIC);
8629277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        alpha.setDuration(ANIM_DUR);
8639277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        set.addAnimation(alpha);
8649277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        return set;
8659277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    }
8669277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
8679277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    static Animation makeFadeAnimation(Context context, float start, float end) {
8689277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        AlphaAnimation anim = new AlphaAnimation(start, end);
8699277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        anim.setInterpolator(DECELERATE_CUBIC);
8709277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        anim.setDuration(ANIM_DUR);
8719277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        return anim;
8729277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    }
87303526560f132021f8fd7290259762ab362d4d567Doris Liu
8749277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    Animation loadAnimation(Fragment fragment, int transit, boolean enter,
875cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int transitionStyle) {
8769277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        Animation animObj = fragment.onCreateAnimation(transit, enter,
877cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                fragment.mNextAnim);
878cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (animObj != null) {
879cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return animObj;
880cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
881cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
882cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (fragment.mNextAnim != 0) {
883d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy            Animation anim = AnimationUtils.loadAnimation(mHost.getContext(), fragment.mNextAnim);
884cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (anim != null) {
885cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                return anim;
886cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
887cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
888cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
889cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (transit == 0) {
890cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
891cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
892cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
893cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        int styleIndex = transitToStyleIndex(transit, enter);
894cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (styleIndex < 0) {
895cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
896cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
89703526560f132021f8fd7290259762ab362d4d567Doris Liu
8989277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        switch (styleIndex) {
8999277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            case ANIM_STYLE_OPEN_ENTER:
900d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                return makeOpenCloseAnimation(mHost.getContext(), 1.125f, 1.0f, 0, 1);
9019277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            case ANIM_STYLE_OPEN_EXIT:
902d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                return makeOpenCloseAnimation(mHost.getContext(), 1.0f, .975f, 1, 0);
9039277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            case ANIM_STYLE_CLOSE_ENTER:
904d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                return makeOpenCloseAnimation(mHost.getContext(), .975f, 1.0f, 0, 1);
9059277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            case ANIM_STYLE_CLOSE_EXIT:
906d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                return makeOpenCloseAnimation(mHost.getContext(), 1.0f, 1.075f, 1, 0);
9079277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            case ANIM_STYLE_FADE_ENTER:
908d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                return makeFadeAnimation(mHost.getContext(), 0, 1);
9099277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            case ANIM_STYLE_FADE_EXIT:
910d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                return makeFadeAnimation(mHost.getContext(), 1, 0);
9119277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn        }
9129277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
9138491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy        if (transitionStyle == 0 && mHost.onHasWindowAnimations()) {
9148491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy            transitionStyle = mHost.onGetWindowAnimations();
915cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
916cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (transitionStyle == 0) {
917cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
918cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
919cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
920cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
921cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //        com.android.internal.R.styleable.FragmentAnimation);
922cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //int anim = attrs.getResourceId(styleIndex, 0);
923cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //attrs.recycle();
924cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
925cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //if (anim == 0) {
926cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //    return null;
927cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //}
928cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
929cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        //return AnimatorInflater.loadAnimator(mActivity, anim);
930cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return null;
931cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
932cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
933abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    public void performPendingDeferredStart(Fragment f) {
934abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        if (f.mDeferStart) {
93579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            if (mExecutingActions) {
93679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                // Wait until we're done executing our pending transactions
93779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                mHavePendingDeferredStart = true;
93879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                return;
93979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            }
940abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            f.mDeferStart = false;
9415506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn            moveToState(f, mCurState, 0, 0, false);
942abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        }
943abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    }
944abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell
94503526560f132021f8fd7290259762ab362d4d567Doris Liu    /**
94603526560f132021f8fd7290259762ab362d4d567Doris Liu     * Sets the to be animated view on hardware layer during the animation. Note
94703526560f132021f8fd7290259762ab362d4d567Doris Liu     * that calling this will replace any existing animation listener on the animation
94803526560f132021f8fd7290259762ab362d4d567Doris Liu     * with a new one, as animations do not support more than one listeners. Therefore,
94903526560f132021f8fd7290259762ab362d4d567Doris Liu     * animations that already have listeners should do the layer change operations
95003526560f132021f8fd7290259762ab362d4d567Doris Liu     * in their existing listeners, rather than calling this function.
95103526560f132021f8fd7290259762ab362d4d567Doris Liu     */
95203526560f132021f8fd7290259762ab362d4d567Doris Liu    private void setHWLayerAnimListenerIfAlpha(final View v, Animation anim) {
95303526560f132021f8fd7290259762ab362d4d567Doris Liu        if (v == null || anim == null) {
95403526560f132021f8fd7290259762ab362d4d567Doris Liu            return;
95503526560f132021f8fd7290259762ab362d4d567Doris Liu        }
95603526560f132021f8fd7290259762ab362d4d567Doris Liu        if (shouldRunOnHWLayer(v, anim)) {
957d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            AnimationListener originalListener = null;
958d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            try {
959d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                if (sAnimationListenerField == null) {
960d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                    sAnimationListenerField = Animation.class.getDeclaredField("mListener");
961d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                    sAnimationListenerField.setAccessible(true);
962d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                }
963d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                originalListener = (AnimationListener) sAnimationListenerField.get(anim);
964d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            } catch (NoSuchFieldException e) {
965d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                Log.e(TAG, "No field with the name mListener is found in Animation class", e);
966d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            } catch (IllegalAccessException e) {
967d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                Log.e(TAG, "Cannot access Animation's mListener field", e);
968d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            }
969d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            // If there's already a listener set on the animation, we need wrap the new listener
970d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            // around the existing listener, so that they will both get animation listener
971d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            // callbacks.
9721802bf381380809e224c72008a400715e2e375b4Adam Powell            ViewCompat.setLayerType(v, ViewCompat.LAYER_TYPE_HARDWARE, null);
973d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu            anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener(v, anim,
974d02828a3781590e76fe86f2bf4ae8fbff4f5e2bdDoris Liu                    originalListener));
97503526560f132021f8fd7290259762ab362d4d567Doris Liu        }
97603526560f132021f8fd7290259762ab362d4d567Doris Liu    }
97703526560f132021f8fd7290259762ab362d4d567Doris Liu
978fd15fbacc1d0cb92f2edf72137e4940be2547aa4Adam Powell    boolean isStateAtLeast(int state) {
979fd15fbacc1d0cb92f2edf72137e4940be2547aa4Adam Powell        return mCurState >= state;
980fd15fbacc1d0cb92f2edf72137e4940be2547aa4Adam Powell    }
981fd15fbacc1d0cb92f2edf72137e4940be2547aa4Adam Powell
9825506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn    void moveToState(Fragment f, int newState, int transit, int transitionStyle,
9835506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn            boolean keepActive) {
984cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Fragments that are not currently added will sit in the onCreate() state.
98574c671b3b67000bf16b4865a8d361344310dccbeDianne Hackborn        if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
986cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            newState = Fragment.CREATED;
987cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
9882c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn        if (f.mRemoving && newState > f.mState) {
9892c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn            // While removing a fragment, we can't change it to a higher state.
9902c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn            newState = f.mState;
9912c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn        }
9926cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell        // Defer start if requested; don't allow it to move to STARTED or higher
9936cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell        // if it's not already started.
9946cafd27a3c89dfed10d7e226dc6168307513d4a6Adam Powell        if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
995abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            newState = Fragment.STOPPED;
996abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        }
997cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (f.mState < newState) {
9989277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            // For fragments that are created from a layout, when restoring from
9999277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            // state we don't want to allow them to be created until they are
10009277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            // being reloaded from the layout.
10019277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            if (f.mFromLayout && !f.mInLayout) {
10029277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                return;
10039277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn            }
1004cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (f.mAnimatingAway != null) {
1005cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // The fragment is currently being animated...  but!  Now we
1006cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // want to move our state back up.  Give up on waiting for the
1007cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // animation, move to whatever the final state should be once
1008cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // the animation is done, and then we can proceed from there.
1009cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                f.mAnimatingAway = null;
10105506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                moveToState(f, f.mStateAfterAnimating, 0, 0, true);
1011cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1012cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            switch (f.mState) {
1013cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.INITIALIZING:
1014cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
1015cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (f.mSavedFragmentState != null) {
1016d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                        f.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader());
1017cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
1018cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                FragmentManagerImpl.VIEW_STATE_TAG);
1019cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.mTarget = getFragment(f.mSavedFragmentState,
1020cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                FragmentManagerImpl.TARGET_STATE_TAG);
1021cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (f.mTarget != null) {
1022cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            f.mTargetRequestCode = f.mSavedFragmentState.getInt(
1023cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
1024cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
102579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                        f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
102679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                                FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
102779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                        if (!f.mUserVisibleHint) {
102879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                            f.mDeferStart = true;
102979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                            if (newState > Fragment.STOPPED) {
103079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                                newState = Fragment.STOPPED;
103179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                            }
103279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                        }
1033cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1034d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                    f.mHost = mHost;
10350adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    f.mParentFragment = mParent;
10360adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    f.mFragmentManager = mParent != null
1037d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                            ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl();
1038cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    f.mCalled = false;
1039b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy                    f.onAttach(mHost.getContext());
1040cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (!f.mCalled) {
1041cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        throw new SuperNotCalledException("Fragment " + f
1042cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                + " did not call through to super.onAttach()");
1043cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
10440adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    if (f.mParentFragment == null) {
1045b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy                        mHost.onAttachFragment(f);
1046cef09fee2126f901aa164f6e89c370ab81cff1b3Adam Powell                    } else {
1047cef09fee2126f901aa164f6e89c370ab81cff1b3Adam Powell                        f.mParentFragment.onAttachFragment(f);
10480adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    }
10490adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
1050cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (!f.mRetaining) {
10510adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        f.performCreate(f.mSavedFragmentState);
10529375145b63d854c64aad99e6e50c5b7e5ba32b95Adam Powell                    } else {
10539375145b63d854c64aad99e6e50c5b7e5ba32b95Adam Powell                        f.restoreChildFragmentState(f.mSavedFragmentState);
10549375145b63d854c64aad99e6e50c5b7e5ba32b95Adam Powell                        f.mState = Fragment.CREATED;
1055cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1056cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    f.mRetaining = false;
1057cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (f.mFromLayout) {
1058cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        // For fragments that are part of the content view
1059cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        // layout, we need to instantiate the view immediately
1060cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        // and the inflater will take care of adding it.
10610adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        f.mView = f.performCreateView(f.getLayoutInflater(
10620adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                                f.mSavedFragmentState), null, f.mSavedFragmentState);
1063cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (f.mView != null) {
1064cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            f.mInnerView = f.mView;
10655e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                            if (Build.VERSION.SDK_INT >= 11) {
10665e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                                ViewCompat.setSaveFromParentEnabled(f.mView, false);
10675e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                            } else {
10685e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                                f.mView = NoSaveStateFrameLayout.wrap(f.mView);
10695e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                            }
1070715a71e5f73cbc33f307cbd3625db8f889a05c1aDianne Hackborn                            if (f.mHidden) f.mView.setVisibility(View.GONE);
1071e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                            f.onViewCreated(f.mView, f.mSavedFragmentState);
1072cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        } else {
1073cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            f.mInnerView = null;
1074cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1075cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1076cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.CREATED:
1077cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (newState > Fragment.CREATED) {
1078e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                        if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
1079cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (!f.mFromLayout) {
1080cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            ViewGroup container = null;
1081cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            if (f.mContainerId != 0) {
108236bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                if (f.mContainerId == View.NO_ID) {
108336bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                    throwException(new IllegalArgumentException(
108436bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                            "Cannot create fragment "
108536bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                                    + f
108636bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                                    + " for a container view with no id"));
108736bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                }
108836bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                container = (ViewGroup) mContainer.onFindViewById(f.mContainerId);
1089cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                if (container == null && !f.mRestored) {
109036bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                    String resName;
109136bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                    try {
109236bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                        resName = f.getResources().getResourceName(f.mContainerId);
109336bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                    } catch (NotFoundException e) {
109436bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                        resName = "unknown";
109536bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                    }
109613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                    throwException(new IllegalArgumentException(
109713fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                            "No view found for id 0x"
109813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                            + Integer.toHexString(f.mContainerId) + " ("
109936bd1e9880a74cc53edef99040bbb24fc1cad909Adam Powell                                            + resName
110013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                            + ") for fragment " + f));
1101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                }
1102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            }
1103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            f.mContainer = container;
11040adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                            f.mView = f.performCreateView(f.getLayoutInflater(
11050adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                                    f.mSavedFragmentState), container, f.mSavedFragmentState);
1106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            if (f.mView != null) {
1107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                f.mInnerView = f.mView;
11085e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                                if (Build.VERSION.SDK_INT >= 11) {
11095e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                                    ViewCompat.setSaveFromParentEnabled(f.mView, false);
11105e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                                } else {
11115e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                                    f.mView = NoSaveStateFrameLayout.wrap(f.mView);
11125e63ab9505a3a4d11374cbbec418c1aba921409dChris Banes                                }
1113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                if (container != null) {
11149277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                                    Animation anim = loadAnimation(f, transit, true,
1115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                            transitionStyle);
1116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    if (anim != null) {
111703526560f132021f8fd7290259762ab362d4d567Doris Liu                                        setHWLayerAnimListenerIfAlpha(f.mView, anim);
1118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                        f.mView.startAnimation(anim);
1119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    }
1120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    container.addView(f.mView);
1121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                }
1122715a71e5f73cbc33f307cbd3625db8f889a05c1aDianne Hackborn                                if (f.mHidden) f.mView.setVisibility(View.GONE);
1123e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                                f.onViewCreated(f.mView, f.mSavedFragmentState);
1124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            } else {
1125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                f.mInnerView = null;
1126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            }
1127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
11280adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
11290adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        f.performActivityCreated(f.mSavedFragmentState);
1130e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                        if (f.mView != null) {
11310adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                            f.restoreViewState(f.mSavedFragmentState);
1132e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                        }
1133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.mSavedFragmentState = null;
1134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.ACTIVITY_CREATED:
11369375145b63d854c64aad99e6e50c5b7e5ba32b95Adam Powell                    if (newState > Fragment.ACTIVITY_CREATED) {
11379375145b63d854c64aad99e6e50c5b7e5ba32b95Adam Powell                        f.mState = Fragment.STOPPED;
11389375145b63d854c64aad99e6e50c5b7e5ba32b95Adam Powell                    }
1139e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                case Fragment.STOPPED:
1140e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                    if (newState > Fragment.STOPPED) {
1141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
11429c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                        f.performStart();
1143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.STARTED:
1145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (newState > Fragment.STARTED) {
1146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
11470adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        f.performResume();
11484e6647fe2551985f33407acd712a4942b090207aDianne Hackborn                        f.mSavedFragmentState = null;
11494e6647fe2551985f33407acd712a4942b090207aDianne Hackborn                        f.mSavedViewState = null;
1150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else if (f.mState > newState) {
1153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            switch (f.mState) {
1154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.RESUMED:
1155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (newState < Fragment.RESUMED) {
1156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
11570adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        f.performPause();
1158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.STARTED:
1160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (newState < Fragment.STARTED) {
1161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
1162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.performStop();
1163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1164e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                case Fragment.STOPPED:
1165218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn                    if (newState < Fragment.STOPPED) {
1166218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn                        if (DEBUG) Log.v(TAG, "movefrom STOPPED: " + f);
1167218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn                        f.performReallyStop();
1168218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn                    }
1169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.ACTIVITY_CREATED:
1170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (newState < Fragment.ACTIVITY_CREATED) {
1171e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                        if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
1172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (f.mView != null) {
1173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            // Need to save the current view state if not
1174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            // done already.
11758491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy                            if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) {
1176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                saveFragmentViewState(f);
1177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            }
1178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
11799c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn                        f.performDestroyView();
1180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (f.mView != null && f.mContainer != null) {
1181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            Animation anim = null;
1182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
11839277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                                anim = loadAnimation(f, transit, false,
1184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                        transitionStyle);
1185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            }
1186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            if (anim != null) {
1187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                final Fragment fragment = f;
1188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                f.mAnimatingAway = f.mView;
1189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                f.mStateAfterAnimating = newState;
119003526560f132021f8fd7290259762ab362d4d567Doris Liu                                final View viewToAnimate = f.mView;
119103526560f132021f8fd7290259762ab362d4d567Doris Liu                                anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener(
119203526560f132021f8fd7290259762ab362d4d567Doris Liu                                        viewToAnimate, anim) {
1193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    @Override
1194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    public void onAnimationEnd(Animation animation) {
119503526560f132021f8fd7290259762ab362d4d567Doris Liu                                        super.onAnimationEnd(animation);
1196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                        if (fragment.mAnimatingAway != null) {
1197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                            fragment.mAnimatingAway = null;
1198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                            moveToState(fragment, fragment.mStateAfterAnimating,
11995506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                                                    0, 0, false);
1200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                        }
1201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    }
1202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                });
12039277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                                f.mView.startAnimation(anim);
1204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            }
12058aa950177d9290b005f0817485f241ddc41c8026George Mount                            f.mContainer.removeView(f.mView);
1206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.mContainer = null;
1208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.mView = null;
1209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.mInnerView = null;
1210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                case Fragment.CREATED:
1212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (newState < Fragment.CREATED) {
1213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (mDestroyed) {
1214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            if (f.mAnimatingAway != null) {
1215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                // The fragment's containing activity is
1216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                // being destroyed, but this fragment is
1217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                // currently animating away.  Stop the
1218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                // animation right now -- it is not needed,
1219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                // and we can't wait any more on destroying
1220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                // the fragment.
1221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                View v = f.mAnimatingAway;
1222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                f.mAnimatingAway = null;
1223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                v.clearAnimation();
1224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            }
1225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (f.mAnimatingAway != null) {
1227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            // We are waiting for the fragment's view to finish
1228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            // animating away.  Just make a note of the state
1229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            // the fragment now should move to once the animation
1230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            // is done.
1231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            f.mStateAfterAnimating = newState;
12322c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn                            newState = Fragment.CREATED;
1233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        } else {
1234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            if (!f.mRetaining) {
12360adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                                f.performDestroy();
123720735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell                            } else {
1238b054427688e7cf0475bec09da9a3fb7688881459Adam Powell                                f.mState = Fragment.INITIALIZING;
1239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            }
1240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1241916455675ddb34d0eb848b2355550268d82c3ce7Adam Powell                            f.performDetach();
12425506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                            if (!keepActive) {
12435506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                                if (!f.mRetaining) {
12445506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                                    makeInactive(f);
12455506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                                } else {
1246d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                                    f.mHost = null;
12476252d78085a07c9d6bb4645a4e8086bf23b0a49aTim Kilbourn                                    f.mParentFragment = null;
12485506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                                    f.mFragmentManager = null;
12495506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                                }
12502c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn                            }
1251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
125520735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell
125620735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell        if (f.mState != newState) {
125720735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell            Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; "
125820735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell                    + "expected state " + newState + " found " + f.mState);
125920735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell            f.mState = newState;
126020735a45289fdad3d5d31228992e0dccd3d5dd4fAdam Powell        }
1261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void moveToState(Fragment f) {
12645506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn        moveToState(f, mCurState, 0, 0, false);
1265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void moveToState(int newState, boolean always) {
1268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        moveToState(newState, 0, 0, always);
1269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void moveToState(int newState, int transit, int transitStyle, boolean always) {
1272d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        if (mHost == null && newState != Fragment.INITIALIZING) {
1273d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy            throw new IllegalStateException("No host");
1274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
12750adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
1276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!always && mCurState == newState) {
1277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return;
1278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
12790adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
1280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mCurState = newState;
1281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mActive != null) {
1282abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            boolean loadersRunning = false;
1283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mActive.size(); i++) {
1284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mActive.get(i);
1285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f != null) {
12865506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                    moveToState(f, newState, transit, transitStyle, false);
1287abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell                    if (f.mLoaderManager != null) {
1288abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell                        loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1289abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell                    }
1290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1293abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            if (!loadersRunning) {
1294abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell                startPendingDeferredFragments();
1295abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            }
1296abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell
1297d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy            if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) {
12988491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy                mHost.onSupportInvalidateOptionsMenu();
1299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mNeedMenuInvalidate = false;
1300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1303abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell
1304abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    void startPendingDeferredFragments() {
13051199ae7067cdf8cf3eb30c057a61ae71a0aea1e5Adam Powell        if (mActive == null) return;
13061199ae7067cdf8cf3eb30c057a61ae71a0aea1e5Adam Powell
1307abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        for (int i=0; i<mActive.size(); i++) {
1308abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            Fragment f = mActive.get(i);
1309abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            if (f != null) {
1310abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell                performPendingDeferredStart(f);
1311abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell            }
1312abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell        }
1313abc968f1eba800c34a4008deb43b015da5d23a5fAdam Powell    }
1314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void makeActive(Fragment f) {
1316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (f.mIndex >= 0) {
1317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return;
1318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAvailIndices == null || mAvailIndices.size() <= 0) {
1321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mActive == null) {
1322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mActive = new ArrayList<Fragment>();
1323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
13240adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            f.setIndex(mActive.size(), mParent);
1325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mActive.add(f);
1326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
13280adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent);
1329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mActive.set(f.mIndex, f);
1330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1331be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn        if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
1332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void makeInactive(Fragment f) {
1335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (f.mIndex < 0) {
1336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return;
1337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1339be2c79d9a5439922030d2a3846c81c61f0e16912Dianne Hackborn        if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
1340cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mActive.set(f.mIndex, null);
1341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAvailIndices == null) {
1342cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAvailIndices = new ArrayList<Integer>();
1343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mAvailIndices.add(f.mIndex);
1345d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        mHost.inactivateFragment(f.mWho);
13469c53b844bd525e6a04e17291efc38713893074cdDianne Hackborn        f.initState();
1347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void addFragment(Fragment fragment, boolean moveToStateNow) {
1350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAdded == null) {
1351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAdded = new ArrayList<Fragment>();
1352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (DEBUG) Log.v(TAG, "add: " + fragment);
1354e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        makeActive(fragment);
1355e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        if (!fragment.mDetached) {
13563a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn            if (mAdded.contains(fragment)) {
13573a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                throw new IllegalStateException("Fragment already added: " + fragment);
13583a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn            }
1359e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            mAdded.add(fragment);
1360e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            fragment.mAdded = true;
1361e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            fragment.mRemoving = false;
13622a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment.mHasMenu && fragment.mMenuVisible) {
1363e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                mNeedMenuInvalidate = true;
1364e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            }
1365e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            if (moveToStateNow) {
1366e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                moveToState(fragment);
1367e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            }
1368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
1372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
1373e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        final boolean inactive = !fragment.isInBackStack();
1374e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        if (!fragment.mDetached || inactive) {
1375464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn            if (mAdded != null) {
1376464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn                mAdded.remove(fragment);
1377464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn            }
13782a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment.mHasMenu && fragment.mMenuVisible) {
1379e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                mNeedMenuInvalidate = true;
1380e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            }
1381e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            fragment.mAdded = false;
1382e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            fragment.mRemoving = true;
1383e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED,
13845506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                    transition, transitionStyle, false);
1385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
1389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (DEBUG) Log.v(TAG, "hide: " + fragment);
1390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!fragment.mHidden) {
1391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            fragment.mHidden = true;
1392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (fragment.mView != null) {
1393495efcad8c8865e3e6f14e4ea3d79cfb1d2410f9Alexander Mironov                Animation anim = loadAnimation(fragment, transition, false,
1394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        transitionStyle);
1395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (anim != null) {
139603526560f132021f8fd7290259762ab362d4d567Doris Liu                    setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    fragment.mView.startAnimation(anim);
1398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                fragment.mView.setVisibility(View.GONE);
1400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
14012a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mNeedMenuInvalidate = true;
1403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            fragment.onHiddenChanged(true);
1405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void showFragment(Fragment fragment, int transition, int transitionStyle) {
1409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (DEBUG) Log.v(TAG, "show: " + fragment);
1410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (fragment.mHidden) {
1411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            fragment.mHidden = false;
1412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (fragment.mView != null) {
14139277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                Animation anim = loadAnimation(fragment, transition, true,
1414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        transitionStyle);
1415cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (anim != null) {
141603526560f132021f8fd7290259762ab362d4d567Doris Liu                    setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    fragment.mView.startAnimation(anim);
1418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                fragment.mView.setVisibility(View.VISIBLE);
1420cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
14212a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn            if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mNeedMenuInvalidate = true;
1423cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            fragment.onHiddenChanged(false);
1425cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1427cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1428e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
1429e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        if (DEBUG) Log.v(TAG, "detach: " + fragment);
1430e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        if (!fragment.mDetached) {
1431e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            fragment.mDetached = true;
1432e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            if (fragment.mAdded) {
1433e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                // We are not already in back stack, so need to remove the fragment.
1434464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn                if (mAdded != null) {
14353a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                    if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
1436464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn                    mAdded.remove(fragment);
1437464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn                }
14382a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                if (fragment.mHasMenu && fragment.mMenuVisible) {
1439e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                    mNeedMenuInvalidate = true;
1440e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                }
1441e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                fragment.mAdded = false;
14425506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false);
1443e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            }
1444e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        }
1445e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    }
1446e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn
1447e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    public void attachFragment(Fragment fragment, int transition, int transitionStyle) {
1448e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        if (DEBUG) Log.v(TAG, "attach: " + fragment);
1449e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        if (fragment.mDetached) {
1450e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            fragment.mDetached = false;
1451e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            if (!fragment.mAdded) {
1452464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn                if (mAdded == null) {
1453464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn                    mAdded = new ArrayList<Fragment>();
1454464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn                }
14553a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                if (mAdded.contains(fragment)) {
14563a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                    throw new IllegalStateException("Fragment already added: " + fragment);
14573a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                }
14583a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
1459e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                mAdded.add(fragment);
1460e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                fragment.mAdded = true;
14612a4d8518f36346ea25a22a736453ff28f2954165Dianne Hackborn                if (fragment.mHasMenu && fragment.mMenuVisible) {
1462e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                    mNeedMenuInvalidate = true;
1463e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn                }
14645506618c80a292ac275d8b0c1046b446c7f58836Dianne Hackborn                moveToState(fragment, mCurState, transition, transitionStyle, false);
1465e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn            }
1466e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        }
1467e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn    }
1468e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn
1469cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Fragment findFragmentById(int id) {
1470464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
1471cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // First look through added fragments.
1472cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=mAdded.size()-1; i>=0; i--) {
1473cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
1474cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f != null && f.mFragmentId == id) {
1475cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    return f;
1476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1477cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1478464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        }
1479464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mActive != null) {
1480cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // Now for any known fragment.
1481cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=mActive.size()-1; i>=0; i--) {
1482cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mActive.get(i);
1483cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f != null && f.mFragmentId == id) {
1484cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    return f;
1485cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1487cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1488cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return null;
1489cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1490cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1491cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Fragment findFragmentByTag(String tag) {
1492464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null && tag != null) {
1493cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // First look through added fragments.
1494cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=mAdded.size()-1; i>=0; i--) {
1495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
1496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f != null && tag.equals(f.mTag)) {
1497cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    return f;
1498cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1499cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1500464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        }
1501464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mActive != null && tag != null) {
1502cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // Now for any known fragment.
1503cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=mActive.size()-1; i>=0; i--) {
1504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mActive.get(i);
1505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f != null && tag.equals(f.mTag)) {
1506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    return f;
1507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1508cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1509cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1510cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return null;
1511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1512cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1513cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Fragment findFragmentByWho(String who) {
1514cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mActive != null && who != null) {
1515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=mActive.size()-1; i>=0; i--) {
1516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mActive.get(i);
15170adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                if (f != null && (f=f.findFragmentByWho(who)) != null) {
1518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    return f;
1519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1522cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return null;
1523cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1524cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1525cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private void checkStateLoss() {
1526cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mStateSaved) {
1527cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException(
1528cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    "Can not perform this action after onSaveInstanceState");
1529cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1530cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mNoTransactionsBecause != null) {
1531cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException(
1532cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    "Can not perform this action inside of " + mNoTransactionsBecause);
1533cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1534cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1535cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1536ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette    /**
1537ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette     * Adds an action to the queue of pending actions.
1538ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette     *
1539ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette     * @param action the action to add
1540ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette     * @param allowStateLoss whether to allow loss of state information
1541ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette     * @throws IllegalStateException if the activity has been destroyed
1542ab6e5dbcc6e05994ebb4257478c54f54085b9aa6Alan Viverette     */
1543cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void enqueueAction(Runnable action, boolean allowStateLoss) {
1544cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!allowStateLoss) {
1545cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            checkStateLoss();
1546cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1547cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        synchronized (this) {
1548d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy            if (mDestroyed || mHost == null) {
1549cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                throw new IllegalStateException("Activity has been destroyed");
1550cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1551cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mPendingActions == null) {
1552cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mPendingActions = new ArrayList<Runnable>();
1553cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1554cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mPendingActions.add(action);
1555cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mPendingActions.size() == 1) {
1556d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                mHost.getHandler().removeCallbacks(mExecCommit);
1557d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                mHost.getHandler().post(mExecCommit);
1558cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1559cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1560cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1561cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1562cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int allocBackStackIndex(BackStackRecord bse) {
1563cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        synchronized (this) {
1564cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1565cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (mBackStackIndices == null) {
1566cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    mBackStackIndices = new ArrayList<BackStackRecord>();
1567cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1568cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                int index = mBackStackIndices.size();
1569cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1570cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBackStackIndices.add(bse);
1571cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                return index;
1572cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1573cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            } else {
1574cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1575cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1576cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBackStackIndices.set(index, bse);
1577cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                return index;
1578cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1579cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1580cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1581cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1582cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setBackStackIndex(int index, BackStackRecord bse) {
1583cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        synchronized (this) {
1584cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mBackStackIndices == null) {
1585cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBackStackIndices = new ArrayList<BackStackRecord>();
1586cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1587cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int N = mBackStackIndices.size();
1588cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (index < N) {
1589cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1590cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBackStackIndices.set(index, bse);
1591cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            } else {
1592cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                while (N < index) {
1593cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    mBackStackIndices.add(null);
1594cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (mAvailBackStackIndices == null) {
1595cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        mAvailBackStackIndices = new ArrayList<Integer>();
1596cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1597cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1598cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    mAvailBackStackIndices.add(N);
1599cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    N++;
1600cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1601cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1602cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBackStackIndices.add(bse);
1603cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1604cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1605cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1606cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1607cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void freeBackStackIndex(int index) {
1608cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        synchronized (this) {
1609cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStackIndices.set(index, null);
1610cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mAvailBackStackIndices == null) {
1611cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mAvailBackStackIndices = new ArrayList<Integer>();
1612cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1613cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1614cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAvailBackStackIndices.add(index);
1615cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1616cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1617cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1618e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell    public void execSingleAction(Runnable action, boolean allowStateLoss) {
1619e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        if (mExecutingActions) {
1620e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell            throw new IllegalStateException("FragmentManager is already executing transactions");
1621e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        }
1622e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell
1623e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        if (Looper.myLooper() != mHost.getHandler().getLooper()) {
1624e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell            throw new IllegalStateException("Must be called from main thread of fragment host");
1625e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        }
1626e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell
1627e880475b147312ca62bed05bbeb37ec820d693aeAdam Powell        if (!allowStateLoss) {
1628e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell            checkStateLoss();
1629e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        }
1630e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell
1631e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        mExecutingActions = true;
1632e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        action.run();
1633e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        mExecutingActions = false;
1634e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell
1635e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        doPendingDeferredStart();
1636e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell    }
1637e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell
1638cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
1639cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Only call from main thread!
1640cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
1641cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean execPendingActions() {
1642cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mExecutingActions) {
1643e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell            throw new IllegalStateException("FragmentManager is already executing transactions");
1644cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1645cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1646d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        if (Looper.myLooper() != mHost.getHandler().getLooper()) {
1647e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell            throw new IllegalStateException("Must be called from main thread of fragment host");
1648cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1649cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1650cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean didSomething = false;
1651cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1652cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        while (true) {
1653cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int numActions;
1654cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1655cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            synchronized (this) {
1656cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (mPendingActions == null || mPendingActions.size() == 0) {
165779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                    break;
1658cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1659cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1660cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                numActions = mPendingActions.size();
1661cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (mTmpActions == null || mTmpActions.length < numActions) {
1662cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    mTmpActions = new Runnable[numActions];
1663cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1664cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mPendingActions.toArray(mTmpActions);
1665cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mPendingActions.clear();
1666d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                mHost.getHandler().removeCallbacks(mExecCommit);
1667cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1668cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1669cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mExecutingActions = true;
1670cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<numActions; i++) {
1671cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mTmpActions[i].run();
16729f491519261135d9de50b60c2182755ccb665bf5Jeff Sharkey                mTmpActions[i] = null;
1673cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1674cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mExecutingActions = false;
1675cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            didSomething = true;
1676cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
167779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell
1678e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        doPendingDeferredStart();
1679e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell
1680e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell        return didSomething;
1681e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell    }
1682e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell
1683e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell    void doPendingDeferredStart() {
168479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        if (mHavePendingDeferredStart) {
168579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            boolean loadersRunning = false;
1686e1fad6fb0ee83d7f2dad3ec3dca6641a425e7244Adam Powell            for (int i = 0; i < mActive.size(); i++) {
168779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                Fragment f = mActive.get(i);
168879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                if (f != null && f.mLoaderManager != null) {
168979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                    loadersRunning |= f.mLoaderManager.hasRunningLoaders();
169079398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                }
169179398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            }
169279398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            if (!loadersRunning) {
169379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                mHavePendingDeferredStart = false;
169479398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell                startPendingDeferredFragments();
169579398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            }
169679398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        }
1697cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
169879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell
1699cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void reportBackStackChanged() {
1700cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStackChangeListeners != null) {
1701cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mBackStackChangeListeners.size(); i++) {
1702cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBackStackChangeListeners.get(i).onBackStackChanged();
1703cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1704cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1705cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1706cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1707cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void addBackStackState(BackStackRecord state) {
1708cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStack == null) {
1709cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStack = new ArrayList<BackStackRecord>();
1710cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1711cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mBackStack.add(state);
1712cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        reportBackStackChanged();
1713cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1714cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1715d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy    @SuppressWarnings("unused")
1716cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean popBackStackState(Handler handler, String name, int id, int flags) {
1717cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStack == null) {
1718cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return false;
1719cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1720cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) {
1721cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int last = mBackStack.size()-1;
1722cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (last < 0) {
1723cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                return false;
1724cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1725cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            final BackStackRecord bss = mBackStack.remove(last);
17269f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
17279f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
17284706c089880423ee5929d4635c6dfc5c3151b20fGeorge Mount            if (mCurState >= Fragment.CREATED) {
17294706c089880423ee5929d4635c6dfc5c3151b20fGeorge Mount                bss.calculateBackFragments(firstOutFragments, lastInFragments);
17304706c089880423ee5929d4635c6dfc5c3151b20fGeorge Mount            }
17319f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            bss.popFromBackStack(true, null, firstOutFragments, lastInFragments);
1732cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            reportBackStackChanged();
1733cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
1734cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int index = -1;
1735cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (name != null || id >= 0) {
1736cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // If a name or ID is specified, look for that place in
1737cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // the stack.
1738cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                index = mBackStack.size()-1;
1739cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                while (index >= 0) {
1740cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    BackStackRecord bss = mBackStack.get(index);
1741cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (name != null && name.equals(bss.getName())) {
1742cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        break;
1743cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1744cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (id >= 0 && id == bss.mIndex) {
1745cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        break;
1746cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1747cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    index--;
1748cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1749cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (index < 0) {
1750cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    return false;
1751cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1752cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
1753cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    index--;
1754cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    // Consume all following entries that match.
1755cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    while (index >= 0) {
1756cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        BackStackRecord bss = mBackStack.get(index);
1757cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if ((name != null && name.equals(bss.getName()))
1758cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                || (id >= 0 && id == bss.mIndex)) {
1759cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            index--;
1760cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            continue;
1761cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1762cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        break;
1763cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1764cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1765cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1766cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (index == mBackStack.size()-1) {
1767cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                return false;
1768cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1769cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            final ArrayList<BackStackRecord> states
1770cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    = new ArrayList<BackStackRecord>();
1771cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=mBackStack.size()-1; i>index; i--) {
1772cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                states.add(mBackStack.remove(i));
1773cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1774cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            final int LAST = states.size()-1;
17759f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
17769f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
17774706c089880423ee5929d4635c6dfc5c3151b20fGeorge Mount            if (mCurState >= Fragment.CREATED) {
17784706c089880423ee5929d4635c6dfc5c3151b20fGeorge Mount                for (int i = 0; i <= LAST; i++) {
17794706c089880423ee5929d4635c6dfc5c3151b20fGeorge Mount                    states.get(i).calculateBackFragments(firstOutFragments, lastInFragments);
17804706c089880423ee5929d4635c6dfc5c3151b20fGeorge Mount                }
17819f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            }
17829f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount            BackStackRecord.TransitionState state = null;
1783cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<=LAST; i++) {
1784cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i));
17859f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount                state = states.get(i).popFromBackStack(i == LAST, state,
17869f4a046cbeb22a24d322a644323b41022ad9d15aGeorge Mount                        firstOutFragments, lastInFragments);
1787cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1788cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            reportBackStackChanged();
1789cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1790cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return true;
1791cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1792cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1793c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    FragmentManagerNonConfig retainNonConfig() {
1794cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ArrayList<Fragment> fragments = null;
1795c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        ArrayList<FragmentManagerNonConfig> childFragments = null;
1796cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mActive != null) {
1797cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mActive.size(); i++) {
1798cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mActive.get(i);
1799c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                if (f != null) {
1800c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                    if (f.mRetainInstance) {
1801c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        if (fragments == null) {
1802c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                            fragments = new ArrayList<Fragment>();
1803c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        }
1804c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        fragments.add(f);
1805c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        f.mRetaining = true;
1806c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
1807c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
1808c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                    }
1809c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                    boolean addedChild = false;
1810c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                    if (f.mChildFragmentManager != null) {
1811c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        FragmentManagerNonConfig child = f.mChildFragmentManager.retainNonConfig();
1812c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        if (child != null) {
1813c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                            if (childFragments == null) {
1814c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                                childFragments = new ArrayList<FragmentManagerNonConfig>();
1815c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                                for (int j = 0; j < i; j++) {
1816c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                                    childFragments.add(null);
1817c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                                }
1818c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                            }
1819c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                            childFragments.add(child);
1820c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                            addedChild = true;
1821c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        }
1822c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                    }
1823c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                    if (childFragments != null && !addedChild) {
1824c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                        childFragments.add(null);
1825cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1826cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1827cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1828cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1829c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        if (fragments == null && childFragments == null) {
1830c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            return null;
1831c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        }
1832c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        return new FragmentManagerNonConfig(fragments, childFragments);
1833cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1834cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1835cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void saveFragmentViewState(Fragment f) {
1836cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (f.mInnerView == null) {
1837cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return;
1838cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1839cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mStateArray == null) {
1840cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mStateArray = new SparseArray<Parcelable>();
1841ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn        } else {
1842ea2c91b0198855073983b4a8437aa71cbd83872fDianne Hackborn            mStateArray.clear();
1843cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1844cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        f.mInnerView.saveHierarchyState(mStateArray);
1845cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mStateArray.size() > 0) {
1846cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            f.mSavedViewState = mStateArray;
1847cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mStateArray = null;
1848cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1849cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1850cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
18515c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    Bundle saveFragmentBasicState(Fragment f) {
18525c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        Bundle result = null;
18535c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
18545c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (mStateBundle == null) {
18555c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mStateBundle = new Bundle();
18565c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
18570adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        f.performSaveInstanceState(mStateBundle);
18585c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (!mStateBundle.isEmpty()) {
18595c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            result = mStateBundle;
18605c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            mStateBundle = null;
18615c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
18625c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
18635c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (f.mView != null) {
18645c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            saveFragmentViewState(f);
18655c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
18665c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        if (f.mSavedViewState != null) {
18675c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            if (result == null) {
18685c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                result = new Bundle();
18695c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            }
18705c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn            result.putSparseParcelableArray(
18715c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                    FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
18725c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        }
187379398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        if (!f.mUserVisibleHint) {
1874f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton            if (result == null) {
1875f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton                result = new Bundle();
1876f4c0cf637ba73374a206cec26c09d4dfa4c1a364Jake Wharton            }
187779398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            // Only add this if it's not the default value
187879398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell            result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
187979398eaefea45e61d839cf4e0534f0eafee70a09Adam Powell        }
18805c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
18815c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn        return result;
18825c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn    }
18835c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn
1884cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    Parcelable saveAllState() {
1885cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Make sure all pending operations have now been executed to get
1886cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // our state update-to-date.
1887cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        execPendingActions();
1888cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1889681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn        if (HONEYCOMB) {
1890681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // As of Honeycomb, we save state after pausing.  Prior to that
1891681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // it is before pausing.  With fragments this is an issue, since
1892681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // there are many things you may do after pausing but before
1893681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // stopping that change the fragment state.  For those older
1894681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // devices, we will not at this point say that we have saved
1895681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // the state, so we will allow them to continue doing fragment
1896681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // transactions.  This retains the same semantics as Honeycomb,
1897681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // though you do have the risk of losing the very most recent state
1898681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            // if the process is killed...  we'll live with that.
1899681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn            mStateSaved = true;
1900681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn        }
1901cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1902cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mActive == null || mActive.size() <= 0) {
1903cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
1904cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1905cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1906cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // First collect all active fragments.
1907cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        int N = mActive.size();
1908cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        FragmentState[] active = new FragmentState[N];
1909cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean haveFragments = false;
1910cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        for (int i=0; i<N; i++) {
1911cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            Fragment f = mActive.get(i);
1912cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (f != null) {
19131b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn                if (f.mIndex < 0) {
191413fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                    throwException(new IllegalStateException(
191513fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                            "Failure saving state: active " + f
191613fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                            + " has cleared index: " + f.mIndex));
19171b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn                }
19181b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn
1919cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                haveFragments = true;
1920cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1921cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                FragmentState fs = new FragmentState(f);
1922cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                active[i] = fs;
1923cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1924cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
19255c1637087453de15e31861f073eae5133c4e9f7bDianne Hackborn                    fs.mSavedFragmentState = saveFragmentBasicState(f);
1926cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1927cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (f.mTarget != null) {
1928cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (f.mTarget.mIndex < 0) {
192913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                            throwException(new IllegalStateException(
193013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                    "Failure saving state: " + f
193113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                    + " has target not in fragment manager: " + f.mTarget));
1932cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1933cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (fs.mSavedFragmentState == null) {
1934cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            fs.mSavedFragmentState = new Bundle();
1935cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1936cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        putFragment(fs.mSavedFragmentState,
1937cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
1938cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        if (f.mTargetRequestCode != 0) {
1939cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            fs.mSavedFragmentState.putInt(
1940cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
1941cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                    f.mTargetRequestCode);
1942cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        }
1943cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
1944cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1945cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                } else {
1946cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    fs.mSavedFragmentState = f.mSavedFragmentState;
1947cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1948cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1949cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
1950cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        + fs.mSavedFragmentState);
1951cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1952cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1953cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1954cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!haveFragments) {
1955cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
1956cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
1957cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1958cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1959cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        int[] added = null;
1960cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        BackStackState[] backStack = null;
1961cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1962cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Build list of currently added fragments.
1963cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAdded != null) {
1964cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            N = mAdded.size();
1965cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (N > 0) {
1966cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                added = new int[N];
1967cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                for (int i=0; i<N; i++) {
1968cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    added[i] = mAdded.get(i).mIndex;
19691b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn                    if (added[i] < 0) {
197013fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                        throwException(new IllegalStateException(
197113fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                "Failure saving state: active " + mAdded.get(i)
197213fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                                + " has cleared index: " + added[i]));
19731b913519b1c03b084779851e81db2e1a11eb0b0dDianne Hackborn                    }
1974cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
1975cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            + ": " + mAdded.get(i));
1976cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1977cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1978cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1979cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1980cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Now save back stack.
1981cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mBackStack != null) {
1982cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            N = mBackStack.size();
1983cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (N > 0) {
1984cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                backStack = new BackStackState[N];
1985cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                for (int i=0; i<N; i++) {
1986d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                    backStack[i] = new BackStackState(mBackStack.get(i));
1987cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
1988cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            + ": " + mBackStack.get(i));
1989cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
1990cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
1991cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
1992cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
1993cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        FragmentManagerState fms = new FragmentManagerState();
1994cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        fms.mActive = active;
1995cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        fms.mAdded = added;
1996cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        fms.mBackStack = backStack;
1997cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return fms;
1998cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
1999cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2000c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell    void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) {
2001cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // If there is no saved state at all, then there can not be
2002cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // any nonConfig fragments either, so that is that.
2003cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (state == null) return;
2004cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        FragmentManagerState fms = (FragmentManagerState)state;
2005cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (fms.mActive == null) return;
2006c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell
2007c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        List<FragmentManagerNonConfig> childNonConfigs = null;
2008c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell
2009cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // First re-attach any non-config instances we are retaining back
2010cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // to their saved state, so we don't try to instantiate them again.
2011cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (nonConfig != null) {
2012c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            List<Fragment> nonConfigFragments = nonConfig.getFragments();
2013c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            childNonConfigs = nonConfig.getChildNonConfigs();
2014c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2015c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            for (int i = 0; i < count; i++) {
2016c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                Fragment f = nonConfigFragments.get(i);
2017cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
2018cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                FragmentState fs = fms.mActive[f.mIndex];
2019cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                fs.mInstance = f;
2020cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                f.mSavedViewState = null;
2021cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                f.mBackStackNesting = 0;
2022cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                f.mInLayout = false;
2023cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                f.mAdded = false;
20242c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn                f.mTarget = null;
2025cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (fs.mSavedFragmentState != null) {
2026d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy                    fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader());
2027cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
2028cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                            FragmentManagerImpl.VIEW_STATE_TAG);
20298e4a59b54e9225b77151805dd6b8867dcd8e60a4Craig Mautner                    f.mSavedFragmentState = fs.mSavedFragmentState;
2030cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2031cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2032cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2033cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2034cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Build the full list of active fragments, instantiating them from
2035cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // their saved state.
2036c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell        mActive = new ArrayList<>(fms.mActive.length);
2037cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAvailIndices != null) {
2038cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAvailIndices.clear();
2039cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2040cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        for (int i=0; i<fms.mActive.length; i++) {
2041cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            FragmentState fs = fms.mActive[i];
2042cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (fs != null) {
2043c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                FragmentManagerNonConfig childNonConfig = null;
2044c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                if (childNonConfigs != null && i < childNonConfigs.size()) {
2045c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                    childNonConfig = childNonConfigs.get(i);
2046c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                }
2047c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                Fragment f = fs.instantiate(mHost, mParent, childNonConfig);
20483a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
2049cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mActive.add(f);
2050cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // Now that the fragment is instantiated (or came from being
2051cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // retained above), clear mInstance in case we end up re-restoring
2052cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                // from this FragmentState again.
2053cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                fs.mInstance = null;
2054cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            } else {
2055cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mActive.add(null);
2056cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (mAvailIndices == null) {
2057cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    mAvailIndices = new ArrayList<Integer>();
2058cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
20593a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
2060cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mAvailIndices.add(i);
2061cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2062cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2063cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2064cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Update the target of all retained fragments.
2065cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (nonConfig != null) {
2066c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            List<Fragment> nonConfigFragments = nonConfig.getFragments();
2067c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2068c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell            for (int i = 0; i < count; i++) {
2069c077b4cd990a9f220a27ad5eca04828e17136064Adam Powell                Fragment f = nonConfigFragments.get(i);
20702c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn                if (f.mTargetIndex >= 0) {
20712c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn                    if (f.mTargetIndex < mActive.size()) {
20722c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn                        f.mTarget = mActive.get(f.mTargetIndex);
2073cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    } else {
2074cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        Log.w(TAG, "Re-attaching retained fragment " + f
20752c4b5dbfee5232bcbbcb74b84ce9147b62a9d789Dianne Hackborn                                + " target no longer exists: " + f.mTargetIndex);
2076cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        f.mTarget = null;
2077cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
2078cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2079cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2080cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2081cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2082cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Build the list of currently added fragments.
2083cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (fms.mAdded != null) {
2084cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAdded = new ArrayList<Fragment>(fms.mAdded.length);
2085cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<fms.mAdded.length; i++) {
2086cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mActive.get(fms.mAdded[i]);
2087cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f == null) {
208813fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                    throwException(new IllegalStateException(
208913fb2b96fa8464e7b8514c57e1ad5ea782b3a52cDianne Hackborn                            "No instantiated fragment for index #" + fms.mAdded[i]));
2090cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2091cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                f.mAdded = true;
20923a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
20933a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                if (mAdded.contains(f)) {
20943a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                    throw new IllegalStateException("Already added!");
20953a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                }
2096cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mAdded.add(f);
2097cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2098cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
2099cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAdded = null;
2100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // Build the back stack.
2103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (fms.mBackStack != null) {
2104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
2105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<fms.mBackStack.length; i++) {
2106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                BackStackRecord bse = fms.mBackStack[i].instantiate(this);
21073a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                if (DEBUG) {
21083a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                    Log.v(TAG, "restoreAllState: back stack #" + i
2109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        + " (index " + bse.mIndex + "): " + bse);
21103a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                    LogWriter logw = new LogWriter(TAG);
21113a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                    PrintWriter pw = new PrintWriter(logw);
21123a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                    bse.dump("  ", pw, false);
21133a7571011a8f2c2e2685c4e3e7a6fa46673ee7eaDianne Hackborn                }
2114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mBackStack.add(bse);
2115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (bse.mIndex >= 0) {
2116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    setBackStackIndex(bse.mIndex, bse);
2117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
2120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mBackStack = null;
2121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2123d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy
21248491eb62f621cd5de4b4caed839be09c77011f53Todd Kennedy    public void attachController(FragmentHostCallback host,
21250adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn            FragmentContainer container, Fragment parent) {
2126d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        if (mHost != null) throw new IllegalStateException("Already attached");
2127d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        mHost = host;
21280adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        mContainer = container;
21290adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        mParent = parent;
2130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void noteStateNotSaved() {
2133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStateSaved = false;
2134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchCreate() {
2137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStateSaved = false;
2138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        moveToState(Fragment.CREATED, false);
2139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchActivityCreated() {
2142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStateSaved = false;
2143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        moveToState(Fragment.ACTIVITY_CREATED, false);
2144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchStart() {
2147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStateSaved = false;
2148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        moveToState(Fragment.STARTED, false);
2149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchResume() {
2152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStateSaved = false;
2153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        moveToState(Fragment.RESUMED, false);
2154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchPause() {
2157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        moveToState(Fragment.STARTED, false);
2158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchStop() {
2161681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn        // See saveAllState() for the explanation of this.  We do this for
2162681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn        // all platform versions, to keep our behavior more consistent between
2163681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn        // them.
2164681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn        mStateSaved = true;
2165681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn
2166e4417c91a0bb2fba42a0aaa99edcca1b238af21aDianne Hackborn        moveToState(Fragment.STOPPED, false);
2167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2169218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn    public void dispatchReallyStop() {
2170218c1e661578e2a17928f7dbb590b43d1c79aeb7Dianne Hackborn        moveToState(Fragment.ACTIVITY_CREATED, false);
2171681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn    }
2172681a6fb06bdedb8661a68a1b9e34727b6059aa39Dianne Hackborn
21730adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    public void dispatchDestroyView() {
21740adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        moveToState(Fragment.CREATED, false);
21750adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn    }
21760adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn
2177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchDestroy() {
2178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mDestroyed = true;
2179e8b402b00c0cbdac050c349a5fc89c34580f3185Dianne Hackborn        execPendingActions();
2180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        moveToState(Fragment.INITIALIZING, false);
2181d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        mHost = null;
21820adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        mContainer = null;
21830adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn        mParent = null;
2184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
21855fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian
21865fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian    public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) {
21875fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        if (mAdded == null) {
21885fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            return;
21895fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        }
21905fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        for (int i = mAdded.size() - 1; i >= 0; --i) {
21915fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            final android.support.v4.app.Fragment f = mAdded.get(i);
21925fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            if (f != null) {
21935fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian                f.performMultiWindowModeChanged(isInMultiWindowMode);
21945fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            }
21955fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        }
21965fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian    }
21975fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian
21985fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian    public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
21995fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        if (mAdded == null) {
22005fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            return;
22015fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        }
22025fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        for (int i = mAdded.size() - 1; i >= 0; --i) {
22035fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            final android.support.v4.app.Fragment f = mAdded.get(i);
22045fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            if (f != null) {
22055fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian                f.performPictureInPictureModeChanged(isInPictureInPictureMode);
22065fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian            }
22075fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian        }
22085fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian    }
22095fa6d3dd3f408ceb2ff7368fd0259713d9382ad8Andrii Kulian
2210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchConfigurationChanged(Configuration newConfig) {
2211464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
2212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
2214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f != null) {
22150adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    f.performConfigurationChanged(newConfig);
2216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchLowMemory() {
2222464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
2223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
2225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (f != null) {
22260adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    f.performLowMemory();
2227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean show = false;
2234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        ArrayList<Fragment> newMenus = null;
2235464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
2236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
22380adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                if (f != null) {
22390adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    if (f.performCreateOptionsMenu(menu, inflater)) {
22400adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        show = true;
22410adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        if (newMenus == null) {
22420adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                            newMenus = new ArrayList<Fragment>();
22430adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        }
22440adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        newMenus.add(f);
2245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
2246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2249c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes
2250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mCreatedMenus != null) {
2251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mCreatedMenus.size(); i++) {
2252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mCreatedMenus.get(i);
2253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (newMenus == null || !newMenus.contains(f)) {
2254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    f.onDestroyOptionsMenu();
2255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2258c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes
2259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mCreatedMenus = newMenus;
2260c1e87ac17c772ee3c5fd3b3f08321226e2c7ffa4Chris Banes
2261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return show;
2262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean dispatchPrepareOptionsMenu(Menu menu) {
2265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean show = false;
2266464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
2267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
22690adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                if (f != null) {
22700adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    if (f.performPrepareOptionsMenu(menu)) {
22710adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                        show = true;
22720adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    }
2273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return show;
2277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean dispatchOptionsItemSelected(MenuItem item) {
2280464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
2281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
22830adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                if (f != null) {
22840adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    if (f.performOptionsItemSelected(item)) {
2285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        return true;
2286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
2287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return false;
2291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean dispatchContextItemSelected(MenuItem item) {
2294464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
2295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
22970adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                if (f != null) {
22980adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    if (f.performContextItemSelected(item)) {
2299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        return true;
2300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
2301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return false;
2305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dispatchOptionsMenuClosed(Menu menu) {
2308464b6f3c93dda03359ec2d37c8205065922f2994Dianne Hackborn        if (mAdded != null) {
2309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                Fragment f = mAdded.get(i);
23110adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                if (f != null) {
23120adacc1aa313d757ae1c517152cef838e0f35c13Dianne Hackborn                    f.performOptionsMenuClosed(menu);
2313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
2314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
2315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2317461b48b4588ac21b97aa40553f04222c2c0344e7Chris Banes
2318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static int reverseTransit(int transit) {
2319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        int rev = 0;
2320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        switch (transit) {
2321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
2323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                break;
2324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
2326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                break;
2327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
2329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                break;
2330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2331cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return rev;
2332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
2333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
23359277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    public static final int ANIM_STYLE_OPEN_ENTER = 1;
23369277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    public static final int ANIM_STYLE_OPEN_EXIT = 2;
23379277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    public static final int ANIM_STYLE_CLOSE_ENTER = 3;
23389277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    public static final int ANIM_STYLE_CLOSE_EXIT = 4;
23399277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    public static final int ANIM_STYLE_FADE_ENTER = 5;
23409277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn    public static final int ANIM_STYLE_FADE_EXIT = 6;
23419277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn
2342cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static int transitToStyleIndex(int transit, boolean enter) {
2343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        int animAttr = -1;
2344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        switch (transit) {
2345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
23469277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                animAttr = enter ? ANIM_STYLE_OPEN_ENTER : ANIM_STYLE_OPEN_EXIT;
2347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                break;
2348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
23499277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                animAttr = enter ? ANIM_STYLE_CLOSE_ENTER : ANIM_STYLE_CLOSE_EXIT;
2350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                break;
2351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
23529277b9e4419c1f0b5236d9b573a7cc0b23d56402Dianne Hackborn                animAttr = enter ? ANIM_STYLE_FADE_ENTER : ANIM_STYLE_FADE_EXIT;
2353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                break;
2354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
2355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return animAttr;
2356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
23570f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
23580f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell    @Override
2359bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
23600f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (!"fragment".equals(name)) {
23610f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            return null;
23620f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
23630f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
23640f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        String fname = attrs.getAttributeValue(null, "class");
23650f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        TypedArray a =  context.obtainStyledAttributes(attrs, FragmentTag.Fragment);
23660f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (fname == null) {
23670f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fname = a.getString(FragmentTag.Fragment_name);
23680f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
23690f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID);
23700f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        String tag = a.getString(FragmentTag.Fragment_tag);
23710f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        a.recycle();
23720f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
2373d608cf6e08769bf320c1b595cbbd9a7664160449Todd Kennedy        if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) {
23740f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // Invalid support lib fragment; let the device's framework handle it.
23750f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // This will allow android.app.Fragments to do the right thing.
23760f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            return null;
23770f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
23780f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
23790f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        int containerId = parent != null ? parent.getId() : 0;
23800f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
23810f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            throw new IllegalArgumentException(attrs.getPositionDescription()
23820f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                    + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
23830f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
23840f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
23850f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        // If we restored from a previous state, we may already have
23860f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        // instantiated this fragment from the state and should use
23870f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        // that instance instead of making a new one.
23880f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
23890f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (fragment == null && tag != null) {
23900f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment = findFragmentByTag(tag);
23910f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
23920f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (fragment == null && containerId != View.NO_ID) {
23930f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment = findFragmentById(containerId);
23940f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
23950f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
23960f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
23970f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                + Integer.toHexString(id) + " fname=" + fname
23980f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                + " existing=" + fragment);
23990f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (fragment == null) {
24000f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment = Fragment.instantiate(context, fname);
24010f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mFromLayout = true;
24020f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mFragmentId = id != 0 ? id : containerId;
24030f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mContainerId = containerId;
24040f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mTag = tag;
24050f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mInLayout = true;
24060f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mFragmentManager = this;
24071b84066e4233b4b0c8a32fffc30f95b8cd20ced4Chris Banes            fragment.mHost = mHost;
2408b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy            fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
24090f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            addFragment(fragment, true);
24100f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
24110f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        } else if (fragment.mInLayout) {
24120f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // A fragment already exists and it is not one we restored from
24130f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // previous state.
24140f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            throw new IllegalArgumentException(attrs.getPositionDescription()
24150f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                    + ": Duplicate id 0x" + Integer.toHexString(id)
24160f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                    + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
24170f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                    + " with another fragment for " + fname);
24180f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        } else {
24190f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // This fragment was retained from a previous instance; get it
24200f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // going now.
24210f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mInLayout = true;
2422e4148d65bbd62585c68c5782c2081bab6b303568Todd Kennedy            fragment.mHost = mHost;
24230f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // If this fragment is newly instantiated (either right now, or
24240f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // from last saved state), then give it the attributes to
24250f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            // initialize itself.
24260f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            if (!fragment.mRetaining) {
2427b979cb86fa389effb7cd79fa045550c10b7b4819Todd Kennedy                fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
24280f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            }
24290f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
24300f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
24310f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        // If we haven't finished entering the CREATED state ourselves yet,
24320f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        // push the inflated child fragment along.
24330f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
24340f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            moveToState(fragment, Fragment.CREATED, 0, 0, false);
24350f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        } else {
24360f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            moveToState(fragment);
24370f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
24380f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
24390f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (fragment.mView == null) {
24400f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            throw new IllegalStateException("Fragment " + fname
24410f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                    + " did not create a view.");
24420f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
24430f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (id != 0) {
24440f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mView.setId(id);
24450f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
24460f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        if (fragment.mView.getTag() == null) {
24470f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell            fragment.mView.setTag(tag);
24480f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        }
24490f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        return fragment.mView;
24500f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell    }
24510f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
2452bf0947be2ead9b3d8e5865bcd3d3652d02a2aa5aChris Banes    LayoutInflaterFactory getLayoutInflaterFactory() {
24530f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        return this;
24540f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell    }
24550f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell
24560f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell    static class FragmentTag {
24570f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        public static final int[] Fragment = {
24580f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell                0x01010003, 0x010100d0, 0x010100d1
24590f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        };
24600f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        public static final int Fragment_id = 1;
24610f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        public static final int Fragment_name = 0;
24620f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell        public static final int Fragment_tag = 2;
24630f3dfb28a503b3fb3e51666dd565b0d17eaebfbbAdam Powell    }
2464cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
2465