blob: b8f1962d0b80053a41e121cadea35133c2c77f87 [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;
Adam Powell14874662013-07-18 19:42:41 -070034import android.util.SuperNotCalledException;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070035import android.view.Menu;
36import android.view.MenuInflater;
37import android.view.MenuItem;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070038import android.view.View;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070039import android.view.ViewGroup;
Dianne Hackborn8c841092013-06-24 13:46:13 -070040import com.android.internal.util.FastPrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070041
Dianne Hackborn625ac272010-09-17 18:29:22 -070042import java.io.FileDescriptor;
43import java.io.PrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070044import java.util.ArrayList;
Dianne Hackbornd173fa32010-12-23 13:58:22 -080045import java.util.Arrays;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070046
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070047/**
48 * Interface for interacting with {@link Fragment} objects inside of an
49 * {@link Activity}
Joe Fernandezb54e7a32011-10-03 15:09:50 -070050 *
51 * <div class="special reference">
52 * <h3>Developer Guides</h3>
53 * <p>For more information about using fragments, read the
54 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
55 * </div>
Dianne Hackborn7871bad2011-12-12 15:19:26 -080056 *
57 * While the FragmentManager API was introduced in
58 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
59 * at is also available for use on older platforms through
60 * {@link android.support.v4.app.FragmentActivity}. See the blog post
61 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
62 * Fragments For All</a> for more details.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070063 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070064public abstract class FragmentManager {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070065 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070066 * Representation of an entry on the fragment back stack, as created
67 * with {@link FragmentTransaction#addToBackStack(String)
68 * FragmentTransaction.addToBackStack()}. Entries can later be
Dianne Hackborn327fbd22011-01-17 14:38:50 -080069 * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070070 * FragmentManager.getBackStackEntry()}.
71 *
72 * <p>Note that you should never hold on to a BackStackEntry object;
73 * the identifier as returned by {@link #getId} is the only thing that
74 * will be persisted across activity instances.
75 */
76 public interface BackStackEntry {
77 /**
78 * Return the unique identifier for the entry. This is the only
79 * representation of the entry that will persist across activity
80 * instances.
81 */
82 public int getId();
83
84 /**
Dianne Hackborn6c285972011-08-29 16:53:49 -070085 * Get the name that was supplied to
86 * {@link FragmentTransaction#addToBackStack(String)
87 * FragmentTransaction.addToBackStack(String)} when creating this entry.
88 */
89 public String getName();
90
91 /**
Dianne Hackborn327fbd22011-01-17 14:38:50 -080092 * Return the full bread crumb title resource identifier for the entry,
93 * or 0 if it does not have one.
94 */
95 public int getBreadCrumbTitleRes();
96
97 /**
98 * Return the short bread crumb title resource identifier for the entry,
99 * or 0 if it does not have one.
100 */
101 public int getBreadCrumbShortTitleRes();
102
103 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700104 * Return the full bread crumb title for the entry, or null if it
105 * does not have one.
106 */
107 public CharSequence getBreadCrumbTitle();
108
109 /**
110 * Return the short bread crumb title for the entry, or null if it
111 * does not have one.
112 */
113 public CharSequence getBreadCrumbShortTitle();
114 }
115
116 /**
117 * Interface to watch for changes to the back stack.
118 */
119 public interface OnBackStackChangedListener {
120 /**
121 * Called whenever the contents of the back stack change.
122 */
123 public void onBackStackChanged();
124 }
125
126 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700127 * Start a series of edit operations on the Fragments associated with
128 * this FragmentManager.
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700129 *
130 * <p>Note: A fragment transaction can only be created/committed prior
131 * to an activity saving its state. If you try to commit a transaction
132 * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()}
133 * (and prior to a following {@link Activity#onStart Activity.onStart}
134 * or {@link Activity#onResume Activity.onResume()}, you will get an error.
135 * This is because the framework takes care of saving your current fragments
136 * in the state, and if changes are made after the state is saved then they
137 * will be lost.</p>
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700138 */
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800139 public abstract FragmentTransaction beginTransaction();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700140
Dianne Hackborn17b9b812011-01-17 17:16:02 -0800141 /** @hide -- remove once prebuilts are in. */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800142 @Deprecated
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800143 public FragmentTransaction openTransaction() {
144 return beginTransaction();
145 }
146
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700147 /**
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800148 * After a {@link FragmentTransaction} is committed with
149 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
150 * is scheduled to be executed asynchronously on the process's main thread.
151 * If you want to immediately executing any such pending operations, you
152 * can call this function (only from the main thread) to do so. Note that
153 * all callbacks and other related behavior will be done from within this
154 * call, so be careful about where this is called from.
155 *
156 * @return Returns true if there were any pending transactions to be
157 * executed.
158 */
159 public abstract boolean executePendingTransactions();
160
161 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700162 * Finds a fragment that was identified by the given id either when inflated
163 * from XML or as the container ID when added in a transaction. This first
164 * searches through fragments that are currently added to the manager's
165 * activity; if no such fragment is found, then all fragments currently
166 * on the back stack associated with this ID are searched.
167 * @return The fragment if found or null otherwise.
168 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700169 public abstract Fragment findFragmentById(int id);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700170
171 /**
172 * Finds a fragment that was identified by the given tag either when inflated
173 * from XML or as supplied when added in a transaction. This first
174 * searches through fragments that are currently added to the manager's
175 * activity; if no such fragment is found, then all fragments currently
176 * on the back stack are searched.
177 * @return The fragment if found or null otherwise.
178 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700179 public abstract Fragment findFragmentByTag(String tag);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700180
181 /**
182 * Flag for {@link #popBackStack(String, int)}
183 * and {@link #popBackStack(int, int)}: If set, and the name or ID of
184 * a back stack entry has been supplied, then all matching entries will
185 * be consumed until one that doesn't match is found or the bottom of
186 * the stack is reached. Otherwise, all entries up to but not including that entry
187 * will be removed.
188 */
189 public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
190
191 /**
Ben Komalo87ffa202011-02-28 12:41:42 -0800192 * Pop the top state off the back stack. This function is asynchronous -- it
193 * enqueues the request to pop, but the action will not be performed until the
194 * application returns to its event loop.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700195 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800196 public abstract void popBackStack();
197
198 /**
199 * Like {@link #popBackStack()}, but performs the operation immediately
200 * inside of the call. This is like calling {@link #executePendingTransactions()}
201 * afterwards.
202 * @return Returns true if there was something popped, else false.
203 */
204 public abstract boolean popBackStackImmediate();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700205
206 /**
207 * Pop the last fragment transition from the manager's fragment
208 * back stack. If there is nothing to pop, false is returned.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800209 * This function is asynchronous -- it enqueues the
210 * request to pop, but the action will not be performed until the application
211 * returns to its event loop.
212 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700213 * @param name If non-null, this is the name of a previous back state
214 * to look for; if found, all states up to that state will be popped. The
215 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
216 * the named state itself is popped. If null, only the top state is popped.
217 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
218 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800219 public abstract void popBackStack(String name, int flags);
220
221 /**
222 * Like {@link #popBackStack(String, int)}, but performs the operation immediately
223 * inside of the call. This is like calling {@link #executePendingTransactions()}
224 * afterwards.
225 * @return Returns true if there was something popped, else false.
226 */
227 public abstract boolean popBackStackImmediate(String name, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700228
229 /**
230 * Pop all back stack states up to the one with the given identifier.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800231 * This function is asynchronous -- it enqueues the
232 * request to pop, but the action will not be performed until the application
233 * returns to its event loop.
234 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700235 * @param id Identifier of the stated to be popped. If no identifier exists,
236 * false is returned.
237 * The identifier is the number returned by
238 * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The
239 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
240 * the named state itself is popped.
241 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
242 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800243 public abstract void popBackStack(int id, int flags);
244
245 /**
246 * Like {@link #popBackStack(int, int)}, but performs the operation immediately
247 * inside of the call. This is like calling {@link #executePendingTransactions()}
248 * afterwards.
249 * @return Returns true if there was something popped, else false.
250 */
251 public abstract boolean popBackStackImmediate(int id, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700252
253 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700254 * Return the number of entries currently in the back stack.
255 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800256 public abstract int getBackStackEntryCount();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700257
258 /**
259 * Return the BackStackEntry at index <var>index</var> in the back stack;
260 * entries start index 0 being the bottom of the stack.
261 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800262 public abstract BackStackEntry getBackStackEntryAt(int index);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700263
264 /**
265 * Add a new listener for changes to the fragment back stack.
266 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700267 public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700268
269 /**
270 * Remove a listener that was previously added with
271 * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
272 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700273 public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700274
275 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700276 * Put a reference to a fragment in a Bundle. This Bundle can be
277 * persisted as saved state, and when later restoring
278 * {@link #getFragment(Bundle, String)} will return the current
279 * instance of the same fragment.
280 *
281 * @param bundle The bundle in which to put the fragment reference.
282 * @param key The name of the entry in the bundle.
283 * @param fragment The Fragment whose reference is to be stored.
284 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700285 public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700286
287 /**
288 * Retrieve the current Fragment instance for a reference previously
289 * placed with {@link #putFragment(Bundle, String, Fragment)}.
290 *
291 * @param bundle The bundle from which to retrieve the fragment reference.
292 * @param key The name of the entry in the bundle.
293 * @return Returns the current Fragment instance that is associated with
294 * the given reference.
295 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700296 public abstract Fragment getFragment(Bundle bundle, String key);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700297
298 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700299 * Save the current instance state of the given Fragment. This can be
300 * used later when creating a new instance of the Fragment and adding
301 * it to the fragment manager, to have it create itself to match the
302 * current state returned here. Note that there are limits on how
303 * this can be used:
304 *
305 * <ul>
306 * <li>The Fragment must currently be attached to the FragmentManager.
307 * <li>A new Fragment created using this saved state must be the same class
308 * type as the Fragment it was created from.
309 * <li>The saved state can not contain dependencies on other fragments --
310 * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
311 * store a fragment reference because that reference may not be valid when
312 * this saved state is later used. Likewise the Fragment's target and
313 * result code are not included in this state.
314 * </ul>
315 *
316 * @param f The Fragment whose state is to be saved.
317 * @return The generated state. This will be null if there was no
318 * interesting state created by the fragment.
319 */
320 public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
321
322 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700323 * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()}
324 * call has been made on the FragmentManager's Activity, so this instance is now dead.
325 */
326 public abstract boolean isDestroyed();
327
328 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700329 * Print the FragmentManager's state into the given stream.
330 *
331 * @param prefix Text to print at the front of each line.
332 * @param fd The raw file descriptor that the dump is being sent to.
333 * @param writer A PrintWriter to which the dump is to be set.
Dianne Hackborn30d71892010-12-11 10:37:55 -0800334 * @param args Additional arguments to the dump request.
Dianne Hackborn625ac272010-09-17 18:29:22 -0700335 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700336 public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800337
338 /**
339 * Control whether the framework's internal fragment manager debugging
340 * logs are turned on. If enabled, you will see output in logcat as
341 * the framework performs fragment operations.
342 */
343 public static void enableDebugLogging(boolean enabled) {
344 FragmentManagerImpl.DEBUG = enabled;
345 }
Adam Powellf0f5fff2011-08-01 13:42:50 -0700346
347 /**
348 * Invalidate the attached activity's options menu as necessary.
349 * This may end up being deferred until we move to the resumed state.
350 */
351 public void invalidateOptionsMenu() { }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700352}
353
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700354final class FragmentManagerState implements Parcelable {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700355 FragmentState[] mActive;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700356 int[] mAdded;
357 BackStackState[] mBackStack;
358
359 public FragmentManagerState() {
360 }
361
362 public FragmentManagerState(Parcel in) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700363 mActive = in.createTypedArray(FragmentState.CREATOR);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700364 mAdded = in.createIntArray();
365 mBackStack = in.createTypedArray(BackStackState.CREATOR);
366 }
367
368 public int describeContents() {
369 return 0;
370 }
371
372 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700373 dest.writeTypedArray(mActive, flags);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700374 dest.writeIntArray(mAdded);
375 dest.writeTypedArray(mBackStack, flags);
376 }
377
378 public static final Parcelable.Creator<FragmentManagerState> CREATOR
379 = new Parcelable.Creator<FragmentManagerState>() {
380 public FragmentManagerState createFromParcel(Parcel in) {
381 return new FragmentManagerState(in);
382 }
383
384 public FragmentManagerState[] newArray(int size) {
385 return new FragmentManagerState[size];
386 }
387 };
Dianne Hackbornba51c3d2010-05-05 18:49:48 -0700388}
389
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700390/**
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700391 * Callbacks from FragmentManagerImpl to its container.
392 */
393interface FragmentContainer {
394 public View findViewById(int id);
395}
396
397/**
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700398 * Container for fragments associated with an activity.
399 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700400final class FragmentManagerImpl extends FragmentManager {
Craig Mautner1c437192012-08-01 10:01:16 -0700401 static boolean DEBUG = false;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700402 static final String TAG = "FragmentManager";
403
Dianne Hackborndef15372010-08-15 12:43:52 -0700404 static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
405 static final String TARGET_STATE_TAG = "android:target_state";
406 static final String VIEW_STATE_TAG = "android:view_state";
Adam Powell78fed9b2011-11-07 10:45:34 -0800407 static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
Dianne Hackborndef15372010-08-15 12:43:52 -0700408
Dianne Hackborn445646c2010-06-25 15:52:59 -0700409 ArrayList<Runnable> mPendingActions;
410 Runnable[] mTmpActions;
411 boolean mExecutingActions;
412
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700413 ArrayList<Fragment> mActive;
414 ArrayList<Fragment> mAdded;
415 ArrayList<Integer> mAvailIndices;
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700416 ArrayList<BackStackRecord> mBackStack;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -0700417 ArrayList<Fragment> mCreatedMenus;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700418
Dianne Hackborndd913a52010-07-22 12:17:04 -0700419 // Must be accessed while locked.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700420 ArrayList<BackStackRecord> mBackStackIndices;
Dianne Hackborndd913a52010-07-22 12:17:04 -0700421 ArrayList<Integer> mAvailBackStackIndices;
422
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700423 ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
424
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700425 int mCurState = Fragment.INITIALIZING;
426 Activity mActivity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700427 FragmentContainer mContainer;
428 Fragment mParent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700429
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700430 boolean mNeedMenuInvalidate;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700431 boolean mStateSaved;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800432 boolean mDestroyed;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700433 String mNoTransactionsBecause;
Adam Powell78fed9b2011-11-07 10:45:34 -0800434 boolean mHavePendingDeferredStart;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700435
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700436 // Temporary vars for state save and restore.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700437 Bundle mStateBundle = null;
438 SparseArray<Parcelable> mStateArray = null;
439
Dianne Hackborn445646c2010-06-25 15:52:59 -0700440 Runnable mExecCommit = new Runnable() {
441 @Override
442 public void run() {
443 execPendingActions();
444 }
445 };
Dianne Hackborn625ac272010-09-17 18:29:22 -0700446
Dianne Hackborn4702a852012-08-17 15:18:29 -0700447 private void throwException(RuntimeException ex) {
448 Log.e(TAG, ex.getMessage());
449 LogWriter logw = new LogWriter(Log.ERROR, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700450 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Dianne Hackborn4702a852012-08-17 15:18:29 -0700451 if (mActivity != null) {
452 Log.e(TAG, "Activity state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700453 try {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700454 mActivity.dump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700455 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700456 pw.flush();
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700457 Log.e(TAG, "Failed dumping state", e);
458 }
459 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700460 Log.e(TAG, "Fragment manager state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700461 try {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700462 dump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700463 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700464 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700465 Log.e(TAG, "Failed dumping state", e);
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700466 }
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700467 }
Dianne Hackborn8c841092013-06-24 13:46:13 -0700468 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700469 throw ex;
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700470 }
471
Dianne Hackborn625ac272010-09-17 18:29:22 -0700472 @Override
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800473 public FragmentTransaction beginTransaction() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700474 return new BackStackRecord(this);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700475 }
476
Dianne Hackborn625ac272010-09-17 18:29:22 -0700477 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800478 public boolean executePendingTransactions() {
479 return execPendingActions();
480 }
481
482 @Override
483 public void popBackStack() {
484 enqueueAction(new Runnable() {
485 @Override public void run() {
486 popBackStackState(mActivity.mHandler, null, -1, 0);
487 }
488 }, false);
489 }
490
491 @Override
492 public boolean popBackStackImmediate() {
493 checkStateLoss();
494 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700495 return popBackStackState(mActivity.mHandler, null, -1, 0);
496 }
497
Dianne Hackborn625ac272010-09-17 18:29:22 -0700498 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800499 public void popBackStack(final String name, final int flags) {
500 enqueueAction(new Runnable() {
501 @Override public void run() {
502 popBackStackState(mActivity.mHandler, name, -1, flags);
503 }
504 }, false);
505 }
506
507 @Override
508 public boolean popBackStackImmediate(String name, int flags) {
509 checkStateLoss();
510 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700511 return popBackStackState(mActivity.mHandler, name, -1, flags);
512 }
513
Dianne Hackborn625ac272010-09-17 18:29:22 -0700514 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800515 public void popBackStack(final int id, final int flags) {
516 if (id < 0) {
517 throw new IllegalArgumentException("Bad id: " + id);
518 }
519 enqueueAction(new Runnable() {
520 @Override public void run() {
521 popBackStackState(mActivity.mHandler, null, id, flags);
522 }
523 }, false);
524 }
525
526 @Override
527 public boolean popBackStackImmediate(int id, int flags) {
528 checkStateLoss();
529 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700530 if (id < 0) {
531 throw new IllegalArgumentException("Bad id: " + id);
532 }
533 return popBackStackState(mActivity.mHandler, null, id, flags);
534 }
535
Dianne Hackborn625ac272010-09-17 18:29:22 -0700536 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800537 public int getBackStackEntryCount() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700538 return mBackStack != null ? mBackStack.size() : 0;
539 }
540
Dianne Hackborn625ac272010-09-17 18:29:22 -0700541 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800542 public BackStackEntry getBackStackEntryAt(int index) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700543 return mBackStack.get(index);
544 }
545
Dianne Hackborn625ac272010-09-17 18:29:22 -0700546 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700547 public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
548 if (mBackStackChangeListeners == null) {
549 mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
550 }
551 mBackStackChangeListeners.add(listener);
552 }
553
Dianne Hackborn625ac272010-09-17 18:29:22 -0700554 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700555 public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
556 if (mBackStackChangeListeners != null) {
557 mBackStackChangeListeners.remove(listener);
558 }
559 }
560
Dianne Hackborn625ac272010-09-17 18:29:22 -0700561 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700562 public void putFragment(Bundle bundle, String key, Fragment fragment) {
563 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700564 throwException(new IllegalStateException("Fragment " + fragment
565 + " is not currently in the FragmentManager"));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700566 }
567 bundle.putInt(key, fragment.mIndex);
568 }
569
Dianne Hackborn625ac272010-09-17 18:29:22 -0700570 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700571 public Fragment getFragment(Bundle bundle, String key) {
Dianne Hackborndef15372010-08-15 12:43:52 -0700572 int index = bundle.getInt(key, -1);
573 if (index == -1) {
574 return null;
575 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700576 if (index >= mActive.size()) {
Cyril Mottier2de50822013-09-30 22:42:26 +0200577 throwException(new IllegalStateException("Fragment no longer exists for key "
Dianne Hackborn4702a852012-08-17 15:18:29 -0700578 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700579 }
580 Fragment f = mActive.get(index);
581 if (f == null) {
Cyril Mottier2de50822013-09-30 22:42:26 +0200582 throwException(new IllegalStateException("Fragment no longer exists for key "
Dianne Hackborn4702a852012-08-17 15:18:29 -0700583 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700584 }
585 return f;
586 }
587
Dianne Hackborn625ac272010-09-17 18:29:22 -0700588 @Override
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700589 public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
590 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700591 throwException(new IllegalStateException("Fragment " + fragment
592 + " is not currently in the FragmentManager"));
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700593 }
594 if (fragment.mState > Fragment.INITIALIZING) {
595 Bundle result = saveFragmentBasicState(fragment);
596 return result != null ? new Fragment.SavedState(result) : null;
597 }
598 return null;
599 }
600
601 @Override
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700602 public boolean isDestroyed() {
603 return mDestroyed;
604 }
605
606 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800607 public String toString() {
608 StringBuilder sb = new StringBuilder(128);
609 sb.append("FragmentManager{");
610 sb.append(Integer.toHexString(System.identityHashCode(this)));
611 sb.append(" in ");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700612 if (mParent != null) {
613 DebugUtils.buildShortClassTag(mParent, sb);
614 } else {
615 DebugUtils.buildShortClassTag(mActivity, sb);
616 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800617 sb.append("}}");
618 return sb.toString();
619 }
620
621 @Override
Dianne Hackborn625ac272010-09-17 18:29:22 -0700622 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700623 String innerPrefix = prefix + " ";
624
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800625 int N;
626 if (mActive != null) {
627 N = mActive.size();
628 if (N > 0) {
629 writer.print(prefix); writer.print("Active Fragments in ");
630 writer.print(Integer.toHexString(System.identityHashCode(this)));
631 writer.println(":");
632 for (int i=0; i<N; i++) {
633 Fragment f = mActive.get(i);
634 writer.print(prefix); writer.print(" #"); writer.print(i);
635 writer.print(": "); writer.println(f);
636 if (f != null) {
637 f.dump(innerPrefix, fd, writer, args);
638 }
639 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700640 }
641 }
642
643 if (mAdded != null) {
644 N = mAdded.size();
645 if (N > 0) {
646 writer.print(prefix); writer.println("Added Fragments:");
647 for (int i=0; i<N; i++) {
648 Fragment f = mAdded.get(i);
649 writer.print(prefix); writer.print(" #"); writer.print(i);
650 writer.print(": "); writer.println(f.toString());
651 }
652 }
653 }
654
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800655 if (mCreatedMenus != null) {
656 N = mCreatedMenus.size();
657 if (N > 0) {
658 writer.print(prefix); writer.println("Fragments Created Menus:");
659 for (int i=0; i<N; i++) {
660 Fragment f = mCreatedMenus.get(i);
661 writer.print(prefix); writer.print(" #"); writer.print(i);
662 writer.print(": "); writer.println(f.toString());
663 }
664 }
665 }
666
Dianne Hackborn625ac272010-09-17 18:29:22 -0700667 if (mBackStack != null) {
668 N = mBackStack.size();
669 if (N > 0) {
670 writer.print(prefix); writer.println("Back Stack:");
671 for (int i=0; i<N; i++) {
672 BackStackRecord bs = mBackStack.get(i);
673 writer.print(prefix); writer.print(" #"); writer.print(i);
674 writer.print(": "); writer.println(bs.toString());
Dianne Hackborn30d71892010-12-11 10:37:55 -0800675 bs.dump(innerPrefix, fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700676 }
677 }
678 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800679
680 synchronized (this) {
681 if (mBackStackIndices != null) {
682 N = mBackStackIndices.size();
683 if (N > 0) {
684 writer.print(prefix); writer.println("Back Stack Indices:");
685 for (int i=0; i<N; i++) {
686 BackStackRecord bs = mBackStackIndices.get(i);
687 writer.print(prefix); writer.print(" #"); writer.print(i);
688 writer.print(": "); writer.println(bs);
689 }
690 }
691 }
692
693 if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
694 writer.print(prefix); writer.print("mAvailBackStackIndices: ");
695 writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
696 }
697 }
698
699 if (mPendingActions != null) {
700 N = mPendingActions.size();
701 if (N > 0) {
702 writer.print(prefix); writer.println("Pending Actions:");
703 for (int i=0; i<N; i++) {
704 Runnable r = mPendingActions.get(i);
705 writer.print(prefix); writer.print(" #"); writer.print(i);
706 writer.print(": "); writer.println(r);
707 }
708 }
709 }
710
711 writer.print(prefix); writer.println("FragmentManager misc state:");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700712 writer.print(prefix); writer.print(" mActivity="); writer.println(mActivity);
713 writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer);
714 if (mParent != null) {
715 writer.print(prefix); writer.print(" mParent="); writer.println(mParent);
716 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800717 writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState);
718 writer.print(" mStateSaved="); writer.print(mStateSaved);
719 writer.print(" mDestroyed="); writer.println(mDestroyed);
720 if (mNeedMenuInvalidate) {
721 writer.print(prefix); writer.print(" mNeedMenuInvalidate=");
722 writer.println(mNeedMenuInvalidate);
723 }
724 if (mNoTransactionsBecause != null) {
725 writer.print(prefix); writer.print(" mNoTransactionsBecause=");
726 writer.println(mNoTransactionsBecause);
727 }
728 if (mAvailIndices != null && mAvailIndices.size() > 0) {
729 writer.print(prefix); writer.print(" mAvailIndices: ");
730 writer.println(Arrays.toString(mAvailIndices.toArray()));
731 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700732 }
733
Chet Haasea18a86b2010-09-07 13:20:00 -0700734 Animator loadAnimator(Fragment fragment, int transit, boolean enter,
Dianne Hackbornf121be72010-05-06 14:10:32 -0700735 int transitionStyle) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700736 Animator animObj = fragment.onCreateAnimator(transit, enter,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700737 fragment.mNextAnim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700738 if (animObj != null) {
739 return animObj;
740 }
741
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700742 if (fragment.mNextAnim != 0) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700743 Animator anim = AnimatorInflater.loadAnimator(mActivity, fragment.mNextAnim);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700744 if (anim != null) {
745 return anim;
746 }
747 }
748
Dianne Hackbornf121be72010-05-06 14:10:32 -0700749 if (transit == 0) {
750 return null;
751 }
752
753 int styleIndex = transitToStyleIndex(transit, enter);
754 if (styleIndex < 0) {
755 return null;
756 }
757
758 if (transitionStyle == 0 && mActivity.getWindow() != null) {
759 transitionStyle = mActivity.getWindow().getAttributes().windowAnimations;
760 }
761 if (transitionStyle == 0) {
762 return null;
763 }
764
765 TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
Chet Haase811ed1062010-08-06 10:38:15 -0700766 com.android.internal.R.styleable.FragmentAnimation);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700767 int anim = attrs.getResourceId(styleIndex, 0);
768 attrs.recycle();
769
770 if (anim == 0) {
771 return null;
772 }
773
Chet Haasea18a86b2010-09-07 13:20:00 -0700774 return AnimatorInflater.loadAnimator(mActivity, anim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700775 }
776
Adam Powell635c60a2011-10-26 10:22:16 -0700777 public void performPendingDeferredStart(Fragment f) {
778 if (f.mDeferStart) {
Adam Powell78fed9b2011-11-07 10:45:34 -0800779 if (mExecutingActions) {
780 // Wait until we're done executing our pending transactions
781 mHavePendingDeferredStart = true;
782 return;
783 }
Adam Powell635c60a2011-10-26 10:22:16 -0700784 f.mDeferStart = false;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700785 moveToState(f, mCurState, 0, 0, false);
Adam Powell635c60a2011-10-26 10:22:16 -0700786 }
787 }
788
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700789 void moveToState(Fragment f, int newState, int transit, int transitionStyle,
790 boolean keepActive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -0700791 if (DEBUG && false) Log.v(TAG, "moveToState: " + f
792 + " oldState=" + f.mState + " newState=" + newState
793 + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
Craig Mautner1c437192012-08-01 10:01:16 -0700794
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700795 // Fragments that are not currently added will sit in the onCreate() state.
Dianne Hackborne181bd92012-09-25 14:15:15 -0700796 if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700797 newState = Fragment.CREATED;
798 }
Dianne Hackbornf9302322011-06-14 18:36:14 -0700799 if (f.mRemoving && newState > f.mState) {
800 // While removing a fragment, we can't change it to a higher state.
801 newState = f.mState;
802 }
Adam Powell2db4e4b2011-11-02 14:30:47 -0700803 // Defer start if requested; don't allow it to move to STARTED or higher
804 // if it's not already started.
805 if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
Adam Powell635c60a2011-10-26 10:22:16 -0700806 newState = Fragment.STOPPED;
807 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700808 if (f.mState < newState) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -0800809 // For fragments that are created from a layout, when restoring from
810 // state we don't want to allow them to be created until they are
811 // being reloaded from the layout.
812 if (f.mFromLayout && !f.mInLayout) {
813 return;
814 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800815 if (f.mAnimatingAway != null) {
816 // The fragment is currently being animated... but! Now we
817 // want to move our state back up. Give up on waiting for the
818 // animation, move to whatever the final state should be once
819 // the animation is done, and then we can proceed from there.
820 f.mAnimatingAway = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700821 moveToState(f, f.mStateAfterAnimating, 0, 0, true);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800822 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700823 switch (f.mState) {
824 case Fragment.INITIALIZING:
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700825 if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
Dianne Hackborndef15372010-08-15 12:43:52 -0700826 if (f.mSavedFragmentState != null) {
827 f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
828 FragmentManagerImpl.VIEW_STATE_TAG);
829 f.mTarget = getFragment(f.mSavedFragmentState,
830 FragmentManagerImpl.TARGET_STATE_TAG);
831 if (f.mTarget != null) {
832 f.mTargetRequestCode = f.mSavedFragmentState.getInt(
833 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
834 }
Adam Powell78fed9b2011-11-07 10:45:34 -0800835 f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
836 FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
837 if (!f.mUserVisibleHint) {
838 f.mDeferStart = true;
839 if (newState > Fragment.STOPPED) {
840 newState = Fragment.STOPPED;
841 }
842 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700843 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700844 f.mActivity = mActivity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700845 f.mParentFragment = mParent;
846 f.mFragmentManager = mParent != null
847 ? mParent.mChildFragmentManager : mActivity.mFragments;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700848 f.mCalled = false;
849 f.onAttach(mActivity);
850 if (!f.mCalled) {
851 throw new SuperNotCalledException("Fragment " + f
852 + " did not call through to super.onAttach()");
853 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700854 if (f.mParentFragment == null) {
855 mActivity.onAttachFragment(f);
856 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700857
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700858 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700859 f.performCreate(f.mSavedFragmentState);
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700860 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700861 f.mRetaining = false;
862 if (f.mFromLayout) {
863 // For fragments that are part of the content view
864 // layout, we need to instantiate the view immediately
865 // and the inflater will take care of adding it.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700866 f.mView = f.performCreateView(f.getLayoutInflater(
867 f.mSavedFragmentState), null, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700868 if (f.mView != null) {
869 f.mView.setSaveFromParentEnabled(false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700870 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700871 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700872 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700873 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700874 case Fragment.CREATED:
875 if (newState > Fragment.CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700876 if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700877 if (!f.mFromLayout) {
878 ViewGroup container = null;
879 if (f.mContainerId != 0) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700880 container = (ViewGroup)mContainer.findViewById(f.mContainerId);
Dianne Hackborn352cc982011-01-04 11:34:18 -0800881 if (container == null && !f.mRestored) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700882 throwException(new IllegalArgumentException(
883 "No view found for id 0x"
884 + Integer.toHexString(f.mContainerId) + " ("
885 + f.getResources().getResourceName(f.mContainerId)
886 + ") for fragment " + f));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700887 }
888 }
889 f.mContainer = container;
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700890 f.mView = f.performCreateView(f.getLayoutInflater(
891 f.mSavedFragmentState), container, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700892 if (f.mView != null) {
893 f.mView.setSaveFromParentEnabled(false);
894 if (container != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700895 Animator anim = loadAnimator(f, transit, true,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700896 transitionStyle);
897 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700898 anim.setTarget(f.mView);
Chet Haase811ed1062010-08-06 10:38:15 -0700899 anim.start();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700900 }
901 container.addView(f.mView);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700902 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700903 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700904 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700905 }
906 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700907
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700908 f.performActivityCreated(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700909 if (f.mView != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700910 f.restoreViewState(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700911 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700912 f.mSavedFragmentState = null;
913 }
Dianne Hackbornc8017682010-07-06 13:34:38 -0700914 case Fragment.ACTIVITY_CREATED:
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700915 case Fragment.STOPPED:
916 if (newState > Fragment.STOPPED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700917 if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700918 f.performStart();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700919 }
920 case Fragment.STARTED:
921 if (newState > Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700922 if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700923 f.mResumed = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700924 f.performResume();
Adam Powell95202512011-08-07 17:20:17 -0700925 // Get rid of this in case we saved it and never needed it.
926 f.mSavedFragmentState = null;
927 f.mSavedViewState = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700928 }
929 }
930 } else if (f.mState > newState) {
931 switch (f.mState) {
932 case Fragment.RESUMED:
933 if (newState < Fragment.RESUMED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700934 if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700935 f.performPause();
Dianne Hackborn2707d602010-07-09 18:01:20 -0700936 f.mResumed = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700937 }
938 case Fragment.STARTED:
939 if (newState < Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700940 if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700941 f.performStop();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700942 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700943 case Fragment.STOPPED:
Dianne Hackbornc8017682010-07-06 13:34:38 -0700944 case Fragment.ACTIVITY_CREATED:
945 if (newState < Fragment.ACTIVITY_CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700946 if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700947 if (f.mView != null) {
948 // Need to save the current view state if not
949 // done already.
Dianne Hackborn625ac272010-09-17 18:29:22 -0700950 if (!mActivity.isFinishing() && f.mSavedViewState == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700951 saveFragmentViewState(f);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700952 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700953 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700954 f.performDestroyView();
Dianne Hackborndef15372010-08-15 12:43:52 -0700955 if (f.mView != null && f.mContainer != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700956 Animator anim = null;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800957 if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700958 anim = loadAnimator(f, transit, false,
Dianne Hackborndef15372010-08-15 12:43:52 -0700959 transitionStyle);
Chet Haaseb20db3e2010-09-10 13:07:30 -0700960 }
961 if (anim != null) {
962 final ViewGroup container = f.mContainer;
963 final View view = f.mView;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800964 final Fragment fragment = f;
Chet Haaseb20db3e2010-09-10 13:07:30 -0700965 container.startViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800966 f.mAnimatingAway = anim;
967 f.mStateAfterAnimating = newState;
Chet Haaseb20db3e2010-09-10 13:07:30 -0700968 anim.addListener(new AnimatorListenerAdapter() {
969 @Override
970 public void onAnimationEnd(Animator anim) {
971 container.endViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800972 if (fragment.mAnimatingAway != null) {
973 fragment.mAnimatingAway = null;
974 moveToState(fragment, fragment.mStateAfterAnimating,
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700975 0, 0, false);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800976 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700977 }
Chet Haaseb20db3e2010-09-10 13:07:30 -0700978 });
979 anim.setTarget(f.mView);
980 anim.start();
981
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700982 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700983 f.mContainer.removeView(f.mView);
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700984 }
985 f.mContainer = null;
986 f.mView = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700987 }
988 case Fragment.CREATED:
989 if (newState < Fragment.CREATED) {
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800990 if (mDestroyed) {
991 if (f.mAnimatingAway != null) {
992 // The fragment's containing activity is
993 // being destroyed, but this fragment is
994 // currently animating away. Stop the
995 // animation right now -- it is not needed,
996 // and we can't wait any more on destroying
997 // the fragment.
Dianne Hackborn1b39e222010-12-28 14:17:18 -0800998 Animator anim = f.mAnimatingAway;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800999 f.mAnimatingAway = null;
Dianne Hackborn1b39e222010-12-28 14:17:18 -08001000 anim.cancel();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001001 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001002 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001003 if (f.mAnimatingAway != null) {
1004 // We are waiting for the fragment's view to finish
1005 // animating away. Just make a note of the state
1006 // the fragment now should move to once the animation
1007 // is done.
1008 f.mStateAfterAnimating = newState;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001009 newState = Fragment.CREATED;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001010 } else {
1011 if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1012 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001013 f.performDestroy();
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001014 }
1015
1016 f.mCalled = false;
1017 f.onDetach();
1018 if (!f.mCalled) {
1019 throw new SuperNotCalledException("Fragment " + f
1020 + " did not call through to super.onDetach()");
1021 }
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001022 if (!keepActive) {
1023 if (!f.mRetaining) {
1024 makeInactive(f);
1025 } else {
1026 f.mActivity = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001027 f.mParentFragment = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001028 f.mFragmentManager = null;
Tim Kilbourn70717862014-02-28 01:00:06 -08001029 f.mChildFragmentManager = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001030 }
Dianne Hackbornf9302322011-06-14 18:36:14 -07001031 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001032 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001033 }
1034 }
1035 }
1036
1037 f.mState = newState;
1038 }
1039
Dianne Hackborn625ac272010-09-17 18:29:22 -07001040 void moveToState(Fragment f) {
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001041 moveToState(f, mCurState, 0, 0, false);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001042 }
1043
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001044 void moveToState(int newState, boolean always) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001045 moveToState(newState, 0, 0, always);
1046 }
1047
1048 void moveToState(int newState, int transit, int transitStyle, boolean always) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001049 if (mActivity == null && newState != Fragment.INITIALIZING) {
1050 throw new IllegalStateException("No activity");
1051 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001052
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001053 if (!always && mCurState == newState) {
1054 return;
1055 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001056
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001057 mCurState = newState;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001058 if (mActive != null) {
Adam Powell635c60a2011-10-26 10:22:16 -07001059 boolean loadersRunning = false;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001060 for (int i=0; i<mActive.size(); i++) {
1061 Fragment f = mActive.get(i);
1062 if (f != null) {
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001063 moveToState(f, newState, transit, transitStyle, false);
Adam Powell635c60a2011-10-26 10:22:16 -07001064 if (f.mLoaderManager != null) {
1065 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1066 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001067 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001068 }
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001069
Adam Powell635c60a2011-10-26 10:22:16 -07001070 if (!loadersRunning) {
1071 startPendingDeferredFragments();
1072 }
1073
Adam Powell89b09da2011-07-27 11:55:29 -07001074 if (mNeedMenuInvalidate && mActivity != null && mCurState == Fragment.RESUMED) {
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001075 mActivity.invalidateOptionsMenu();
1076 mNeedMenuInvalidate = false;
1077 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001078 }
1079 }
1080
Adam Powell635c60a2011-10-26 10:22:16 -07001081 void startPendingDeferredFragments() {
Adam Powell37510902011-10-31 11:48:24 -07001082 if (mActive == null) return;
1083
Adam Powell635c60a2011-10-26 10:22:16 -07001084 for (int i=0; i<mActive.size(); i++) {
1085 Fragment f = mActive.get(i);
1086 if (f != null) {
1087 performPendingDeferredStart(f);
1088 }
1089 }
1090 }
1091
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001092 void makeActive(Fragment f) {
1093 if (f.mIndex >= 0) {
1094 return;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001095 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001096
1097 if (mAvailIndices == null || mAvailIndices.size() <= 0) {
1098 if (mActive == null) {
1099 mActive = new ArrayList<Fragment>();
1100 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001101 f.setIndex(mActive.size(), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001102 mActive.add(f);
1103
1104 } else {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001105 f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001106 mActive.set(f.mIndex, f);
1107 }
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001108 if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001109 }
1110
1111 void makeInactive(Fragment f) {
1112 if (f.mIndex < 0) {
1113 return;
1114 }
1115
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001116 if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001117 mActive.set(f.mIndex, null);
1118 if (mAvailIndices == null) {
1119 mAvailIndices = new ArrayList<Integer>();
1120 }
1121 mAvailIndices.add(f.mIndex);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001122 mActivity.invalidateFragment(f.mWho);
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001123 f.initState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001124 }
1125
1126 public void addFragment(Fragment fragment, boolean moveToStateNow) {
1127 if (mAdded == null) {
1128 mAdded = new ArrayList<Fragment>();
1129 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001130 if (DEBUG) Log.v(TAG, "add: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001131 makeActive(fragment);
1132 if (!fragment.mDetached) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001133 if (mAdded.contains(fragment)) {
1134 throw new IllegalStateException("Fragment already added: " + fragment);
1135 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001136 mAdded.add(fragment);
1137 fragment.mAdded = true;
1138 fragment.mRemoving = false;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001139 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001140 mNeedMenuInvalidate = true;
1141 }
1142 if (moveToStateNow) {
1143 moveToState(fragment);
1144 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001145 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001146 }
1147
Dianne Hackbornf121be72010-05-06 14:10:32 -07001148 public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001149 if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001150 final boolean inactive = !fragment.isInBackStack();
1151 if (!fragment.mDetached || inactive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001152 if (false) {
1153 // Would be nice to catch a bad remove here, but we need
1154 // time to test this to make sure we aren't crashes cases
1155 // where it is not a problem.
1156 if (!mAdded.contains(fragment)) {
1157 throw new IllegalStateException("Fragment not added: " + fragment);
1158 }
1159 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001160 if (mAdded != null) {
1161 mAdded.remove(fragment);
1162 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001163 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001164 mNeedMenuInvalidate = true;
1165 }
1166 fragment.mAdded = false;
1167 fragment.mRemoving = true;
1168 moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED,
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001169 transition, transitionStyle, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001170 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07001171 }
1172
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001173 public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
1174 if (DEBUG) Log.v(TAG, "hide: " + fragment);
1175 if (!fragment.mHidden) {
1176 fragment.mHidden = true;
1177 if (fragment.mView != null) {
Adam Powell27562932013-06-07 10:16:08 -07001178 Animator anim = loadAnimator(fragment, transition, false,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001179 transitionStyle);
1180 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001181 anim.setTarget(fragment.mView);
Chet Haase61eb40d2010-12-28 13:59:17 -08001182 // Delay the actual hide operation until the animation finishes, otherwise
1183 // the fragment will just immediately disappear
1184 final Fragment finalFragment = fragment;
1185 anim.addListener(new AnimatorListenerAdapter() {
1186 @Override
1187 public void onAnimationEnd(Animator animation) {
Chet Haaseb29407f2011-01-11 14:09:34 -08001188 if (finalFragment.mView != null) {
1189 finalFragment.mView.setVisibility(View.GONE);
1190 }
Chet Haase61eb40d2010-12-28 13:59:17 -08001191 }
1192 });
Chet Haase811ed1062010-08-06 10:38:15 -07001193 anim.start();
Chet Haasee646b28c2010-12-28 14:48:32 -08001194 } else {
1195 fragment.mView.setVisibility(View.GONE);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001196 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001197 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001198 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001199 mNeedMenuInvalidate = true;
1200 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001201 fragment.onHiddenChanged(true);
1202 }
1203 }
1204
1205 public void showFragment(Fragment fragment, int transition, int transitionStyle) {
1206 if (DEBUG) Log.v(TAG, "show: " + fragment);
1207 if (fragment.mHidden) {
1208 fragment.mHidden = false;
1209 if (fragment.mView != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -07001210 Animator anim = loadAnimator(fragment, transition, true,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001211 transitionStyle);
1212 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001213 anim.setTarget(fragment.mView);
Chet Haase811ed1062010-08-06 10:38:15 -07001214 anim.start();
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001215 }
1216 fragment.mView.setVisibility(View.VISIBLE);
1217 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001218 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001219 mNeedMenuInvalidate = true;
1220 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001221 fragment.onHiddenChanged(false);
1222 }
1223 }
1224
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001225 public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
1226 if (DEBUG) Log.v(TAG, "detach: " + fragment);
1227 if (!fragment.mDetached) {
1228 fragment.mDetached = true;
1229 if (fragment.mAdded) {
1230 // We are not already in back stack, so need to remove the fragment.
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001231 if (mAdded != null) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001232 if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001233 mAdded.remove(fragment);
1234 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001235 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001236 mNeedMenuInvalidate = true;
1237 }
1238 fragment.mAdded = false;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001239 moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001240 }
1241 }
1242 }
1243
1244 public void attachFragment(Fragment fragment, int transition, int transitionStyle) {
1245 if (DEBUG) Log.v(TAG, "attach: " + fragment);
1246 if (fragment.mDetached) {
1247 fragment.mDetached = false;
1248 if (!fragment.mAdded) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001249 if (mAdded == null) {
1250 mAdded = new ArrayList<Fragment>();
1251 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001252 if (mAdded.contains(fragment)) {
1253 throw new IllegalStateException("Fragment already added: " + fragment);
1254 }
1255 if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001256 mAdded.add(fragment);
1257 fragment.mAdded = true;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001258 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001259 mNeedMenuInvalidate = true;
1260 }
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001261 moveToState(fragment, mCurState, transition, transitionStyle, false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001262 }
1263 }
1264 }
1265
Dianne Hackbornf121be72010-05-06 14:10:32 -07001266 public Fragment findFragmentById(int id) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001267 if (mAdded != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001268 // First look through added fragments.
1269 for (int i=mAdded.size()-1; i>=0; i--) {
1270 Fragment f = mAdded.get(i);
1271 if (f != null && f.mFragmentId == id) {
1272 return f;
1273 }
1274 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001275 }
1276 if (mActive != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001277 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001278 for (int i=mActive.size()-1; i>=0; i--) {
1279 Fragment f = mActive.get(i);
1280 if (f != null && f.mFragmentId == id) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001281 return f;
1282 }
1283 }
1284 }
1285 return null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001286 }
1287
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001288 public Fragment findFragmentByTag(String tag) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001289 if (mAdded != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001290 // First look through added fragments.
1291 for (int i=mAdded.size()-1; i>=0; i--) {
1292 Fragment f = mAdded.get(i);
1293 if (f != null && tag.equals(f.mTag)) {
1294 return f;
1295 }
1296 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001297 }
1298 if (mActive != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001299 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001300 for (int i=mActive.size()-1; i>=0; i--) {
1301 Fragment f = mActive.get(i);
1302 if (f != null && tag.equals(f.mTag)) {
1303 return f;
1304 }
1305 }
1306 }
1307 return null;
1308 }
1309
1310 public Fragment findFragmentByWho(String who) {
1311 if (mActive != null && who != null) {
1312 for (int i=mActive.size()-1; i>=0; i--) {
1313 Fragment f = mActive.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001314 if (f != null && (f=f.findFragmentByWho(who)) != null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001315 return f;
1316 }
1317 }
1318 }
1319 return null;
1320 }
1321
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001322 private void checkStateLoss() {
1323 if (mStateSaved) {
1324 throw new IllegalStateException(
1325 "Can not perform this action after onSaveInstanceState");
1326 }
1327 if (mNoTransactionsBecause != null) {
1328 throw new IllegalStateException(
1329 "Can not perform this action inside of " + mNoTransactionsBecause);
1330 }
1331 }
1332
Alan Viverette95a46092013-08-14 11:17:25 -07001333 /**
1334 * Adds an action to the queue of pending actions.
1335 *
1336 * @param action the action to add
1337 * @param allowStateLoss whether to allow loss of state information
1338 * @throws IllegalStateException if the activity has been destroyed
1339 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -07001340 public void enqueueAction(Runnable action, boolean allowStateLoss) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001341 if (!allowStateLoss) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001342 checkStateLoss();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001343 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001344 synchronized (this) {
Alan Viverette95a46092013-08-14 11:17:25 -07001345 if (mDestroyed || mActivity == null) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001346 throw new IllegalStateException("Activity has been destroyed");
1347 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001348 if (mPendingActions == null) {
1349 mPendingActions = new ArrayList<Runnable>();
1350 }
1351 mPendingActions.add(action);
1352 if (mPendingActions.size() == 1) {
1353 mActivity.mHandler.removeCallbacks(mExecCommit);
1354 mActivity.mHandler.post(mExecCommit);
1355 }
1356 }
1357 }
1358
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001359 public int allocBackStackIndex(BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001360 synchronized (this) {
1361 if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1362 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001363 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001364 }
1365 int index = mBackStackIndices.size();
1366 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1367 mBackStackIndices.add(bse);
1368 return index;
1369
1370 } else {
1371 int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1372 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1373 mBackStackIndices.set(index, bse);
1374 return index;
1375 }
1376 }
1377 }
1378
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001379 public void setBackStackIndex(int index, BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001380 synchronized (this) {
1381 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001382 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001383 }
1384 int N = mBackStackIndices.size();
1385 if (index < N) {
1386 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1387 mBackStackIndices.set(index, bse);
1388 } else {
1389 while (N < index) {
1390 mBackStackIndices.add(null);
1391 if (mAvailBackStackIndices == null) {
1392 mAvailBackStackIndices = new ArrayList<Integer>();
1393 }
1394 if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1395 mAvailBackStackIndices.add(N);
1396 N++;
1397 }
1398 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1399 mBackStackIndices.add(bse);
1400 }
1401 }
1402 }
1403
1404 public void freeBackStackIndex(int index) {
1405 synchronized (this) {
1406 mBackStackIndices.set(index, null);
1407 if (mAvailBackStackIndices == null) {
1408 mAvailBackStackIndices = new ArrayList<Integer>();
1409 }
1410 if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1411 mAvailBackStackIndices.add(index);
1412 }
1413 }
1414
Dianne Hackborn445646c2010-06-25 15:52:59 -07001415 /**
1416 * Only call from main thread!
1417 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001418 public boolean execPendingActions() {
Dianne Hackborn445646c2010-06-25 15:52:59 -07001419 if (mExecutingActions) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001420 throw new IllegalStateException("Recursive entry to executePendingTransactions");
Dianne Hackborn445646c2010-06-25 15:52:59 -07001421 }
1422
Dianne Hackbornd9b3b7e2010-11-16 18:22:49 -08001423 if (Looper.myLooper() != mActivity.mHandler.getLooper()) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001424 throw new IllegalStateException("Must be called from main thread of process");
1425 }
1426
1427 boolean didSomething = false;
1428
Dianne Hackborn445646c2010-06-25 15:52:59 -07001429 while (true) {
1430 int numActions;
1431
1432 synchronized (this) {
1433 if (mPendingActions == null || mPendingActions.size() == 0) {
Adam Powell78fed9b2011-11-07 10:45:34 -08001434 break;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001435 }
1436
1437 numActions = mPendingActions.size();
1438 if (mTmpActions == null || mTmpActions.length < numActions) {
1439 mTmpActions = new Runnable[numActions];
1440 }
1441 mPendingActions.toArray(mTmpActions);
1442 mPendingActions.clear();
1443 mActivity.mHandler.removeCallbacks(mExecCommit);
1444 }
1445
1446 mExecutingActions = true;
1447 for (int i=0; i<numActions; i++) {
1448 mTmpActions[i].run();
Jeff Sharkey0d325282011-07-13 09:36:27 -07001449 mTmpActions[i] = null;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001450 }
1451 mExecutingActions = false;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001452 didSomething = true;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001453 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001454
1455 if (mHavePendingDeferredStart) {
1456 boolean loadersRunning = false;
1457 for (int i=0; i<mActive.size(); i++) {
1458 Fragment f = mActive.get(i);
1459 if (f != null && f.mLoaderManager != null) {
1460 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1461 }
1462 }
1463 if (!loadersRunning) {
1464 mHavePendingDeferredStart = false;
1465 startPendingDeferredFragments();
1466 }
1467 }
1468 return didSomething;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001469 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001470
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001471 void reportBackStackChanged() {
1472 if (mBackStackChangeListeners != null) {
1473 for (int i=0; i<mBackStackChangeListeners.size(); i++) {
1474 mBackStackChangeListeners.get(i).onBackStackChanged();
1475 }
1476 }
1477 }
1478
1479 void addBackStackState(BackStackRecord state) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001480 if (mBackStack == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001481 mBackStack = new ArrayList<BackStackRecord>();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001482 }
1483 mBackStack.add(state);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001484 reportBackStackChanged();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001485 }
1486
Dianne Hackborndd913a52010-07-22 12:17:04 -07001487 boolean popBackStackState(Handler handler, String name, int id, int flags) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001488 if (mBackStack == null) {
1489 return false;
1490 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001491 if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001492 int last = mBackStack.size()-1;
1493 if (last < 0) {
1494 return false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001495 }
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001496 final BackStackRecord bss = mBackStack.remove(last);
George Mountd4c3c912014-06-09 12:31:34 -07001497 bss.popFromBackStack(true, null);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001498 reportBackStackChanged();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001499 } else {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001500 int index = -1;
1501 if (name != null || id >= 0) {
1502 // If a name or ID is specified, look for that place in
1503 // the stack.
1504 index = mBackStack.size()-1;
1505 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001506 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001507 if (name != null && name.equals(bss.getName())) {
1508 break;
1509 }
1510 if (id >= 0 && id == bss.mIndex) {
1511 break;
1512 }
1513 index--;
Dianne Hackbornf121be72010-05-06 14:10:32 -07001514 }
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001515 if (index < 0) {
1516 return false;
Dianne Hackborndd913a52010-07-22 12:17:04 -07001517 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001518 if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001519 index--;
1520 // Consume all following entries that match.
1521 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001522 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001523 if ((name != null && name.equals(bss.getName()))
1524 || (id >= 0 && id == bss.mIndex)) {
1525 index--;
1526 continue;
1527 }
1528 break;
1529 }
1530 }
Dianne Hackborndd913a52010-07-22 12:17:04 -07001531 }
1532 if (index == mBackStack.size()-1) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001533 return false;
1534 }
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001535 final ArrayList<BackStackRecord> states
1536 = new ArrayList<BackStackRecord>();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001537 for (int i=mBackStack.size()-1; i>index; i--) {
1538 states.add(mBackStack.remove(i));
1539 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001540 final int LAST = states.size()-1;
George Mountd4c3c912014-06-09 12:31:34 -07001541 BackStackRecord.TransitionState state = null;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001542 for (int i=0; i<=LAST; i++) {
1543 if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i));
George Mountd4c3c912014-06-09 12:31:34 -07001544 state = states.get(i).popFromBackStack(i == LAST, state);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001545 }
1546 reportBackStackChanged();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001547 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001548 return true;
1549 }
1550
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001551 ArrayList<Fragment> retainNonConfig() {
1552 ArrayList<Fragment> fragments = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001553 if (mActive != null) {
1554 for (int i=0; i<mActive.size(); i++) {
1555 Fragment f = mActive.get(i);
1556 if (f != null && f.mRetainInstance) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001557 if (fragments == null) {
1558 fragments = new ArrayList<Fragment>();
1559 }
1560 fragments.add(f);
1561 f.mRetaining = true;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001562 f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001563 if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001564 }
1565 }
1566 }
1567 return fragments;
1568 }
1569
1570 void saveFragmentViewState(Fragment f) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001571 if (f.mView == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001572 return;
1573 }
1574 if (mStateArray == null) {
1575 mStateArray = new SparseArray<Parcelable>();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001576 } else {
1577 mStateArray.clear();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001578 }
1579 f.mView.saveHierarchyState(mStateArray);
1580 if (mStateArray.size() > 0) {
1581 f.mSavedViewState = mStateArray;
1582 mStateArray = null;
1583 }
1584 }
1585
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001586 Bundle saveFragmentBasicState(Fragment f) {
1587 Bundle result = null;
1588
1589 if (mStateBundle == null) {
1590 mStateBundle = new Bundle();
1591 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001592 f.performSaveInstanceState(mStateBundle);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001593 if (!mStateBundle.isEmpty()) {
1594 result = mStateBundle;
1595 mStateBundle = null;
1596 }
1597
1598 if (f.mView != null) {
1599 saveFragmentViewState(f);
Dianne Hackborn13332762011-06-03 17:34:45 -07001600 }
1601 if (f.mSavedViewState != null) {
1602 if (result == null) {
1603 result = new Bundle();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001604 }
Dianne Hackborn13332762011-06-03 17:34:45 -07001605 result.putSparseParcelableArray(
1606 FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001607 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001608 if (!f.mUserVisibleHint) {
Jake Wharton258029e2012-04-22 17:17:01 -04001609 if (result == null) {
1610 result = new Bundle();
1611 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001612 // Only add this if it's not the default value
1613 result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
1614 }
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001615
1616 return result;
1617 }
1618
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001619 Parcelable saveAllState() {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001620 // Make sure all pending operations have now been executed to get
1621 // our state update-to-date.
1622 execPendingActions();
1623
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001624 mStateSaved = true;
1625
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001626 if (mActive == null || mActive.size() <= 0) {
1627 return null;
1628 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001629
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001630 // First collect all active fragments.
1631 int N = mActive.size();
1632 FragmentState[] active = new FragmentState[N];
1633 boolean haveFragments = false;
1634 for (int i=0; i<N; i++) {
1635 Fragment f = mActive.get(i);
1636 if (f != null) {
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001637 if (f.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001638 throwException(new IllegalStateException(
1639 "Failure saving state: active " + f
1640 + " has cleared index: " + f.mIndex));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001641 }
1642
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001643 haveFragments = true;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001644
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001645 FragmentState fs = new FragmentState(f);
1646 active[i] = fs;
1647
Dianne Hackborn625ac272010-09-17 18:29:22 -07001648 if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001649 fs.mSavedFragmentState = saveFragmentBasicState(f);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001650
1651 if (f.mTarget != null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001652 if (f.mTarget.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001653 throwException(new IllegalStateException(
1654 "Failure saving state: " + f
1655 + " has target not in fragment manager: " + f.mTarget));
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001656 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001657 if (fs.mSavedFragmentState == null) {
1658 fs.mSavedFragmentState = new Bundle();
1659 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001660 putFragment(fs.mSavedFragmentState,
1661 FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
1662 if (f.mTargetRequestCode != 0) {
1663 fs.mSavedFragmentState.putInt(
1664 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
1665 f.mTargetRequestCode);
1666 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001667 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001668
Dianne Hackborn625ac272010-09-17 18:29:22 -07001669 } else {
1670 fs.mSavedFragmentState = f.mSavedFragmentState;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001671 }
1672
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001673 if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
1674 + fs.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001675 }
1676 }
1677
1678 if (!haveFragments) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001679 if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001680 return null;
1681 }
1682
1683 int[] added = null;
1684 BackStackState[] backStack = null;
1685
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001686 // Build list of currently added fragments.
Dianne Hackborn625ac272010-09-17 18:29:22 -07001687 if (mAdded != null) {
1688 N = mAdded.size();
1689 if (N > 0) {
1690 added = new int[N];
1691 for (int i=0; i<N; i++) {
1692 added[i] = mAdded.get(i).mIndex;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001693 if (added[i] < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001694 throwException(new IllegalStateException(
1695 "Failure saving state: active " + mAdded.get(i)
1696 + " has cleared index: " + added[i]));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001697 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001698 if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
1699 + ": " + mAdded.get(i));
1700 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001701 }
1702 }
1703
1704 // Now save back stack.
1705 if (mBackStack != null) {
1706 N = mBackStack.size();
1707 if (N > 0) {
1708 backStack = new BackStackState[N];
1709 for (int i=0; i<N; i++) {
1710 backStack[i] = new BackStackState(this, mBackStack.get(i));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001711 if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
1712 + ": " + mBackStack.get(i));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001713 }
1714 }
1715 }
1716
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001717 FragmentManagerState fms = new FragmentManagerState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001718 fms.mActive = active;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001719 fms.mAdded = added;
1720 fms.mBackStack = backStack;
1721 return fms;
1722 }
1723
1724 void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) {
1725 // If there is no saved state at all, then there can not be
1726 // any nonConfig fragments either, so that is that.
1727 if (state == null) return;
1728 FragmentManagerState fms = (FragmentManagerState)state;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001729 if (fms.mActive == null) return;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001730
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001731 // First re-attach any non-config instances we are retaining back
1732 // to their saved state, so we don't try to instantiate them again.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001733 if (nonConfig != null) {
1734 for (int i=0; i<nonConfig.size(); i++) {
1735 Fragment f = nonConfig.get(i);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001736 if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001737 FragmentState fs = fms.mActive[f.mIndex];
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001738 fs.mInstance = f;
1739 f.mSavedViewState = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001740 f.mBackStackNesting = 0;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001741 f.mInLayout = false;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001742 f.mAdded = false;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001743 f.mTarget = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001744 if (fs.mSavedFragmentState != null) {
Dianne Hackborn51642462010-10-28 10:32:37 -07001745 fs.mSavedFragmentState.setClassLoader(mActivity.getClassLoader());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001746 f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
Dianne Hackborndef15372010-08-15 12:43:52 -07001747 FragmentManagerImpl.VIEW_STATE_TAG);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001748 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001749 }
1750 }
1751
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001752 // Build the full list of active fragments, instantiating them from
1753 // their saved state.
1754 mActive = new ArrayList<Fragment>(fms.mActive.length);
1755 if (mAvailIndices != null) {
1756 mAvailIndices.clear();
1757 }
1758 for (int i=0; i<fms.mActive.length; i++) {
1759 FragmentState fs = fms.mActive[i];
1760 if (fs != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001761 Fragment f = fs.instantiate(mActivity, mParent);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001762 if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001763 mActive.add(f);
Dianne Hackborn30d71892010-12-11 10:37:55 -08001764 // Now that the fragment is instantiated (or came from being
1765 // retained above), clear mInstance in case we end up re-restoring
1766 // from this FragmentState again.
1767 fs.mInstance = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001768 } else {
1769 mActive.add(null);
1770 if (mAvailIndices == null) {
1771 mAvailIndices = new ArrayList<Integer>();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001772 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001773 if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001774 mAvailIndices.add(i);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001775 }
1776 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001777
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001778 // Update the target of all retained fragments.
1779 if (nonConfig != null) {
1780 for (int i=0; i<nonConfig.size(); i++) {
1781 Fragment f = nonConfig.get(i);
Dianne Hackbornf9302322011-06-14 18:36:14 -07001782 if (f.mTargetIndex >= 0) {
1783 if (f.mTargetIndex < mActive.size()) {
1784 f.mTarget = mActive.get(f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001785 } else {
1786 Log.w(TAG, "Re-attaching retained fragment " + f
Dianne Hackbornf9302322011-06-14 18:36:14 -07001787 + " target no longer exists: " + f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001788 f.mTarget = null;
1789 }
1790 }
1791 }
1792 }
1793
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001794 // Build the list of currently added fragments.
1795 if (fms.mAdded != null) {
1796 mAdded = new ArrayList<Fragment>(fms.mAdded.length);
1797 for (int i=0; i<fms.mAdded.length; i++) {
1798 Fragment f = mActive.get(fms.mAdded[i]);
1799 if (f == null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001800 throwException(new IllegalStateException(
1801 "No instantiated fragment for index #" + fms.mAdded[i]));
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001802 }
1803 f.mAdded = true;
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001804 if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
1805 if (mAdded.contains(f)) {
1806 throw new IllegalStateException("Already added!");
1807 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001808 mAdded.add(f);
1809 }
1810 } else {
1811 mAdded = null;
1812 }
1813
1814 // Build the back stack.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001815 if (fms.mBackStack != null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001816 mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001817 for (int i=0; i<fms.mBackStack.length; i++) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001818 BackStackRecord bse = fms.mBackStack[i].instantiate(this);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001819 if (DEBUG) {
1820 Log.v(TAG, "restoreAllState: back stack #" + i
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001821 + " (index " + bse.mIndex + "): " + bse);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001822 LogWriter logw = new LogWriter(Log.VERBOSE, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001823 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001824 bse.dump(" ", pw, false);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001825 pw.flush();
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001826 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001827 mBackStack.add(bse);
Dianne Hackborndd913a52010-07-22 12:17:04 -07001828 if (bse.mIndex >= 0) {
1829 setBackStackIndex(bse.mIndex, bse);
1830 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001831 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001832 } else {
1833 mBackStack = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001834 }
1835 }
1836
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001837 public void attachActivity(Activity activity, FragmentContainer container, Fragment parent) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001838 if (mActivity != null) throw new IllegalStateException("Already attached");
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001839 mActivity = activity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001840 mContainer = container;
1841 mParent = parent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001842 }
1843
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001844 public void noteStateNotSaved() {
1845 mStateSaved = false;
1846 }
1847
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001848 public void dispatchCreate() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001849 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001850 moveToState(Fragment.CREATED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001851 }
1852
Dianne Hackbornc8017682010-07-06 13:34:38 -07001853 public void dispatchActivityCreated() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001854 mStateSaved = false;
Dianne Hackbornc8017682010-07-06 13:34:38 -07001855 moveToState(Fragment.ACTIVITY_CREATED, false);
1856 }
1857
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001858 public void dispatchStart() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001859 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001860 moveToState(Fragment.STARTED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001861 }
1862
1863 public void dispatchResume() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001864 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001865 moveToState(Fragment.RESUMED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001866 }
1867
1868 public void dispatchPause() {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001869 moveToState(Fragment.STARTED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001870 }
1871
1872 public void dispatchStop() {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001873 moveToState(Fragment.STOPPED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001874 }
1875
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001876 public void dispatchDestroyView() {
1877 moveToState(Fragment.CREATED, false);
1878 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001879
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001880 public void dispatchDestroy() {
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001881 mDestroyed = true;
Dianne Hackbornc6938232011-07-21 16:25:26 -07001882 execPendingActions();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001883 moveToState(Fragment.INITIALIZING, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001884 mActivity = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001885 mContainer = null;
1886 mParent = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001887 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07001888
Dianne Hackborn9d071802010-12-08 14:49:15 -08001889 public void dispatchConfigurationChanged(Configuration newConfig) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001890 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08001891 for (int i=0; i<mAdded.size(); i++) {
1892 Fragment f = mAdded.get(i);
1893 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001894 f.performConfigurationChanged(newConfig);
Dianne Hackborn9d071802010-12-08 14:49:15 -08001895 }
1896 }
1897 }
1898 }
1899
1900 public void dispatchLowMemory() {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001901 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08001902 for (int i=0; i<mAdded.size(); i++) {
1903 Fragment f = mAdded.get(i);
1904 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001905 f.performLowMemory();
Dianne Hackborn9d071802010-12-08 14:49:15 -08001906 }
1907 }
1908 }
1909 }
1910
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001911 public void dispatchTrimMemory(int level) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001912 if (mAdded != null) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001913 for (int i=0; i<mAdded.size(); i++) {
1914 Fragment f = mAdded.get(i);
1915 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001916 f.performTrimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001917 }
1918 }
1919 }
1920 }
1921
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001922 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1923 boolean show = false;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001924 ArrayList<Fragment> newMenus = null;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001925 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001926 for (int i=0; i<mAdded.size(); i++) {
1927 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001928 if (f != null) {
1929 if (f.performCreateOptionsMenu(menu, inflater)) {
1930 show = true;
1931 if (newMenus == null) {
1932 newMenus = new ArrayList<Fragment>();
1933 }
1934 newMenus.add(f);
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001935 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001936 }
1937 }
1938 }
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001939
1940 if (mCreatedMenus != null) {
1941 for (int i=0; i<mCreatedMenus.size(); i++) {
1942 Fragment f = mCreatedMenus.get(i);
1943 if (newMenus == null || !newMenus.contains(f)) {
1944 f.onDestroyOptionsMenu();
1945 }
1946 }
1947 }
1948
1949 mCreatedMenus = newMenus;
1950
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001951 return show;
1952 }
1953
1954 public boolean dispatchPrepareOptionsMenu(Menu menu) {
1955 boolean show = false;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001956 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001957 for (int i=0; i<mAdded.size(); i++) {
1958 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001959 if (f != null) {
1960 if (f.performPrepareOptionsMenu(menu)) {
1961 show = true;
1962 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001963 }
1964 }
1965 }
1966 return show;
1967 }
1968
1969 public boolean dispatchOptionsItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001970 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001971 for (int i=0; i<mAdded.size(); i++) {
1972 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001973 if (f != null) {
1974 if (f.performOptionsItemSelected(item)) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001975 return true;
1976 }
1977 }
1978 }
1979 }
1980 return false;
1981 }
1982
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001983 public boolean dispatchContextItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001984 if (mAdded != null) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001985 for (int i=0; i<mAdded.size(); i++) {
1986 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001987 if (f != null) {
1988 if (f.performContextItemSelected(item)) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001989 return true;
1990 }
1991 }
1992 }
1993 }
1994 return false;
1995 }
1996
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001997 public void dispatchOptionsMenuClosed(Menu menu) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001998 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001999 for (int i=0; i<mAdded.size(); i++) {
2000 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002001 if (f != null) {
2002 f.performOptionsMenuClosed(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002003 }
2004 }
2005 }
2006 }
Adam Powellf0f5fff2011-08-01 13:42:50 -07002007
2008 @Override
2009 public void invalidateOptionsMenu() {
2010 if (mActivity != null && mCurState == Fragment.RESUMED) {
2011 mActivity.invalidateOptionsMenu();
2012 } else {
2013 mNeedMenuInvalidate = true;
2014 }
2015 }
2016
Dianne Hackbornf121be72010-05-06 14:10:32 -07002017 public static int reverseTransit(int transit) {
2018 int rev = 0;
2019 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002020 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2021 rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002022 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002023 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2024 rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002025 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002026 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2027 rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002028 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002029 }
2030 return rev;
2031
2032 }
2033
2034 public static int transitToStyleIndex(int transit, boolean enter) {
2035 int animAttr = -1;
2036 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002037 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002038 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002039 ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation
2040 : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002041 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002042 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002043 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002044 ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation
2045 : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002046 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002047 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
Chet Haase9ff82bf2010-10-05 14:30:51 -07002048 animAttr = enter
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002049 ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation
2050 : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002051 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002052 }
2053 return animAttr;
2054 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002055}