blob: ef69fddf01e77aee8a8fea38efd58eaee2182883 [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;
Adam Powell371a8092014-06-20 12:51:12 -070022import android.content.Context;
Dianne Hackborn9d071802010-12-08 14:49:15 -080023import android.content.res.Configuration;
Dianne Hackbornf121be72010-05-06 14:10:32 -070024import android.content.res.TypedArray;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070025import android.os.Bundle;
Dianne Hackbornf43a33c2012-09-27 00:48:11 -070026import android.os.Debug;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070027import android.os.Handler;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -080028import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070029import android.os.Parcel;
30import android.os.Parcelable;
Adam Powell371a8092014-06-20 12:51:12 -070031import android.util.AttributeSet;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080032import android.util.DebugUtils;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070033import android.util.Log;
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -080034import android.util.LogWriter;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070035import android.util.SparseArray;
Adam Powell14874662013-07-18 19:42:41 -070036import android.util.SuperNotCalledException;
Adam Powell371a8092014-06-20 12:51:12 -070037import android.view.LayoutInflater;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070038import android.view.Menu;
39import android.view.MenuInflater;
40import android.view.MenuItem;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070041import android.view.View;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070042import android.view.ViewGroup;
Dianne Hackborn8c841092013-06-24 13:46:13 -070043import com.android.internal.util.FastPrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070044
Dianne Hackborn625ac272010-09-17 18:29:22 -070045import java.io.FileDescriptor;
46import java.io.PrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070047import java.util.ArrayList;
Dianne Hackbornd173fa32010-12-23 13:58:22 -080048import java.util.Arrays;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070049
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070050/**
51 * Interface for interacting with {@link Fragment} objects inside of an
52 * {@link Activity}
Joe Fernandezb54e7a32011-10-03 15:09:50 -070053 *
54 * <div class="special reference">
55 * <h3>Developer Guides</h3>
56 * <p>For more information about using fragments, read the
57 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
58 * </div>
Dianne Hackborn7871bad2011-12-12 15:19:26 -080059 *
60 * While the FragmentManager API was introduced in
61 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
62 * at is also available for use on older platforms through
63 * {@link android.support.v4.app.FragmentActivity}. See the blog post
64 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
65 * Fragments For All</a> for more details.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070066 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070067public abstract class FragmentManager {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070068 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070069 * Representation of an entry on the fragment back stack, as created
70 * with {@link FragmentTransaction#addToBackStack(String)
71 * FragmentTransaction.addToBackStack()}. Entries can later be
Dianne Hackborn327fbd22011-01-17 14:38:50 -080072 * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070073 * FragmentManager.getBackStackEntry()}.
74 *
75 * <p>Note that you should never hold on to a BackStackEntry object;
76 * the identifier as returned by {@link #getId} is the only thing that
77 * will be persisted across activity instances.
78 */
79 public interface BackStackEntry {
80 /**
81 * Return the unique identifier for the entry. This is the only
82 * representation of the entry that will persist across activity
83 * instances.
84 */
85 public int getId();
86
87 /**
Dianne Hackborn6c285972011-08-29 16:53:49 -070088 * Get the name that was supplied to
89 * {@link FragmentTransaction#addToBackStack(String)
90 * FragmentTransaction.addToBackStack(String)} when creating this entry.
91 */
92 public String getName();
93
94 /**
Dianne Hackborn327fbd22011-01-17 14:38:50 -080095 * Return the full bread crumb title resource identifier for the entry,
96 * or 0 if it does not have one.
97 */
98 public int getBreadCrumbTitleRes();
99
100 /**
101 * Return the short bread crumb title resource identifier for the entry,
102 * or 0 if it does not have one.
103 */
104 public int getBreadCrumbShortTitleRes();
105
106 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700107 * Return the full bread crumb title for the entry, or null if it
108 * does not have one.
109 */
110 public CharSequence getBreadCrumbTitle();
111
112 /**
113 * Return the short bread crumb title for the entry, or null if it
114 * does not have one.
115 */
116 public CharSequence getBreadCrumbShortTitle();
117 }
118
119 /**
120 * Interface to watch for changes to the back stack.
121 */
122 public interface OnBackStackChangedListener {
123 /**
124 * Called whenever the contents of the back stack change.
125 */
126 public void onBackStackChanged();
127 }
128
129 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700130 * Start a series of edit operations on the Fragments associated with
131 * this FragmentManager.
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700132 *
133 * <p>Note: A fragment transaction can only be created/committed prior
134 * to an activity saving its state. If you try to commit a transaction
135 * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()}
136 * (and prior to a following {@link Activity#onStart Activity.onStart}
137 * or {@link Activity#onResume Activity.onResume()}, you will get an error.
138 * This is because the framework takes care of saving your current fragments
139 * in the state, and if changes are made after the state is saved then they
140 * will be lost.</p>
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700141 */
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800142 public abstract FragmentTransaction beginTransaction();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700143
Dianne Hackborn17b9b812011-01-17 17:16:02 -0800144 /** @hide -- remove once prebuilts are in. */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800145 @Deprecated
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800146 public FragmentTransaction openTransaction() {
147 return beginTransaction();
148 }
149
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700150 /**
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800151 * After a {@link FragmentTransaction} is committed with
152 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
153 * is scheduled to be executed asynchronously on the process's main thread.
154 * If you want to immediately executing any such pending operations, you
155 * can call this function (only from the main thread) to do so. Note that
156 * all callbacks and other related behavior will be done from within this
157 * call, so be careful about where this is called from.
158 *
159 * @return Returns true if there were any pending transactions to be
160 * executed.
161 */
162 public abstract boolean executePendingTransactions();
163
164 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700165 * Finds a fragment that was identified by the given id either when inflated
166 * from XML or as the container ID when added in a transaction. This first
167 * searches through fragments that are currently added to the manager's
168 * activity; if no such fragment is found, then all fragments currently
169 * on the back stack associated with this ID are searched.
170 * @return The fragment if found or null otherwise.
171 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700172 public abstract Fragment findFragmentById(int id);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700173
174 /**
175 * Finds a fragment that was identified by the given tag either when inflated
176 * from XML or as supplied when added in a transaction. This first
177 * searches through fragments that are currently added to the manager's
178 * activity; if no such fragment is found, then all fragments currently
179 * on the back stack are searched.
180 * @return The fragment if found or null otherwise.
181 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700182 public abstract Fragment findFragmentByTag(String tag);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700183
184 /**
185 * Flag for {@link #popBackStack(String, int)}
186 * and {@link #popBackStack(int, int)}: If set, and the name or ID of
187 * a back stack entry has been supplied, then all matching entries will
188 * be consumed until one that doesn't match is found or the bottom of
189 * the stack is reached. Otherwise, all entries up to but not including that entry
190 * will be removed.
191 */
192 public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
193
194 /**
Ben Komalo87ffa202011-02-28 12:41:42 -0800195 * Pop the top state off the back stack. This function is asynchronous -- it
196 * enqueues the request to pop, but the action will not be performed until the
197 * application returns to its event loop.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700198 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800199 public abstract void popBackStack();
200
201 /**
202 * Like {@link #popBackStack()}, but performs the operation immediately
203 * inside of the call. This is like calling {@link #executePendingTransactions()}
204 * afterwards.
205 * @return Returns true if there was something popped, else false.
206 */
207 public abstract boolean popBackStackImmediate();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700208
209 /**
210 * Pop the last fragment transition from the manager's fragment
211 * back stack. If there is nothing to pop, false is returned.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800212 * This function is asynchronous -- it enqueues the
213 * request to pop, but the action will not be performed until the application
214 * returns to its event loop.
215 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700216 * @param name If non-null, this is the name of a previous back state
217 * to look for; if found, all states up to that state will be popped. The
218 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
219 * the named state itself is popped. If null, only the top state is popped.
220 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
221 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800222 public abstract void popBackStack(String name, int flags);
223
224 /**
225 * Like {@link #popBackStack(String, int)}, but performs the operation immediately
226 * inside of the call. This is like calling {@link #executePendingTransactions()}
227 * afterwards.
228 * @return Returns true if there was something popped, else false.
229 */
230 public abstract boolean popBackStackImmediate(String name, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700231
232 /**
233 * Pop all back stack states up to the one with the given identifier.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800234 * This function is asynchronous -- it enqueues the
235 * request to pop, but the action will not be performed until the application
236 * returns to its event loop.
237 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700238 * @param id Identifier of the stated to be popped. If no identifier exists,
239 * false is returned.
240 * The identifier is the number returned by
241 * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The
242 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
243 * the named state itself is popped.
244 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
245 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800246 public abstract void popBackStack(int id, int flags);
247
248 /**
249 * Like {@link #popBackStack(int, int)}, but performs the operation immediately
250 * inside of the call. This is like calling {@link #executePendingTransactions()}
251 * afterwards.
252 * @return Returns true if there was something popped, else false.
253 */
254 public abstract boolean popBackStackImmediate(int id, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700255
256 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700257 * Return the number of entries currently in the back stack.
258 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800259 public abstract int getBackStackEntryCount();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700260
261 /**
262 * Return the BackStackEntry at index <var>index</var> in the back stack;
263 * entries start index 0 being the bottom of the stack.
264 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800265 public abstract BackStackEntry getBackStackEntryAt(int index);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700266
267 /**
268 * Add a new listener for changes to the fragment back stack.
269 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700270 public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700271
272 /**
273 * Remove a listener that was previously added with
274 * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
275 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700276 public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700277
278 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700279 * Put a reference to a fragment in a Bundle. This Bundle can be
280 * persisted as saved state, and when later restoring
281 * {@link #getFragment(Bundle, String)} will return the current
282 * instance of the same fragment.
283 *
284 * @param bundle The bundle in which to put the fragment reference.
285 * @param key The name of the entry in the bundle.
286 * @param fragment The Fragment whose reference is to be stored.
287 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700288 public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700289
290 /**
291 * Retrieve the current Fragment instance for a reference previously
292 * placed with {@link #putFragment(Bundle, String, Fragment)}.
293 *
294 * @param bundle The bundle from which to retrieve the fragment reference.
295 * @param key The name of the entry in the bundle.
296 * @return Returns the current Fragment instance that is associated with
297 * the given reference.
298 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700299 public abstract Fragment getFragment(Bundle bundle, String key);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700300
301 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700302 * Save the current instance state of the given Fragment. This can be
303 * used later when creating a new instance of the Fragment and adding
304 * it to the fragment manager, to have it create itself to match the
305 * current state returned here. Note that there are limits on how
306 * this can be used:
307 *
308 * <ul>
309 * <li>The Fragment must currently be attached to the FragmentManager.
310 * <li>A new Fragment created using this saved state must be the same class
311 * type as the Fragment it was created from.
312 * <li>The saved state can not contain dependencies on other fragments --
313 * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
314 * store a fragment reference because that reference may not be valid when
315 * this saved state is later used. Likewise the Fragment's target and
316 * result code are not included in this state.
317 * </ul>
318 *
319 * @param f The Fragment whose state is to be saved.
320 * @return The generated state. This will be null if there was no
321 * interesting state created by the fragment.
322 */
323 public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
324
325 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700326 * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()}
327 * call has been made on the FragmentManager's Activity, so this instance is now dead.
328 */
329 public abstract boolean isDestroyed();
330
331 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700332 * Print the FragmentManager's state into the given stream.
333 *
334 * @param prefix Text to print at the front of each line.
335 * @param fd The raw file descriptor that the dump is being sent to.
336 * @param writer A PrintWriter to which the dump is to be set.
Dianne Hackborn30d71892010-12-11 10:37:55 -0800337 * @param args Additional arguments to the dump request.
Dianne Hackborn625ac272010-09-17 18:29:22 -0700338 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700339 public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800340
341 /**
342 * Control whether the framework's internal fragment manager debugging
343 * logs are turned on. If enabled, you will see output in logcat as
344 * the framework performs fragment operations.
345 */
346 public static void enableDebugLogging(boolean enabled) {
347 FragmentManagerImpl.DEBUG = enabled;
348 }
Adam Powellf0f5fff2011-08-01 13:42:50 -0700349
350 /**
351 * Invalidate the attached activity's options menu as necessary.
352 * This may end up being deferred until we move to the resumed state.
353 */
354 public void invalidateOptionsMenu() { }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700355}
356
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700357final class FragmentManagerState implements Parcelable {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700358 FragmentState[] mActive;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700359 int[] mAdded;
360 BackStackState[] mBackStack;
361
362 public FragmentManagerState() {
363 }
364
365 public FragmentManagerState(Parcel in) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700366 mActive = in.createTypedArray(FragmentState.CREATOR);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700367 mAdded = in.createIntArray();
368 mBackStack = in.createTypedArray(BackStackState.CREATOR);
369 }
370
371 public int describeContents() {
372 return 0;
373 }
374
375 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700376 dest.writeTypedArray(mActive, flags);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700377 dest.writeIntArray(mAdded);
378 dest.writeTypedArray(mBackStack, flags);
379 }
380
381 public static final Parcelable.Creator<FragmentManagerState> CREATOR
382 = new Parcelable.Creator<FragmentManagerState>() {
383 public FragmentManagerState createFromParcel(Parcel in) {
384 return new FragmentManagerState(in);
385 }
386
387 public FragmentManagerState[] newArray(int size) {
388 return new FragmentManagerState[size];
389 }
390 };
Dianne Hackbornba51c3d2010-05-05 18:49:48 -0700391}
392
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700393/**
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700394 * Callbacks from FragmentManagerImpl to its container.
395 */
396interface FragmentContainer {
397 public View findViewById(int id);
398}
399
400/**
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700401 * Container for fragments associated with an activity.
402 */
Adam Powell371a8092014-06-20 12:51:12 -0700403final class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 {
Craig Mautner1c437192012-08-01 10:01:16 -0700404 static boolean DEBUG = false;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700405 static final String TAG = "FragmentManager";
406
Dianne Hackborndef15372010-08-15 12:43:52 -0700407 static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
408 static final String TARGET_STATE_TAG = "android:target_state";
409 static final String VIEW_STATE_TAG = "android:view_state";
Adam Powell78fed9b2011-11-07 10:45:34 -0800410 static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
Dianne Hackborndef15372010-08-15 12:43:52 -0700411
Dianne Hackborn445646c2010-06-25 15:52:59 -0700412 ArrayList<Runnable> mPendingActions;
413 Runnable[] mTmpActions;
414 boolean mExecutingActions;
415
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700416 ArrayList<Fragment> mActive;
417 ArrayList<Fragment> mAdded;
418 ArrayList<Integer> mAvailIndices;
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700419 ArrayList<BackStackRecord> mBackStack;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -0700420 ArrayList<Fragment> mCreatedMenus;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700421
Dianne Hackborndd913a52010-07-22 12:17:04 -0700422 // Must be accessed while locked.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700423 ArrayList<BackStackRecord> mBackStackIndices;
Dianne Hackborndd913a52010-07-22 12:17:04 -0700424 ArrayList<Integer> mAvailBackStackIndices;
425
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700426 ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
427
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700428 int mCurState = Fragment.INITIALIZING;
429 Activity mActivity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700430 FragmentContainer mContainer;
431 Fragment mParent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700432
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700433 boolean mNeedMenuInvalidate;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700434 boolean mStateSaved;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800435 boolean mDestroyed;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700436 String mNoTransactionsBecause;
Adam Powell78fed9b2011-11-07 10:45:34 -0800437 boolean mHavePendingDeferredStart;
Adam Powell371a8092014-06-20 12:51:12 -0700438
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700439 // Temporary vars for state save and restore.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700440 Bundle mStateBundle = null;
441 SparseArray<Parcelable> mStateArray = null;
442
Dianne Hackborn445646c2010-06-25 15:52:59 -0700443 Runnable mExecCommit = new Runnable() {
444 @Override
445 public void run() {
446 execPendingActions();
447 }
448 };
Dianne Hackborn625ac272010-09-17 18:29:22 -0700449
Dianne Hackborn4702a852012-08-17 15:18:29 -0700450 private void throwException(RuntimeException ex) {
451 Log.e(TAG, ex.getMessage());
452 LogWriter logw = new LogWriter(Log.ERROR, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700453 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Dianne Hackborn4702a852012-08-17 15:18:29 -0700454 if (mActivity != null) {
455 Log.e(TAG, "Activity state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700456 try {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700457 mActivity.dump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700458 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700459 pw.flush();
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700460 Log.e(TAG, "Failed dumping state", e);
461 }
462 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700463 Log.e(TAG, "Fragment manager state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700464 try {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700465 dump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700466 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700467 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700468 Log.e(TAG, "Failed dumping state", e);
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700469 }
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700470 }
Dianne Hackborn8c841092013-06-24 13:46:13 -0700471 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700472 throw ex;
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700473 }
474
Dianne Hackborn625ac272010-09-17 18:29:22 -0700475 @Override
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800476 public FragmentTransaction beginTransaction() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700477 return new BackStackRecord(this);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700478 }
479
Dianne Hackborn625ac272010-09-17 18:29:22 -0700480 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800481 public boolean executePendingTransactions() {
482 return execPendingActions();
483 }
484
485 @Override
486 public void popBackStack() {
487 enqueueAction(new Runnable() {
488 @Override public void run() {
489 popBackStackState(mActivity.mHandler, null, -1, 0);
490 }
491 }, false);
492 }
493
494 @Override
495 public boolean popBackStackImmediate() {
496 checkStateLoss();
497 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700498 return popBackStackState(mActivity.mHandler, null, -1, 0);
499 }
500
Dianne Hackborn625ac272010-09-17 18:29:22 -0700501 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800502 public void popBackStack(final String name, final int flags) {
503 enqueueAction(new Runnable() {
504 @Override public void run() {
505 popBackStackState(mActivity.mHandler, name, -1, flags);
506 }
507 }, false);
508 }
509
510 @Override
511 public boolean popBackStackImmediate(String name, int flags) {
512 checkStateLoss();
513 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700514 return popBackStackState(mActivity.mHandler, name, -1, flags);
515 }
516
Dianne Hackborn625ac272010-09-17 18:29:22 -0700517 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800518 public void popBackStack(final int id, final int flags) {
519 if (id < 0) {
520 throw new IllegalArgumentException("Bad id: " + id);
521 }
522 enqueueAction(new Runnable() {
523 @Override public void run() {
524 popBackStackState(mActivity.mHandler, null, id, flags);
525 }
526 }, false);
527 }
528
529 @Override
530 public boolean popBackStackImmediate(int id, int flags) {
531 checkStateLoss();
532 executePendingTransactions();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700533 if (id < 0) {
534 throw new IllegalArgumentException("Bad id: " + id);
535 }
536 return popBackStackState(mActivity.mHandler, null, id, flags);
537 }
538
Dianne Hackborn625ac272010-09-17 18:29:22 -0700539 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800540 public int getBackStackEntryCount() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700541 return mBackStack != null ? mBackStack.size() : 0;
542 }
543
Dianne Hackborn625ac272010-09-17 18:29:22 -0700544 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800545 public BackStackEntry getBackStackEntryAt(int index) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700546 return mBackStack.get(index);
547 }
548
Dianne Hackborn625ac272010-09-17 18:29:22 -0700549 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700550 public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
551 if (mBackStackChangeListeners == null) {
552 mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
553 }
554 mBackStackChangeListeners.add(listener);
555 }
556
Dianne Hackborn625ac272010-09-17 18:29:22 -0700557 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700558 public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
559 if (mBackStackChangeListeners != null) {
560 mBackStackChangeListeners.remove(listener);
561 }
562 }
563
Dianne Hackborn625ac272010-09-17 18:29:22 -0700564 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700565 public void putFragment(Bundle bundle, String key, Fragment fragment) {
566 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700567 throwException(new IllegalStateException("Fragment " + fragment
568 + " is not currently in the FragmentManager"));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700569 }
570 bundle.putInt(key, fragment.mIndex);
571 }
572
Dianne Hackborn625ac272010-09-17 18:29:22 -0700573 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700574 public Fragment getFragment(Bundle bundle, String key) {
Dianne Hackborndef15372010-08-15 12:43:52 -0700575 int index = bundle.getInt(key, -1);
576 if (index == -1) {
577 return null;
578 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700579 if (index >= mActive.size()) {
Cyril Mottier2de50822013-09-30 22:42:26 +0200580 throwException(new IllegalStateException("Fragment no longer exists for key "
Dianne Hackborn4702a852012-08-17 15:18:29 -0700581 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700582 }
583 Fragment f = mActive.get(index);
584 if (f == null) {
Cyril Mottier2de50822013-09-30 22:42:26 +0200585 throwException(new IllegalStateException("Fragment no longer exists for key "
Dianne Hackborn4702a852012-08-17 15:18:29 -0700586 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700587 }
588 return f;
589 }
590
Dianne Hackborn625ac272010-09-17 18:29:22 -0700591 @Override
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700592 public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
593 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700594 throwException(new IllegalStateException("Fragment " + fragment
595 + " is not currently in the FragmentManager"));
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700596 }
597 if (fragment.mState > Fragment.INITIALIZING) {
598 Bundle result = saveFragmentBasicState(fragment);
599 return result != null ? new Fragment.SavedState(result) : null;
600 }
601 return null;
602 }
603
604 @Override
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700605 public boolean isDestroyed() {
606 return mDestroyed;
607 }
608
609 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800610 public String toString() {
611 StringBuilder sb = new StringBuilder(128);
612 sb.append("FragmentManager{");
613 sb.append(Integer.toHexString(System.identityHashCode(this)));
614 sb.append(" in ");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700615 if (mParent != null) {
616 DebugUtils.buildShortClassTag(mParent, sb);
617 } else {
618 DebugUtils.buildShortClassTag(mActivity, sb);
619 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800620 sb.append("}}");
621 return sb.toString();
622 }
623
624 @Override
Dianne Hackborn625ac272010-09-17 18:29:22 -0700625 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700626 String innerPrefix = prefix + " ";
627
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800628 int N;
629 if (mActive != null) {
630 N = mActive.size();
631 if (N > 0) {
632 writer.print(prefix); writer.print("Active Fragments in ");
633 writer.print(Integer.toHexString(System.identityHashCode(this)));
634 writer.println(":");
635 for (int i=0; i<N; i++) {
636 Fragment f = mActive.get(i);
637 writer.print(prefix); writer.print(" #"); writer.print(i);
638 writer.print(": "); writer.println(f);
639 if (f != null) {
640 f.dump(innerPrefix, fd, writer, args);
641 }
642 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700643 }
644 }
645
646 if (mAdded != null) {
647 N = mAdded.size();
648 if (N > 0) {
649 writer.print(prefix); writer.println("Added Fragments:");
650 for (int i=0; i<N; i++) {
651 Fragment f = mAdded.get(i);
652 writer.print(prefix); writer.print(" #"); writer.print(i);
653 writer.print(": "); writer.println(f.toString());
654 }
655 }
656 }
657
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800658 if (mCreatedMenus != null) {
659 N = mCreatedMenus.size();
660 if (N > 0) {
661 writer.print(prefix); writer.println("Fragments Created Menus:");
662 for (int i=0; i<N; i++) {
663 Fragment f = mCreatedMenus.get(i);
664 writer.print(prefix); writer.print(" #"); writer.print(i);
665 writer.print(": "); writer.println(f.toString());
666 }
667 }
668 }
669
Dianne Hackborn625ac272010-09-17 18:29:22 -0700670 if (mBackStack != null) {
671 N = mBackStack.size();
672 if (N > 0) {
673 writer.print(prefix); writer.println("Back Stack:");
674 for (int i=0; i<N; i++) {
675 BackStackRecord bs = mBackStack.get(i);
676 writer.print(prefix); writer.print(" #"); writer.print(i);
677 writer.print(": "); writer.println(bs.toString());
Dianne Hackborn30d71892010-12-11 10:37:55 -0800678 bs.dump(innerPrefix, fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700679 }
680 }
681 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800682
683 synchronized (this) {
684 if (mBackStackIndices != null) {
685 N = mBackStackIndices.size();
686 if (N > 0) {
687 writer.print(prefix); writer.println("Back Stack Indices:");
688 for (int i=0; i<N; i++) {
689 BackStackRecord bs = mBackStackIndices.get(i);
690 writer.print(prefix); writer.print(" #"); writer.print(i);
691 writer.print(": "); writer.println(bs);
692 }
693 }
694 }
695
696 if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
697 writer.print(prefix); writer.print("mAvailBackStackIndices: ");
698 writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
699 }
700 }
701
702 if (mPendingActions != null) {
703 N = mPendingActions.size();
704 if (N > 0) {
705 writer.print(prefix); writer.println("Pending Actions:");
706 for (int i=0; i<N; i++) {
707 Runnable r = mPendingActions.get(i);
708 writer.print(prefix); writer.print(" #"); writer.print(i);
709 writer.print(": "); writer.println(r);
710 }
711 }
712 }
713
714 writer.print(prefix); writer.println("FragmentManager misc state:");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700715 writer.print(prefix); writer.print(" mActivity="); writer.println(mActivity);
716 writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer);
717 if (mParent != null) {
718 writer.print(prefix); writer.print(" mParent="); writer.println(mParent);
719 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800720 writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState);
721 writer.print(" mStateSaved="); writer.print(mStateSaved);
722 writer.print(" mDestroyed="); writer.println(mDestroyed);
723 if (mNeedMenuInvalidate) {
724 writer.print(prefix); writer.print(" mNeedMenuInvalidate=");
725 writer.println(mNeedMenuInvalidate);
726 }
727 if (mNoTransactionsBecause != null) {
728 writer.print(prefix); writer.print(" mNoTransactionsBecause=");
729 writer.println(mNoTransactionsBecause);
730 }
731 if (mAvailIndices != null && mAvailIndices.size() > 0) {
732 writer.print(prefix); writer.print(" mAvailIndices: ");
733 writer.println(Arrays.toString(mAvailIndices.toArray()));
734 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700735 }
736
Chet Haasea18a86b2010-09-07 13:20:00 -0700737 Animator loadAnimator(Fragment fragment, int transit, boolean enter,
Dianne Hackbornf121be72010-05-06 14:10:32 -0700738 int transitionStyle) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700739 Animator animObj = fragment.onCreateAnimator(transit, enter,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700740 fragment.mNextAnim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700741 if (animObj != null) {
742 return animObj;
743 }
744
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700745 if (fragment.mNextAnim != 0) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700746 Animator anim = AnimatorInflater.loadAnimator(mActivity, fragment.mNextAnim);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700747 if (anim != null) {
748 return anim;
749 }
750 }
751
Dianne Hackbornf121be72010-05-06 14:10:32 -0700752 if (transit == 0) {
753 return null;
754 }
755
756 int styleIndex = transitToStyleIndex(transit, enter);
757 if (styleIndex < 0) {
758 return null;
759 }
760
761 if (transitionStyle == 0 && mActivity.getWindow() != null) {
762 transitionStyle = mActivity.getWindow().getAttributes().windowAnimations;
763 }
764 if (transitionStyle == 0) {
765 return null;
766 }
767
768 TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
Chet Haase811ed1062010-08-06 10:38:15 -0700769 com.android.internal.R.styleable.FragmentAnimation);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700770 int anim = attrs.getResourceId(styleIndex, 0);
771 attrs.recycle();
772
773 if (anim == 0) {
774 return null;
775 }
776
Chet Haasea18a86b2010-09-07 13:20:00 -0700777 return AnimatorInflater.loadAnimator(mActivity, anim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700778 }
779
Adam Powell635c60a2011-10-26 10:22:16 -0700780 public void performPendingDeferredStart(Fragment f) {
781 if (f.mDeferStart) {
Adam Powell78fed9b2011-11-07 10:45:34 -0800782 if (mExecutingActions) {
783 // Wait until we're done executing our pending transactions
784 mHavePendingDeferredStart = true;
785 return;
786 }
Adam Powell635c60a2011-10-26 10:22:16 -0700787 f.mDeferStart = false;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700788 moveToState(f, mCurState, 0, 0, false);
Adam Powell635c60a2011-10-26 10:22:16 -0700789 }
790 }
791
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700792 void moveToState(Fragment f, int newState, int transit, int transitionStyle,
793 boolean keepActive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -0700794 if (DEBUG && false) Log.v(TAG, "moveToState: " + f
795 + " oldState=" + f.mState + " newState=" + newState
796 + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
Craig Mautner1c437192012-08-01 10:01:16 -0700797
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700798 // Fragments that are not currently added will sit in the onCreate() state.
Dianne Hackborne181bd92012-09-25 14:15:15 -0700799 if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700800 newState = Fragment.CREATED;
801 }
Dianne Hackbornf9302322011-06-14 18:36:14 -0700802 if (f.mRemoving && newState > f.mState) {
803 // While removing a fragment, we can't change it to a higher state.
804 newState = f.mState;
805 }
Adam Powell2db4e4b2011-11-02 14:30:47 -0700806 // Defer start if requested; don't allow it to move to STARTED or higher
807 // if it's not already started.
808 if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
Adam Powell635c60a2011-10-26 10:22:16 -0700809 newState = Fragment.STOPPED;
810 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700811 if (f.mState < newState) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -0800812 // For fragments that are created from a layout, when restoring from
813 // state we don't want to allow them to be created until they are
814 // being reloaded from the layout.
815 if (f.mFromLayout && !f.mInLayout) {
816 return;
817 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800818 if (f.mAnimatingAway != null) {
819 // The fragment is currently being animated... but! Now we
820 // want to move our state back up. Give up on waiting for the
821 // animation, move to whatever the final state should be once
822 // the animation is done, and then we can proceed from there.
823 f.mAnimatingAway = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700824 moveToState(f, f.mStateAfterAnimating, 0, 0, true);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800825 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700826 switch (f.mState) {
827 case Fragment.INITIALIZING:
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700828 if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
Dianne Hackborndef15372010-08-15 12:43:52 -0700829 if (f.mSavedFragmentState != null) {
830 f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
831 FragmentManagerImpl.VIEW_STATE_TAG);
832 f.mTarget = getFragment(f.mSavedFragmentState,
833 FragmentManagerImpl.TARGET_STATE_TAG);
834 if (f.mTarget != null) {
835 f.mTargetRequestCode = f.mSavedFragmentState.getInt(
836 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
837 }
Adam Powell78fed9b2011-11-07 10:45:34 -0800838 f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
839 FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
840 if (!f.mUserVisibleHint) {
841 f.mDeferStart = true;
842 if (newState > Fragment.STOPPED) {
843 newState = Fragment.STOPPED;
844 }
845 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700846 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700847 f.mActivity = mActivity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700848 f.mParentFragment = mParent;
849 f.mFragmentManager = mParent != null
850 ? mParent.mChildFragmentManager : mActivity.mFragments;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700851 f.mCalled = false;
852 f.onAttach(mActivity);
853 if (!f.mCalled) {
854 throw new SuperNotCalledException("Fragment " + f
855 + " did not call through to super.onAttach()");
856 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700857 if (f.mParentFragment == null) {
858 mActivity.onAttachFragment(f);
859 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700860
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700861 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700862 f.performCreate(f.mSavedFragmentState);
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700863 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700864 f.mRetaining = false;
865 if (f.mFromLayout) {
866 // For fragments that are part of the content view
867 // layout, we need to instantiate the view immediately
868 // and the inflater will take care of adding it.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700869 f.mView = f.performCreateView(f.getLayoutInflater(
870 f.mSavedFragmentState), null, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700871 if (f.mView != null) {
872 f.mView.setSaveFromParentEnabled(false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700873 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700874 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700875 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700876 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700877 case Fragment.CREATED:
878 if (newState > Fragment.CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700879 if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700880 if (!f.mFromLayout) {
881 ViewGroup container = null;
882 if (f.mContainerId != 0) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700883 container = (ViewGroup)mContainer.findViewById(f.mContainerId);
Dianne Hackborn352cc982011-01-04 11:34:18 -0800884 if (container == null && !f.mRestored) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700885 throwException(new IllegalArgumentException(
886 "No view found for id 0x"
887 + Integer.toHexString(f.mContainerId) + " ("
888 + f.getResources().getResourceName(f.mContainerId)
889 + ") for fragment " + f));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700890 }
891 }
892 f.mContainer = container;
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700893 f.mView = f.performCreateView(f.getLayoutInflater(
894 f.mSavedFragmentState), container, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700895 if (f.mView != null) {
896 f.mView.setSaveFromParentEnabled(false);
897 if (container != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700898 Animator anim = loadAnimator(f, transit, true,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700899 transitionStyle);
900 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700901 anim.setTarget(f.mView);
Chet Haase811ed1062010-08-06 10:38:15 -0700902 anim.start();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700903 }
904 container.addView(f.mView);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700905 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700906 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700907 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700908 }
909 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700910
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700911 f.performActivityCreated(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700912 if (f.mView != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700913 f.restoreViewState(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700914 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700915 f.mSavedFragmentState = null;
916 }
Dianne Hackbornc8017682010-07-06 13:34:38 -0700917 case Fragment.ACTIVITY_CREATED:
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700918 case Fragment.STOPPED:
919 if (newState > Fragment.STOPPED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700920 if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700921 f.performStart();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700922 }
923 case Fragment.STARTED:
924 if (newState > Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700925 if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700926 f.mResumed = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700927 f.performResume();
Adam Powell95202512011-08-07 17:20:17 -0700928 // Get rid of this in case we saved it and never needed it.
929 f.mSavedFragmentState = null;
930 f.mSavedViewState = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700931 }
932 }
933 } else if (f.mState > newState) {
934 switch (f.mState) {
935 case Fragment.RESUMED:
936 if (newState < Fragment.RESUMED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700937 if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700938 f.performPause();
Dianne Hackborn2707d602010-07-09 18:01:20 -0700939 f.mResumed = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700940 }
941 case Fragment.STARTED:
942 if (newState < Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700943 if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
Dianne Hackborn2707d602010-07-09 18:01:20 -0700944 f.performStop();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700945 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700946 case Fragment.STOPPED:
Dianne Hackbornc8017682010-07-06 13:34:38 -0700947 case Fragment.ACTIVITY_CREATED:
948 if (newState < Fragment.ACTIVITY_CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700949 if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700950 if (f.mView != null) {
951 // Need to save the current view state if not
952 // done already.
Dianne Hackborn625ac272010-09-17 18:29:22 -0700953 if (!mActivity.isFinishing() && f.mSavedViewState == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700954 saveFragmentViewState(f);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700955 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700956 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700957 f.performDestroyView();
Dianne Hackborndef15372010-08-15 12:43:52 -0700958 if (f.mView != null && f.mContainer != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700959 Animator anim = null;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800960 if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
Chet Haaseb20db3e2010-09-10 13:07:30 -0700961 anim = loadAnimator(f, transit, false,
Dianne Hackborndef15372010-08-15 12:43:52 -0700962 transitionStyle);
Chet Haaseb20db3e2010-09-10 13:07:30 -0700963 }
964 if (anim != null) {
965 final ViewGroup container = f.mContainer;
966 final View view = f.mView;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800967 final Fragment fragment = f;
Chet Haaseb20db3e2010-09-10 13:07:30 -0700968 container.startViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800969 f.mAnimatingAway = anim;
970 f.mStateAfterAnimating = newState;
Chet Haaseb20db3e2010-09-10 13:07:30 -0700971 anim.addListener(new AnimatorListenerAdapter() {
972 @Override
973 public void onAnimationEnd(Animator anim) {
974 container.endViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800975 if (fragment.mAnimatingAway != null) {
976 fragment.mAnimatingAway = null;
977 moveToState(fragment, fragment.mStateAfterAnimating,
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700978 0, 0, false);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800979 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 }
Chet Haaseb20db3e2010-09-10 13:07:30 -0700981 });
982 anim.setTarget(f.mView);
983 anim.start();
984
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700985 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700986 f.mContainer.removeView(f.mView);
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700987 }
988 f.mContainer = null;
989 f.mView = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700990 }
991 case Fragment.CREATED:
992 if (newState < Fragment.CREATED) {
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800993 if (mDestroyed) {
994 if (f.mAnimatingAway != null) {
995 // The fragment's containing activity is
996 // being destroyed, but this fragment is
997 // currently animating away. Stop the
998 // animation right now -- it is not needed,
999 // and we can't wait any more on destroying
1000 // the fragment.
Dianne Hackborn1b39e222010-12-28 14:17:18 -08001001 Animator anim = f.mAnimatingAway;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001002 f.mAnimatingAway = null;
Dianne Hackborn1b39e222010-12-28 14:17:18 -08001003 anim.cancel();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001004 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001005 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001006 if (f.mAnimatingAway != null) {
1007 // We are waiting for the fragment's view to finish
1008 // animating away. Just make a note of the state
1009 // the fragment now should move to once the animation
1010 // is done.
1011 f.mStateAfterAnimating = newState;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001012 newState = Fragment.CREATED;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001013 } else {
1014 if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1015 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001016 f.performDestroy();
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001017 }
1018
1019 f.mCalled = false;
1020 f.onDetach();
1021 if (!f.mCalled) {
1022 throw new SuperNotCalledException("Fragment " + f
1023 + " did not call through to super.onDetach()");
1024 }
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001025 if (!keepActive) {
1026 if (!f.mRetaining) {
1027 makeInactive(f);
1028 } else {
1029 f.mActivity = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001030 f.mParentFragment = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001031 f.mFragmentManager = null;
Tim Kilbourn70717862014-02-28 01:00:06 -08001032 f.mChildFragmentManager = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001033 }
Dianne Hackbornf9302322011-06-14 18:36:14 -07001034 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001035 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001036 }
1037 }
1038 }
1039
1040 f.mState = newState;
1041 }
1042
Dianne Hackborn625ac272010-09-17 18:29:22 -07001043 void moveToState(Fragment f) {
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001044 moveToState(f, mCurState, 0, 0, false);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001045 }
1046
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001047 void moveToState(int newState, boolean always) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001048 moveToState(newState, 0, 0, always);
1049 }
1050
1051 void moveToState(int newState, int transit, int transitStyle, boolean always) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001052 if (mActivity == null && newState != Fragment.INITIALIZING) {
1053 throw new IllegalStateException("No activity");
1054 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001055
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001056 if (!always && mCurState == newState) {
1057 return;
1058 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001059
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001060 mCurState = newState;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001061 if (mActive != null) {
Adam Powell635c60a2011-10-26 10:22:16 -07001062 boolean loadersRunning = false;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001063 for (int i=0; i<mActive.size(); i++) {
1064 Fragment f = mActive.get(i);
1065 if (f != null) {
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001066 moveToState(f, newState, transit, transitStyle, false);
Adam Powell635c60a2011-10-26 10:22:16 -07001067 if (f.mLoaderManager != null) {
1068 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1069 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001070 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001071 }
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001072
Adam Powell635c60a2011-10-26 10:22:16 -07001073 if (!loadersRunning) {
1074 startPendingDeferredFragments();
1075 }
1076
Adam Powell89b09da2011-07-27 11:55:29 -07001077 if (mNeedMenuInvalidate && mActivity != null && mCurState == Fragment.RESUMED) {
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001078 mActivity.invalidateOptionsMenu();
1079 mNeedMenuInvalidate = false;
1080 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001081 }
1082 }
1083
Adam Powell635c60a2011-10-26 10:22:16 -07001084 void startPendingDeferredFragments() {
Adam Powell37510902011-10-31 11:48:24 -07001085 if (mActive == null) return;
1086
Adam Powell635c60a2011-10-26 10:22:16 -07001087 for (int i=0; i<mActive.size(); i++) {
1088 Fragment f = mActive.get(i);
1089 if (f != null) {
1090 performPendingDeferredStart(f);
1091 }
1092 }
1093 }
1094
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001095 void makeActive(Fragment f) {
1096 if (f.mIndex >= 0) {
1097 return;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001098 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001099
1100 if (mAvailIndices == null || mAvailIndices.size() <= 0) {
1101 if (mActive == null) {
1102 mActive = new ArrayList<Fragment>();
1103 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001104 f.setIndex(mActive.size(), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001105 mActive.add(f);
1106
1107 } else {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001108 f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001109 mActive.set(f.mIndex, f);
1110 }
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001111 if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001112 }
1113
1114 void makeInactive(Fragment f) {
1115 if (f.mIndex < 0) {
1116 return;
1117 }
1118
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001119 if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001120 mActive.set(f.mIndex, null);
1121 if (mAvailIndices == null) {
1122 mAvailIndices = new ArrayList<Integer>();
1123 }
1124 mAvailIndices.add(f.mIndex);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001125 mActivity.invalidateFragment(f.mWho);
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001126 f.initState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001127 }
1128
1129 public void addFragment(Fragment fragment, boolean moveToStateNow) {
1130 if (mAdded == null) {
1131 mAdded = new ArrayList<Fragment>();
1132 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001133 if (DEBUG) Log.v(TAG, "add: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001134 makeActive(fragment);
1135 if (!fragment.mDetached) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001136 if (mAdded.contains(fragment)) {
1137 throw new IllegalStateException("Fragment already added: " + fragment);
1138 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001139 mAdded.add(fragment);
1140 fragment.mAdded = true;
1141 fragment.mRemoving = false;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001142 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001143 mNeedMenuInvalidate = true;
1144 }
1145 if (moveToStateNow) {
1146 moveToState(fragment);
1147 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001148 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001149 }
1150
Dianne Hackbornf121be72010-05-06 14:10:32 -07001151 public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001152 if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001153 final boolean inactive = !fragment.isInBackStack();
1154 if (!fragment.mDetached || inactive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001155 if (false) {
1156 // Would be nice to catch a bad remove here, but we need
1157 // time to test this to make sure we aren't crashes cases
1158 // where it is not a problem.
1159 if (!mAdded.contains(fragment)) {
1160 throw new IllegalStateException("Fragment not added: " + fragment);
1161 }
1162 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001163 if (mAdded != null) {
1164 mAdded.remove(fragment);
1165 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001166 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001167 mNeedMenuInvalidate = true;
1168 }
1169 fragment.mAdded = false;
1170 fragment.mRemoving = true;
1171 moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED,
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001172 transition, transitionStyle, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001173 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07001174 }
1175
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001176 public void hideFragment(Fragment fragment, int transition, int transitionStyle) {
1177 if (DEBUG) Log.v(TAG, "hide: " + fragment);
1178 if (!fragment.mHidden) {
1179 fragment.mHidden = true;
1180 if (fragment.mView != null) {
Adam Powell27562932013-06-07 10:16:08 -07001181 Animator anim = loadAnimator(fragment, transition, false,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001182 transitionStyle);
1183 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001184 anim.setTarget(fragment.mView);
Chet Haase61eb40d2010-12-28 13:59:17 -08001185 // Delay the actual hide operation until the animation finishes, otherwise
1186 // the fragment will just immediately disappear
1187 final Fragment finalFragment = fragment;
1188 anim.addListener(new AnimatorListenerAdapter() {
1189 @Override
1190 public void onAnimationEnd(Animator animation) {
Chet Haaseb29407f2011-01-11 14:09:34 -08001191 if (finalFragment.mView != null) {
1192 finalFragment.mView.setVisibility(View.GONE);
1193 }
Chet Haase61eb40d2010-12-28 13:59:17 -08001194 }
1195 });
Chet Haase811ed1062010-08-06 10:38:15 -07001196 anim.start();
Chet Haasee646b28c2010-12-28 14:48:32 -08001197 } else {
1198 fragment.mView.setVisibility(View.GONE);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001199 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001200 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001201 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001202 mNeedMenuInvalidate = true;
1203 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001204 fragment.onHiddenChanged(true);
1205 }
1206 }
1207
1208 public void showFragment(Fragment fragment, int transition, int transitionStyle) {
1209 if (DEBUG) Log.v(TAG, "show: " + fragment);
1210 if (fragment.mHidden) {
1211 fragment.mHidden = false;
1212 if (fragment.mView != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -07001213 Animator anim = loadAnimator(fragment, transition, true,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001214 transitionStyle);
1215 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001216 anim.setTarget(fragment.mView);
Chet Haase811ed1062010-08-06 10:38:15 -07001217 anim.start();
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001218 }
1219 fragment.mView.setVisibility(View.VISIBLE);
1220 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001221 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001222 mNeedMenuInvalidate = true;
1223 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001224 fragment.onHiddenChanged(false);
1225 }
1226 }
1227
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001228 public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
1229 if (DEBUG) Log.v(TAG, "detach: " + fragment);
1230 if (!fragment.mDetached) {
1231 fragment.mDetached = true;
1232 if (fragment.mAdded) {
1233 // We are not already in back stack, so need to remove the fragment.
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001234 if (mAdded != null) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001235 if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001236 mAdded.remove(fragment);
1237 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001238 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001239 mNeedMenuInvalidate = true;
1240 }
1241 fragment.mAdded = false;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001242 moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001243 }
1244 }
1245 }
1246
1247 public void attachFragment(Fragment fragment, int transition, int transitionStyle) {
1248 if (DEBUG) Log.v(TAG, "attach: " + fragment);
1249 if (fragment.mDetached) {
1250 fragment.mDetached = false;
1251 if (!fragment.mAdded) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001252 if (mAdded == null) {
1253 mAdded = new ArrayList<Fragment>();
1254 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001255 if (mAdded.contains(fragment)) {
1256 throw new IllegalStateException("Fragment already added: " + fragment);
1257 }
1258 if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001259 mAdded.add(fragment);
1260 fragment.mAdded = true;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001261 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001262 mNeedMenuInvalidate = true;
1263 }
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001264 moveToState(fragment, mCurState, transition, transitionStyle, false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001265 }
1266 }
1267 }
1268
Dianne Hackbornf121be72010-05-06 14:10:32 -07001269 public Fragment findFragmentById(int id) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001270 if (mAdded != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001271 // First look through added fragments.
1272 for (int i=mAdded.size()-1; i>=0; i--) {
1273 Fragment f = mAdded.get(i);
1274 if (f != null && f.mFragmentId == id) {
1275 return f;
1276 }
1277 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001278 }
1279 if (mActive != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001280 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001281 for (int i=mActive.size()-1; i>=0; i--) {
1282 Fragment f = mActive.get(i);
1283 if (f != null && f.mFragmentId == id) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001284 return f;
1285 }
1286 }
1287 }
1288 return null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001289 }
1290
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001291 public Fragment findFragmentByTag(String tag) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001292 if (mAdded != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001293 // First look through added fragments.
1294 for (int i=mAdded.size()-1; i>=0; i--) {
1295 Fragment f = mAdded.get(i);
1296 if (f != null && tag.equals(f.mTag)) {
1297 return f;
1298 }
1299 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001300 }
1301 if (mActive != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001302 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001303 for (int i=mActive.size()-1; i>=0; i--) {
1304 Fragment f = mActive.get(i);
1305 if (f != null && tag.equals(f.mTag)) {
1306 return f;
1307 }
1308 }
1309 }
1310 return null;
1311 }
1312
1313 public Fragment findFragmentByWho(String who) {
1314 if (mActive != null && who != null) {
1315 for (int i=mActive.size()-1; i>=0; i--) {
1316 Fragment f = mActive.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001317 if (f != null && (f=f.findFragmentByWho(who)) != null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001318 return f;
1319 }
1320 }
1321 }
1322 return null;
1323 }
1324
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001325 private void checkStateLoss() {
1326 if (mStateSaved) {
1327 throw new IllegalStateException(
1328 "Can not perform this action after onSaveInstanceState");
1329 }
1330 if (mNoTransactionsBecause != null) {
1331 throw new IllegalStateException(
1332 "Can not perform this action inside of " + mNoTransactionsBecause);
1333 }
1334 }
1335
Alan Viverette95a46092013-08-14 11:17:25 -07001336 /**
1337 * Adds an action to the queue of pending actions.
1338 *
1339 * @param action the action to add
1340 * @param allowStateLoss whether to allow loss of state information
1341 * @throws IllegalStateException if the activity has been destroyed
1342 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -07001343 public void enqueueAction(Runnable action, boolean allowStateLoss) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001344 if (!allowStateLoss) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001345 checkStateLoss();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001346 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001347 synchronized (this) {
Alan Viverette95a46092013-08-14 11:17:25 -07001348 if (mDestroyed || mActivity == null) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001349 throw new IllegalStateException("Activity has been destroyed");
1350 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001351 if (mPendingActions == null) {
1352 mPendingActions = new ArrayList<Runnable>();
1353 }
1354 mPendingActions.add(action);
1355 if (mPendingActions.size() == 1) {
1356 mActivity.mHandler.removeCallbacks(mExecCommit);
1357 mActivity.mHandler.post(mExecCommit);
1358 }
1359 }
1360 }
1361
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001362 public int allocBackStackIndex(BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001363 synchronized (this) {
1364 if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1365 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001366 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001367 }
1368 int index = mBackStackIndices.size();
1369 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1370 mBackStackIndices.add(bse);
1371 return index;
1372
1373 } else {
1374 int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1375 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1376 mBackStackIndices.set(index, bse);
1377 return index;
1378 }
1379 }
1380 }
1381
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001382 public void setBackStackIndex(int index, BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001383 synchronized (this) {
1384 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001385 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001386 }
1387 int N = mBackStackIndices.size();
1388 if (index < N) {
1389 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1390 mBackStackIndices.set(index, bse);
1391 } else {
1392 while (N < index) {
1393 mBackStackIndices.add(null);
1394 if (mAvailBackStackIndices == null) {
1395 mAvailBackStackIndices = new ArrayList<Integer>();
1396 }
1397 if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1398 mAvailBackStackIndices.add(N);
1399 N++;
1400 }
1401 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1402 mBackStackIndices.add(bse);
1403 }
1404 }
1405 }
1406
1407 public void freeBackStackIndex(int index) {
1408 synchronized (this) {
1409 mBackStackIndices.set(index, null);
1410 if (mAvailBackStackIndices == null) {
1411 mAvailBackStackIndices = new ArrayList<Integer>();
1412 }
1413 if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1414 mAvailBackStackIndices.add(index);
1415 }
1416 }
1417
Dianne Hackborn445646c2010-06-25 15:52:59 -07001418 /**
1419 * Only call from main thread!
1420 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001421 public boolean execPendingActions() {
Dianne Hackborn445646c2010-06-25 15:52:59 -07001422 if (mExecutingActions) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001423 throw new IllegalStateException("Recursive entry to executePendingTransactions");
Dianne Hackborn445646c2010-06-25 15:52:59 -07001424 }
1425
Dianne Hackbornd9b3b7e2010-11-16 18:22:49 -08001426 if (Looper.myLooper() != mActivity.mHandler.getLooper()) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001427 throw new IllegalStateException("Must be called from main thread of process");
1428 }
1429
1430 boolean didSomething = false;
1431
Dianne Hackborn445646c2010-06-25 15:52:59 -07001432 while (true) {
1433 int numActions;
1434
1435 synchronized (this) {
1436 if (mPendingActions == null || mPendingActions.size() == 0) {
Adam Powell78fed9b2011-11-07 10:45:34 -08001437 break;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001438 }
1439
1440 numActions = mPendingActions.size();
1441 if (mTmpActions == null || mTmpActions.length < numActions) {
1442 mTmpActions = new Runnable[numActions];
1443 }
1444 mPendingActions.toArray(mTmpActions);
1445 mPendingActions.clear();
1446 mActivity.mHandler.removeCallbacks(mExecCommit);
1447 }
1448
1449 mExecutingActions = true;
1450 for (int i=0; i<numActions; i++) {
1451 mTmpActions[i].run();
Jeff Sharkey0d325282011-07-13 09:36:27 -07001452 mTmpActions[i] = null;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001453 }
1454 mExecutingActions = false;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001455 didSomething = true;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001456 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001457
1458 if (mHavePendingDeferredStart) {
1459 boolean loadersRunning = false;
1460 for (int i=0; i<mActive.size(); i++) {
1461 Fragment f = mActive.get(i);
1462 if (f != null && f.mLoaderManager != null) {
1463 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1464 }
1465 }
1466 if (!loadersRunning) {
1467 mHavePendingDeferredStart = false;
1468 startPendingDeferredFragments();
1469 }
1470 }
1471 return didSomething;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001472 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001473
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001474 void reportBackStackChanged() {
1475 if (mBackStackChangeListeners != null) {
1476 for (int i=0; i<mBackStackChangeListeners.size(); i++) {
1477 mBackStackChangeListeners.get(i).onBackStackChanged();
1478 }
1479 }
1480 }
1481
1482 void addBackStackState(BackStackRecord state) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001483 if (mBackStack == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001484 mBackStack = new ArrayList<BackStackRecord>();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001485 }
1486 mBackStack.add(state);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001487 reportBackStackChanged();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001488 }
1489
Dianne Hackborndd913a52010-07-22 12:17:04 -07001490 boolean popBackStackState(Handler handler, String name, int id, int flags) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001491 if (mBackStack == null) {
1492 return false;
1493 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001494 if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001495 int last = mBackStack.size()-1;
1496 if (last < 0) {
1497 return false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001498 }
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001499 final BackStackRecord bss = mBackStack.remove(last);
George Mountc03da0e2014-08-22 17:04:02 -07001500 SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
1501 SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
1502 bss.calculateBackFragments(firstOutFragments, lastInFragments);
1503 bss.popFromBackStack(true, null, firstOutFragments, lastInFragments);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001504 reportBackStackChanged();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001505 } else {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001506 int index = -1;
1507 if (name != null || id >= 0) {
1508 // If a name or ID is specified, look for that place in
1509 // the stack.
1510 index = mBackStack.size()-1;
1511 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001512 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001513 if (name != null && name.equals(bss.getName())) {
1514 break;
1515 }
1516 if (id >= 0 && id == bss.mIndex) {
1517 break;
1518 }
1519 index--;
Dianne Hackbornf121be72010-05-06 14:10:32 -07001520 }
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001521 if (index < 0) {
1522 return false;
Dianne Hackborndd913a52010-07-22 12:17:04 -07001523 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001524 if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001525 index--;
1526 // Consume all following entries that match.
1527 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001528 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001529 if ((name != null && name.equals(bss.getName()))
1530 || (id >= 0 && id == bss.mIndex)) {
1531 index--;
1532 continue;
1533 }
1534 break;
1535 }
1536 }
Dianne Hackborndd913a52010-07-22 12:17:04 -07001537 }
1538 if (index == mBackStack.size()-1) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001539 return false;
1540 }
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001541 final ArrayList<BackStackRecord> states
1542 = new ArrayList<BackStackRecord>();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001543 for (int i=mBackStack.size()-1; i>index; i--) {
1544 states.add(mBackStack.remove(i));
1545 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001546 final int LAST = states.size()-1;
George Mountc03da0e2014-08-22 17:04:02 -07001547 SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>();
1548 SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>();
1549 for (int i=0; i<=LAST; i++) {
1550 states.get(i).calculateBackFragments(firstOutFragments, lastInFragments);
1551 }
George Mountd4c3c912014-06-09 12:31:34 -07001552 BackStackRecord.TransitionState state = null;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001553 for (int i=0; i<=LAST; i++) {
1554 if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i));
George Mountc03da0e2014-08-22 17:04:02 -07001555 state = states.get(i).popFromBackStack(i == LAST, state,
1556 firstOutFragments, lastInFragments);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001557 }
1558 reportBackStackChanged();
Dianne Hackbornf121be72010-05-06 14:10:32 -07001559 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001560 return true;
1561 }
1562
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001563 ArrayList<Fragment> retainNonConfig() {
1564 ArrayList<Fragment> fragments = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001565 if (mActive != null) {
1566 for (int i=0; i<mActive.size(); i++) {
1567 Fragment f = mActive.get(i);
1568 if (f != null && f.mRetainInstance) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001569 if (fragments == null) {
1570 fragments = new ArrayList<Fragment>();
1571 }
1572 fragments.add(f);
1573 f.mRetaining = true;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001574 f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001575 if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001576 }
1577 }
1578 }
1579 return fragments;
1580 }
1581
1582 void saveFragmentViewState(Fragment f) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001583 if (f.mView == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001584 return;
1585 }
1586 if (mStateArray == null) {
1587 mStateArray = new SparseArray<Parcelable>();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001588 } else {
1589 mStateArray.clear();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001590 }
1591 f.mView.saveHierarchyState(mStateArray);
1592 if (mStateArray.size() > 0) {
1593 f.mSavedViewState = mStateArray;
1594 mStateArray = null;
1595 }
1596 }
1597
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001598 Bundle saveFragmentBasicState(Fragment f) {
1599 Bundle result = null;
1600
1601 if (mStateBundle == null) {
1602 mStateBundle = new Bundle();
1603 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001604 f.performSaveInstanceState(mStateBundle);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001605 if (!mStateBundle.isEmpty()) {
1606 result = mStateBundle;
1607 mStateBundle = null;
1608 }
1609
1610 if (f.mView != null) {
1611 saveFragmentViewState(f);
Dianne Hackborn13332762011-06-03 17:34:45 -07001612 }
1613 if (f.mSavedViewState != null) {
1614 if (result == null) {
1615 result = new Bundle();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001616 }
Dianne Hackborn13332762011-06-03 17:34:45 -07001617 result.putSparseParcelableArray(
1618 FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001619 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001620 if (!f.mUserVisibleHint) {
Jake Wharton258029e2012-04-22 17:17:01 -04001621 if (result == null) {
1622 result = new Bundle();
1623 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001624 // Only add this if it's not the default value
1625 result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
1626 }
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001627
1628 return result;
1629 }
1630
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001631 Parcelable saveAllState() {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001632 // Make sure all pending operations have now been executed to get
1633 // our state update-to-date.
1634 execPendingActions();
1635
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001636 mStateSaved = true;
1637
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001638 if (mActive == null || mActive.size() <= 0) {
1639 return null;
1640 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001641
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001642 // First collect all active fragments.
1643 int N = mActive.size();
1644 FragmentState[] active = new FragmentState[N];
1645 boolean haveFragments = false;
1646 for (int i=0; i<N; i++) {
1647 Fragment f = mActive.get(i);
1648 if (f != null) {
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001649 if (f.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001650 throwException(new IllegalStateException(
1651 "Failure saving state: active " + f
1652 + " has cleared index: " + f.mIndex));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001653 }
1654
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001655 haveFragments = true;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001656
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001657 FragmentState fs = new FragmentState(f);
1658 active[i] = fs;
1659
Dianne Hackborn625ac272010-09-17 18:29:22 -07001660 if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07001661 fs.mSavedFragmentState = saveFragmentBasicState(f);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001662
1663 if (f.mTarget != null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001664 if (f.mTarget.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001665 throwException(new IllegalStateException(
1666 "Failure saving state: " + f
1667 + " has target not in fragment manager: " + f.mTarget));
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001668 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001669 if (fs.mSavedFragmentState == null) {
1670 fs.mSavedFragmentState = new Bundle();
1671 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001672 putFragment(fs.mSavedFragmentState,
1673 FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
1674 if (f.mTargetRequestCode != 0) {
1675 fs.mSavedFragmentState.putInt(
1676 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
1677 f.mTargetRequestCode);
1678 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001679 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001680
Dianne Hackborn625ac272010-09-17 18:29:22 -07001681 } else {
1682 fs.mSavedFragmentState = f.mSavedFragmentState;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001683 }
1684
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001685 if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
1686 + fs.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001687 }
1688 }
1689
1690 if (!haveFragments) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001691 if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001692 return null;
1693 }
1694
1695 int[] added = null;
1696 BackStackState[] backStack = null;
1697
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001698 // Build list of currently added fragments.
Dianne Hackborn625ac272010-09-17 18:29:22 -07001699 if (mAdded != null) {
1700 N = mAdded.size();
1701 if (N > 0) {
1702 added = new int[N];
1703 for (int i=0; i<N; i++) {
1704 added[i] = mAdded.get(i).mIndex;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001705 if (added[i] < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001706 throwException(new IllegalStateException(
1707 "Failure saving state: active " + mAdded.get(i)
1708 + " has cleared index: " + added[i]));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07001709 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001710 if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
1711 + ": " + mAdded.get(i));
1712 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001713 }
1714 }
1715
1716 // Now save back stack.
1717 if (mBackStack != null) {
1718 N = mBackStack.size();
1719 if (N > 0) {
1720 backStack = new BackStackState[N];
1721 for (int i=0; i<N; i++) {
1722 backStack[i] = new BackStackState(this, mBackStack.get(i));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001723 if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
1724 + ": " + mBackStack.get(i));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001725 }
1726 }
1727 }
1728
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001729 FragmentManagerState fms = new FragmentManagerState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001730 fms.mActive = active;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001731 fms.mAdded = added;
1732 fms.mBackStack = backStack;
1733 return fms;
1734 }
1735
1736 void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) {
1737 // If there is no saved state at all, then there can not be
1738 // any nonConfig fragments either, so that is that.
1739 if (state == null) return;
1740 FragmentManagerState fms = (FragmentManagerState)state;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001741 if (fms.mActive == null) return;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001742
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001743 // First re-attach any non-config instances we are retaining back
1744 // to their saved state, so we don't try to instantiate them again.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001745 if (nonConfig != null) {
1746 for (int i=0; i<nonConfig.size(); i++) {
1747 Fragment f = nonConfig.get(i);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001748 if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001749 FragmentState fs = fms.mActive[f.mIndex];
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001750 fs.mInstance = f;
1751 f.mSavedViewState = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001752 f.mBackStackNesting = 0;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001753 f.mInLayout = false;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001754 f.mAdded = false;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001755 f.mTarget = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001756 if (fs.mSavedFragmentState != null) {
Dianne Hackborn51642462010-10-28 10:32:37 -07001757 fs.mSavedFragmentState.setClassLoader(mActivity.getClassLoader());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001758 f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
Dianne Hackborndef15372010-08-15 12:43:52 -07001759 FragmentManagerImpl.VIEW_STATE_TAG);
Craig Mautner33acfbe2014-06-28 14:00:53 -07001760 f.mSavedFragmentState = fs.mSavedFragmentState;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001761 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001762 }
1763 }
1764
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001765 // Build the full list of active fragments, instantiating them from
1766 // their saved state.
1767 mActive = new ArrayList<Fragment>(fms.mActive.length);
1768 if (mAvailIndices != null) {
1769 mAvailIndices.clear();
1770 }
1771 for (int i=0; i<fms.mActive.length; i++) {
1772 FragmentState fs = fms.mActive[i];
1773 if (fs != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001774 Fragment f = fs.instantiate(mActivity, mParent);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001775 if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001776 mActive.add(f);
Dianne Hackborn30d71892010-12-11 10:37:55 -08001777 // Now that the fragment is instantiated (or came from being
1778 // retained above), clear mInstance in case we end up re-restoring
1779 // from this FragmentState again.
1780 fs.mInstance = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001781 } else {
1782 mActive.add(null);
1783 if (mAvailIndices == null) {
1784 mAvailIndices = new ArrayList<Integer>();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001785 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001786 if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001787 mAvailIndices.add(i);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001788 }
1789 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001790
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001791 // Update the target of all retained fragments.
1792 if (nonConfig != null) {
1793 for (int i=0; i<nonConfig.size(); i++) {
1794 Fragment f = nonConfig.get(i);
Dianne Hackbornf9302322011-06-14 18:36:14 -07001795 if (f.mTargetIndex >= 0) {
1796 if (f.mTargetIndex < mActive.size()) {
1797 f.mTarget = mActive.get(f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001798 } else {
1799 Log.w(TAG, "Re-attaching retained fragment " + f
Dianne Hackbornf9302322011-06-14 18:36:14 -07001800 + " target no longer exists: " + f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001801 f.mTarget = null;
1802 }
1803 }
1804 }
1805 }
1806
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001807 // Build the list of currently added fragments.
1808 if (fms.mAdded != null) {
1809 mAdded = new ArrayList<Fragment>(fms.mAdded.length);
1810 for (int i=0; i<fms.mAdded.length; i++) {
1811 Fragment f = mActive.get(fms.mAdded[i]);
1812 if (f == null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001813 throwException(new IllegalStateException(
1814 "No instantiated fragment for index #" + fms.mAdded[i]));
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001815 }
1816 f.mAdded = true;
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001817 if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
1818 if (mAdded.contains(f)) {
1819 throw new IllegalStateException("Already added!");
1820 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001821 mAdded.add(f);
1822 }
1823 } else {
1824 mAdded = null;
1825 }
1826
1827 // Build the back stack.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001828 if (fms.mBackStack != null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001829 mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001830 for (int i=0; i<fms.mBackStack.length; i++) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001831 BackStackRecord bse = fms.mBackStack[i].instantiate(this);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001832 if (DEBUG) {
1833 Log.v(TAG, "restoreAllState: back stack #" + i
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07001834 + " (index " + bse.mIndex + "): " + bse);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001835 LogWriter logw = new LogWriter(Log.VERBOSE, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001836 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001837 bse.dump(" ", pw, false);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001838 pw.flush();
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001839 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001840 mBackStack.add(bse);
Dianne Hackborndd913a52010-07-22 12:17:04 -07001841 if (bse.mIndex >= 0) {
1842 setBackStackIndex(bse.mIndex, bse);
1843 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001844 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001845 } else {
1846 mBackStack = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001847 }
1848 }
1849
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001850 public void attachActivity(Activity activity, FragmentContainer container, Fragment parent) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07001851 if (mActivity != null) throw new IllegalStateException("Already attached");
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001852 mActivity = activity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001853 mContainer = container;
1854 mParent = parent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001855 }
1856
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001857 public void noteStateNotSaved() {
1858 mStateSaved = false;
1859 }
1860
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001861 public void dispatchCreate() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001862 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001863 moveToState(Fragment.CREATED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001864 }
1865
Dianne Hackbornc8017682010-07-06 13:34:38 -07001866 public void dispatchActivityCreated() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001867 mStateSaved = false;
Dianne Hackbornc8017682010-07-06 13:34:38 -07001868 moveToState(Fragment.ACTIVITY_CREATED, false);
1869 }
1870
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001871 public void dispatchStart() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001872 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001873 moveToState(Fragment.STARTED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001874 }
1875
1876 public void dispatchResume() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07001877 mStateSaved = false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001878 moveToState(Fragment.RESUMED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001879 }
1880
1881 public void dispatchPause() {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001882 moveToState(Fragment.STARTED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001883 }
1884
1885 public void dispatchStop() {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001886 moveToState(Fragment.STOPPED, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001887 }
1888
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001889 public void dispatchDestroyView() {
1890 moveToState(Fragment.CREATED, false);
1891 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001892
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001893 public void dispatchDestroy() {
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001894 mDestroyed = true;
Dianne Hackbornc6938232011-07-21 16:25:26 -07001895 execPendingActions();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001896 moveToState(Fragment.INITIALIZING, false);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001897 mActivity = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001898 mContainer = null;
1899 mParent = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001900 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07001901
Dianne Hackborn9d071802010-12-08 14:49:15 -08001902 public void dispatchConfigurationChanged(Configuration newConfig) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001903 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08001904 for (int i=0; i<mAdded.size(); i++) {
1905 Fragment f = mAdded.get(i);
1906 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001907 f.performConfigurationChanged(newConfig);
Dianne Hackborn9d071802010-12-08 14:49:15 -08001908 }
1909 }
1910 }
1911 }
1912
1913 public void dispatchLowMemory() {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001914 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08001915 for (int i=0; i<mAdded.size(); i++) {
1916 Fragment f = mAdded.get(i);
1917 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001918 f.performLowMemory();
Dianne Hackborn9d071802010-12-08 14:49:15 -08001919 }
1920 }
1921 }
1922 }
1923
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001924 public void dispatchTrimMemory(int level) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001925 if (mAdded != null) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001926 for (int i=0; i<mAdded.size(); i++) {
1927 Fragment f = mAdded.get(i);
1928 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001929 f.performTrimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001930 }
1931 }
1932 }
1933 }
1934
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001935 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1936 boolean show = false;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001937 ArrayList<Fragment> newMenus = null;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001938 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001939 for (int i=0; i<mAdded.size(); i++) {
1940 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001941 if (f != null) {
1942 if (f.performCreateOptionsMenu(menu, inflater)) {
1943 show = true;
1944 if (newMenus == null) {
1945 newMenus = new ArrayList<Fragment>();
1946 }
1947 newMenus.add(f);
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001948 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001949 }
1950 }
1951 }
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001952
1953 if (mCreatedMenus != null) {
1954 for (int i=0; i<mCreatedMenus.size(); i++) {
1955 Fragment f = mCreatedMenus.get(i);
1956 if (newMenus == null || !newMenus.contains(f)) {
1957 f.onDestroyOptionsMenu();
1958 }
1959 }
1960 }
1961
1962 mCreatedMenus = newMenus;
1963
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001964 return show;
1965 }
1966
1967 public boolean dispatchPrepareOptionsMenu(Menu menu) {
1968 boolean show = false;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001969 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001970 for (int i=0; i<mAdded.size(); i++) {
1971 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001972 if (f != null) {
1973 if (f.performPrepareOptionsMenu(menu)) {
1974 show = true;
1975 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001976 }
1977 }
1978 }
1979 return show;
1980 }
1981
1982 public boolean dispatchOptionsItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001983 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001984 for (int i=0; i<mAdded.size(); i++) {
1985 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001986 if (f != null) {
1987 if (f.performOptionsItemSelected(item)) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001988 return true;
1989 }
1990 }
1991 }
1992 }
1993 return false;
1994 }
1995
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001996 public boolean dispatchContextItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001997 if (mAdded != null) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001998 for (int i=0; i<mAdded.size(); i++) {
1999 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002000 if (f != null) {
2001 if (f.performContextItemSelected(item)) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07002002 return true;
2003 }
2004 }
2005 }
2006 }
2007 return false;
2008 }
2009
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002010 public void dispatchOptionsMenuClosed(Menu menu) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002011 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002012 for (int i=0; i<mAdded.size(); i++) {
2013 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002014 if (f != null) {
2015 f.performOptionsMenuClosed(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002016 }
2017 }
2018 }
2019 }
Adam Powellf0f5fff2011-08-01 13:42:50 -07002020
2021 @Override
2022 public void invalidateOptionsMenu() {
2023 if (mActivity != null && mCurState == Fragment.RESUMED) {
2024 mActivity.invalidateOptionsMenu();
2025 } else {
2026 mNeedMenuInvalidate = true;
2027 }
2028 }
2029
Dianne Hackbornf121be72010-05-06 14:10:32 -07002030 public static int reverseTransit(int transit) {
2031 int rev = 0;
2032 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002033 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2034 rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002035 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002036 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2037 rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002038 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002039 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2040 rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002041 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002042 }
2043 return rev;
2044
2045 }
2046
2047 public static int transitToStyleIndex(int transit, boolean enter) {
2048 int animAttr = -1;
2049 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002050 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002051 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002052 ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation
2053 : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002054 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002055 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002056 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002057 ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation
2058 : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002059 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002060 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
Chet Haase9ff82bf2010-10-05 14:30:51 -07002061 animAttr = enter
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002062 ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation
2063 : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002064 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002065 }
2066 return animAttr;
2067 }
Adam Powell371a8092014-06-20 12:51:12 -07002068
2069 @Override
2070 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
2071 if (!"fragment".equals(name)) {
2072 return null;
2073 }
2074
2075 String fname = attrs.getAttributeValue(null, "class");
2076 TypedArray a =
2077 context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
2078 if (fname == null) {
2079 fname = a.getString(com.android.internal.R.styleable.Fragment_name);
2080 }
2081 int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID);
2082 String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
2083 a.recycle();
2084
2085 int containerId = parent != null ? parent.getId() : 0;
2086 if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
2087 throw new IllegalArgumentException(attrs.getPositionDescription()
2088 + ": Must specify unique android:id, android:tag, or have a parent with"
2089 + " an id for " + fname);
2090 }
2091
2092 // If we restored from a previous state, we may already have
2093 // instantiated this fragment from the state and should use
2094 // that instance instead of making a new one.
2095 Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
2096 if (fragment == null && tag != null) {
2097 fragment = findFragmentByTag(tag);
2098 }
2099 if (fragment == null && containerId != View.NO_ID) {
2100 fragment = findFragmentById(containerId);
2101 }
2102
2103 if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
2104 + Integer.toHexString(id) + " fname=" + fname
2105 + " existing=" + fragment);
2106 if (fragment == null) {
2107 fragment = Fragment.instantiate(context, fname);
2108 fragment.mFromLayout = true;
2109 fragment.mFragmentId = id != 0 ? id : containerId;
2110 fragment.mContainerId = containerId;
2111 fragment.mTag = tag;
2112 fragment.mInLayout = true;
2113 fragment.mFragmentManager = this;
2114 fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState);
2115 addFragment(fragment, true);
2116 } else if (fragment.mInLayout) {
2117 // A fragment already exists and it is not one we restored from
2118 // previous state.
2119 throw new IllegalArgumentException(attrs.getPositionDescription()
2120 + ": Duplicate id 0x" + Integer.toHexString(id)
2121 + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
2122 + " with another fragment for " + fname);
2123 } else {
2124 // This fragment was retained from a previous instance; get it
2125 // going now.
2126 fragment.mInLayout = true;
2127 // If this fragment is newly instantiated (either right now, or
2128 // from last saved state), then give it the attributes to
2129 // initialize itself.
2130 if (!fragment.mRetaining) {
2131 fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState);
2132 }
2133 }
2134
2135 // If we haven't finished entering the CREATED state ourselves yet,
2136 // push the inflated child fragment along.
2137 if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
2138 moveToState(fragment, Fragment.CREATED, 0, 0, false);
2139 } else {
2140 moveToState(fragment);
2141 }
2142
2143 if (fragment.mView == null) {
2144 throw new IllegalStateException("Fragment " + fname
2145 + " did not create a view.");
2146 }
2147 if (id != 0) {
2148 fragment.mView.setId(id);
2149 }
2150 if (fragment.mView.getTag() == null) {
2151 fragment.mView.setTag(tag);
2152 }
2153 return fragment.mView;
2154 }
2155
2156 @Override
2157 public View onCreateView(String name, Context context, AttributeSet attrs) {
2158 return null;
2159 }
2160
2161 LayoutInflater.Factory2 getLayoutInflaterFactory() {
2162 return this;
2163 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002164}