blob: 7358f735ae360dbf9482795a8af983f3fec82290 [file] [log] [blame]
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Chet Haasea18a86b2010-09-07 13:20:00 -070019import android.animation.Animator;
20import android.animation.AnimatorInflater;
Chet Haaseb20db3e2010-09-10 13:07:30 -070021import android.animation.AnimatorListenerAdapter;
Dianne Hackborn9d071802010-12-08 14:49:15 -080022import android.content.res.Configuration;
Dianne Hackbornf121be72010-05-06 14:10:32 -070023import android.content.res.TypedArray;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070024import android.os.Bundle;
Dianne Hackbornf43a33c2012-09-27 00:48:11 -070025import android.os.Debug;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070026import android.os.Handler;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -080027import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070028import android.os.Parcel;
29import android.os.Parcelable;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080030import android.util.DebugUtils;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070031import android.util.Log;
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -080032import android.util.LogWriter;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070033import android.util.SparseArray;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070034import android.view.Menu;
35import android.view.MenuInflater;
36import android.view.MenuItem;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070037import android.view.View;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070038import android.view.ViewGroup;
Dianne Hackborn8c841092013-06-24 13:46:13 -070039import com.android.internal.util.FastPrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070040
Dianne Hackborn625ac272010-09-17 18:29:22 -070041import java.io.FileDescriptor;
42import java.io.PrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070043import java.util.ArrayList;
Dianne Hackbornd173fa32010-12-23 13:58:22 -080044import java.util.Arrays;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070045
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070046/**
47 * Interface for interacting with {@link Fragment} objects inside of an
48 * {@link Activity}
Joe Fernandezb54e7a32011-10-03 15:09:50 -070049 *
50 * <div class="special reference">
51 * <h3>Developer Guides</h3>
52 * <p>For more information about using fragments, read the
53 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
54 * </div>
Dianne Hackborn7871bad2011-12-12 15:19:26 -080055 *
56 * While the FragmentManager API was introduced in
57 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
58 * at is also available for use on older platforms through
59 * {@link android.support.v4.app.FragmentActivity}. See the blog post
60 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
61 * Fragments For All</a> for more details.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070062 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070063public abstract class FragmentManager {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070064 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070065 * Representation of an entry on the fragment back stack, as created
66 * with {@link FragmentTransaction#addToBackStack(String)
67 * FragmentTransaction.addToBackStack()}. Entries can later be
Dianne Hackborn327fbd22011-01-17 14:38:50 -080068 * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070069 * FragmentManager.getBackStackEntry()}.
70 *
71 * <p>Note that you should never hold on to a BackStackEntry object;
72 * the identifier as returned by {@link #getId} is the only thing that
73 * will be persisted across activity instances.
74 */
75 public interface BackStackEntry {
76 /**
77 * Return the unique identifier for the entry. This is the only
78 * representation of the entry that will persist across activity
79 * instances.
80 */
81 public int getId();
82
83 /**
Dianne Hackborn6c285972011-08-29 16:53:49 -070084 * Get the name that was supplied to
85 * {@link FragmentTransaction#addToBackStack(String)
86 * FragmentTransaction.addToBackStack(String)} when creating this entry.
87 */
88 public String getName();
89
90 /**
Dianne Hackborn327fbd22011-01-17 14:38:50 -080091 * Return the full bread crumb title resource identifier for the entry,
92 * or 0 if it does not have one.
93 */
94 public int getBreadCrumbTitleRes();
95
96 /**
97 * Return the short bread crumb title resource identifier for the entry,
98 * or 0 if it does not have one.
99 */
100 public int getBreadCrumbShortTitleRes();
101
102 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700103 * Return the full bread crumb title for the entry, or null if it
104 * does not have one.
105 */
106 public CharSequence getBreadCrumbTitle();
107
108 /**
109 * Return the short bread crumb title for the entry, or null if it
110 * does not have one.
111 */
112 public CharSequence getBreadCrumbShortTitle();
113 }
114
115 /**
116 * Interface to watch for changes to the back stack.
117 */
118 public interface OnBackStackChangedListener {
119 /**
120 * Called whenever the contents of the back stack change.
121 */
122 public void onBackStackChanged();
123 }
124
125 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700126 * Start a series of edit operations on the Fragments associated with
127 * this FragmentManager.
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700128 *
129 * <p>Note: A fragment transaction can only be created/committed prior
130 * to an activity saving its state. If you try to commit a transaction
131 * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()}
132 * (and prior to a following {@link Activity#onStart Activity.onStart}
133 * or {@link Activity#onResume Activity.onResume()}, you will get an error.
134 * This is because the framework takes care of saving your current fragments
135 * in the state, and if changes are made after the state is saved then they
136 * will be lost.</p>
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700137 */
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800138 public abstract FragmentTransaction beginTransaction();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700139
Dianne Hackborn17b9b812011-01-17 17:16:02 -0800140 /** @hide -- remove once prebuilts are in. */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800141 @Deprecated
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800142 public FragmentTransaction openTransaction() {
143 return beginTransaction();
144 }
145
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700146 /**
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800147 * After a {@link FragmentTransaction} is committed with
148 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
149 * is scheduled to be executed asynchronously on the process's main thread.
150 * If you want to immediately executing any such pending operations, you
151 * can call this function (only from the main thread) to do so. Note that
152 * all callbacks and other related behavior will be done from within this
153 * call, so be careful about where this is called from.
154 *
155 * @return Returns true if there were any pending transactions to be
156 * executed.
157 */
158 public abstract boolean executePendingTransactions();
159
160 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700161 * Finds a fragment that was identified by the given id either when inflated
162 * from XML or as the container ID when added in a transaction. This first
163 * searches through fragments that are currently added to the manager's
164 * activity; if no such fragment is found, then all fragments currently
165 * on the back stack associated with this ID are searched.
166 * @return The fragment if found or null otherwise.
167 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700168 public abstract Fragment findFragmentById(int id);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700169
170 /**
171 * Finds a fragment that was identified by the given tag either when inflated
172 * from XML or as supplied when added in a transaction. This first
173 * searches through fragments that are currently added to the manager's
174 * activity; if no such fragment is found, then all fragments currently
175 * on the back stack are searched.
176 * @return The fragment if found or null otherwise.
177 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700178 public abstract Fragment findFragmentByTag(String tag);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700179
180 /**
181 * Flag for {@link #popBackStack(String, int)}
182 * and {@link #popBackStack(int, int)}: If set, and the name or ID of
183 * a back stack entry has been supplied, then all matching entries will
184 * be consumed until one that doesn't match is found or the bottom of
185 * the stack is reached. Otherwise, all entries up to but not including that entry
186 * will be removed.
187 */
188 public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
189
190 /**
Ben Komalo87ffa202011-02-28 12:41:42 -0800191 * Pop the top state off the back stack. This function is asynchronous -- it
192 * enqueues the request to pop, but the action will not be performed until the
193 * application returns to its event loop.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700194 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800195 public abstract void popBackStack();
196
197 /**
198 * Like {@link #popBackStack()}, but performs the operation immediately
199 * inside of the call. This is like calling {@link #executePendingTransactions()}
200 * afterwards.
201 * @return Returns true if there was something popped, else false.
202 */
203 public abstract boolean popBackStackImmediate();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700204
205 /**
206 * Pop the last fragment transition from the manager's fragment
207 * back stack. If there is nothing to pop, false is returned.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800208 * This function is asynchronous -- it enqueues the
209 * request to pop, but the action will not be performed until the application
210 * returns to its event loop.
211 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700212 * @param name If non-null, this is the name of a previous back state
213 * to look for; if found, all states up to that state will be popped. The
214 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
215 * the named state itself is popped. If null, only the top state is popped.
216 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
217 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800218 public abstract void popBackStack(String name, int flags);
219
220 /**
221 * Like {@link #popBackStack(String, int)}, but performs the operation immediately
222 * inside of the call. This is like calling {@link #executePendingTransactions()}
223 * afterwards.
224 * @return Returns true if there was something popped, else false.
225 */
226 public abstract boolean popBackStackImmediate(String name, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700227
228 /**
229 * Pop all back stack states up to the one with the given identifier.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800230 * This function is asynchronous -- it enqueues the
231 * request to pop, but the action will not be performed until the application
232 * returns to its event loop.
233 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700234 * @param id Identifier of the stated to be popped. If no identifier exists,
235 * false is returned.
236 * The identifier is the number returned by
237 * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The
238 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
239 * the named state itself is popped.
240 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
241 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800242 public abstract void popBackStack(int id, int flags);
243
244 /**
245 * Like {@link #popBackStack(int, int)}, but performs the operation immediately
246 * inside of the call. This is like calling {@link #executePendingTransactions()}
247 * afterwards.
248 * @return Returns true if there was something popped, else false.
249 */
250 public abstract boolean popBackStackImmediate(int id, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700251
252 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700253 * Return the number of entries currently in the back stack.
254 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800255 public abstract int getBackStackEntryCount();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700256
257 /**
258 * Return the BackStackEntry at index <var>index</var> in the back stack;
259 * entries start index 0 being the bottom of the stack.
260 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800261 public abstract BackStackEntry getBackStackEntryAt(int index);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700262
263 /**
264 * Add a new listener for changes to the fragment back stack.
265 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700266 public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700267
268 /**
269 * Remove a listener that was previously added with
270 * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
271 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700272 public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700273
274 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700275 * Put a reference to a fragment in a Bundle. This Bundle can be
276 * persisted as saved state, and when later restoring
277 * {@link #getFragment(Bundle, String)} will return the current
278 * instance of the same fragment.
279 *
280 * @param bundle The bundle in which to put the fragment reference.
281 * @param key The name of the entry in the bundle.
282 * @param fragment The Fragment whose reference is to be stored.
283 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700284 public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700285
286 /**
287 * Retrieve the current Fragment instance for a reference previously
288 * placed with {@link #putFragment(Bundle, String, Fragment)}.
289 *
290 * @param bundle The bundle from which to retrieve the fragment reference.
291 * @param key The name of the entry in the bundle.
292 * @return Returns the current Fragment instance that is associated with
293 * the given reference.
294 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700295 public abstract Fragment getFragment(Bundle bundle, String key);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700296
297 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700298 * Save the current instance state of the given Fragment. This can be
299 * used later when creating a new instance of the Fragment and adding
300 * it to the fragment manager, to have it create itself to match the
301 * current state returned here. Note that there are limits on how
302 * this can be used:
303 *
304 * <ul>
305 * <li>The Fragment must currently be attached to the FragmentManager.
306 * <li>A new Fragment created using this saved state must be the same class
307 * type as the Fragment it was created from.
308 * <li>The saved state can not contain dependencies on other fragments --
309 * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
310 * store a fragment reference because that reference may not be valid when
311 * this saved state is later used. Likewise the Fragment's target and
312 * result code are not included in this state.
313 * </ul>
314 *
315 * @param f The Fragment whose state is to be saved.
316 * @return The generated state. This will be null if there was no
317 * interesting state created by the fragment.
318 */
319 public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
320
321 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700322 * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()}
323 * call has been made on the FragmentManager's Activity, so this instance is now dead.
324 */
325 public abstract boolean isDestroyed();
326
327 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700328 * Print the FragmentManager's state into the given stream.
329 *
330 * @param prefix Text to print at the front of each line.
331 * @param fd The raw file descriptor that the dump is being sent to.
332 * @param writer A PrintWriter to which the dump is to be set.
Dianne Hackborn30d71892010-12-11 10:37:55 -0800333 * @param args Additional arguments to the dump request.
Dianne Hackborn625ac272010-09-17 18:29:22 -0700334 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700335 public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800336
337 /**
338 * Control whether the framework's internal fragment manager debugging
339 * logs are turned on. If enabled, you will see output in logcat as
340 * the framework performs fragment operations.
341 */
342 public static void enableDebugLogging(boolean enabled) {
343 FragmentManagerImpl.DEBUG = enabled;
344 }
Adam Powellf0f5fff2011-08-01 13:42:50 -0700345
346 /**
347 * Invalidate the attached activity's options menu as necessary.
348 * This may end up being deferred until we move to the resumed state.
349 */
350 public void invalidateOptionsMenu() { }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700351}
352
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700353final class FragmentManagerState implements Parcelable {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700354 FragmentState[] mActive;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700355 int[] mAdded;
356 BackStackState[] mBackStack;
357
358 public FragmentManagerState() {
359 }
360
361 public FragmentManagerState(Parcel in) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700362 mActive = in.createTypedArray(FragmentState.CREATOR);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700363 mAdded = in.createIntArray();
364 mBackStack = in.createTypedArray(BackStackState.CREATOR);
365 }
366
367 public int describeContents() {
368 return 0;
369 }
370
371 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700372 dest.writeTypedArray(mActive, flags);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700373 dest.writeIntArray(mAdded);
374 dest.writeTypedArray(mBackStack, flags);
375 }
376
377 public static final Parcelable.Creator<FragmentManagerState> CREATOR
378 = new Parcelable.Creator<FragmentManagerState>() {
379 public FragmentManagerState createFromParcel(Parcel in) {
380 return new FragmentManagerState(in);
381 }
382
383 public FragmentManagerState[] newArray(int size) {
384 return new FragmentManagerState[size];
385 }
386 };
Dianne Hackbornba51c3d2010-05-05 18:49:48 -0700387}
388
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700389/**
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700390 * Callbacks from FragmentManagerImpl to its container.
391 */
392interface FragmentContainer {
393 public View findViewById(int id);
394}
395
396/**
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700397 * Container for fragments associated with an activity.
398 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700399final class FragmentManagerImpl extends FragmentManager {
Craig Mautner1c437192012-08-01 10:01:16 -0700400 static boolean DEBUG = false;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700401 static final String TAG = "FragmentManager";
402
Dianne Hackborndef15372010-08-15 12:43:52 -0700403 static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
404 static final String TARGET_STATE_TAG = "android:target_state";
405 static final String VIEW_STATE_TAG = "android:view_state";
Adam Powell78fed9b2011-11-07 10:45:34 -0800406 static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
Dianne Hackborndef15372010-08-15 12:43:52 -0700407
Dianne Hackborn445646c2010-06-25 15:52:59 -0700408 ArrayList<Runnable> mPendingActions;
409 Runnable[] mTmpActions;
410 boolean mExecutingActions;
411
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700412 ArrayList<Fragment> mActive;
413 ArrayList<Fragment> mAdded;
414 ArrayList<Integer> mAvailIndices;
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700415 ArrayList<BackStackRecord> mBackStack;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -0700416 ArrayList<Fragment> mCreatedMenus;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700417
Dianne Hackborndd913a52010-07-22 12:17:04 -0700418 // Must be accessed while locked.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700419 ArrayList<BackStackRecord> mBackStackIndices;
Dianne Hackborndd913a52010-07-22 12:17:04 -0700420 ArrayList<Integer> mAvailBackStackIndices;
421
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700422 ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
423
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700424 int mCurState = Fragment.INITIALIZING;
425 Activity mActivity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700426 FragmentContainer mContainer;
427 Fragment mParent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700428
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700429 boolean mNeedMenuInvalidate;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700430 boolean mStateSaved;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800431 boolean mDestroyed;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700432 String mNoTransactionsBecause;
Adam Powell78fed9b2011-11-07 10:45:34 -0800433 boolean mHavePendingDeferredStart;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700434
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700435 // Temporary vars for state save and restore.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700436 Bundle mStateBundle = null;
437 SparseArray<Parcelable> mStateArray = null;
438
Dianne Hackborn445646c2010-06-25 15:52:59 -0700439 Runnable mExecCommit = new Runnable() {
440 @Override
441 public void run() {
442 execPendingActions();
443 }
444 };
Dianne Hackborn625ac272010-09-17 18:29:22 -0700445
Dianne Hackborn4702a852012-08-17 15:18:29 -0700446 private void throwException(RuntimeException ex) {
447 Log.e(TAG, ex.getMessage());
448 LogWriter logw = new LogWriter(Log.ERROR, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700449 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Dianne Hackborn4702a852012-08-17 15:18:29 -0700450 if (mActivity != null) {
451 Log.e(TAG, "Activity state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700452 try {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700453 mActivity.dump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700454 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700455 pw.flush();
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700456 Log.e(TAG, "Failed dumping state", e);
457 }
458 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700459 Log.e(TAG, "Fragment manager state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700460 try {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700461 dump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700462 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700463 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700464 Log.e(TAG, "Failed dumping state", e);
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700465 }
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700466 }
Dianne Hackborn8c841092013-06-24 13:46:13 -0700467 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700468 throw ex;
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700469 }
470
Dianne Hackborn625ac272010-09-17 18:29:22 -0700471 @Override
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800472 public FragmentTransaction beginTransaction() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700473 return new BackStackRecord(this);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700474 }
475
Dianne Hackborn625ac272010-09-17 18:29:22 -0700476 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800477 public boolean executePendingTransactions() {
478 return execPendingActions();
479 }
480
481 @Override
482 public void popBackStack() {
483 enqueueAction(new Runnable() {
484 @Override public void run() {
485 popBackStackState(mActivity.mHandler, null, -1, 0);
486 }
487 }, false);
488 }
489
490 @Override
491 public boolean popBackStackImmediate() {
492 checkStateLoss();
493 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700494 return popBackStackState(mActivity.mHandler, null, -1, 0);
495 }
496
Dianne Hackborn625ac272010-09-17 18:29:22 -0700497 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800498 public void popBackStack(final String name, final int flags) {
499 enqueueAction(new Runnable() {
500 @Override public void run() {
501 popBackStackState(mActivity.mHandler, name, -1, flags);
502 }
503 }, false);
504 }
505
506 @Override
507 public boolean popBackStackImmediate(String name, int flags) {
508 checkStateLoss();
509 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700510 return popBackStackState(mActivity.mHandler, name, -1, flags);
511 }
512
Dianne Hackborn625ac272010-09-17 18:29:22 -0700513 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800514 public void popBackStack(final int id, final int flags) {
515 if (id < 0) {
516 throw new IllegalArgumentException("Bad id: " + id);
517 }
518 enqueueAction(new Runnable() {
519 @Override public void run() {
520 popBackStackState(mActivity.mHandler, null, id, flags);
521 }
522 }, false);
523 }
524
525 @Override
526 public boolean popBackStackImmediate(int id, int flags) {
527 checkStateLoss();
528 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700529 if (id < 0) {
530 throw new IllegalArgumentException("Bad id: " + id);
531 }
532 return popBackStackState(mActivity.mHandler, null, id, flags);
533 }
534
Dianne Hackborn625ac272010-09-17 18:29:22 -0700535 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800536 public int getBackStackEntryCount() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700537 return mBackStack != null ? mBackStack.size() : 0;
538 }
539
Dianne Hackborn625ac272010-09-17 18:29:22 -0700540 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800541 public BackStackEntry getBackStackEntryAt(int index) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700542 return mBackStack.get(index);
543 }
544
Dianne Hackborn625ac272010-09-17 18:29:22 -0700545 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700546 public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
547 if (mBackStackChangeListeners == null) {
548 mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
549 }
550 mBackStackChangeListeners.add(listener);
551 }
552
Dianne Hackborn625ac272010-09-17 18:29:22 -0700553 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700554 public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
555 if (mBackStackChangeListeners != null) {
556 mBackStackChangeListeners.remove(listener);
557 }
558 }
559
Dianne Hackborn625ac272010-09-17 18:29:22 -0700560 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700561 public void putFragment(Bundle bundle, String key, Fragment fragment) {
562 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700563 throwException(new IllegalStateException("Fragment " + fragment
564 + " is not currently in the FragmentManager"));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700565 }
566 bundle.putInt(key, fragment.mIndex);
567 }
568
Dianne Hackborn625ac272010-09-17 18:29:22 -0700569 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700570 public Fragment getFragment(Bundle bundle, String key) {
Dianne Hackborndef15372010-08-15 12:43:52 -0700571 int index = bundle.getInt(key, -1);
572 if (index == -1) {
573 return null;
574 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700575 if (index >= mActive.size()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700576 throwException(new IllegalStateException("Fragement no longer exists for key "
577 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700578 }
579 Fragment f = mActive.get(index);
580 if (f == null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700581 throwException(new IllegalStateException("Fragement no longer exists for key "
582 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700583 }
584 return f;
585 }
586
Dianne Hackborn625ac272010-09-17 18:29:22 -0700587 @Override
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700588 public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
589 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700590 throwException(new IllegalStateException("Fragment " + fragment
591 + " is not currently in the FragmentManager"));
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700592 }
593 if (fragment.mState > Fragment.INITIALIZING) {
594 Bundle result = saveFragmentBasicState(fragment);
595 return result != null ? new Fragment.SavedState(result) : null;
596 }
597 return null;
598 }
599
600 @Override
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700601 public boolean isDestroyed() {
602 return mDestroyed;
603 }
604
605 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800606 public String toString() {
607 StringBuilder sb = new StringBuilder(128);
608 sb.append("FragmentManager{");
609 sb.append(Integer.toHexString(System.identityHashCode(this)));
610 sb.append(" in ");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700611 if (mParent != null) {
612 DebugUtils.buildShortClassTag(mParent, sb);
613 } else {
614 DebugUtils.buildShortClassTag(mActivity, sb);
615 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800616 sb.append("}}");
617 return sb.toString();
618 }
619
620 @Override
Dianne Hackborn625ac272010-09-17 18:29:22 -0700621 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700622 String innerPrefix = prefix + " ";
623
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800624 int N;
625 if (mActive != null) {
626 N = mActive.size();
627 if (N > 0) {
628 writer.print(prefix); writer.print("Active Fragments in ");
629 writer.print(Integer.toHexString(System.identityHashCode(this)));
630 writer.println(":");
631 for (int i=0; i<N; i++) {
632 Fragment f = mActive.get(i);
633 writer.print(prefix); writer.print(" #"); writer.print(i);
634 writer.print(": "); writer.println(f);
635 if (f != null) {
636 f.dump(innerPrefix, fd, writer, args);
637 }
638 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700639 }
640 }
641
642 if (mAdded != null) {
643 N = mAdded.size();
644 if (N > 0) {
645 writer.print(prefix); writer.println("Added Fragments:");
646 for (int i=0; i<N; i++) {
647 Fragment f = mAdded.get(i);
648 writer.print(prefix); writer.print(" #"); writer.print(i);
649 writer.print(": "); writer.println(f.toString());
650 }
651 }
652 }
653
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800654 if (mCreatedMenus != null) {
655 N = mCreatedMenus.size();
656 if (N > 0) {
657 writer.print(prefix); writer.println("Fragments Created Menus:");
658 for (int i=0; i<N; i++) {
659 Fragment f = mCreatedMenus.get(i);
660 writer.print(prefix); writer.print(" #"); writer.print(i);
661 writer.print(": "); writer.println(f.toString());
662 }
663 }
664 }
665
Dianne Hackborn625ac272010-09-17 18:29:22 -0700666 if (mBackStack != null) {
667 N = mBackStack.size();
668 if (N > 0) {
669 writer.print(prefix); writer.println("Back Stack:");
670 for (int i=0; i<N; i++) {
671 BackStackRecord bs = mBackStack.get(i);
672 writer.print(prefix); writer.print(" #"); writer.print(i);
673 writer.print(": "); writer.println(bs.toString());
Dianne Hackborn30d71892010-12-11 10:37:55 -0800674 bs.dump(innerPrefix, fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700675 }
676 }
677 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800678
679 synchronized (this) {
680 if (mBackStackIndices != null) {
681 N = mBackStackIndices.size();
682 if (N > 0) {
683 writer.print(prefix); writer.println("Back Stack Indices:");
684 for (int i=0; i<N; i++) {
685 BackStackRecord bs = mBackStackIndices.get(i);
686 writer.print(prefix); writer.print(" #"); writer.print(i);
687 writer.print(": "); writer.println(bs);
688 }
689 }
690 }
691
692 if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
693 writer.print(prefix); writer.print("mAvailBackStackIndices: ");
694 writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
695 }
696 }
697
698 if (mPendingActions != null) {
699 N = mPendingActions.size();
700 if (N > 0) {
701 writer.print(prefix); writer.println("Pending Actions:");
702 for (int i=0; i<N; i++) {
703 Runnable r = mPendingActions.get(i);
704 writer.print(prefix); writer.print(" #"); writer.print(i);
705 writer.print(": "); writer.println(r);
706 }
707 }
708 }
709
710 writer.print(prefix); writer.println("FragmentManager misc state:");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700711 writer.print(prefix); writer.print(" mActivity="); writer.println(mActivity);
712 writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer);
713 if (mParent != null) {
714 writer.print(prefix); writer.print(" mParent="); writer.println(mParent);
715 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800716 writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState);
717 writer.print(" mStateSaved="); writer.print(mStateSaved);
718 writer.print(" mDestroyed="); writer.println(mDestroyed);
719 if (mNeedMenuInvalidate) {
720 writer.print(prefix); writer.print(" mNeedMenuInvalidate=");
721 writer.println(mNeedMenuInvalidate);
722 }
723 if (mNoTransactionsBecause != null) {
724 writer.print(prefix); writer.print(" mNoTransactionsBecause=");
725 writer.println(mNoTransactionsBecause);
726 }
727 if (mAvailIndices != null && mAvailIndices.size() > 0) {
728 writer.print(prefix); writer.print(" mAvailIndices: ");
729 writer.println(Arrays.toString(mAvailIndices.toArray()));
730 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700731 }
732
Chet Haasea18a86b2010-09-07 13:20:00 -0700733 Animator loadAnimator(Fragment fragment, int transit, boolean enter,
Dianne Hackbornf121be72010-05-06 14:10:32 -0700734 int transitionStyle) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700735 Animator animObj = fragment.onCreateAnimator(transit, enter,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700736 fragment.mNextAnim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700737 if (animObj != null) {
738 return animObj;
739 }
740
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700741 if (fragment.mNextAnim != 0) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700742 Animator anim = AnimatorInflater.loadAnimator(mActivity, fragment.mNextAnim);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700743 if (anim != null) {
744 return anim;
745 }
746 }
747
Dianne Hackbornf121be72010-05-06 14:10:32 -0700748 if (transit == 0) {
749 return null;
750 }
751
752 int styleIndex = transitToStyleIndex(transit, enter);
753 if (styleIndex < 0) {
754 return null;
755 }
756
757 if (transitionStyle == 0 && mActivity.getWindow() != null) {
758 transitionStyle = mActivity.getWindow().getAttributes().windowAnimations;
759 }
760 if (transitionStyle == 0) {
761 return null;
762 }
763
764 TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
Chet Haase811ed1062010-08-06 10:38:15 -0700765 com.android.internal.R.styleable.FragmentAnimation);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700766 int anim = attrs.getResourceId(styleIndex, 0);
767 attrs.recycle();
768
769 if (anim == 0) {
770 return null;
771 }
772
Chet Haasea18a86b2010-09-07 13:20:00 -0700773 return AnimatorInflater.loadAnimator(mActivity, anim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700774 }
775
Adam Powell635c60a2011-10-26 10:22:16 -0700776 public void performPendingDeferredStart(Fragment f) {
777 if (f.mDeferStart) {
Adam Powell78fed9b2011-11-07 10:45:34 -0800778 if (mExecutingActions) {
779 // Wait until we're done executing our pending transactions
780 mHavePendingDeferredStart = true;
781 return;
782 }
Adam Powell635c60a2011-10-26 10:22:16 -0700783 f.mDeferStart = false;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700784 moveToState(f, mCurState, 0, 0, false);
Adam Powell635c60a2011-10-26 10:22:16 -0700785 }
786 }
787
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700788 void moveToState(Fragment f, int newState, int transit, int transitionStyle,
789 boolean keepActive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -0700790 if (DEBUG && false) Log.v(TAG, "moveToState: " + f
791 + " oldState=" + f.mState + " newState=" + newState
792 + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
Craig Mautner1c437192012-08-01 10:01:16 -0700793
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700794 // Fragments that are not currently added will sit in the onCreate() state.
Dianne Hackborne181bd92012-09-25 14:15:15 -0700795 if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700796 newState = Fragment.CREATED;
797 }
Dianne Hackbornf9302322011-06-14 18:36:14 -0700798 if (f.mRemoving && newState > f.mState) {
799 // While removing a fragment, we can't change it to a higher state.
800 newState = f.mState;
801 }
Adam Powell2db4e4b2011-11-02 14:30:47 -0700802 // Defer start if requested; don't allow it to move to STARTED or higher
803 // if it's not already started.
804 if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
Adam Powell635c60a2011-10-26 10:22:16 -0700805 newState = Fragment.STOPPED;
806 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700807 if (f.mState < newState) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -0800808 // For fragments that are created from a layout, when restoring from
809 // state we don't want to allow them to be created until they are
810 // being reloaded from the layout.
811 if (f.mFromLayout && !f.mInLayout) {
812 return;
813 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800814 if (f.mAnimatingAway != null) {
815 // The fragment is currently being animated... but! Now we
816 // want to move our state back up. Give up on waiting for the
817 // animation, move to whatever the final state should be once
818 // the animation is done, and then we can proceed from there.
819 f.mAnimatingAway = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700820 moveToState(f, f.mStateAfterAnimating, 0, 0, true);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800821 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700822 switch (f.mState) {
823 case Fragment.INITIALIZING:
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700824 if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
Dianne Hackborndef15372010-08-15 12:43:52 -0700825 if (f.mSavedFragmentState != null) {
826 f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
827 FragmentManagerImpl.VIEW_STATE_TAG);
828 f.mTarget = getFragment(f.mSavedFragmentState,
829 FragmentManagerImpl.TARGET_STATE_TAG);
830 if (f.mTarget != null) {
831 f.mTargetRequestCode = f.mSavedFragmentState.getInt(
832 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
833 }
Adam Powell78fed9b2011-11-07 10:45:34 -0800834 f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
835 FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
836 if (!f.mUserVisibleHint) {
837 f.mDeferStart = true;
838 if (newState > Fragment.STOPPED) {
839 newState = Fragment.STOPPED;
840 }
841 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700842 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700843 f.mActivity = mActivity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700844 f.mParentFragment = mParent;
845 f.mFragmentManager = mParent != null
846 ? mParent.mChildFragmentManager : mActivity.mFragments;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700847 f.mCalled = false;
848 f.onAttach(mActivity);
849 if (!f.mCalled) {
850 throw new SuperNotCalledException("Fragment " + f
851 + " did not call through to super.onAttach()");
852 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700853 if (f.mParentFragment == null) {
854 mActivity.onAttachFragment(f);
855 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700856
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700857 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700858 f.performCreate(f.mSavedFragmentState);
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700859 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700860 f.mRetaining = false;
861 if (f.mFromLayout) {
862 // For fragments that are part of the content view
863 // layout, we need to instantiate the view immediately
864 // and the inflater will take care of adding it.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700865 f.mView = f.performCreateView(f.getLayoutInflater(
866 f.mSavedFragmentState), null, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700867 if (f.mView != null) {
868 f.mView.setSaveFromParentEnabled(false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700869 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700870 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700871 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700872 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700873 case Fragment.CREATED:
874 if (newState > Fragment.CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700875 if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700876 if (!f.mFromLayout) {
877 ViewGroup container = null;
878 if (f.mContainerId != 0) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700879 container = (ViewGroup)mContainer.findViewById(f.mContainerId);
Dianne Hackborn352cc982011-01-04 11:34:18 -0800880 if (container == null && !f.mRestored) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700881 throwException(new IllegalArgumentException(
882 "No view found for id 0x"
883 + Integer.toHexString(f.mContainerId) + " ("
884 + f.getResources().getResourceName(f.mContainerId)
885 + ") for fragment " + f));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700886 }
887 }
888 f.mContainer = container;
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700889 f.mView = f.performCreateView(f.getLayoutInflater(
890 f.mSavedFragmentState), container, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700891 if (f.mView != null) {
892 f.mView.setSaveFromParentEnabled(false);
893 if (container != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700894 Animator anim = loadAnimator(f, transit, true,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700895 transitionStyle);
896 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700897 anim.setTarget(f.mView);
Chet Haase811ed1062010-08-06 10:38:15 -0700898 anim.start();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700899 }
900 container.addView(f.mView);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700901 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700902 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700903 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700904 }
905 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700906
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700907 f.performActivityCreated(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700908 if (f.mView != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700909 f.restoreViewState(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700910 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700911 f.mSavedFragmentState = null;
912 }
Dianne Hackbornc8017682010-07-06 13:34:38 -0700913 case Fragment.ACTIVITY_CREATED:
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700914 case Fragment.STOPPED:
915 if (newState > Fragment.STOPPED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700916 if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700917 f.performStart();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700918 }
919 case Fragment.STARTED:
920 if (newState > Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700921 if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700922 f.mResumed = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700923 f.performResume();
Adam Powell95202512011-08-07 17:20:17 -0700924 // Get rid of this in case we saved it and never needed it.
925 f.mSavedFragmentState = null;
926 f.mSavedViewState = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700927 }
928 }
929 } else if (f.mState > newState) {
930 switch (f.mState) {
931 case Fragment.RESUMED:
932 if (newState < Fragment.RESUMED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700933 if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700934 f.performPause();
Dianne Hackborn2707d602010-07-09 18:01:20 -0700935 f.mResumed = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700936 }
937 case Fragment.STARTED:
938 if (newState < Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700939 if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700940 f.performStop();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700941 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700942 case Fragment.STOPPED:
Dianne Hackbornc8017682010-07-06 13:34:38 -0700943 case Fragment.ACTIVITY_CREATED:
944 if (newState < Fragment.ACTIVITY_CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700945 if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700946 if (f.mView != null) {
947 // Need to save the current view state if not
948 // done already.
Dianne Hackborn625ac272010-09-17 18:29:22 -0700949 if (!mActivity.isFinishing() && f.mSavedViewState == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700950 saveFragmentViewState(f);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700951 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700952 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700953 f.performDestroyView();
Dianne Hackborndef15372010-08-15 12:43:52 -0700954 if (f.mView != null && f.mContainer != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700955 Animator anim = null;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800956 if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700957 anim = loadAnimator(f, transit, false,
Dianne Hackborndef15372010-08-15 12:43:52 -0700958 transitionStyle);
Chet Haaseb20db3e2010-09-10 13:07:30 -0700959 }
960 if (anim != null) {
961 final ViewGroup container = f.mContainer;
962 final View view = f.mView;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800963 final Fragment fragment = f;
Chet Haaseb20db3e2010-09-10 13:07:30 -0700964 container.startViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800965 f.mAnimatingAway = anim;
966 f.mStateAfterAnimating = newState;
Chet Haaseb20db3e2010-09-10 13:07:30 -0700967 anim.addListener(new AnimatorListenerAdapter() {
968 @Override
969 public void onAnimationEnd(Animator anim) {
970 container.endViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800971 if (fragment.mAnimatingAway != null) {
972 fragment.mAnimatingAway = null;
973 moveToState(fragment, fragment.mStateAfterAnimating,
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700974 0, 0, false);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800975 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700976 }
Chet Haaseb20db3e2010-09-10 13:07:30 -0700977 });
978 anim.setTarget(f.mView);
979 anim.start();
980
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700981 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700982 f.mContainer.removeView(f.mView);
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700983 }
984 f.mContainer = null;
985 f.mView = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700986 }
987 case Fragment.CREATED:
988 if (newState < Fragment.CREATED) {
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800989 if (mDestroyed) {
990 if (f.mAnimatingAway != null) {
991 // The fragment's containing activity is
992 // being destroyed, but this fragment is
993 // currently animating away. Stop the
994 // animation right now -- it is not needed,
995 // and we can't wait any more on destroying
996 // the fragment.
Dianne Hackborn1b39e222010-12-28 14:17:18 -0800997 Animator anim = f.mAnimatingAway;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800998 f.mAnimatingAway = null;
Dianne Hackborn1b39e222010-12-28 14:17:18 -0800999 anim.cancel();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001000 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001001 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001002 if (f.mAnimatingAway != null) {
1003 // We are waiting for the fragment's view to finish
1004 // animating away. Just make a note of the state
1005 // the fragment now should move to once the animation
1006 // is done.
1007 f.mStateAfterAnimating = newState;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001008 newState = Fragment.CREATED;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001009 } else {
1010 if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1011 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001012 f.performDestroy();
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001013 }
1014
1015 f.mCalled = false;
1016 f.onDetach();
1017 if (!f.mCalled) {
1018 throw new SuperNotCalledException("Fragment " + f
1019 + " did not call through to super.onDetach()");
1020 }
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001021 if (!keepActive) {
1022 if (!f.mRetaining) {
1023 makeInactive(f);
1024 } else {
1025 f.mActivity = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001026 f.mParentFragment = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001027 f.mFragmentManager = null;
1028 }
Dianne Hackbornf9302322011-06-14 18:36:14 -07001029 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001030 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001031 }
1032 }
1033 }
1034
1035 f.mState = newState;
1036 }
1037
Dianne Hackborn625ac272010-09-17 18:29:22 -07001038 void moveToState(Fragment f) {
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001039 moveToState(f, mCurState, 0, 0, false);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001040 }
1041
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001042 void moveToState(int newState, boolean always) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001043 moveToState(newState, 0, 0, always);
1044 }
1045
1046 void moveToState(int newState, int transit, int transitStyle, boolean always) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001047 if (mActivity == null && newState != Fragment.INITIALIZING) {
1048 throw new IllegalStateException("No activity");
1049 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001050
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001051 if (!always && mCurState == newState) {
1052 return;
1053 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001054
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001055 mCurState = newState;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001056 if (mActive != null) {
Adam Powell635c60a2011-10-26 10:22:16 -07001057 boolean loadersRunning = false;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001058 for (int i=0; i<mActive.size(); i++) {
1059 Fragment f = mActive.get(i);
1060 if (f != null) {
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001061 moveToState(f, newState, transit, transitStyle, false);
Adam Powell635c60a2011-10-26 10:22:16 -07001062 if (f.mLoaderManager != null) {
1063 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1064 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001065 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001066 }
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001067
Adam Powell635c60a2011-10-26 10:22:16 -07001068 if (!loadersRunning) {
1069 startPendingDeferredFragments();
1070 }
1071
Adam Powell89b09da2011-07-27 11:55:29 -07001072 if (mNeedMenuInvalidate && mActivity != null && mCurState == Fragment.RESUMED) {
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001073 mActivity.invalidateOptionsMenu();
1074 mNeedMenuInvalidate = false;
1075 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001076 }
1077 }
1078
Adam Powell635c60a2011-10-26 10:22:16 -07001079 void startPendingDeferredFragments() {
Adam Powell37510902011-10-31 11:48:24 -07001080 if (mActive == null) return;
1081
Adam Powell635c60a2011-10-26 10:22:16 -07001082 for (int i=0; i<mActive.size(); i++) {
1083 Fragment f = mActive.get(i);
1084 if (f != null) {
1085 performPendingDeferredStart(f);
1086 }
1087 }
1088 }
1089
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001090 void makeActive(Fragment f) {
1091 if (f.mIndex >= 0) {
1092 return;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001093 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001094
1095 if (mAvailIndices == null || mAvailIndices.size() <= 0) {
1096 if (mActive == null) {
1097 mActive = new ArrayList<Fragment>();
1098 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001099 f.setIndex(mActive.size(), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001100 mActive.add(f);
1101
1102 } else {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001103 f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001104 mActive.set(f.mIndex, f);
1105 }
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001106 if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001107 }
1108
1109 void makeInactive(Fragment f) {
1110 if (f.mIndex < 0) {
1111 return;
1112 }
1113
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001114 if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001115 mActive.set(f.mIndex, null);
1116 if (mAvailIndices == null) {
1117 mAvailIndices = new ArrayList<Integer>();
1118 }
1119 mAvailIndices.add(f.mIndex);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001120 mActivity.invalidateFragment(f.mWho);
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001121 f.initState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001122 }
1123
1124 public void addFragment(Fragment fragment, boolean moveToStateNow) {
1125 if (mAdded == null) {
1126 mAdded = new ArrayList<Fragment>();
1127 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001128 if (DEBUG) Log.v(TAG, "add: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001129 makeActive(fragment);
1130 if (!fragment.mDetached) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001131 if (mAdded.contains(fragment)) {
1132 throw new IllegalStateException("Fragment already added: " + fragment);
1133 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001134 mAdded.add(fragment);
1135 fragment.mAdded = true;
1136 fragment.mRemoving = false;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001137 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001138 mNeedMenuInvalidate = true;
1139 }
1140 if (moveToStateNow) {
1141 moveToState(fragment);
1142 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001143 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001144 }
1145
Dianne Hackbornf121be72010-05-06 14:10:32 -07001146 public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001147 if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001148 final boolean inactive = !fragment.isInBackStack();
1149 if (!fragment.mDetached || inactive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001150 if (false) {
1151 // Would be nice to catch a bad remove here, but we need
1152 // time to test this to make sure we aren't crashes cases
1153 // where it is not a problem.
1154 if (!mAdded.contains(fragment)) {
1155 throw new IllegalStateException("Fragment not added: " + fragment);
1156 }
1157 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001158 if (mAdded != null) {
1159 mAdded.remove(fragment);
1160 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001161 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001162 mNeedMenuInvalidate = true;
1163 }
1164 fragment.mAdded = false;
1165 fragment.mRemoving = true;
1166 moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED,
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001167 transition, transitionStyle, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001168 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07001169 }
1170
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001171 public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
1172 if (DEBUG) Log.v(TAG, "hide: " + fragment);
1173 if (!fragment.mHidden) {
1174 fragment.mHidden = true;
1175 if (fragment.mView != null) {
Adam Powell27562932013-06-07 10:16:08 -07001176 Animator anim = loadAnimator(fragment, transition, false,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001177 transitionStyle);
1178 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001179 anim.setTarget(fragment.mView);
Chet Haase61eb40d2010-12-28 13:59:17 -08001180 // Delay the actual hide operation until the animation finishes, otherwise
1181 // the fragment will just immediately disappear
1182 final Fragment finalFragment = fragment;
1183 anim.addListener(new AnimatorListenerAdapter() {
1184 @Override
1185 public void onAnimationEnd(Animator animation) {
Chet Haaseb29407f2011-01-11 14:09:34 -08001186 if (finalFragment.mView != null) {
1187 finalFragment.mView.setVisibility(View.GONE);
1188 }
Chet Haase61eb40d2010-12-28 13:59:17 -08001189 }
1190 });
Chet Haase811ed1062010-08-06 10:38:15 -07001191 anim.start();
Chet Haasee646b28c2010-12-28 14:48:32 -08001192 } else {
1193 fragment.mView.setVisibility(View.GONE);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001194 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001195 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001196 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001197 mNeedMenuInvalidate = true;
1198 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001199 fragment.onHiddenChanged(true);
1200 }
1201 }
1202
1203 public void showFragment(Fragment fragment, int transition, int transitionStyle) {
1204 if (DEBUG) Log.v(TAG, "show: " + fragment);
1205 if (fragment.mHidden) {
1206 fragment.mHidden = false;
1207 if (fragment.mView != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -07001208 Animator anim = loadAnimator(fragment, transition, true,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001209 transitionStyle);
1210 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001211 anim.setTarget(fragment.mView);
Chet Haase811ed1062010-08-06 10:38:15 -07001212 anim.start();
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001213 }
1214 fragment.mView.setVisibility(View.VISIBLE);
1215 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001216 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001217 mNeedMenuInvalidate = true;
1218 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001219 fragment.onHiddenChanged(false);
1220 }
1221 }
1222
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001223 public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
1224 if (DEBUG) Log.v(TAG, "detach: " + fragment);
1225 if (!fragment.mDetached) {
1226 fragment.mDetached = true;
1227 if (fragment.mAdded) {
1228 // We are not already in back stack, so need to remove the fragment.
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001229 if (mAdded != null) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001230 if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001231 mAdded.remove(fragment);
1232 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001233 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001234 mNeedMenuInvalidate = true;
1235 }
1236 fragment.mAdded = false;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001237 moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001238 }
1239 }
1240 }
1241
1242 public void attachFragment(Fragment fragment, int transition, int transitionStyle) {
1243 if (DEBUG) Log.v(TAG, "attach: " + fragment);
1244 if (fragment.mDetached) {
1245 fragment.mDetached = false;
1246 if (!fragment.mAdded) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001247 if (mAdded == null) {
1248 mAdded = new ArrayList<Fragment>();
1249 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001250 if (mAdded.contains(fragment)) {
1251 throw new IllegalStateException("Fragment already added: " + fragment);
1252 }
1253 if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001254 mAdded.add(fragment);
1255 fragment.mAdded = true;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001256 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001257 mNeedMenuInvalidate = true;
1258 }
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001259 moveToState(fragment, mCurState, transition, transitionStyle, false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001260 }
1261 }
1262 }
1263
Dianne Hackbornf121be72010-05-06 14:10:32 -07001264 public Fragment findFragmentById(int id) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001265 if (mAdded != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001266 // First look through added fragments.
1267 for (int i=mAdded.size()-1; i>=0; i--) {
1268 Fragment f = mAdded.get(i);
1269 if (f != null && f.mFragmentId == id) {
1270 return f;
1271 }
1272 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001273 }
1274 if (mActive != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001275 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001276 for (int i=mActive.size()-1; i>=0; i--) {
1277 Fragment f = mActive.get(i);
1278 if (f != null && f.mFragmentId == id) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001279 return f;
1280 }
1281 }
1282 }
1283 return null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001284 }
1285
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001286 public Fragment findFragmentByTag(String tag) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001287 if (mAdded != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001288 // First look through added fragments.
1289 for (int i=mAdded.size()-1; i>=0; i--) {
1290 Fragment f = mAdded.get(i);
1291 if (f != null && tag.equals(f.mTag)) {
1292 return f;
1293 }
1294 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001295 }
1296 if (mActive != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001297 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001298 for (int i=mActive.size()-1; i>=0; i--) {
1299 Fragment f = mActive.get(i);
1300 if (f != null && tag.equals(f.mTag)) {
1301 return f;
1302 }
1303 }
1304 }
1305 return null;
1306 }
1307
1308 public Fragment findFragmentByWho(String who) {
1309 if (mActive != null && who != null) {
1310 for (int i=mActive.size()-1; i>=0; i--) {
1311 Fragment f = mActive.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001312 if (f != null && (f=f.findFragmentByWho(who)) != null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001313 return f;
1314 }
1315 }
1316 }
1317 return null;
1318 }
1319
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001320 private void checkStateLoss() {
1321 if (mStateSaved) {
1322 throw new IllegalStateException(
1323 "Can not perform this action after onSaveInstanceState");
1324 }
1325 if (mNoTransactionsBecause != null) {
1326 throw new IllegalStateException(
1327 "Can not perform this action inside of " + mNoTransactionsBecause);
1328 }
1329 }
1330
Dianne Hackbornab36acb2010-11-05 14:12:11 -07001331 public void enqueueAction(Runnable action, boolean allowStateLoss) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001332 if (!allowStateLoss) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001333 checkStateLoss();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001334 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001335 synchronized (this) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001336 if (mActivity == null) {
1337 throw new IllegalStateException("Activity has been destroyed");
1338 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001339 if (mPendingActions == null) {
1340 mPendingActions = new ArrayList<Runnable>();
1341 }
1342 mPendingActions.add(action);
1343 if (mPendingActions.size() == 1) {
1344 mActivity.mHandler.removeCallbacks(mExecCommit);
1345 mActivity.mHandler.post(mExecCommit);
1346 }
1347 }
1348 }
1349
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001350 public int allocBackStackIndex(BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001351 synchronized (this) {
1352 if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1353 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001354 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001355 }
1356 int index = mBackStackIndices.size();
1357 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1358 mBackStackIndices.add(bse);
1359 return index;
1360
1361 } else {
1362 int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1363 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1364 mBackStackIndices.set(index, bse);
1365 return index;
1366 }
1367 }
1368 }
1369
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001370 public void setBackStackIndex(int index, BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001371 synchronized (this) {
1372 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001373 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001374 }
1375 int N = mBackStackIndices.size();
1376 if (index < N) {
1377 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1378 mBackStackIndices.set(index, bse);
1379 } else {
1380 while (N < index) {
1381 mBackStackIndices.add(null);
1382 if (mAvailBackStackIndices == null) {
1383 mAvailBackStackIndices = new ArrayList<Integer>();
1384 }
1385 if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1386 mAvailBackStackIndices.add(N);
1387 N++;
1388 }
1389 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1390 mBackStackIndices.add(bse);
1391 }
1392 }
1393 }
1394
1395 public void freeBackStackIndex(int index) {
1396 synchronized (this) {
1397 mBackStackIndices.set(index, null);
1398 if (mAvailBackStackIndices == null) {
1399 mAvailBackStackIndices = new ArrayList<Integer>();
1400 }
1401 if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1402 mAvailBackStackIndices.add(index);
1403 }
1404 }
1405
Dianne Hackborn445646c2010-06-25 15:52:59 -07001406 /**
1407 * Only call from main thread!
1408 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001409 public boolean execPendingActions() {
Dianne Hackborn445646c2010-06-25 15:52:59 -07001410 if (mExecutingActions) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001411 throw new IllegalStateException("Recursive entry to executePendingTransactions");
Dianne Hackborn445646c2010-06-25 15:52:59 -07001412 }
1413
Dianne Hackbornd9b3b7e2010-11-16 18:22:49 -08001414 if (Looper.myLooper() != mActivity.mHandler.getLooper()) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001415 throw new IllegalStateException("Must be called from main thread of process");
1416 }
1417
1418 boolean didSomething = false;
1419
Dianne Hackborn445646c2010-06-25 15:52:59 -07001420 while (true) {
1421 int numActions;
1422
1423 synchronized (this) {
1424 if (mPendingActions == null || mPendingActions.size() == 0) {
Adam Powell78fed9b2011-11-07 10:45:34 -08001425 break;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001426 }
1427
1428 numActions = mPendingActions.size();
1429 if (mTmpActions == null || mTmpActions.length < numActions) {
1430 mTmpActions = new Runnable[numActions];
1431 }
1432 mPendingActions.toArray(mTmpActions);
1433 mPendingActions.clear();
1434 mActivity.mHandler.removeCallbacks(mExecCommit);
1435 }
1436
1437 mExecutingActions = true;
1438 for (int i=0; i<numActions; i++) {
1439 mTmpActions[i].run();
Jeff Sharkey0d325282011-07-13 09:36:27 -07001440 mTmpActions[i] = null;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001441 }
1442 mExecutingActions = false;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001443 didSomething = true;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001444 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001445
1446 if (mHavePendingDeferredStart) {
1447 boolean loadersRunning = false;
1448 for (int i=0; i<mActive.size(); i++) {
1449 Fragment f = mActive.get(i);
1450 if (f != null && f.mLoaderManager != null) {
1451 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1452 }
1453 }
1454 if (!loadersRunning) {
1455 mHavePendingDeferredStart = false;
1456 startPendingDeferredFragments();
1457 }
1458 }
1459 return didSomething;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001460 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001461
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001462 void reportBackStackChanged() {
1463 if (mBackStackChangeListeners != null) {
1464 for (int i=0; i<mBackStackChangeListeners.size(); i++) {
1465 mBackStackChangeListeners.get(i).onBackStackChanged();
1466 }
1467 }
1468 }
1469
1470 void addBackStackState(BackStackRecord state) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001471 if (mBackStack == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001472 mBackStack = new ArrayList<BackStackRecord>();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001473 }
1474 mBackStack.add(state);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001475 reportBackStackChanged();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001476 }
1477
Dianne Hackborndd913a52010-07-22 12:17:04 -07001478 boolean popBackStackState(Handler handler, String name, int id, int flags) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001479 if (mBackStack == null) {
1480 return false;
1481 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001482 if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001483 int last = mBackStack.size()-1;
1484 if (last < 0) {
1485 return false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001486 }
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001487 final BackStackRecord bss = mBackStack.remove(last);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001488 bss.popFromBackStack(true);
1489 reportBackStackChanged();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001490 } else {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001491 int index = -1;
1492 if (name != null || id >= 0) {
1493 // If a name or ID is specified, look for that place in
1494 // the stack.
1495 index = mBackStack.size()-1;
1496 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001497 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001498 if (name != null && name.equals(bss.getName())) {
1499 break;
1500 }
1501 if (id >= 0 && id == bss.mIndex) {
1502 break;
1503 }
1504 index--;
Dianne Hackbornf121be72010-05-06 14:10:32 -07001505 }
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001506 if (index < 0) {
1507 return false;
Dianne Hackborndd913a52010-07-22 12:17:04 -07001508 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001509 if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001510 index--;
1511 // Consume all following entries that match.
1512 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001513 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001514 if ((name != null && name.equals(bss.getName()))
1515 || (id >= 0 && id == bss.mIndex)) {
1516 index--;
1517 continue;
1518 }
1519 break;
1520 }
1521 }
Dianne Hackborndd913a52010-07-22 12:17:04 -07001522 }
1523 if (index == mBackStack.size()-1) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001524 return false;
1525 }
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001526 final ArrayList<BackStackRecord> states
1527 = new ArrayList<BackStackRecord>();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001528 for (int i=mBackStack.size()-1; i>index; i--) {
1529 states.add(mBackStack.remove(i));
1530 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001531 final int LAST = states.size()-1;
1532 for (int i=0; i<=LAST; i++) {
1533 if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i));
1534 states.get(i).popFromBackStack(i == LAST);
1535 }
1536 reportBackStackChanged();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001537 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001538 return true;
1539 }
1540
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001541 ArrayList<Fragment> retainNonConfig() {
1542 ArrayList<Fragment> fragments = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001543 if (mActive != null) {
1544 for (int i=0; i<mActive.size(); i++) {
1545 Fragment f = mActive.get(i);
1546 if (f != null && f.mRetainInstance) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001547 if (fragments == null) {
1548 fragments = new ArrayList<Fragment>();
1549 }
1550 fragments.add(f);
1551 f.mRetaining = true;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001552 f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001553 if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001554 }
1555 }
1556 }
1557 return fragments;
1558 }
1559
1560 void saveFragmentViewState(Fragment f) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001561 if (f.mView == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001562 return;
1563 }
1564 if (mStateArray == null) {
1565 mStateArray = new SparseArray<Parcelable>();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001566 } else {
1567 mStateArray.clear();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001568 }
1569 f.mView.saveHierarchyState(mStateArray);
1570 if (mStateArray.size() > 0) {
1571 f.mSavedViewState = mStateArray;
1572 mStateArray = null;
1573 }
1574 }
1575
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001576 Bundle saveFragmentBasicState(Fragment f) {
1577 Bundle result = null;
1578
1579 if (mStateBundle == null) {
1580 mStateBundle = new Bundle();
1581 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001582 f.performSaveInstanceState(mStateBundle);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001583 if (!mStateBundle.isEmpty()) {
1584 result = mStateBundle;
1585 mStateBundle = null;
1586 }
1587
1588 if (f.mView != null) {
1589 saveFragmentViewState(f);
Dianne Hackborn13332762011-06-03 17:34:45 -07001590 }
1591 if (f.mSavedViewState != null) {
1592 if (result == null) {
1593 result = new Bundle();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001594 }
Dianne Hackborn13332762011-06-03 17:34:45 -07001595 result.putSparseParcelableArray(
1596 FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001597 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001598 if (!f.mUserVisibleHint) {
Jake Wharton258029e2012-04-22 17:17:01 -04001599 if (result == null) {
1600 result = new Bundle();
1601 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001602 // Only add this if it's not the default value
1603 result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
1604 }
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001605
1606 return result;
1607 }
1608
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001609 Parcelable saveAllState() {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001610 // Make sure all pending operations have now been executed to get
1611 // our state update-to-date.
1612 execPendingActions();
1613
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001614 mStateSaved = true;
1615
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001616 if (mActive == null || mActive.size() <= 0) {
1617 return null;
1618 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001619
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001620 // First collect all active fragments.
1621 int N = mActive.size();
1622 FragmentState[] active = new FragmentState[N];
1623 boolean haveFragments = false;
1624 for (int i=0; i<N; i++) {
1625 Fragment f = mActive.get(i);
1626 if (f != null) {
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001627 if (f.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001628 throwException(new IllegalStateException(
1629 "Failure saving state: active " + f
1630 + " has cleared index: " + f.mIndex));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001631 }
1632
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001633 haveFragments = true;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001634
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001635 FragmentState fs = new FragmentState(f);
1636 active[i] = fs;
1637
Dianne Hackborn625ac272010-09-17 18:29:22 -07001638 if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001639 fs.mSavedFragmentState = saveFragmentBasicState(f);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001640
1641 if (f.mTarget != null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001642 if (f.mTarget.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001643 throwException(new IllegalStateException(
1644 "Failure saving state: " + f
1645 + " has target not in fragment manager: " + f.mTarget));
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001646 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001647 if (fs.mSavedFragmentState == null) {
1648 fs.mSavedFragmentState = new Bundle();
1649 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001650 putFragment(fs.mSavedFragmentState,
1651 FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
1652 if (f.mTargetRequestCode != 0) {
1653 fs.mSavedFragmentState.putInt(
1654 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
1655 f.mTargetRequestCode);
1656 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001657 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001658
Dianne Hackborn625ac272010-09-17 18:29:22 -07001659 } else {
1660 fs.mSavedFragmentState = f.mSavedFragmentState;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001661 }
1662
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001663 if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
1664 + fs.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001665 }
1666 }
1667
1668 if (!haveFragments) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001669 if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001670 return null;
1671 }
1672
1673 int[] added = null;
1674 BackStackState[] backStack = null;
1675
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001676 // Build list of currently added fragments.
Dianne Hackborn625ac272010-09-17 18:29:22 -07001677 if (mAdded != null) {
1678 N = mAdded.size();
1679 if (N > 0) {
1680 added = new int[N];
1681 for (int i=0; i<N; i++) {
1682 added[i] = mAdded.get(i).mIndex;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001683 if (added[i] < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001684 throwException(new IllegalStateException(
1685 "Failure saving state: active " + mAdded.get(i)
1686 + " has cleared index: " + added[i]));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001687 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001688 if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
1689 + ": " + mAdded.get(i));
1690 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001691 }
1692 }
1693
1694 // Now save back stack.
1695 if (mBackStack != null) {
1696 N = mBackStack.size();
1697 if (N > 0) {
1698 backStack = new BackStackState[N];
1699 for (int i=0; i<N; i++) {
1700 backStack[i] = new BackStackState(this, mBackStack.get(i));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001701 if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
1702 + ": " + mBackStack.get(i));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001703 }
1704 }
1705 }
1706
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001707 FragmentManagerState fms = new FragmentManagerState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001708 fms.mActive = active;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001709 fms.mAdded = added;
1710 fms.mBackStack = backStack;
1711 return fms;
1712 }
1713
1714 void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) {
1715 // If there is no saved state at all, then there can not be
1716 // any nonConfig fragments either, so that is that.
1717 if (state == null) return;
1718 FragmentManagerState fms = (FragmentManagerState)state;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001719 if (fms.mActive == null) return;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001720
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001721 // First re-attach any non-config instances we are retaining back
1722 // to their saved state, so we don't try to instantiate them again.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001723 if (nonConfig != null) {
1724 for (int i=0; i<nonConfig.size(); i++) {
1725 Fragment f = nonConfig.get(i);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001726 if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001727 FragmentState fs = fms.mActive[f.mIndex];
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001728 fs.mInstance = f;
1729 f.mSavedViewState = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001730 f.mBackStackNesting = 0;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001731 f.mInLayout = false;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001732 f.mAdded = false;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001733 f.mTarget = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001734 if (fs.mSavedFragmentState != null) {
Dianne Hackborn51642462010-10-28 10:32:37 -07001735 fs.mSavedFragmentState.setClassLoader(mActivity.getClassLoader());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001736 f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
Dianne Hackborndef15372010-08-15 12:43:52 -07001737 FragmentManagerImpl.VIEW_STATE_TAG);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001738 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001739 }
1740 }
1741
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001742 // Build the full list of active fragments, instantiating them from
1743 // their saved state.
1744 mActive = new ArrayList<Fragment>(fms.mActive.length);
1745 if (mAvailIndices != null) {
1746 mAvailIndices.clear();
1747 }
1748 for (int i=0; i<fms.mActive.length; i++) {
1749 FragmentState fs = fms.mActive[i];
1750 if (fs != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001751 Fragment f = fs.instantiate(mActivity, mParent);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001752 if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001753 mActive.add(f);
Dianne Hackborn30d71892010-12-11 10:37:55 -08001754 // Now that the fragment is instantiated (or came from being
1755 // retained above), clear mInstance in case we end up re-restoring
1756 // from this FragmentState again.
1757 fs.mInstance = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001758 } else {
1759 mActive.add(null);
1760 if (mAvailIndices == null) {
1761 mAvailIndices = new ArrayList<Integer>();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001762 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001763 if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001764 mAvailIndices.add(i);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001765 }
1766 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001767
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001768 // Update the target of all retained fragments.
1769 if (nonConfig != null) {
1770 for (int i=0; i<nonConfig.size(); i++) {
1771 Fragment f = nonConfig.get(i);
Dianne Hackbornf9302322011-06-14 18:36:14 -07001772 if (f.mTargetIndex >= 0) {
1773 if (f.mTargetIndex < mActive.size()) {
1774 f.mTarget = mActive.get(f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001775 } else {
1776 Log.w(TAG, "Re-attaching retained fragment " + f
Dianne Hackbornf9302322011-06-14 18:36:14 -07001777 + " target no longer exists: " + f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001778 f.mTarget = null;
1779 }
1780 }
1781 }
1782 }
1783
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001784 // Build the list of currently added fragments.
1785 if (fms.mAdded != null) {
1786 mAdded = new ArrayList<Fragment>(fms.mAdded.length);
1787 for (int i=0; i<fms.mAdded.length; i++) {
1788 Fragment f = mActive.get(fms.mAdded[i]);
1789 if (f == null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001790 throwException(new IllegalStateException(
1791 "No instantiated fragment for index #" + fms.mAdded[i]));
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001792 }
1793 f.mAdded = true;
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001794 if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
1795 if (mAdded.contains(f)) {
1796 throw new IllegalStateException("Already added!");
1797 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001798 mAdded.add(f);
1799 }
1800 } else {
1801 mAdded = null;
1802 }
1803
1804 // Build the back stack.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001805 if (fms.mBackStack != null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001806 mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001807 for (int i=0; i<fms.mBackStack.length; i++) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001808 BackStackRecord bse = fms.mBackStack[i].instantiate(this);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001809 if (DEBUG) {
1810 Log.v(TAG, "restoreAllState: back stack #" + i
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001811 + " (index " + bse.mIndex + "): " + bse);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001812 LogWriter logw = new LogWriter(Log.VERBOSE, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001813 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001814 bse.dump(" ", pw, false);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001815 pw.flush();
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001816 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001817 mBackStack.add(bse);
Dianne Hackborndd913a52010-07-22 12:17:04 -07001818 if (bse.mIndex >= 0) {
1819 setBackStackIndex(bse.mIndex, bse);
1820 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001821 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001822 } else {
1823 mBackStack = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001824 }
1825 }
1826
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001827 public void attachActivity(Activity activity, FragmentContainer container, Fragment parent) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001828 if (mActivity != null) throw new IllegalStateException("Already attached");
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001829 mActivity = activity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001830 mContainer = container;
1831 mParent = parent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001832 }
1833
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001834 public void noteStateNotSaved() {
1835 mStateSaved = false;
1836 }
1837
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001838 public void dispatchCreate() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001839 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001840 moveToState(Fragment.CREATED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001841 }
1842
Dianne Hackbornc8017682010-07-06 13:34:38 -07001843 public void dispatchActivityCreated() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001844 mStateSaved = false;
Dianne Hackbornc8017682010-07-06 13:34:38 -07001845 moveToState(Fragment.ACTIVITY_CREATED, false);
1846 }
1847
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001848 public void dispatchStart() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001849 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001850 moveToState(Fragment.STARTED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001851 }
1852
1853 public void dispatchResume() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001854 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001855 moveToState(Fragment.RESUMED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001856 }
1857
1858 public void dispatchPause() {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001859 moveToState(Fragment.STARTED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001860 }
1861
1862 public void dispatchStop() {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001863 moveToState(Fragment.STOPPED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001864 }
1865
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001866 public void dispatchDestroyView() {
1867 moveToState(Fragment.CREATED, false);
1868 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001869
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001870 public void dispatchDestroy() {
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001871 mDestroyed = true;
Dianne Hackbornc6938232011-07-21 16:25:26 -07001872 execPendingActions();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001873 moveToState(Fragment.INITIALIZING, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001874 mActivity = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001875 mContainer = null;
1876 mParent = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001877 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07001878
Dianne Hackborn9d071802010-12-08 14:49:15 -08001879 public void dispatchConfigurationChanged(Configuration newConfig) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001880 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08001881 for (int i=0; i<mAdded.size(); i++) {
1882 Fragment f = mAdded.get(i);
1883 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001884 f.performConfigurationChanged(newConfig);
Dianne Hackborn9d071802010-12-08 14:49:15 -08001885 }
1886 }
1887 }
1888 }
1889
1890 public void dispatchLowMemory() {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001891 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08001892 for (int i=0; i<mAdded.size(); i++) {
1893 Fragment f = mAdded.get(i);
1894 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001895 f.performLowMemory();
Dianne Hackborn9d071802010-12-08 14:49:15 -08001896 }
1897 }
1898 }
1899 }
1900
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001901 public void dispatchTrimMemory(int level) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001902 if (mAdded != null) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001903 for (int i=0; i<mAdded.size(); i++) {
1904 Fragment f = mAdded.get(i);
1905 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001906 f.performTrimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001907 }
1908 }
1909 }
1910 }
1911
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001912 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1913 boolean show = false;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001914 ArrayList<Fragment> newMenus = null;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001915 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001916 for (int i=0; i<mAdded.size(); i++) {
1917 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001918 if (f != null) {
1919 if (f.performCreateOptionsMenu(menu, inflater)) {
1920 show = true;
1921 if (newMenus == null) {
1922 newMenus = new ArrayList<Fragment>();
1923 }
1924 newMenus.add(f);
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001925 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001926 }
1927 }
1928 }
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001929
1930 if (mCreatedMenus != null) {
1931 for (int i=0; i<mCreatedMenus.size(); i++) {
1932 Fragment f = mCreatedMenus.get(i);
1933 if (newMenus == null || !newMenus.contains(f)) {
1934 f.onDestroyOptionsMenu();
1935 }
1936 }
1937 }
1938
1939 mCreatedMenus = newMenus;
1940
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001941 return show;
1942 }
1943
1944 public boolean dispatchPrepareOptionsMenu(Menu menu) {
1945 boolean show = false;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001946 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001947 for (int i=0; i<mAdded.size(); i++) {
1948 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001949 if (f != null) {
1950 if (f.performPrepareOptionsMenu(menu)) {
1951 show = true;
1952 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001953 }
1954 }
1955 }
1956 return show;
1957 }
1958
1959 public boolean dispatchOptionsItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001960 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001961 for (int i=0; i<mAdded.size(); i++) {
1962 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001963 if (f != null) {
1964 if (f.performOptionsItemSelected(item)) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001965 return true;
1966 }
1967 }
1968 }
1969 }
1970 return false;
1971 }
1972
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001973 public boolean dispatchContextItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001974 if (mAdded != null) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001975 for (int i=0; i<mAdded.size(); i++) {
1976 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001977 if (f != null) {
1978 if (f.performContextItemSelected(item)) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001979 return true;
1980 }
1981 }
1982 }
1983 }
1984 return false;
1985 }
1986
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001987 public void dispatchOptionsMenuClosed(Menu menu) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001988 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001989 for (int i=0; i<mAdded.size(); i++) {
1990 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001991 if (f != null) {
1992 f.performOptionsMenuClosed(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001993 }
1994 }
1995 }
1996 }
Adam Powellf0f5fff2011-08-01 13:42:50 -07001997
1998 @Override
1999 public void invalidateOptionsMenu() {
2000 if (mActivity != null && mCurState == Fragment.RESUMED) {
2001 mActivity.invalidateOptionsMenu();
2002 } else {
2003 mNeedMenuInvalidate = true;
2004 }
2005 }
2006
Dianne Hackbornf121be72010-05-06 14:10:32 -07002007 public static int reverseTransit(int transit) {
2008 int rev = 0;
2009 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002010 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2011 rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002012 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002013 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2014 rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002015 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002016 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2017 rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002018 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002019 }
2020 return rev;
2021
2022 }
2023
2024 public static int transitToStyleIndex(int transit, boolean enter) {
2025 int animAttr = -1;
2026 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002027 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002028 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002029 ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation
2030 : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002031 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002032 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002033 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002034 ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation
2035 : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002036 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002037 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
Chet Haase9ff82bf2010-10-05 14:30:51 -07002038 animAttr = enter
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002039 ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation
2040 : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002041 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002042 }
2043 return animAttr;
2044 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002045}