FragmentManager.java revision c03da0e7a9ef721709d51cf8a2d539a5bd8a320e
12dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn/*
22dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * Copyright (C) 2010 The Android Open Source Project
32dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn *
42dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
52dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * you may not use this file except in compliance with the License.
62dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * You may obtain a copy of the License at
72dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn *
82dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
92dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn *
102dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * Unless required by applicable law or agreed to in writing, software
112dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
122dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * See the License for the specific language governing permissions and
142dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * limitations under the License.
152dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn */
162dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
172dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackbornpackage android.app;
182dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
19a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.animation.Animator;
20a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.animation.AnimatorInflater;
21b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haaseimport android.animation.AnimatorListenerAdapter;
22371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powellimport android.content.Context;
239d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackbornimport android.content.res.Configuration;
24f121be737c59390d97e21a92be8e166001534c7dDianne Hackbornimport android.content.res.TypedArray;
252dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackbornimport android.os.Bundle;
26f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackbornimport android.os.Debug;
27ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackbornimport android.os.Handler;
283a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackbornimport android.os.Looper;
29b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackbornimport android.os.Parcel;
30b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackbornimport android.os.Parcelable;
31371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powellimport android.util.AttributeSet;
32a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackbornimport android.util.DebugUtils;
335ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackbornimport android.util.Log;
345d9d03a0234faa3cffd11502f973057045cafe82Dianne Hackbornimport android.util.LogWriter;
35b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackbornimport android.util.SparseArray;
361487466dc2ce14cccf0ff2bd2f824238aaa0044eAdam Powellimport android.util.SuperNotCalledException;
37371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powellimport android.view.LayoutInflater;
38b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackbornimport android.view.Menu;
39b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackbornimport android.view.MenuInflater;
40b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackbornimport android.view.MenuItem;
415ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackbornimport android.view.View;
422dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackbornimport android.view.ViewGroup;
438c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackbornimport com.android.internal.util.FastPrintWriter;
442dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
45625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackbornimport java.io.FileDescriptor;
46625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackbornimport java.io.PrintWriter;
472dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackbornimport java.util.ArrayList;
48d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackbornimport java.util.Arrays;
492dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
50b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn/**
51b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn * Interface for interacting with {@link Fragment} objects inside of an
52b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn * {@link Activity}
53b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez *
54b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <div class="special reference">
55b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <h3>Developer Guides</h3>
56b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <p>For more information about using fragments, read the
57b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
58b54e7a3d9f60ac605f404f9eb3c5e92ca51bbd23Joe Fernandez * </div>
597871badd5d4d29d80207e9cc09a0681f26a151d0Dianne Hackborn *
607871badd5d4d29d80207e9cc09a0681f26a151d0Dianne Hackborn * While the FragmentManager API was introduced in
617871badd5d4d29d80207e9cc09a0681f26a151d0Dianne Hackborn * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
627871badd5d4d29d80207e9cc09a0681f26a151d0Dianne Hackborn * at is also available for use on older platforms through
637871badd5d4d29d80207e9cc09a0681f26a151d0Dianne Hackborn * {@link android.support.v4.app.FragmentActivity}.  See the blog post
647871badd5d4d29d80207e9cc09a0681f26a151d0Dianne Hackborn * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
657871badd5d4d29d80207e9cc09a0681f26a151d0Dianne Hackborn * Fragments For All</a> for more details.
66b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn */
67ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackbornpublic abstract class FragmentManager {
68b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
69c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * Representation of an entry on the fragment back stack, as created
70c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * with {@link FragmentTransaction#addToBackStack(String)
71c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * FragmentTransaction.addToBackStack()}.  Entries can later be
72327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
73c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * FragmentManager.getBackStackEntry()}.
74c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     *
75c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * <p>Note that you should never hold on to a BackStackEntry object;
76c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * the identifier as returned by {@link #getId} is the only thing that
77c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * will be persisted across activity instances.
78c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     */
79c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public interface BackStackEntry {
80c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        /**
81c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * Return the unique identifier for the entry.  This is the only
82c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * representation of the entry that will persist across activity
83c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * instances.
84c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         */
85c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        public int getId();
86c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
87c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        /**
886c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn         * Get the name that was supplied to
896c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn         * {@link FragmentTransaction#addToBackStack(String)
906c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn         * FragmentTransaction.addToBackStack(String)} when creating this entry.
916c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn         */
926c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn        public String getName();
936c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn
946c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn        /**
95327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn         * Return the full bread crumb title resource identifier for the entry,
96327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn         * or 0 if it does not have one.
97327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn         */
98327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        public int getBreadCrumbTitleRes();
99327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
100327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        /**
101327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn         * Return the short bread crumb title resource identifier for the entry,
102327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn         * or 0 if it does not have one.
103327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn         */
104327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        public int getBreadCrumbShortTitleRes();
105327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn
106327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn        /**
107c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * Return the full bread crumb title for the entry, or null if it
108c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * does not have one.
109c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         */
110c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        public CharSequence getBreadCrumbTitle();
111c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
112c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        /**
113c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * Return the short bread crumb title for the entry, or null if it
114c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * does not have one.
115c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         */
116c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        public CharSequence getBreadCrumbShortTitle();
117c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
118c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
119c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    /**
120c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * Interface to watch for changes to the back stack.
121c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     */
122c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public interface OnBackStackChangedListener {
123c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        /**
124c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         * Called whenever the contents of the back stack change.
125c6669ca63299219d815464129dac051ab2404286Dianne Hackborn         */
126c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        public void onBackStackChanged();
127c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
128c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
129c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    /**
130b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Start a series of edit operations on the Fragments associated with
131b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * this FragmentManager.
132fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     *
133fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * <p>Note: A fragment transaction can only be created/committed prior
134fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * to an activity saving its state.  If you try to commit a transaction
135fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()}
136fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * (and prior to a following {@link Activity#onStart Activity.onStart}
137fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * or {@link Activity#onResume Activity.onResume()}, you will get an error.
138fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * This is because the framework takes care of saving your current fragments
139fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * in the state, and if changes are made after the state is saved then they
140fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn     * will be lost.</p>
141b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
14248e7b458694acdf3a4fc58e62437f1dbc4f29d83Dianne Hackborn    public abstract FragmentTransaction beginTransaction();
143b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
14417b9b81418c9166e181a992f27598e4de18d7203Dianne Hackborn    /** @hide -- remove once prebuilts are in. */
145327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    @Deprecated
14648e7b458694acdf3a4fc58e62437f1dbc4f29d83Dianne Hackborn    public FragmentTransaction openTransaction() {
14748e7b458694acdf3a4fc58e62437f1dbc4f29d83Dianne Hackborn        return beginTransaction();
14848e7b458694acdf3a4fc58e62437f1dbc4f29d83Dianne Hackborn    }
14948e7b458694acdf3a4fc58e62437f1dbc4f29d83Dianne Hackborn
150b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
1513a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * After a {@link FragmentTransaction} is committed with
1523a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
1533a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * is scheduled to be executed asynchronously on the process's main thread.
1543a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * If you want to immediately executing any such pending operations, you
1553a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * can call this function (only from the main thread) to do so.  Note that
1563a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * all callbacks and other related behavior will be done from within this
1573a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * call, so be careful about where this is called from.
1583a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     *
1593a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * @return Returns true if there were any pending transactions to be
1603a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * executed.
1613a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     */
1623a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public abstract boolean executePendingTransactions();
1633a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
1643a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    /**
165b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Finds a fragment that was identified by the given id either when inflated
166b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * from XML or as the container ID when added in a transaction.  This first
167b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * searches through fragments that are currently added to the manager's
168b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * activity; if no such fragment is found, then all fragments currently
169b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * on the back stack associated with this ID are searched.
170b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @return The fragment if found or null otherwise.
171b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
172ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public abstract Fragment findFragmentById(int id);
173b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
174b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
175b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Finds a fragment that was identified by the given tag either when inflated
176b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * from XML or as supplied when added in a transaction.  This first
177b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * searches through fragments that are currently added to the manager's
178b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * activity; if no such fragment is found, then all fragments currently
179b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * on the back stack are searched.
180b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @return The fragment if found or null otherwise.
181b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
182ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public abstract Fragment findFragmentByTag(String tag);
183b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
184b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
185b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Flag for {@link #popBackStack(String, int)}
186b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * and {@link #popBackStack(int, int)}: If set, and the name or ID of
187b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * a back stack entry has been supplied, then all matching entries will
188b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * be consumed until one that doesn't match is found or the bottom of
189b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * the stack is reached.  Otherwise, all entries up to but not including that entry
190b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * will be removed.
191b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
192b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
193b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
194b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
19587ffa20a2fd4dff11262b87607a9aca992bdd05dBen Komalo     * Pop the top state off the back stack.  This function is asynchronous -- it
19687ffa20a2fd4dff11262b87607a9aca992bdd05dBen Komalo     * enqueues the request to pop, but the action will not be performed until the
19787ffa20a2fd4dff11262b87607a9aca992bdd05dBen Komalo     * application returns to its event loop.
198b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
1993a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public abstract void popBackStack();
2003a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
2013a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    /**
2023a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * Like {@link #popBackStack()}, but performs the operation immediately
2033a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * inside of the call.  This is like calling {@link #executePendingTransactions()}
2043a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * afterwards.
2053a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * @return Returns true if there was something popped, else false.
2063a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     */
2073a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public abstract boolean popBackStackImmediate();
208b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
209b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
210b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Pop the last fragment transition from the manager's fragment
211b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * back stack.  If there is nothing to pop, false is returned.
212327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * This function is asynchronous -- it enqueues the
213327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * request to pop, but the action will not be performed until the application
214327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * returns to its event loop.
215327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     *
216b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param name If non-null, this is the name of a previous back state
217b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * to look for; if found, all states up to that state will be popped.  The
218b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
219b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * the named state itself is popped. If null, only the top state is popped.
220b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
221b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
2223a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public abstract void popBackStack(String name, int flags);
2233a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
2243a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    /**
2253a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * Like {@link #popBackStack(String, int)}, but performs the operation immediately
2263a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * inside of the call.  This is like calling {@link #executePendingTransactions()}
2273a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * afterwards.
2283a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * @return Returns true if there was something popped, else false.
2293a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     */
2303a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public abstract boolean popBackStackImmediate(String name, int flags);
231b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
232b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
233b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Pop all back stack states up to the one with the given identifier.
234327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * This function is asynchronous -- it enqueues the
235327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * request to pop, but the action will not be performed until the application
236327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     * returns to its event loop.
237327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn     *
238b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param id Identifier of the stated to be popped. If no identifier exists,
239b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * false is returned.
240b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * The identifier is the number returned by
241b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * {@link FragmentTransaction#commit() FragmentTransaction.commit()}.  The
242b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
243b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * the named state itself is popped.
244b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
245b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
2463a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public abstract void popBackStack(int id, int flags);
2473a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
2483a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    /**
2493a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * Like {@link #popBackStack(int, int)}, but performs the operation immediately
2503a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * inside of the call.  This is like calling {@link #executePendingTransactions()}
2513a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * afterwards.
2523a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     * @return Returns true if there was something popped, else false.
2533a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn     */
2543a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public abstract boolean popBackStackImmediate(int id, int flags);
255b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
256b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
257c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * Return the number of entries currently in the back stack.
258c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     */
259327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public abstract int getBackStackEntryCount();
260c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
261c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    /**
262c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * Return the BackStackEntry at index <var>index</var> in the back stack;
263c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * entries start index 0 being the bottom of the stack.
264c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     */
265327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public abstract BackStackEntry getBackStackEntryAt(int index);
266c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
267c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    /**
268c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * Add a new listener for changes to the fragment back stack.
269c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     */
270ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
271c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
272c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    /**
273c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * Remove a listener that was previously added with
274c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
275c6669ca63299219d815464129dac051ab2404286Dianne Hackborn     */
276ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
277c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
278c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    /**
279b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Put a reference to a fragment in a Bundle.  This Bundle can be
280b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * persisted as saved state, and when later restoring
281b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * {@link #getFragment(Bundle, String)} will return the current
282b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * instance of the same fragment.
283b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     *
284b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param bundle The bundle in which to put the fragment reference.
285b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param key The name of the entry in the bundle.
286b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param fragment The Fragment whose reference is to be stored.
287b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
288ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
289b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
290b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    /**
291b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * Retrieve the current Fragment instance for a reference previously
292b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * placed with {@link #putFragment(Bundle, String, Fragment)}.
293b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     *
294b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param bundle The bundle from which to retrieve the fragment reference.
295b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @param key The name of the entry in the bundle.
296b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * @return Returns the current Fragment instance that is associated with
297b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     * the given reference.
298b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn     */
299ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public abstract Fragment getFragment(Bundle bundle, String key);
300625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
301625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    /**
302b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * Save the current instance state of the given Fragment.  This can be
303b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * used later when creating a new instance of the Fragment and adding
304b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * it to the fragment manager, to have it create itself to match the
305b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * current state returned here.  Note that there are limits on how
306b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * this can be used:
307b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     *
308b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * <ul>
309b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * <li>The Fragment must currently be attached to the FragmentManager.
310b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * <li>A new Fragment created using this saved state must be the same class
311b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * type as the Fragment it was created from.
312b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * <li>The saved state can not contain dependencies on other fragments --
313b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
314b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * store a fragment reference because that reference may not be valid when
315b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * this saved state is later used.  Likewise the Fragment's target and
316b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * result code are not included in this state.
317b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * </ul>
318b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     *
319b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * @param f The Fragment whose state is to be saved.
320b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * @return The generated state.  This will be null if there was no
321b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     * interesting state created by the fragment.
322b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn     */
323b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn    public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
324b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn
325b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn    /**
3266d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn     * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()}
3276d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn     * call has been made on the FragmentManager's Activity, so this instance is now dead.
3286d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn     */
3296d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn    public abstract boolean isDestroyed();
3306d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn
3316d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn    /**
332625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn     * Print the FragmentManager's state into the given stream.
333625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn     *
334625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn     * @param prefix Text to print at the front of each line.
335625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn     * @param fd The raw file descriptor that the dump is being sent to.
336625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn     * @param writer A PrintWriter to which the dump is to be set.
33730d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn     * @param args Additional arguments to the dump request.
338625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn     */
339ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
340a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn
341a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn    /**
342a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn     * Control whether the framework's internal fragment manager debugging
343a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn     * logs are turned on.  If enabled, you will see output in logcat as
344a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn     * the framework performs fragment operations.
345a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn     */
346a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn    public static void enableDebugLogging(boolean enabled) {
347a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn        FragmentManagerImpl.DEBUG = enabled;
348a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn    }
349f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell
350f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell    /**
351f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell     * Invalidate the attached activity's options menu as necessary.
352f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell     * This may end up being deferred until we move to the resumed state.
353f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell     */
354f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell    public void invalidateOptionsMenu() { }
355b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn}
356b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
357b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackbornfinal class FragmentManagerState implements Parcelable {
3586e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    FragmentState[] mActive;
359b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    int[] mAdded;
360b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    BackStackState[] mBackStack;
361b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
362b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    public FragmentManagerState() {
363b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
364b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
365b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    public FragmentManagerState(Parcel in) {
3666e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        mActive = in.createTypedArray(FragmentState.CREATOR);
367b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        mAdded = in.createIntArray();
368b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        mBackStack = in.createTypedArray(BackStackState.CREATOR);
369b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
370b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
371b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    public int describeContents() {
372b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        return 0;
373b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
374b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
375b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    public void writeToParcel(Parcel dest, int flags) {
3766e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        dest.writeTypedArray(mActive, flags);
377b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        dest.writeIntArray(mAdded);
378b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        dest.writeTypedArray(mBackStack, flags);
379b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
380b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
381b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    public static final Parcelable.Creator<FragmentManagerState> CREATOR
382b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            = new Parcelable.Creator<FragmentManagerState>() {
383b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        public FragmentManagerState createFromParcel(Parcel in) {
384b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            return new FragmentManagerState(in);
385b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
386b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
387b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        public FragmentManagerState[] newArray(int size) {
388b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            return new FragmentManagerState[size];
389b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
390b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    };
391ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn}
392ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn
3932dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn/**
39462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn * Callbacks from FragmentManagerImpl to its container.
39562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn */
39662bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborninterface FragmentContainer {
39762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn    public View findViewById(int id);
39862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn}
39962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn
40062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn/**
4012dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn * Container for fragments associated with an activity.
4022dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn */
403371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powellfinal class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 {
4041c43719c4709702fe3187e9c42ba9f4d22921034Craig Mautner    static boolean DEBUG = false;
4055ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    static final String TAG = "FragmentManager";
4065ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
407def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn    static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
408def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn    static final String TARGET_STATE_TAG = "android:target_state";
409def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn    static final String VIEW_STATE_TAG = "android:view_state";
41078fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell    static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
411def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn
412445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    ArrayList<Runnable> mPendingActions;
413445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    Runnable[] mTmpActions;
414445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    boolean mExecutingActions;
415445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
4166e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    ArrayList<Fragment> mActive;
4176e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    ArrayList<Fragment> mAdded;
4186e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    ArrayList<Integer> mAvailIndices;
419c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    ArrayList<BackStackRecord> mBackStack;
4208eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn    ArrayList<Fragment> mCreatedMenus;
4212dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
422dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    // Must be accessed while locked.
423c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    ArrayList<BackStackRecord> mBackStackIndices;
424dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    ArrayList<Integer> mAvailBackStackIndices;
425dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn
426c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
427c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
4282dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    int mCurState = Fragment.INITIALIZING;
4292dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    Activity mActivity;
43062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn    FragmentContainer mContainer;
43162bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn    Fragment mParent;
4322dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
433b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    boolean mNeedMenuInvalidate;
4343e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn    boolean mStateSaved;
435d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn    boolean mDestroyed;
436fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn    String mNoTransactionsBecause;
43778fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell    boolean mHavePendingDeferredStart;
438371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
439b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    // Temporary vars for state save and restore.
440b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    Bundle mStateBundle = null;
441b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    SparseArray<Parcelable> mStateArray = null;
442b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
443445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    Runnable mExecCommit = new Runnable() {
444445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        @Override
445445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        public void run() {
446445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            execPendingActions();
447445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
448445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    };
449625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
4504702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn    private void throwException(RuntimeException ex) {
4514702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn        Log.e(TAG, ex.getMessage());
4524702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn        LogWriter logw = new LogWriter(Log.ERROR, TAG);
4538c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn        PrintWriter pw = new FastPrintWriter(logw, false, 1024);
4544702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn        if (mActivity != null) {
4554702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn            Log.e(TAG, "Activity state:");
4565bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn            try {
4574702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                mActivity.dump("  ", null, pw, new String[] { });
4585bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn            } catch (Exception e) {
4598c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn                pw.flush();
4605bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn                Log.e(TAG, "Failed dumping state", e);
4615bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn            }
4625bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn        } else {
4634702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn            Log.e(TAG, "Fragment manager state:");
4645bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn            try {
4654702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                dump("  ", null, pw, new String[] { });
4665bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn            } catch (Exception e) {
4678c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn                pw.flush();
4684702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                Log.e(TAG, "Failed dumping state", e);
4695bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn            }
4705bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn        }
4718c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn        pw.flush();
4724702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn        throw ex;
4735bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn    }
4745bf6e1a1dbfffa0732d2cc5dd1d02cf1d2f3b9b4Dianne Hackborn
475625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
47648e7b458694acdf3a4fc58e62437f1dbc4f29d83Dianne Hackborn    public FragmentTransaction beginTransaction() {
477c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return new BackStackRecord(this);
478b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    }
479b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
480625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
4813a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public boolean executePendingTransactions() {
4823a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        return execPendingActions();
4833a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    }
4843a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
4853a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    @Override
4863a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public void popBackStack() {
4873a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        enqueueAction(new Runnable() {
4883a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            @Override public void run() {
4893a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn                popBackStackState(mActivity.mHandler, null, -1, 0);
4903a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            }
4913a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        }, false);
4923a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    }
4933a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
4943a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    @Override
4953a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public boolean popBackStackImmediate() {
4963a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        checkStateLoss();
4973a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        executePendingTransactions();
498b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        return popBackStackState(mActivity.mHandler, null, -1, 0);
499b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    }
500b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
501625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
5023a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public void popBackStack(final String name, final int flags) {
5033a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        enqueueAction(new Runnable() {
5043a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            @Override public void run() {
5053a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn                popBackStackState(mActivity.mHandler, name, -1, flags);
5063a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            }
5073a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        }, false);
5083a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    }
5093a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
5103a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    @Override
5113a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public boolean popBackStackImmediate(String name, int flags) {
5123a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        checkStateLoss();
5133a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        executePendingTransactions();
514b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        return popBackStackState(mActivity.mHandler, name, -1, flags);
515b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    }
516b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
517625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
5183a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public void popBackStack(final int id, final int flags) {
5193a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        if (id < 0) {
5203a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            throw new IllegalArgumentException("Bad id: " + id);
5213a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        }
5223a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        enqueueAction(new Runnable() {
5233a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            @Override public void run() {
5243a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn                popBackStackState(mActivity.mHandler, null, id, flags);
5253a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            }
5263a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        }, false);
5273a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    }
5283a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
5293a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    @Override
5303a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public boolean popBackStackImmediate(int id, int flags) {
5313a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        checkStateLoss();
5323a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        executePendingTransactions();
533b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        if (id < 0) {
534b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn            throw new IllegalArgumentException("Bad id: " + id);
535b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        }
536b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        return popBackStackState(mActivity.mHandler, null, id, flags);
537b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    }
538b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
539625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
540327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public int getBackStackEntryCount() {
541c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return mBackStack != null ? mBackStack.size() : 0;
542c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
543c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
544625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
545327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn    public BackStackEntry getBackStackEntryAt(int index) {
546c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        return mBackStack.get(index);
547c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
548c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
549625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
550c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
551c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        if (mBackStackChangeListeners == null) {
552c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
553c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        }
554c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        mBackStackChangeListeners.add(listener);
555c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
556c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
557625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
558c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
559c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        if (mBackStackChangeListeners != null) {
560c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            mBackStackChangeListeners.remove(listener);
561c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        }
562c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
563c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
564625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
565b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    public void putFragment(Bundle bundle, String key, Fragment fragment) {
566b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        if (fragment.mIndex < 0) {
5674702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn            throwException(new IllegalStateException("Fragment " + fragment
5684702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                    + " is not currently in the FragmentManager"));
569b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        }
570b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        bundle.putInt(key, fragment.mIndex);
571b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    }
572b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
573625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
574b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    public Fragment getFragment(Bundle bundle, String key) {
575def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn        int index = bundle.getInt(key, -1);
576def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn        if (index == -1) {
577def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn            return null;
578def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn        }
579b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        if (index >= mActive.size()) {
5802de5082f437b90f08ba10210a5b9e25d1173bb9cCyril Mottier            throwException(new IllegalStateException("Fragment no longer exists for key "
5814702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                    + key + ": index " + index));
582b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        }
583b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        Fragment f = mActive.get(index);
584b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        if (f == null) {
5852de5082f437b90f08ba10210a5b9e25d1173bb9cCyril Mottier            throwException(new IllegalStateException("Fragment no longer exists for key "
5864702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                    + key + ": index " + index));
587b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        }
588b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn        return f;
589b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn    }
590b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn
591625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    @Override
592b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn    public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
593b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        if (fragment.mIndex < 0) {
5944702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn            throwException(new IllegalStateException("Fragment " + fragment
5954702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                    + " is not currently in the FragmentManager"));
596b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        }
597b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        if (fragment.mState > Fragment.INITIALIZING) {
598b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            Bundle result = saveFragmentBasicState(fragment);
599b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            return result != null ? new Fragment.SavedState(result) : null;
600b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        }
601b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        return null;
602b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn    }
603b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn
604b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn    @Override
6056d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn    public boolean isDestroyed() {
6066d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn        return mDestroyed;
6076d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn    }
6086d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn
6096d9dcbccec126d9b87ab6587e686e28b87e5a04dDianne Hackborn    @Override
610a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn    public String toString() {
611a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn        StringBuilder sb = new StringBuilder(128);
612a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn        sb.append("FragmentManager{");
613a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn        sb.append(Integer.toHexString(System.identityHashCode(this)));
614a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn        sb.append(" in ");
61562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        if (mParent != null) {
61662bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn            DebugUtils.buildShortClassTag(mParent, sb);
61762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        } else {
61862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn            DebugUtils.buildShortClassTag(mActivity, sb);
61962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        }
620a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn        sb.append("}}");
621a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn        return sb.toString();
622a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn    }
623a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn
624a2ea747faaf5fcd437afbaaf4085cfc29e7c16b8Dianne Hackborn    @Override
625625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
626625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn        String innerPrefix = prefix + "    ";
627625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
628d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        int N;
629d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        if (mActive != null) {
630d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            N = mActive.size();
631d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            if (N > 0) {
632d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                writer.print(prefix); writer.print("Active Fragments in ");
633d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        writer.print(Integer.toHexString(System.identityHashCode(this)));
634d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        writer.println(":");
635d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                for (int i=0; i<N; i++) {
636d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    Fragment f = mActive.get(i);
637d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
638d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            writer.print(": "); writer.println(f);
639d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    if (f != null) {
640d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        f.dump(innerPrefix, fd, writer, args);
641d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    }
642d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                }
643625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            }
644625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn        }
645625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
646625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn        if (mAdded != null) {
647625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            N = mAdded.size();
648625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            if (N > 0) {
649625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                writer.print(prefix); writer.println("Added Fragments:");
650625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                for (int i=0; i<N; i++) {
651625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    Fragment f = mAdded.get(i);
652625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
653625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                            writer.print(": "); writer.println(f.toString());
654625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                }
655625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            }
656625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn        }
657625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
658d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        if (mCreatedMenus != null) {
659d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            N = mCreatedMenus.size();
660d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            if (N > 0) {
661d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                writer.print(prefix); writer.println("Fragments Created Menus:");
662d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                for (int i=0; i<N; i++) {
663d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    Fragment f = mCreatedMenus.get(i);
664d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
665d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            writer.print(": "); writer.println(f.toString());
666d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                }
667d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            }
668d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        }
669d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn
670625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn        if (mBackStack != null) {
671625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            N = mBackStack.size();
672625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            if (N > 0) {
673625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                writer.print(prefix); writer.println("Back Stack:");
674625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                for (int i=0; i<N; i++) {
675625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    BackStackRecord bs = mBackStack.get(i);
676625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
677625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                            writer.print(": "); writer.println(bs.toString());
67830d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn                    bs.dump(innerPrefix, fd, writer, args);
679625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                }
680625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            }
681625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn        }
682d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn
683d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        synchronized (this) {
684d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            if (mBackStackIndices != null) {
685d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                N = mBackStackIndices.size();
686d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                if (N > 0) {
687d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    writer.print(prefix); writer.println("Back Stack Indices:");
688d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    for (int i=0; i<N; i++) {
689d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        BackStackRecord bs = mBackStackIndices.get(i);
690d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        writer.print(prefix); writer.print("  #"); writer.print(i);
691d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                writer.print(": "); writer.println(bs);
692d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    }
693d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                }
694d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            }
695d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn
696d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
697d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                writer.print(prefix); writer.print("mAvailBackStackIndices: ");
698d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
699d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            }
700d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        }
701d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn
702d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        if (mPendingActions != null) {
703d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            N = mPendingActions.size();
704d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            if (N > 0) {
705d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                writer.print(prefix); writer.println("Pending Actions:");
706d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                for (int i=0; i<N; i++) {
707d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    Runnable r = mPendingActions.get(i);
708d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    writer.print(prefix); writer.print("  #"); writer.print(i);
709d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            writer.print(": "); writer.println(r);
710d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                }
711d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            }
712d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        }
713d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn
714d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        writer.print(prefix); writer.println("FragmentManager misc state:");
71562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        writer.print(prefix); writer.print("  mActivity="); writer.println(mActivity);
71662bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        writer.print(prefix); writer.print("  mContainer="); writer.println(mContainer);
71762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        if (mParent != null) {
71862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn            writer.print(prefix); writer.print("  mParent="); writer.println(mParent);
71962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        }
720d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        writer.print(prefix); writer.print("  mCurState="); writer.print(mCurState);
721d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                writer.print(" mStateSaved="); writer.print(mStateSaved);
722d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                writer.print(" mDestroyed="); writer.println(mDestroyed);
723d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        if (mNeedMenuInvalidate) {
724d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            writer.print(prefix); writer.print("  mNeedMenuInvalidate=");
725d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    writer.println(mNeedMenuInvalidate);
726d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        }
727d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        if (mNoTransactionsBecause != null) {
728d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            writer.print(prefix); writer.print("  mNoTransactionsBecause=");
729d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    writer.println(mNoTransactionsBecause);
730d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        }
731d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        if (mAvailIndices != null && mAvailIndices.size() > 0) {
732d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            writer.print(prefix); writer.print("  mAvailIndices: ");
733d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                    writer.println(Arrays.toString(mAvailIndices.toArray()));
734d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        }
735625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    }
736625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
737a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    Animator loadAnimator(Fragment fragment, int transit, boolean enter,
738f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            int transitionStyle) {
739a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        Animator animObj = fragment.onCreateAnimator(transit, enter,
7405ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                fragment.mNextAnim);
741f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        if (animObj != null) {
742f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            return animObj;
743f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
744f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
7455ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (fragment.mNextAnim != 0) {
746a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Animator anim = AnimatorInflater.loadAnimator(mActivity, fragment.mNextAnim);
7475ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            if (anim != null) {
7485ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                return anim;
7495ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
7505ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
7515ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
752f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        if (transit == 0) {
753f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            return null;
754f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
755f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
756f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        int styleIndex = transitToStyleIndex(transit, enter);
757f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        if (styleIndex < 0) {
758f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            return null;
759f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
760f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
761f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        if (transitionStyle == 0 && mActivity.getWindow() != null) {
762f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            transitionStyle = mActivity.getWindow().getAttributes().windowAnimations;
763f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
764f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        if (transitionStyle == 0) {
765f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            return null;
766f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
767f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
768f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
769811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                com.android.internal.R.styleable.FragmentAnimation);
770f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        int anim = attrs.getResourceId(styleIndex, 0);
771f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        attrs.recycle();
772f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
773f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        if (anim == 0) {
774f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            return null;
775f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
776f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
777a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return AnimatorInflater.loadAnimator(mActivity, anim);
778f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    }
779f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
780635c60af623c73d2409f5729c0953638b5d6c497Adam Powell    public void performPendingDeferredStart(Fragment f) {
781635c60af623c73d2409f5729c0953638b5d6c497Adam Powell        if (f.mDeferStart) {
78278fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            if (mExecutingActions) {
78378fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                // Wait until we're done executing our pending transactions
78478fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                mHavePendingDeferredStart = true;
78578fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                return;
78678fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            }
787635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            f.mDeferStart = false;
788ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            moveToState(f, mCurState, 0, 0, false);
789635c60af623c73d2409f5729c0953638b5d6c497Adam Powell        }
790635c60af623c73d2409f5729c0953638b5d6c497Adam Powell    }
791635c60af623c73d2409f5729c0953638b5d6c497Adam Powell
792ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn    void moveToState(Fragment f, int newState, int transit, int transitionStyle,
793ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn            boolean keepActive) {
794f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn        if (DEBUG && false) Log.v(TAG, "moveToState: " + f
795f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            + " oldState=" + f.mState + " newState=" + newState
796f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
7971c43719c4709702fe3187e9c42ba9f4d22921034Craig Mautner
7986e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // Fragments that are not currently added will sit in the onCreate() state.
799e181bd9b5e6a3e5382a1ff3c40b9ce2cb43c7971Dianne Hackborn        if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
8006e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            newState = Fragment.CREATED;
8016e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
802f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn        if (f.mRemoving && newState > f.mState) {
803f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn            // While removing a fragment, we can't change it to a higher state.
804f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn            newState = f.mState;
805f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn        }
8062db4e4bd1b5a2b11b07b870660b7f4b3e1f07061Adam Powell        // Defer start if requested; don't allow it to move to STARTED or higher
8072db4e4bd1b5a2b11b07b870660b7f4b3e1f07061Adam Powell        // if it's not already started.
8082db4e4bd1b5a2b11b07b870660b7f4b3e1f07061Adam Powell        if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
809635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            newState = Fragment.STOPPED;
810635c60af623c73d2409f5729c0953638b5d6c497Adam Powell        }
8112dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        if (f.mState < newState) {
812e3a7f628c6d9fef42be24999b3137ebe5c6f3525Dianne Hackborn            // For fragments that are created from a layout, when restoring from
813e3a7f628c6d9fef42be24999b3137ebe5c6f3525Dianne Hackborn            // state we don't want to allow them to be created until they are
814e3a7f628c6d9fef42be24999b3137ebe5c6f3525Dianne Hackborn            // being reloaded from the layout.
815e3a7f628c6d9fef42be24999b3137ebe5c6f3525Dianne Hackborn            if (f.mFromLayout && !f.mInLayout) {
816e3a7f628c6d9fef42be24999b3137ebe5c6f3525Dianne Hackborn                return;
817e3a7f628c6d9fef42be24999b3137ebe5c6f3525Dianne Hackborn            }
818d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            if (f.mAnimatingAway != null) {
819d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                // The fragment is currently being animated...  but!  Now we
820d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                // want to move our state back up.  Give up on waiting for the
821d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                // animation, move to whatever the final state should be once
822d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                // the animation is done, and then we can proceed from there.
823d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                f.mAnimatingAway = null;
824ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                moveToState(f, f.mStateAfterAnimating, 0, 0, true);
825d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn            }
8262dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn            switch (f.mState) {
8272dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                case Fragment.INITIALIZING:
8285ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                    if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
829def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                    if (f.mSavedFragmentState != null) {
830def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                        f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
831def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                                FragmentManagerImpl.VIEW_STATE_TAG);
832def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                        f.mTarget = getFragment(f.mSavedFragmentState,
833def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                                FragmentManagerImpl.TARGET_STATE_TAG);
834def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                        if (f.mTarget != null) {
835def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                            f.mTargetRequestCode = f.mSavedFragmentState.getInt(
836def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
837def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                        }
83878fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                        f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
83978fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                                FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
84078fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                        if (!f.mUserVisibleHint) {
84178fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                            f.mDeferStart = true;
84278fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                            if (newState > Fragment.STOPPED) {
84378fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                                newState = Fragment.STOPPED;
84478fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                            }
84578fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                        }
846def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                    }
8472dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    f.mActivity = mActivity;
84862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    f.mParentFragment = mParent;
84962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    f.mFragmentManager = mParent != null
85062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                            ? mParent.mChildFragmentManager : mActivity.mFragments;
8512dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    f.mCalled = false;
8522dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    f.onAttach(mActivity);
8532dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    if (!f.mCalled) {
8542dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                        throw new SuperNotCalledException("Fragment " + f
8552dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                                + " did not call through to super.onAttach()");
8562dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
8571b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn                    if (f.mParentFragment == null) {
8581b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn                        mActivity.onAttachFragment(f);
8591b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn                    }
86062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn
861b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    if (!f.mRetaining) {
86262bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        f.performCreate(f.mSavedFragmentState);
8632dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
864b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    f.mRetaining = false;
865b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    if (f.mFromLayout) {
866b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        // For fragments that are part of the content view
867b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        // layout, we need to instantiate the view immediately
868b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        // and the inflater will take care of adding it.
8691b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn                        f.mView = f.performCreateView(f.getLayoutInflater(
8701b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn                                f.mSavedFragmentState), null, f.mSavedFragmentState);
8716e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                        if (f.mView != null) {
8726e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                            f.mView.setSaveFromParentEnabled(false);
87316f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                            if (f.mHidden) f.mView.setVisibility(View.GONE);
87416f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                            f.onViewCreated(f.mView, f.mSavedFragmentState);
8756e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                        }
876b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    }
8776e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                case Fragment.CREATED:
8786e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                    if (newState > Fragment.CREATED) {
87916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                        if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
880b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        if (!f.mFromLayout) {
881b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            ViewGroup container = null;
882b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            if (f.mContainerId != 0) {
88362bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                                container = (ViewGroup)mContainer.findViewById(f.mContainerId);
884352cc98b4621b76959af8f8a698fd75c6d6132e8Dianne Hackborn                                if (container == null && !f.mRestored) {
8854702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                    throwException(new IllegalArgumentException(
8864702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                            "No view found for id 0x"
8874702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                            + Integer.toHexString(f.mContainerId) + " ("
8884702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                            + f.getResources().getResourceName(f.mContainerId)
8894702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                            + ") for fragment " + f));
890b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                }
891b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            }
892b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            f.mContainer = container;
8931b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn                            f.mView = f.performCreateView(f.getLayoutInflater(
8941b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn                                    f.mSavedFragmentState), container, f.mSavedFragmentState);
895b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            if (f.mView != null) {
896b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                f.mView.setSaveFromParentEnabled(false);
897b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                if (container != null) {
898a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                                    Animator anim = loadAnimator(f, transit, true,
899b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                            transitionStyle);
900b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                    if (anim != null) {
901b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                        anim.setTarget(f.mView);
902811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                                        anim.start();
903b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                    }
904b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                    container.addView(f.mView);
905b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                }
90616f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                                if (f.mHidden) f.mView.setVisibility(View.GONE);
90716f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                                f.onViewCreated(f.mView, f.mSavedFragmentState);
908b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            }
909b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        }
9101b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn
91162bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        f.performActivityCreated(f.mSavedFragmentState);
91216f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                        if (f.mView != null) {
91362bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                            f.restoreViewState(f.mSavedFragmentState);
91416f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                        }
915b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        f.mSavedFragmentState = null;
9162dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
917c801768e4d29667a2608695449ebc2833ba0f200Dianne Hackborn                case Fragment.ACTIVITY_CREATED:
91816f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                case Fragment.STOPPED:
91916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                    if (newState > Fragment.STOPPED) {
9205ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
921afc4b283fdaedec9bf32492a019b43cc33edc9b6Dianne Hackborn                        f.performStart();
9222dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
9232dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                case Fragment.STARTED:
9242dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    if (newState > Fragment.STARTED) {
9255ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
9262707d6026240bcca6f0e35e2e1138958882e90ceDianne Hackborn                        f.mResumed = true;
92762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        f.performResume();
9289520251003cc3a321028bb80693749e5047eb3f8Adam Powell                        // Get rid of this in case we saved it and never needed it.
9299520251003cc3a321028bb80693749e5047eb3f8Adam Powell                        f.mSavedFragmentState = null;
9309520251003cc3a321028bb80693749e5047eb3f8Adam Powell                        f.mSavedViewState = null;
9312dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
9322dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn            }
9332dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        } else if (f.mState > newState) {
9342dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn            switch (f.mState) {
9352dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                case Fragment.RESUMED:
9362dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    if (newState < Fragment.RESUMED) {
9375ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
93862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        f.performPause();
9392707d6026240bcca6f0e35e2e1138958882e90ceDianne Hackborn                        f.mResumed = false;
9402dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
9412dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                case Fragment.STARTED:
9422dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    if (newState < Fragment.STARTED) {
9435ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
9442707d6026240bcca6f0e35e2e1138958882e90ceDianne Hackborn                        f.performStop();
9452dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
94616f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                case Fragment.STOPPED:
947c801768e4d29667a2608695449ebc2833ba0f200Dianne Hackborn                case Fragment.ACTIVITY_CREATED:
948c801768e4d29667a2608695449ebc2833ba0f200Dianne Hackborn                    if (newState < Fragment.ACTIVITY_CREATED) {
94916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                        if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
950b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        if (f.mView != null) {
951b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            // Need to save the current view state if not
952b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            // done already.
953625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                            if (!mActivity.isFinishing() && f.mSavedViewState == null) {
954b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                saveFragmentViewState(f);
955b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            }
956def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                        }
957afc4b283fdaedec9bf32492a019b43cc33edc9b6Dianne Hackborn                        f.performDestroyView();
958def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                        if (f.mView != null && f.mContainer != null) {
959b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                            Animator anim = null;
960d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
961b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                anim = loadAnimator(f, transit, false,
962def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                                        transitionStyle);
963b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                            }
964b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                            if (anim != null) {
965b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                final ViewGroup container = f.mContainer;
966b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                final View view = f.mView;
967d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                final Fragment fragment = f;
968b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                container.startViewTransition(view);
969d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                f.mAnimatingAway = anim;
970d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                f.mStateAfterAnimating = newState;
971b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                anim.addListener(new AnimatorListenerAdapter() {
972b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                    @Override
973b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                    public void onAnimationEnd(Animator anim) {
974b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                        container.endViewTransition(view);
975d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                        if (fragment.mAnimatingAway != null) {
976d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                            fragment.mAnimatingAway = null;
977d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                            moveToState(fragment, fragment.mStateAfterAnimating,
978ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                                                    0, 0, false);
979d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                        }
980b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                    }
981b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                });
982b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                anim.setTarget(f.mView);
983b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                                anim.start();
984b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase
985f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                            }
986def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                            f.mContainer.removeView(f.mView);
9872dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                        }
9882dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                        f.mContainer = null;
9892dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                        f.mView = null;
990b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    }
991b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                case Fragment.CREATED:
992b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    if (newState < Fragment.CREATED) {
993d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        if (mDestroyed) {
994d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            if (f.mAnimatingAway != null) {
995d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                // The fragment's containing activity is
996d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                // being destroyed, but this fragment is
997d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                // currently animating away.  Stop the
998d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                // animation right now -- it is not needed,
999d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                // and we can't wait any more on destroying
1000d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                // the fragment.
10011b39e221e2d2db98f8aea7dd7b7ded3eca6f3d55Dianne Hackborn                                Animator anim = f.mAnimatingAway;
1002d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                f.mAnimatingAway = null;
10031b39e221e2d2db98f8aea7dd7b7ded3eca6f3d55Dianne Hackborn                                anim.cancel();
1004d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            }
1005d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        }
1006d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        if (f.mAnimatingAway != null) {
1007d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            // We are waiting for the fragment's view to finish
1008d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            // animating away.  Just make a note of the state
1009d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            // the fragment now should move to once the animation
1010d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            // is done.
1011d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            f.mStateAfterAnimating = newState;
1012f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                            newState = Fragment.CREATED;
1013d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                        } else {
1014d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1015d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            if (!f.mRetaining) {
101662bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                                f.performDestroy();
1017d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            }
1018d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn
1019b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            f.mCalled = false;
1020d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                            f.onDetach();
1021b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            if (!f.mCalled) {
1022b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                                throw new SuperNotCalledException("Fragment " + f
1023d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn                                        + " did not call through to super.onDetach()");
1024b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                            }
1025ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                            if (!keepActive) {
1026ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                                if (!f.mRetaining) {
1027ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                                    makeInactive(f);
1028ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                                } else {
1029ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                                    f.mActivity = null;
103062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                                    f.mParentFragment = null;
1031ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                                    f.mFragmentManager = null;
10327071786e0074bea42b01236f817383cab69c1b63Tim Kilbourn                                    f.mChildFragmentManager = null;
1033ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                                }
1034f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                            }
10352dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                        }
10362dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn                    }
10372dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn            }
10382dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        }
10392dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
10402dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        f.mState = newState;
10412dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
10422dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
1043625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    void moveToState(Fragment f) {
1044ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn        moveToState(f, mCurState, 0, 0, false);
1045625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn    }
1046625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
1047ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn    void moveToState(int newState, boolean always) {
1048f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        moveToState(newState, 0, 0, always);
1049f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    }
1050f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
1051f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    void moveToState(int newState, int transit, int transitStyle, boolean always) {
10522dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        if (mActivity == null && newState != Fragment.INITIALIZING) {
10532dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn            throw new IllegalStateException("No activity");
10542dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        }
105562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn
1056ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        if (!always && mCurState == newState) {
1057ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn            return;
1058ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        }
105962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn
10602dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        mCurState = newState;
10616e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mActive != null) {
1062635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            boolean loadersRunning = false;
10636e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            for (int i=0; i<mActive.size(); i++) {
10646e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                Fragment f = mActive.get(i);
10656e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                if (f != null) {
1066ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                    moveToState(f, newState, transit, transitStyle, false);
1067635c60af623c73d2409f5729c0953638b5d6c497Adam Powell                    if (f.mLoaderManager != null) {
1068635c60af623c73d2409f5729c0953638b5d6c497Adam Powell                        loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1069635c60af623c73d2409f5729c0953638b5d6c497Adam Powell                    }
10706e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                }
10716e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            }
10725f36c96f6d3623a826466db68cc1586f078ac307Dianne Hackborn
1073635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            if (!loadersRunning) {
1074635c60af623c73d2409f5729c0953638b5d6c497Adam Powell                startPendingDeferredFragments();
1075635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            }
1076635c60af623c73d2409f5729c0953638b5d6c497Adam Powell
107789b09da7b3b1e69264d9ec710c66eb2f891b313eAdam Powell            if (mNeedMenuInvalidate && mActivity != null && mCurState == Fragment.RESUMED) {
10785f36c96f6d3623a826466db68cc1586f078ac307Dianne Hackborn                mActivity.invalidateOptionsMenu();
10795f36c96f6d3623a826466db68cc1586f078ac307Dianne Hackborn                mNeedMenuInvalidate = false;
10805f36c96f6d3623a826466db68cc1586f078ac307Dianne Hackborn            }
10816e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
10826e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    }
10836e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
1084635c60af623c73d2409f5729c0953638b5d6c497Adam Powell    void startPendingDeferredFragments() {
108537510908a7b570accb2c4829842790b3d9d3a102Adam Powell        if (mActive == null) return;
108637510908a7b570accb2c4829842790b3d9d3a102Adam Powell
1087635c60af623c73d2409f5729c0953638b5d6c497Adam Powell        for (int i=0; i<mActive.size(); i++) {
1088635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            Fragment f = mActive.get(i);
1089635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            if (f != null) {
1090635c60af623c73d2409f5729c0953638b5d6c497Adam Powell                performPendingDeferredStart(f);
1091635c60af623c73d2409f5729c0953638b5d6c497Adam Powell            }
1092635c60af623c73d2409f5729c0953638b5d6c497Adam Powell        }
1093635c60af623c73d2409f5729c0953638b5d6c497Adam Powell    }
1094635c60af623c73d2409f5729c0953638b5d6c497Adam Powell
10956e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    void makeActive(Fragment f) {
10966e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (f.mIndex >= 0) {
10976e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            return;
10986e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
10996e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
11006e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mAvailIndices == null || mAvailIndices.size() <= 0) {
11016e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            if (mActive == null) {
11026e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                mActive = new ArrayList<Fragment>();
1103ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn            }
110462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn            f.setIndex(mActive.size(), mParent);
11056e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mActive.add(f);
11066e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
11076e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        } else {
110862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn            f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent);
11096e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mActive.set(f.mIndex, f);
11102dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        }
111103fcc333cf0fbbc4df1215649746d6360801efd8Dianne Hackborn        if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
11122dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
11132dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
11146e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    void makeInactive(Fragment f) {
11156e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (f.mIndex < 0) {
11166e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            return;
11176e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
11186e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
111903fcc333cf0fbbc4df1215649746d6360801efd8Dianne Hackborn        if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
11206e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        mActive.set(f.mIndex, null);
11216e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mAvailIndices == null) {
11226e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mAvailIndices = new ArrayList<Integer>();
11236e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
11246e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        mAvailIndices.add(f.mIndex);
112562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        mActivity.invalidateFragment(f.mWho);
1126afc4b283fdaedec9bf32492a019b43cc33edc9b6Dianne Hackborn        f.initState();
11276e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    }
11286e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
1129ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn    public void addFragment(Fragment fragment, boolean moveToStateNow) {
11306e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mAdded == null) {
11316e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mAdded = new ArrayList<Fragment>();
1132ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        }
11335e0d59547c4042037e23a42ba4e2521721e9c7d3Dianne Hackborn        if (DEBUG) Log.v(TAG, "add: " + fragment);
113416f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        makeActive(fragment);
113516f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        if (!fragment.mDetached) {
1136f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            if (mAdded.contains(fragment)) {
1137f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                throw new IllegalStateException("Fragment already added: " + fragment);
1138f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            }
113916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            mAdded.add(fragment);
114016f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            fragment.mAdded = true;
114116f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            fragment.mRemoving = false;
11426c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn            if (fragment.mHasMenu && fragment.mMenuVisible) {
114316f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                mNeedMenuInvalidate = true;
114416f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            }
114516f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            if (moveToStateNow) {
114616f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                moveToState(fragment);
114716f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            }
1148ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        }
11492dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
11502dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
1151f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
11525e0d59547c4042037e23a42ba4e2521721e9c7d3Dianne Hackborn        if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
115316f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        final boolean inactive = !fragment.isInBackStack();
115416f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        if (!fragment.mDetached || inactive) {
1155f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            if (false) {
1156f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                // Would be nice to catch a bad remove here, but we need
1157f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                // time to test this to make sure we aren't crashes cases
1158f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                // where it is not a problem.
1159f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (!mAdded.contains(fragment)) {
1160f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    throw new IllegalStateException("Fragment not added: " + fragment);
1161f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                }
1162f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn            }
1163acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn            if (mAdded != null) {
1164acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn                mAdded.remove(fragment);
1165acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn            }
11666c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn            if (fragment.mHasMenu && fragment.mMenuVisible) {
116716f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                mNeedMenuInvalidate = true;
116816f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            }
116916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            fragment.mAdded = false;
117016f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            fragment.mRemoving = true;
117116f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED,
1172ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                    transition, transitionStyle, false);
11735e0d59547c4042037e23a42ba4e2521721e9c7d3Dianne Hackborn        }
1174f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    }
1175f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
11765ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
11775ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (DEBUG) Log.v(TAG, "hide: " + fragment);
11785ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (!fragment.mHidden) {
11795ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            fragment.mHidden = true;
11805ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            if (fragment.mView != null) {
11812756293910032a2e7dfe007182b6b22a0fb9459eAdam Powell                Animator anim = loadAnimator(fragment, transition, false,
11825ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        transitionStyle);
11835ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                if (anim != null) {
1184b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                    anim.setTarget(fragment.mView);
118561eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                    // Delay the actual hide operation until the animation finishes, otherwise
118661eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                    // the fragment will just immediately disappear
118761eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                    final Fragment finalFragment = fragment;
118861eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                    anim.addListener(new AnimatorListenerAdapter() {
118961eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                        @Override
119061eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                        public void onAnimationEnd(Animator animation) {
1191b29407faf39a1421ac61958a34431286c8397929Chet Haase                            if (finalFragment.mView != null) {
1192b29407faf39a1421ac61958a34431286c8397929Chet Haase                                finalFragment.mView.setVisibility(View.GONE);
1193b29407faf39a1421ac61958a34431286c8397929Chet Haase                            }
119461eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                        }
119561eb40d276d7fe5c4bead019b7565b99ead07731Chet Haase                    });
1196811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                    anim.start();
1197e646b28cb3bdd10e46c209e367b8ee055ff5a4baChet Haase                } else {
1198e646b28cb3bdd10e46c209e367b8ee055ff5a4baChet Haase                    fragment.mView.setVisibility(View.GONE);
11995ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                }
12005ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
12016c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn            if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1202b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                mNeedMenuInvalidate = true;
1203b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            }
12045ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            fragment.onHiddenChanged(true);
12055ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
12065ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
12075ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
12085ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    public void showFragment(Fragment fragment, int transition, int transitionStyle) {
12095ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (DEBUG) Log.v(TAG, "show: " + fragment);
12105ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        if (fragment.mHidden) {
12115ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            fragment.mHidden = false;
12125ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            if (fragment.mView != null) {
1213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                Animator anim = loadAnimator(fragment, transition, true,
12145ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                        transitionStyle);
12155ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                if (anim != null) {
1216b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                    anim.setTarget(fragment.mView);
1217811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                    anim.start();
12185ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                }
12195ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                fragment.mView.setVisibility(View.VISIBLE);
12205ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
12216c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn            if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1222b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                mNeedMenuInvalidate = true;
1223b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            }
12245ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            fragment.onHiddenChanged(false);
12255ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn        }
12265ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn    }
12275ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn
122816f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn    public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
122916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        if (DEBUG) Log.v(TAG, "detach: " + fragment);
123016f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        if (!fragment.mDetached) {
123116f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            fragment.mDetached = true;
123216f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            if (fragment.mAdded) {
123316f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                // We are not already in back stack, so need to remove the fragment.
1234acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn                if (mAdded != null) {
1235f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
1236acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn                    mAdded.remove(fragment);
1237acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn                }
12386c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn                if (fragment.mHasMenu && fragment.mMenuVisible) {
123916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                    mNeedMenuInvalidate = true;
124016f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                }
124116f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                fragment.mAdded = false;
1242ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false);
124316f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            }
124416f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        }
124516f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn    }
124616f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn
124716f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn    public void attachFragment(Fragment fragment, int transition, int transitionStyle) {
124816f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        if (DEBUG) Log.v(TAG, "attach: " + fragment);
124916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        if (fragment.mDetached) {
125016f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            fragment.mDetached = false;
125116f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            if (!fragment.mAdded) {
1252acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn                if (mAdded == null) {
1253acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn                    mAdded = new ArrayList<Fragment>();
1254acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn                }
1255f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (mAdded.contains(fragment)) {
1256f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    throw new IllegalStateException("Fragment already added: " + fragment);
1257f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                }
1258f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
125916f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                mAdded.add(fragment);
126016f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                fragment.mAdded = true;
12616c285977a0dc9605348bd9530282c7d006cbf8bdDianne Hackborn                if (fragment.mHasMenu && fragment.mMenuVisible) {
126216f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                    mNeedMenuInvalidate = true;
126316f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn                }
1264ee76efb74b5886f98cdfebfbefe9b69224e016fbDianne Hackborn                moveToState(fragment, mCurState, transition, transitionStyle, false);
126516f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn            }
126616f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        }
126716f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn    }
126816f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn
1269f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    public Fragment findFragmentById(int id) {
1270acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
12715ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            // First look through added fragments.
12725ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            for (int i=mAdded.size()-1; i>=0; i--) {
12735ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                Fragment f = mAdded.get(i);
12745ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                if (f != null && f.mFragmentId == id) {
12755ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                    return f;
12765ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                }
12775ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
1278acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        }
1279acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mActive != null) {
12805ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            // Now for any known fragment.
12816e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            for (int i=mActive.size()-1; i>=0; i--) {
12826e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                Fragment f = mActive.get(i);
12836e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                if (f != null && f.mFragmentId == id) {
1284b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    return f;
1285b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                }
1286b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
1287b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1288b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        return null;
1289b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
1290b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
1291b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    public Fragment findFragmentByTag(String tag) {
1292acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null && tag != null) {
12935ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            // First look through added fragments.
12945ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            for (int i=mAdded.size()-1; i>=0; i--) {
12955ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                Fragment f = mAdded.get(i);
12965ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                if (f != null && tag.equals(f.mTag)) {
12975ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                    return f;
12985ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn                }
12995ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            }
1300acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        }
1301acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mActive != null && tag != null) {
13025ae74d6e89a30e79ea85c487b32223ef55314985Dianne Hackborn            // Now for any known fragment.
13036e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            for (int i=mActive.size()-1; i>=0; i--) {
13046e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                Fragment f = mActive.get(i);
13056e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                if (f != null && tag.equals(f.mTag)) {
13066e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                    return f;
13076e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                }
13086e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            }
13096e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
13106e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        return null;
13116e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    }
13126e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
13136e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    public Fragment findFragmentByWho(String who) {
13146e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mActive != null && who != null) {
13156e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            for (int i=mActive.size()-1; i>=0; i--) {
13166e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                Fragment f = mActive.get(i);
131762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                if (f != null && (f=f.findFragmentByWho(who)) != null) {
1318f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                    return f;
1319f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                }
1320f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            }
1321f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
1322f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        return null;
13232dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
13242dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
13253a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    private void checkStateLoss() {
13263a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        if (mStateSaved) {
13273a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            throw new IllegalStateException(
13283a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn                    "Can not perform this action after onSaveInstanceState");
13293a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        }
13303a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        if (mNoTransactionsBecause != null) {
13313a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            throw new IllegalStateException(
13323a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn                    "Can not perform this action inside of " + mNoTransactionsBecause);
13333a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        }
13343a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    }
13353a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
133695a4609b90abe02474c815c216780a1383f13cb2Alan Viverette    /**
133795a4609b90abe02474c815c216780a1383f13cb2Alan Viverette     * Adds an action to the queue of pending actions.
133895a4609b90abe02474c815c216780a1383f13cb2Alan Viverette     *
133995a4609b90abe02474c815c216780a1383f13cb2Alan Viverette     * @param action the action to add
134095a4609b90abe02474c815c216780a1383f13cb2Alan Viverette     * @param allowStateLoss whether to allow loss of state information
134195a4609b90abe02474c815c216780a1383f13cb2Alan Viverette     * @throws IllegalStateException if the activity has been destroyed
134295a4609b90abe02474c815c216780a1383f13cb2Alan Viverette     */
1343ab36acb39941ce981dddda9f9cf4d2d23a56fd26Dianne Hackborn    public void enqueueAction(Runnable action, boolean allowStateLoss) {
13446908cd154c5a2ed2e3b21d40f51952d45be69184Dianne Hackborn        if (!allowStateLoss) {
13453a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            checkStateLoss();
1346fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn        }
1347445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        synchronized (this) {
134895a4609b90abe02474c815c216780a1383f13cb2Alan Viverette            if (mDestroyed || mActivity == null) {
13496908cd154c5a2ed2e3b21d40f51952d45be69184Dianne Hackborn                throw new IllegalStateException("Activity has been destroyed");
13506908cd154c5a2ed2e3b21d40f51952d45be69184Dianne Hackborn            }
1351445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (mPendingActions == null) {
1352445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mPendingActions = new ArrayList<Runnable>();
1353445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
1354445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mPendingActions.add(action);
1355445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            if (mPendingActions.size() == 1) {
1356445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mActivity.mHandler.removeCallbacks(mExecCommit);
1357445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mActivity.mHandler.post(mExecCommit);
1358445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
1359445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
1360445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    }
1361445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
1362c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public int allocBackStackIndex(BackStackRecord bse) {
1363dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        synchronized (this) {
1364dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1365dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                if (mBackStackIndices == null) {
1366c6669ca63299219d815464129dac051ab2404286Dianne Hackborn                    mBackStackIndices = new ArrayList<BackStackRecord>();
1367dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                }
1368dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                int index = mBackStackIndices.size();
1369dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1370dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                mBackStackIndices.add(bse);
1371dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                return index;
1372dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn
1373dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            } else {
1374dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1375dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1376dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                mBackStackIndices.set(index, bse);
1377dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                return index;
1378dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            }
1379dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        }
1380dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    }
1381dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn
1382c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    public void setBackStackIndex(int index, BackStackRecord bse) {
1383dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        synchronized (this) {
1384dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            if (mBackStackIndices == null) {
1385c6669ca63299219d815464129dac051ab2404286Dianne Hackborn                mBackStackIndices = new ArrayList<BackStackRecord>();
1386dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            }
1387dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            int N = mBackStackIndices.size();
1388dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            if (index < N) {
1389dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1390dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                mBackStackIndices.set(index, bse);
1391dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            } else {
1392dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                while (N < index) {
1393dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                    mBackStackIndices.add(null);
1394dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                    if (mAvailBackStackIndices == null) {
1395dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                        mAvailBackStackIndices = new ArrayList<Integer>();
1396dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                    }
1397dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                    if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1398dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                    mAvailBackStackIndices.add(N);
1399dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                    N++;
1400dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                }
1401dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1402dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                mBackStackIndices.add(bse);
1403dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            }
1404dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        }
1405dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    }
1406dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn
1407dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    public void freeBackStackIndex(int index) {
1408dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        synchronized (this) {
1409dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            mBackStackIndices.set(index, null);
1410dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            if (mAvailBackStackIndices == null) {
1411dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                mAvailBackStackIndices = new ArrayList<Integer>();
1412dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            }
1413dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1414dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            mAvailBackStackIndices.add(index);
1415dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn        }
1416dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    }
1417dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn
1418445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    /**
1419445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     * Only call from main thread!
1420445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn     */
14213a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn    public boolean execPendingActions() {
1422445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        if (mExecutingActions) {
14233a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            throw new IllegalStateException("Recursive entry to executePendingTransactions");
1424445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
1425445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
1426d9b3b7e8e1d8c919c3e5f5851daa80a2651ea7d1Dianne Hackborn        if (Looper.myLooper() != mActivity.mHandler.getLooper()) {
14273a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            throw new IllegalStateException("Must be called from main thread of process");
14283a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        }
14293a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
14303a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        boolean didSomething = false;
14313a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
1432445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        while (true) {
1433445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            int numActions;
1434445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
1435445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            synchronized (this) {
1436445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                if (mPendingActions == null || mPendingActions.size() == 0) {
143778fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                    break;
1438445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                }
1439445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
1440445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                numActions = mPendingActions.size();
1441445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                if (mTmpActions == null || mTmpActions.length < numActions) {
1442445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                    mTmpActions = new Runnable[numActions];
1443445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                }
1444445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mPendingActions.toArray(mTmpActions);
1445445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mPendingActions.clear();
1446445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mActivity.mHandler.removeCallbacks(mExecCommit);
1447445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
1448445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn
1449445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mExecutingActions = true;
1450445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            for (int i=0; i<numActions; i++) {
1451445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn                mTmpActions[i].run();
14520d32528be3b708e8a7ebb4e8d7fd2a2c94d6638eJeff Sharkey                mTmpActions[i] = null;
1453445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            }
1454445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn            mExecutingActions = false;
14553a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            didSomething = true;
1456445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn        }
145778fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell
145878fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell        if (mHavePendingDeferredStart) {
145978fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            boolean loadersRunning = false;
146078fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            for (int i=0; i<mActive.size(); i++) {
146178fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                Fragment f = mActive.get(i);
146278fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                if (f != null && f.mLoaderManager != null) {
146378fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                    loadersRunning |= f.mLoaderManager.hasRunningLoaders();
146478fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                }
146578fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            }
146678fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            if (!loadersRunning) {
146778fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                mHavePendingDeferredStart = false;
146878fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell                startPendingDeferredFragments();
146978fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            }
147078fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell        }
147178fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell        return didSomething;
1472445646c52128a763b56ed7bb3bd009e2f33e3e4fDianne Hackborn    }
147378fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell
1474c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    void reportBackStackChanged() {
1475c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        if (mBackStackChangeListeners != null) {
1476c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            for (int i=0; i<mBackStackChangeListeners.size(); i++) {
1477c6669ca63299219d815464129dac051ab2404286Dianne Hackborn                mBackStackChangeListeners.get(i).onBackStackChanged();
1478c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            }
1479c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        }
1480c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    }
1481c6669ca63299219d815464129dac051ab2404286Dianne Hackborn
1482c6669ca63299219d815464129dac051ab2404286Dianne Hackborn    void addBackStackState(BackStackRecord state) {
1483ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        if (mBackStack == null) {
1484c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            mBackStack = new ArrayList<BackStackRecord>();
1485ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        }
1486ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        mBackStack.add(state);
1487c6669ca63299219d815464129dac051ab2404286Dianne Hackborn        reportBackStackChanged();
1488ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn    }
1489ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn
1490dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn    boolean popBackStackState(Handler handler, String name, int id, int flags) {
1491ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        if (mBackStack == null) {
1492ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn            return false;
1493ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        }
14943a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) {
1495f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            int last = mBackStack.size()-1;
1496f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            if (last < 0) {
1497f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                return false;
1498f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            }
1499c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            final BackStackRecord bss = mBackStack.remove(last);
1500c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
1501c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
1502c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            bss.calculateBackFragments(firstOutFragments, lastInFragments);
1503c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            bss.popFromBackStack(true, null, firstOutFragments, lastInFragments);
15043a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            reportBackStackChanged();
1505f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        } else {
1506b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn            int index = -1;
1507b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn            if (name != null || id >= 0) {
1508b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                // If a name or ID is specified, look for that place in
1509b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                // the stack.
1510b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                index = mBackStack.size()-1;
1511b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                while (index >= 0) {
1512c6669ca63299219d815464129dac051ab2404286Dianne Hackborn                    BackStackRecord bss = mBackStack.get(index);
1513b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    if (name != null && name.equals(bss.getName())) {
1514b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                        break;
1515b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    }
1516b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    if (id >= 0 && id == bss.mIndex) {
1517b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                        break;
1518b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    }
1519b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    index--;
1520dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                }
1521b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                if (index < 0) {
1522b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    return false;
1523b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                }
15243a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn                if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
1525b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    index--;
1526b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    // Consume all following entries that match.
1527b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    while (index >= 0) {
1528c6669ca63299219d815464129dac051ab2404286Dianne Hackborn                        BackStackRecord bss = mBackStack.get(index);
1529b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                        if ((name != null && name.equals(bss.getName()))
1530b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                                || (id >= 0 && id == bss.mIndex)) {
1531b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                            index--;
1532b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                            continue;
1533b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                        }
1534b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                        break;
1535b3cf10ffa8ff9cac0da8b23a0d84076b3f501400Dianne Hackborn                    }
1536f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                }
1537dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            }
1538dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn            if (index == mBackStack.size()-1) {
1539f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                return false;
1540f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            }
1541c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            final ArrayList<BackStackRecord> states
1542c6669ca63299219d815464129dac051ab2404286Dianne Hackborn                    = new ArrayList<BackStackRecord>();
1543f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn            for (int i=mBackStack.size()-1; i>index; i--) {
1544f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                states.add(mBackStack.remove(i));
1545ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn            }
15463a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            final int LAST = states.size()-1;
1547c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
1548c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
1549c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            for (int i=0; i<=LAST; i++) {
1550c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount                states.get(i).calculateBackFragments(firstOutFragments, lastInFragments);
1551c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount            }
1552d4c3c91dd0757eec9703ef90ea4c5a7ee99f18caGeorge Mount            BackStackRecord.TransitionState state = null;
15533a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            for (int i=0; i<=LAST; i++) {
15543a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn                if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i));
1555c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount                state = states.get(i).popFromBackStack(i == LAST, state,
1556c03da0e7a9ef721709d51cf8a2d539a5bd8a320eGeorge Mount                        firstOutFragments, lastInFragments);
15573a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            }
15583a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn            reportBackStackChanged();
1559f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
1560ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        return true;
1561ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn    }
1562ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn
1563b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    ArrayList<Fragment> retainNonConfig() {
1564b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        ArrayList<Fragment> fragments = null;
15656e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mActive != null) {
15666e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            for (int i=0; i<mActive.size(); i++) {
15676e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                Fragment f = mActive.get(i);
15686e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                if (f != null && f.mRetainInstance) {
1569b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    if (fragments == null) {
1570b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                        fragments = new ArrayList<Fragment>();
1571b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    }
1572b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    fragments.add(f);
1573b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    f.mRetaining = true;
1574f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                    f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
157503fcc333cf0fbbc4df1215649746d6360801efd8Dianne Hackborn                    if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
1576b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                }
1577b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
1578b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1579b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        return fragments;
1580b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
1581b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
1582b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    void saveFragmentViewState(Fragment f) {
15836e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (f.mView == null) {
1584b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            return;
1585b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1586b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        if (mStateArray == null) {
1587b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            mStateArray = new SparseArray<Parcelable>();
1588b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        } else {
1589b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            mStateArray.clear();
1590b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1591b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        f.mView.saveHierarchyState(mStateArray);
1592b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        if (mStateArray.size() > 0) {
1593b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            f.mSavedViewState = mStateArray;
1594b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            mStateArray = null;
1595b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1596b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
1597b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
1598b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn    Bundle saveFragmentBasicState(Fragment f) {
1599b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        Bundle result = null;
1600b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn
1601b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        if (mStateBundle == null) {
1602b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            mStateBundle = new Bundle();
1603b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        }
160462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        f.performSaveInstanceState(mStateBundle);
1605b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        if (!mStateBundle.isEmpty()) {
1606b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            result = mStateBundle;
1607b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            mStateBundle = null;
1608b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        }
1609b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn
1610b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        if (f.mView != null) {
1611b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            saveFragmentViewState(f);
16121333276c7af53bca06ad166f129d39fc756930dfDianne Hackborn        }
16131333276c7af53bca06ad166f129d39fc756930dfDianne Hackborn        if (f.mSavedViewState != null) {
16141333276c7af53bca06ad166f129d39fc756930dfDianne Hackborn            if (result == null) {
16151333276c7af53bca06ad166f129d39fc756930dfDianne Hackborn                result = new Bundle();
1616b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn            }
16171333276c7af53bca06ad166f129d39fc756930dfDianne Hackborn            result.putSparseParcelableArray(
16181333276c7af53bca06ad166f129d39fc756930dfDianne Hackborn                    FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
1619b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        }
162078fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell        if (!f.mUserVisibleHint) {
1621258029eeaaace45fab6244cd09cc389aa57fedacJake Wharton            if (result == null) {
1622258029eeaaace45fab6244cd09cc389aa57fedacJake Wharton                result = new Bundle();
1623258029eeaaace45fab6244cd09cc389aa57fedacJake Wharton            }
162478fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            // Only add this if it's not the default value
162578fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell            result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
162678fed9b78f8b3b92979b94dda2640cdeaffb2573Adam Powell        }
1627b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn
1628b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn        return result;
1629b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn    }
1630b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn
16316e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn    Parcelable saveAllState() {
16323a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        // Make sure all pending operations have now been executed to get
16333a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        // our state update-to-date.
16343a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn        execPendingActions();
16353a57fb9e9c4f205ca6bd4f036b3080f92588d16dDianne Hackborn
16363e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        mStateSaved = true;
16373e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn
16386e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mActive == null || mActive.size() <= 0) {
16396e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            return null;
1640b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1641b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
16426e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // First collect all active fragments.
16436e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        int N = mActive.size();
16446e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        FragmentState[] active = new FragmentState[N];
16456e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        boolean haveFragments = false;
16466e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        for (int i=0; i<N; i++) {
16476e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            Fragment f = mActive.get(i);
16486e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            if (f != null) {
164961af8a8ef77fd0e1a760d01a5c7784ae1d0efc37Dianne Hackborn                if (f.mIndex < 0) {
16504702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                    throwException(new IllegalStateException(
16514702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                            "Failure saving state: active " + f
16524702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                            + " has cleared index: " + f.mIndex));
165361af8a8ef77fd0e1a760d01a5c7784ae1d0efc37Dianne Hackborn                }
165461af8a8ef77fd0e1a760d01a5c7784ae1d0efc37Dianne Hackborn
16556e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                haveFragments = true;
165661af8a8ef77fd0e1a760d01a5c7784ae1d0efc37Dianne Hackborn
16576e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                FragmentState fs = new FragmentState(f);
16586e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                active[i] = fs;
16596e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
1660625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
1661b46ed7636be9341b6ce0b158b3d86f34a437e6daDianne Hackborn                    fs.mSavedFragmentState = saveFragmentBasicState(f);
1662625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn
1663625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    if (f.mTarget != null) {
16645d9d03a0234faa3cffd11502f973057045cafe82Dianne Hackborn                        if (f.mTarget.mIndex < 0) {
16654702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                            throwException(new IllegalStateException(
16664702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                    "Failure saving state: " + f
16674702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                    + " has target not in fragment manager: " + f.mTarget));
16685d9d03a0234faa3cffd11502f973057045cafe82Dianne Hackborn                        }
16696e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                        if (fs.mSavedFragmentState == null) {
16706e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                            fs.mSavedFragmentState = new Bundle();
16716e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                        }
1672625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                        putFragment(fs.mSavedFragmentState,
1673625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                                FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
1674625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                        if (f.mTargetRequestCode != 0) {
1675625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                            fs.mSavedFragmentState.putInt(
1676625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                                    FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
1677625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                                    f.mTargetRequestCode);
1678625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                        }
1679def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                    }
1680def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn
1681625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                } else {
1682625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    fs.mSavedFragmentState = f.mSavedFragmentState;
16836e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                }
16846e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
1685b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn                if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
1686b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn                        + fs.mSavedFragmentState);
1687b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
1688b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1689b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
16906e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (!haveFragments) {
1691b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn            if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
1692b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            return null;
1693b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1694b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
1695b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        int[] added = null;
1696b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        BackStackState[] backStack = null;
1697b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
16986e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // Build list of currently added fragments.
1699625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn        if (mAdded != null) {
1700625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            N = mAdded.size();
1701625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn            if (N > 0) {
1702625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                added = new int[N];
1703625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                for (int i=0; i<N; i++) {
1704625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    added[i] = mAdded.get(i).mIndex;
170561af8a8ef77fd0e1a760d01a5c7784ae1d0efc37Dianne Hackborn                    if (added[i] < 0) {
17064702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                        throwException(new IllegalStateException(
17074702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                "Failure saving state: active " + mAdded.get(i)
17084702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                                + " has cleared index: " + added[i]));
170961af8a8ef77fd0e1a760d01a5c7784ae1d0efc37Dianne Hackborn                    }
1710625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                    if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
1711625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                            + ": " + mAdded.get(i));
1712625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                }
1713b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
1714b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1715b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
1716b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        // Now save back stack.
1717b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        if (mBackStack != null) {
1718b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            N = mBackStack.size();
1719b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            if (N > 0) {
1720b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                backStack = new BackStackState[N];
1721b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                for (int i=0; i<N; i++) {
1722b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    backStack[i] = new BackStackState(this, mBackStack.get(i));
1723b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn                    if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
1724b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn                            + ": " + mBackStack.get(i));
1725b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                }
1726b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
1727b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1728b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
1729b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        FragmentManagerState fms = new FragmentManagerState();
17306e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        fms.mActive = active;
1731b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        fms.mAdded = added;
1732b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        fms.mBackStack = backStack;
1733b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        return fms;
1734b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
1735b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
1736b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) {
1737b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        // If there is no saved state at all, then there can not be
1738b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        // any nonConfig fragments either, so that is that.
1739b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        if (state == null) return;
1740b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        FragmentManagerState fms = (FragmentManagerState)state;
17416e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (fms.mActive == null) return;
1742b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
17436e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // First re-attach any non-config instances we are retaining back
17446e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // to their saved state, so we don't try to instantiate them again.
1745b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        if (nonConfig != null) {
1746b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            for (int i=0; i<nonConfig.size(); i++) {
1747b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                Fragment f = nonConfig.get(i);
1748b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
17496e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                FragmentState fs = fms.mActive[f.mIndex];
1750b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                fs.mInstance = f;
1751b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                f.mSavedViewState = null;
17526e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                f.mBackStackNesting = 0;
1753625ac271f80777668f832a344486a6fcdc06d0aeDianne Hackborn                f.mInLayout = false;
17546e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                f.mAdded = false;
1755f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                f.mTarget = null;
1756b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                if (fs.mSavedFragmentState != null) {
17575164246d7e47b9c995ca1e1587f3056eb777f60bDianne Hackborn                    fs.mSavedFragmentState.setClassLoader(mActivity.getClassLoader());
1758b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                    f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
1759def1537e9e8d0dd190cde5310ddae8b921088c9bDianne Hackborn                            FragmentManagerImpl.VIEW_STATE_TAG);
176033acfbed073fdda5bcd2c97213a0361bc5958702Craig Mautner                    f.mSavedFragmentState = fs.mSavedFragmentState;
1761b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                }
1762b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
1763b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1764b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
17656e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // Build the full list of active fragments, instantiating them from
17666e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // their saved state.
17676e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        mActive = new ArrayList<Fragment>(fms.mActive.length);
17686e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (mAvailIndices != null) {
17696e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mAvailIndices.clear();
17706e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
17716e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        for (int i=0; i<fms.mActive.length; i++) {
17726e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            FragmentState fs = fms.mActive[i];
17736e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            if (fs != null) {
177462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                Fragment f = fs.instantiate(mActivity, mParent);
1775f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
1776b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn                mActive.add(f);
177730d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn                // Now that the fragment is instantiated (or came from being
177830d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn                // retained above), clear mInstance in case we end up re-restoring
177930d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn                // from this FragmentState again.
178030d7189067524000c738c188c4ff91f84f474d25Dianne Hackborn                fs.mInstance = null;
17816e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            } else {
17826e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                mActive.add(null);
17836e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                if (mAvailIndices == null) {
17846e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                    mAvailIndices = new ArrayList<Integer>();
17856e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                }
1786f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
17876e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                mAvailIndices.add(i);
1788b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
17896e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        }
17906e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
17913e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        // Update the target of all retained fragments.
17923e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        if (nonConfig != null) {
17933e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn            for (int i=0; i<nonConfig.size(); i++) {
17943e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn                Fragment f = nonConfig.get(i);
1795f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                if (f.mTargetIndex >= 0) {
1796f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                    if (f.mTargetIndex < mActive.size()) {
1797f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                        f.mTarget = mActive.get(f.mTargetIndex);
17983e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn                    } else {
17993e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn                        Log.w(TAG, "Re-attaching retained fragment " + f
1800f930232fd1c8d301d91853c8fe5dca43979ac807Dianne Hackborn                                + " target no longer exists: " + f.mTargetIndex);
18013e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn                        f.mTarget = null;
18023e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn                    }
18033e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn                }
18043e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn            }
18053e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        }
18063e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn
18076e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // Build the list of currently added fragments.
18086e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        if (fms.mAdded != null) {
18096e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mAdded = new ArrayList<Fragment>(fms.mAdded.length);
1810b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            for (int i=0; i<fms.mAdded.length; i++) {
18116e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                Fragment f = mActive.get(fms.mAdded[i]);
18126e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                if (f == null) {
18134702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                    throwException(new IllegalStateException(
18144702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn                            "No instantiated fragment for index #" + fms.mAdded[i]));
1815b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                }
18166e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                f.mAdded = true;
1817f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
1818f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (mAdded.contains(f)) {
1819f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    throw new IllegalStateException("Already added!");
1820f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                }
18216e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn                mAdded.add(f);
1822b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
18236e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        } else {
18246e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mAdded = null;
1825b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
18266e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn
18276e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        // Build the back stack.
1828b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        if (fms.mBackStack != null) {
1829c6669ca63299219d815464129dac051ab2404286Dianne Hackborn            mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
1830b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            for (int i=0; i<fms.mBackStack.length; i++) {
1831c6669ca63299219d815464129dac051ab2404286Dianne Hackborn                BackStackRecord bse = fms.mBackStack[i].instantiate(this);
1832f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                if (DEBUG) {
1833f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    Log.v(TAG, "restoreAllState: back stack #" + i
1834b7a2e4772220c4b41df1260cedaf8912f4b07547Dianne Hackborn                        + " (index " + bse.mIndex + "): " + bse);
1835f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    LogWriter logw = new LogWriter(Log.VERBOSE, TAG);
18368c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn                    PrintWriter pw = new FastPrintWriter(logw, false, 1024);
1837f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                    bse.dump("  ", pw, false);
18388c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn                    pw.flush();
1839f43a33c5ea5adb8d100ab0c3da965bac33155cb8Dianne Hackborn                }
1840b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn                mBackStack.add(bse);
1841dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                if (bse.mIndex >= 0) {
1842dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                    setBackStackIndex(bse.mIndex, bse);
1843dd913a50cd72d6dd23c4ea437f0ebe2be05ca2e8Dianne Hackborn                }
1844b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn            }
18456e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn        } else {
18466e8304e57ec533e257a2496183125f257be1ff60Dianne Hackborn            mBackStack = null;
1847b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn        }
1848b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    }
1849b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn
185062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn    public void attachActivity(Activity activity, FragmentContainer container, Fragment parent) {
18514702a856973a553deb82f71b1d3b6c3db5dbf4baDianne Hackborn        if (mActivity != null) throw new IllegalStateException("Already attached");
18522dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        mActivity = activity;
185362bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        mContainer = container;
185462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        mParent = parent;
18552dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
18562dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
1857fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn    public void noteStateNotSaved() {
1858fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn        mStateSaved = false;
1859fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn    }
1860fb3cffeb35368da22f99b85d45039c4e6e471c06Dianne Hackborn
1861b4bc78b16a05554c57508b488e21dd8eca4e13e6Dianne Hackborn    public void dispatchCreate() {
18623e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        mStateSaved = false;
1863ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        moveToState(Fragment.CREATED, false);
18642dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
18652dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
1866c801768e4d29667a2608695449ebc2833ba0f200Dianne Hackborn    public void dispatchActivityCreated() {
18673e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        mStateSaved = false;
1868c801768e4d29667a2608695449ebc2833ba0f200Dianne Hackborn        moveToState(Fragment.ACTIVITY_CREATED, false);
1869c801768e4d29667a2608695449ebc2833ba0f200Dianne Hackborn    }
1870c801768e4d29667a2608695449ebc2833ba0f200Dianne Hackborn
18712dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    public void dispatchStart() {
18723e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        mStateSaved = false;
1873ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        moveToState(Fragment.STARTED, false);
18742dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
18752dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
18762dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    public void dispatchResume() {
18773e449ce00ed2d3b271e50bc7a52798f630973bf1Dianne Hackborn        mStateSaved = false;
1878ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        moveToState(Fragment.RESUMED, false);
18792dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
18802dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
18812dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    public void dispatchPause() {
1882ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        moveToState(Fragment.STARTED, false);
18832dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
18842dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
18852dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    public void dispatchStop() {
188616f6e89c2a4bbf73fe15cb2e81c8fec98c7ac831Dianne Hackborn        moveToState(Fragment.STOPPED, false);
18872dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
18882dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn
188962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn    public void dispatchDestroyView() {
189062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        moveToState(Fragment.CREATED, false);
189162bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn    }
18921b8ecc5031051b4bba620fac27552e84ca666496Dianne Hackborn
18932dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    public void dispatchDestroy() {
1894d173fa3b1cb8e4294aba7564c0171894be6c3c24Dianne Hackborn        mDestroyed = true;
1895c693823fd4cb927160c3f423670587be5e09ac20Dianne Hackborn        execPendingActions();
1896ba51c3d02d08709e933de6f6c76dee3fd2f4bfa1Dianne Hackborn        moveToState(Fragment.INITIALIZING, false);
18972dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn        mActivity = null;
189862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        mContainer = null;
189962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn        mParent = null;
19002dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn    }
1901f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
19029d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn    public void dispatchConfigurationChanged(Configuration newConfig) {
1903acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
19049d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
19059d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn                Fragment f = mAdded.get(i);
19069d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn                if (f != null) {
190762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    f.performConfigurationChanged(newConfig);
19089d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn                }
19099d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn            }
19109d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn        }
19119d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn    }
19129d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn
19139d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn    public void dispatchLowMemory() {
1914acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
19159d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
19169d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn                Fragment f = mAdded.get(i);
19179d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn                if (f != null) {
191862bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    f.performLowMemory();
19199d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn                }
19209d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn            }
19219d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn        }
19229d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn    }
19239d0718042f7c0a50d825c621f82ce9a92071f07aDianne Hackborn
1924c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn    public void dispatchTrimMemory(int level) {
1925acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
1926c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
1927c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn                Fragment f = mAdded.get(i);
1928c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn                if (f != null) {
192962bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    f.performTrimMemory(level);
1930c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn                }
1931c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn            }
1932c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn        }
1933c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn    }
1934c68c913d357e2955d4bd7ca52829071e531c7825Dianne Hackborn
1935b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1936b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        boolean show = false;
19378eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn        ArrayList<Fragment> newMenus = null;
1938acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
1939b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
1940b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                Fragment f = mAdded.get(i);
194162bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                if (f != null) {
194262bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    if (f.performCreateOptionsMenu(menu, inflater)) {
194362bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        show = true;
194462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        if (newMenus == null) {
194562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                            newMenus = new ArrayList<Fragment>();
194662bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        }
194762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        newMenus.add(f);
19488eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn                    }
1949b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                }
1950b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            }
1951b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        }
19528eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn
19538eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn        if (mCreatedMenus != null) {
19548eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn            for (int i=0; i<mCreatedMenus.size(); i++) {
19558eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn                Fragment f = mCreatedMenus.get(i);
19568eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn                if (newMenus == null || !newMenus.contains(f)) {
19578eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn                    f.onDestroyOptionsMenu();
19588eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn                }
19598eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn            }
19608eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn        }
19618eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn
19628eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn        mCreatedMenus = newMenus;
19638eb2e244f9b14d946ee587d0b673b866865026c0Dianne Hackborn
1964b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        return show;
1965b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    }
1966b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn
1967b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    public boolean dispatchPrepareOptionsMenu(Menu menu) {
1968b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        boolean show = false;
1969acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
1970b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
1971b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                Fragment f = mAdded.get(i);
197262bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                if (f != null) {
197362bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    if (f.performPrepareOptionsMenu(menu)) {
197462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                        show = true;
197562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    }
1976b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                }
1977b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            }
1978b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        }
1979b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        return show;
1980b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    }
1981b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn
1982b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    public boolean dispatchOptionsItemSelected(MenuItem item) {
1983acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
1984b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
1985b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                Fragment f = mAdded.get(i);
198662bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                if (f != null) {
198762bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    if (f.performOptionsItemSelected(item)) {
1988b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                        return true;
1989b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                    }
1990b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                }
1991b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            }
1992b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        }
1993b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        return false;
1994b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    }
1995b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn
19965ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    public boolean dispatchContextItemSelected(MenuItem item) {
1997acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
19985ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
19995ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn                Fragment f = mAdded.get(i);
200062bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                if (f != null) {
200162bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    if (f.performContextItemSelected(item)) {
20025ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn                        return true;
20035ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn                    }
20045ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn                }
20055ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn            }
20065ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        }
20075ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn        return false;
20085ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn    }
20095ddd127d5a38d80c0d8087d1770f41f61f84f048Dianne Hackborn
2010b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    public void dispatchOptionsMenuClosed(Menu menu) {
2011acdfbcca6f91bdf3f77ed7b2e699c97ce98c1fb8Dianne Hackborn        if (mAdded != null) {
2012b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            for (int i=0; i<mAdded.size(); i++) {
2013b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                Fragment f = mAdded.get(i);
201462bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                if (f != null) {
201562bea2f1710be0d1a42c07109fd4307ded660d3bDianne Hackborn                    f.performOptionsMenuClosed(menu);
2016b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn                }
2017b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn            }
2018b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn        }
2019b31e84bc4513e46bac4be8f8d0513f78e360fb11Dianne Hackborn    }
2020f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell
2021f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell    @Override
2022f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell    public void invalidateOptionsMenu() {
2023f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell        if (mActivity != null && mCurState == Fragment.RESUMED) {
2024f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell            mActivity.invalidateOptionsMenu();
2025f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell        } else {
2026f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell            mNeedMenuInvalidate = true;
2027f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell        }
2028f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell    }
2029f0f5fffc6f578f531df7b208cfbfb53f884c0380Adam Powell
2030f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    public static int reverseTransit(int transit) {
2031f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        int rev = 0;
2032f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        switch (transit) {
2033811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2034811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
2035f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                break;
2036811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2037811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
2038f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                break;
2039327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2040327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn                rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
20419ff82bf2b33513052500473d0d6d025a80dcecbfChet Haase                break;
2042f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
2043f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        return rev;
2044f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
2045f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    }
2046f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn
2047f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    public static int transitToStyleIndex(int transit, boolean enter) {
2048f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        int animAttr = -1;
2049f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        switch (transit) {
2050811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase            case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2051f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                animAttr = enter
2052811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                    ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation
2053811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                    : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation;
2054f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                break;
2055811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase            case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2056f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                animAttr = enter
2057811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                    ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation
2058811ed1065f39469cf2cf6adba22cab397ed88d5eChet Haase                    : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation;
2059f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn                break;
2060327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn            case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
20619ff82bf2b33513052500473d0d6d025a80dcecbfChet Haase                animAttr = enter
2062327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn                    ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation
2063327fbd2c8fa294b919475feb4c74a74ee1981e02Dianne Hackborn                    : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation;
20649ff82bf2b33513052500473d0d6d025a80dcecbfChet Haase                break;
2065f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        }
2066f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn        return animAttr;
2067f121be737c59390d97e21a92be8e166001534c7dDianne Hackborn    }
2068371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2069371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    @Override
2070371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
2071371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (!"fragment".equals(name)) {
2072371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            return null;
2073371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2074371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2075371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        String fname = attrs.getAttributeValue(null, "class");
2076371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        TypedArray a =
2077371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
2078371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (fname == null) {
2079371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fname = a.getString(com.android.internal.R.styleable.Fragment_name);
2080371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2081371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID);
2082371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
2083371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        a.recycle();
2084371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2085371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        int containerId = parent != null ? parent.getId() : 0;
2086371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
2087371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            throw new IllegalArgumentException(attrs.getPositionDescription()
2088371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                    + ": Must specify unique android:id, android:tag, or have a parent with"
2089371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                    + " an id for " + fname);
2090371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2091371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2092371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        // If we restored from a previous state, we may already have
2093371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        // instantiated this fragment from the state and should use
2094371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        // that instance instead of making a new one.
2095371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
2096371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (fragment == null && tag != null) {
2097371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment = findFragmentByTag(tag);
2098371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2099371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (fragment == null && containerId != View.NO_ID) {
2100371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment = findFragmentById(containerId);
2101371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2102371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2103371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
2104371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                + Integer.toHexString(id) + " fname=" + fname
2105371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                + " existing=" + fragment);
2106371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (fragment == null) {
2107371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment = Fragment.instantiate(context, fname);
2108371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mFromLayout = true;
2109371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mFragmentId = id != 0 ? id : containerId;
2110371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mContainerId = containerId;
2111371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mTag = tag;
2112371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mInLayout = true;
2113371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mFragmentManager = this;
2114371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState);
2115371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            addFragment(fragment, true);
2116371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        } else if (fragment.mInLayout) {
2117371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            // A fragment already exists and it is not one we restored from
2118371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            // previous state.
2119371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            throw new IllegalArgumentException(attrs.getPositionDescription()
2120371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                    + ": Duplicate id 0x" + Integer.toHexString(id)
2121371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                    + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
2122371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                    + " with another fragment for " + fname);
2123371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        } else {
2124371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            // This fragment was retained from a previous instance; get it
2125371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            // going now.
2126371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mInLayout = true;
2127371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            // If this fragment is newly instantiated (either right now, or
2128371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            // from last saved state), then give it the attributes to
2129371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            // initialize itself.
2130371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            if (!fragment.mRetaining) {
2131371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState);
2132371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            }
2133371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2134371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2135371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        // If we haven't finished entering the CREATED state ourselves yet,
2136371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        // push the inflated child fragment along.
2137371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
2138371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            moveToState(fragment, Fragment.CREATED, 0, 0, false);
2139371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        } else {
2140371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            moveToState(fragment);
2141371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2142371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2143371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (fragment.mView == null) {
2144371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            throw new IllegalStateException("Fragment " + fname
2145371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell                    + " did not create a view.");
2146371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2147371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (id != 0) {
2148371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mView.setId(id);
2149371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2150371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        if (fragment.mView.getTag() == null) {
2151371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell            fragment.mView.setTag(tag);
2152371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        }
2153371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        return fragment.mView;
2154371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    }
2155371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2156371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    @Override
2157371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    public View onCreateView(String name, Context context, AttributeSet attrs) {
2158371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        return null;
2159371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    }
2160371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell
2161371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    LayoutInflater.Factory2 getLayoutInflaterFactory() {
2162371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell        return this;
2163371a809179c843d7ae661a10bc9b4b8cfcaff566Adam Powell    }
21642dedce6e84679ead961a485c7fe4b0f77c713b6aDianne Hackborn}
2165