blob: b2df1acbadaf5ef8a3aba03c0bb53db4397f8505 [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;
Doris Liude9284d2015-05-22 15:41:46 -070022import android.animation.AnimatorSet;
23import android.animation.PropertyValuesHolder;
24import android.animation.ValueAnimator;
Adam Powell371a8092014-06-20 12:51:12 -070025import android.content.Context;
Dianne Hackborn9d071802010-12-08 14:49:15 -080026import android.content.res.Configuration;
Adam Powelle01f5952016-02-23 15:25:42 -080027import android.content.res.Resources.NotFoundException;
Dianne Hackbornf121be72010-05-06 14:10:32 -070028import android.content.res.TypedArray;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070029import android.os.Bundle;
Dianne Hackbornf43a33c2012-09-27 00:48:11 -070030import android.os.Debug;
Dianne Hackborn3a57fb92010-11-15 17:58:52 -080031import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070032import android.os.Parcel;
33import android.os.Parcelable;
George Mounteca8e222016-07-07 13:13:05 -070034import android.transition.Transition;
Adam Powell371a8092014-06-20 12:51:12 -070035import android.util.AttributeSet;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080036import android.util.DebugUtils;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070037import android.util.Log;
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -080038import android.util.LogWriter;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070039import android.util.SparseArray;
Adam Powell14874662013-07-18 19:42:41 -070040import android.util.SuperNotCalledException;
Adam Powell371a8092014-06-20 12:51:12 -070041import android.view.LayoutInflater;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070042import android.view.Menu;
43import android.view.MenuInflater;
44import android.view.MenuItem;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -070045import android.view.View;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070046import android.view.ViewGroup;
Dianne Hackborn8c841092013-06-24 13:46:13 -070047import com.android.internal.util.FastPrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070048
Dianne Hackborn625ac272010-09-17 18:29:22 -070049import java.io.FileDescriptor;
50import java.io.PrintWriter;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070051import java.util.ArrayList;
Dianne Hackbornd173fa32010-12-23 13:58:22 -080052import java.util.Arrays;
Todd Kennedya5fc6f02015-04-14 18:22:54 -070053import java.util.List;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070054
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070055/**
56 * Interface for interacting with {@link Fragment} objects inside of an
57 * {@link Activity}
Joe Fernandezb54e7a32011-10-03 15:09:50 -070058 *
59 * <div class="special reference">
60 * <h3>Developer Guides</h3>
61 * <p>For more information about using fragments, read the
Andrew Solovayf5396802016-08-26 15:46:38 -070062 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Joe Fernandezb54e7a32011-10-03 15:09:50 -070063 * </div>
Dianne Hackborn7871bad2011-12-12 15:19:26 -080064 *
65 * While the FragmentManager API was introduced in
66 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
67 * at is also available for use on older platforms through
68 * {@link android.support.v4.app.FragmentActivity}. See the blog post
69 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
70 * Fragments For All</a> for more details.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070071 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -070072public abstract class FragmentManager {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070073 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070074 * Representation of an entry on the fragment back stack, as created
75 * with {@link FragmentTransaction#addToBackStack(String)
76 * FragmentTransaction.addToBackStack()}. Entries can later be
Dianne Hackborn327fbd22011-01-17 14:38:50 -080077 * retrieved with {@link FragmentManager#getBackStackEntryAt(int)
Mark Dolinerd0646dc2014-08-27 16:04:02 -070078 * FragmentManager.getBackStackEntryAt()}.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070079 *
80 * <p>Note that you should never hold on to a BackStackEntry object;
81 * the identifier as returned by {@link #getId} is the only thing that
82 * will be persisted across activity instances.
83 */
84 public interface BackStackEntry {
85 /**
86 * Return the unique identifier for the entry. This is the only
87 * representation of the entry that will persist across activity
88 * instances.
89 */
90 public int getId();
91
92 /**
Dianne Hackborn6c285972011-08-29 16:53:49 -070093 * Get the name that was supplied to
94 * {@link FragmentTransaction#addToBackStack(String)
95 * FragmentTransaction.addToBackStack(String)} when creating this entry.
96 */
97 public String getName();
98
99 /**
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800100 * Return the full bread crumb title resource identifier for the entry,
101 * or 0 if it does not have one.
102 */
103 public int getBreadCrumbTitleRes();
104
105 /**
106 * Return the short bread crumb title resource identifier for the entry,
107 * or 0 if it does not have one.
108 */
109 public int getBreadCrumbShortTitleRes();
110
111 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700112 * Return the full bread crumb title for the entry, or null if it
113 * does not have one.
114 */
115 public CharSequence getBreadCrumbTitle();
116
117 /**
118 * Return the short bread crumb title for the entry, or null if it
119 * does not have one.
120 */
121 public CharSequence getBreadCrumbShortTitle();
122 }
123
124 /**
125 * Interface to watch for changes to the back stack.
126 */
127 public interface OnBackStackChangedListener {
128 /**
129 * Called whenever the contents of the back stack change.
130 */
131 public void onBackStackChanged();
132 }
133
134 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700135 * Start a series of edit operations on the Fragments associated with
136 * this FragmentManager.
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700137 *
138 * <p>Note: A fragment transaction can only be created/committed prior
139 * to an activity saving its state. If you try to commit a transaction
140 * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()}
141 * (and prior to a following {@link Activity#onStart Activity.onStart}
142 * or {@link Activity#onResume Activity.onResume()}, you will get an error.
143 * This is because the framework takes care of saving your current fragments
144 * in the state, and if changes are made after the state is saved then they
145 * will be lost.</p>
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700146 */
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800147 public abstract FragmentTransaction beginTransaction();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700148
Dianne Hackborn17b9b812011-01-17 17:16:02 -0800149 /** @hide -- remove once prebuilts are in. */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800150 @Deprecated
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800151 public FragmentTransaction openTransaction() {
152 return beginTransaction();
153 }
154
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700155 /**
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800156 * After a {@link FragmentTransaction} is committed with
157 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
158 * is scheduled to be executed asynchronously on the process's main thread.
159 * If you want to immediately executing any such pending operations, you
160 * can call this function (only from the main thread) to do so. Note that
161 * all callbacks and other related behavior will be done from within this
162 * call, so be careful about where this is called from.
163 *
164 * @return Returns true if there were any pending transactions to be
165 * executed.
166 */
167 public abstract boolean executePendingTransactions();
168
169 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700170 * Finds a fragment that was identified by the given id either when inflated
171 * from XML or as the container ID when added in a transaction. This first
172 * searches through fragments that are currently added to the manager's
173 * activity; if no such fragment is found, then all fragments currently
174 * on the back stack associated with this ID are searched.
175 * @return The fragment if found or null otherwise.
176 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700177 public abstract Fragment findFragmentById(int id);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700178
179 /**
180 * Finds a fragment that was identified by the given tag either when inflated
181 * from XML or as supplied when added in a transaction. This first
182 * searches through fragments that are currently added to the manager's
183 * activity; if no such fragment is found, then all fragments currently
184 * on the back stack are searched.
185 * @return The fragment if found or null otherwise.
186 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700187 public abstract Fragment findFragmentByTag(String tag);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700188
189 /**
190 * Flag for {@link #popBackStack(String, int)}
191 * and {@link #popBackStack(int, int)}: If set, and the name or ID of
192 * a back stack entry has been supplied, then all matching entries will
193 * be consumed until one that doesn't match is found or the bottom of
194 * the stack is reached. Otherwise, all entries up to but not including that entry
195 * will be removed.
196 */
197 public static final int POP_BACK_STACK_INCLUSIVE = 1<<0;
198
199 /**
Ben Komalo87ffa202011-02-28 12:41:42 -0800200 * Pop the top state off the back stack. This function is asynchronous -- it
201 * enqueues the request to pop, but the action will not be performed until the
202 * application returns to its event loop.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700203 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800204 public abstract void popBackStack();
205
206 /**
207 * Like {@link #popBackStack()}, but performs the operation immediately
208 * inside of the call. This is like calling {@link #executePendingTransactions()}
209 * afterwards.
210 * @return Returns true if there was something popped, else false.
211 */
212 public abstract boolean popBackStackImmediate();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700213
214 /**
215 * Pop the last fragment transition from the manager's fragment
216 * back stack. If there is nothing to pop, false is returned.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800217 * This function is asynchronous -- it enqueues the
218 * request to pop, but the action will not be performed until the application
219 * returns to its event loop.
220 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700221 * @param name If non-null, this is the name of a previous back state
222 * to look for; if found, all states up to that state will be popped. The
223 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
224 * the named state itself is popped. If null, only the top state is popped.
225 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
226 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800227 public abstract void popBackStack(String name, int flags);
228
229 /**
230 * Like {@link #popBackStack(String, int)}, but performs the operation immediately
231 * inside of the call. This is like calling {@link #executePendingTransactions()}
232 * afterwards.
233 * @return Returns true if there was something popped, else false.
234 */
235 public abstract boolean popBackStackImmediate(String name, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700236
237 /**
238 * Pop all back stack states up to the one with the given identifier.
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800239 * This function is asynchronous -- it enqueues the
240 * request to pop, but the action will not be performed until the application
241 * returns to its event loop.
242 *
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700243 * @param id Identifier of the stated to be popped. If no identifier exists,
244 * false is returned.
245 * The identifier is the number returned by
246 * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The
247 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether
248 * the named state itself is popped.
249 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
250 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800251 public abstract void popBackStack(int id, int flags);
252
253 /**
254 * Like {@link #popBackStack(int, int)}, but performs the operation immediately
255 * inside of the call. This is like calling {@link #executePendingTransactions()}
256 * afterwards.
257 * @return Returns true if there was something popped, else false.
258 */
259 public abstract boolean popBackStackImmediate(int id, int flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700260
261 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700262 * Return the number of entries currently in the back stack.
263 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800264 public abstract int getBackStackEntryCount();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700265
266 /**
267 * Return the BackStackEntry at index <var>index</var> in the back stack;
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700268 * where the item on the bottom of the stack has index 0.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700269 */
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800270 public abstract BackStackEntry getBackStackEntryAt(int index);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700271
272 /**
273 * Add a new listener for changes to the fragment back stack.
274 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700275 public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700276
277 /**
278 * Remove a listener that was previously added with
279 * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
280 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700281 public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700282
283 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700284 * Put a reference to a fragment in a Bundle. This Bundle can be
285 * persisted as saved state, and when later restoring
286 * {@link #getFragment(Bundle, String)} will return the current
287 * instance of the same fragment.
288 *
289 * @param bundle The bundle in which to put the fragment reference.
290 * @param key The name of the entry in the bundle.
291 * @param fragment The Fragment whose reference is to be stored.
292 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700293 public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700294
295 /**
296 * Retrieve the current Fragment instance for a reference previously
297 * placed with {@link #putFragment(Bundle, String, Fragment)}.
298 *
299 * @param bundle The bundle from which to retrieve the fragment reference.
300 * @param key The name of the entry in the bundle.
301 * @return Returns the current Fragment instance that is associated with
302 * the given reference.
303 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700304 public abstract Fragment getFragment(Bundle bundle, String key);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700305
306 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700307 * Save the current instance state of the given Fragment. This can be
308 * used later when creating a new instance of the Fragment and adding
309 * it to the fragment manager, to have it create itself to match the
310 * current state returned here. Note that there are limits on how
311 * this can be used:
312 *
313 * <ul>
314 * <li>The Fragment must currently be attached to the FragmentManager.
315 * <li>A new Fragment created using this saved state must be the same class
316 * type as the Fragment it was created from.
317 * <li>The saved state can not contain dependencies on other fragments --
318 * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to
319 * store a fragment reference because that reference may not be valid when
320 * this saved state is later used. Likewise the Fragment's target and
321 * result code are not included in this state.
322 * </ul>
323 *
324 * @param f The Fragment whose state is to be saved.
325 * @return The generated state. This will be null if there was no
326 * interesting state created by the fragment.
327 */
328 public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f);
329
330 /**
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700331 * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()}
332 * call has been made on the FragmentManager's Activity, so this instance is now dead.
333 */
334 public abstract boolean isDestroyed();
335
336 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700337 * Print the FragmentManager's state into the given stream.
338 *
339 * @param prefix Text to print at the front of each line.
340 * @param fd The raw file descriptor that the dump is being sent to.
341 * @param writer A PrintWriter to which the dump is to be set.
Dianne Hackborn30d71892010-12-11 10:37:55 -0800342 * @param args Additional arguments to the dump request.
Dianne Hackborn625ac272010-09-17 18:29:22 -0700343 */
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700344 public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800345
346 /**
347 * Control whether the framework's internal fragment manager debugging
348 * logs are turned on. If enabled, you will see output in logcat as
349 * the framework performs fragment operations.
350 */
351 public static void enableDebugLogging(boolean enabled) {
352 FragmentManagerImpl.DEBUG = enabled;
353 }
Adam Powellf0f5fff2011-08-01 13:42:50 -0700354
355 /**
356 * Invalidate the attached activity's options menu as necessary.
357 * This may end up being deferred until we move to the resumed state.
358 */
359 public void invalidateOptionsMenu() { }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700360}
361
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700362final class FragmentManagerState implements Parcelable {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700363 FragmentState[] mActive;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700364 int[] mAdded;
365 BackStackState[] mBackStack;
366
367 public FragmentManagerState() {
368 }
369
370 public FragmentManagerState(Parcel in) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700371 mActive = in.createTypedArray(FragmentState.CREATOR);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700372 mAdded = in.createIntArray();
373 mBackStack = in.createTypedArray(BackStackState.CREATOR);
374 }
375
376 public int describeContents() {
377 return 0;
378 }
379
380 public void writeToParcel(Parcel dest, int flags) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700381 dest.writeTypedArray(mActive, flags);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700382 dest.writeIntArray(mAdded);
383 dest.writeTypedArray(mBackStack, flags);
384 }
385
386 public static final Parcelable.Creator<FragmentManagerState> CREATOR
387 = new Parcelable.Creator<FragmentManagerState>() {
388 public FragmentManagerState createFromParcel(Parcel in) {
389 return new FragmentManagerState(in);
390 }
391
392 public FragmentManagerState[] newArray(int size) {
393 return new FragmentManagerState[size];
394 }
395 };
Dianne Hackbornba51c3d2010-05-05 18:49:48 -0700396}
397
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700398/**
399 * Container for fragments associated with an activity.
400 */
Adam Powell371a8092014-06-20 12:51:12 -0700401final class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 {
Craig Mautner1c437192012-08-01 10:01:16 -0700402 static boolean DEBUG = false;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700403 static final String TAG = "FragmentManager";
404
Dianne Hackborndef15372010-08-15 12:43:52 -0700405 static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state";
406 static final String TARGET_STATE_TAG = "android:target_state";
407 static final String VIEW_STATE_TAG = "android:view_state";
Adam Powell78fed9b2011-11-07 10:45:34 -0800408 static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint";
Dianne Hackborndef15372010-08-15 12:43:52 -0700409
Doris Liude9284d2015-05-22 15:41:46 -0700410 static class AnimateOnHWLayerIfNeededListener implements Animator.AnimatorListener {
411 private boolean mShouldRunOnHWLayer = false;
412 private View mView;
413 public AnimateOnHWLayerIfNeededListener(final View v) {
414 if (v == null) {
415 return;
416 }
417 mView = v;
418 }
419
420 @Override
421 public void onAnimationStart(Animator animation) {
422 mShouldRunOnHWLayer = shouldRunOnHWLayer(mView, animation);
423 if (mShouldRunOnHWLayer) {
424 mView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
425 }
426 }
427
428 @Override
429 public void onAnimationEnd(Animator animation) {
430 if (mShouldRunOnHWLayer) {
431 mView.setLayerType(View.LAYER_TYPE_NONE, null);
432 }
433 mView = null;
434 animation.removeListener(this);
435 }
436
437 @Override
438 public void onAnimationCancel(Animator animation) {
439
440 }
441
442 @Override
443 public void onAnimationRepeat(Animator animation) {
444
445 }
446 }
447
George Mounteca8e222016-07-07 13:13:05 -0700448 ArrayList<OpGenerator> mPendingActions;
Dianne Hackborn445646c2010-06-25 15:52:59 -0700449 boolean mExecutingActions;
450
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700451 ArrayList<Fragment> mActive;
452 ArrayList<Fragment> mAdded;
453 ArrayList<Integer> mAvailIndices;
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700454 ArrayList<BackStackRecord> mBackStack;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -0700455 ArrayList<Fragment> mCreatedMenus;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700456
Dianne Hackborndd913a52010-07-22 12:17:04 -0700457 // Must be accessed while locked.
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700458 ArrayList<BackStackRecord> mBackStackIndices;
Dianne Hackborndd913a52010-07-22 12:17:04 -0700459 ArrayList<Integer> mAvailBackStackIndices;
460
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700461 ArrayList<OnBackStackChangedListener> mBackStackChangeListeners;
462
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700463 int mCurState = Fragment.INITIALIZING;
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700464 FragmentHostCallback<?> mHost;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700465 FragmentContainer mContainer;
466 Fragment mParent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700467
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700468 boolean mNeedMenuInvalidate;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700469 boolean mStateSaved;
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800470 boolean mDestroyed;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700471 String mNoTransactionsBecause;
Adam Powell78fed9b2011-11-07 10:45:34 -0800472 boolean mHavePendingDeferredStart;
Adam Powell371a8092014-06-20 12:51:12 -0700473
George Mounteca8e222016-07-07 13:13:05 -0700474 // Temporary vars for optimizing execution of BackStackRecords:
475 ArrayList<BackStackRecord> mTmpRecords;
476 ArrayList<Boolean> mTmpIsPop;
477 SparseArray<BackStackRecord.FragmentContainerTransition> mTmpFragmentsContainerTransitions;
478 ArrayList<Fragment> mTmpAddedFragments;
479
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700480 // Temporary vars for state save and restore.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700481 Bundle mStateBundle = null;
482 SparseArray<Parcelable> mStateArray = null;
483
Dianne Hackborn445646c2010-06-25 15:52:59 -0700484 Runnable mExecCommit = new Runnable() {
485 @Override
486 public void run() {
487 execPendingActions();
488 }
489 };
Dianne Hackborn625ac272010-09-17 18:29:22 -0700490
Dianne Hackborn4702a852012-08-17 15:18:29 -0700491 private void throwException(RuntimeException ex) {
492 Log.e(TAG, ex.getMessage());
493 LogWriter logw = new LogWriter(Log.ERROR, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700494 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700495 if (mHost != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700496 Log.e(TAG, "Activity state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700497 try {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700498 mHost.onDump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700499 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700500 pw.flush();
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700501 Log.e(TAG, "Failed dumping state", e);
502 }
503 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700504 Log.e(TAG, "Fragment manager state:");
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700505 try {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700506 dump(" ", null, pw, new String[] { });
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700507 } catch (Exception e) {
Dianne Hackborn8c841092013-06-24 13:46:13 -0700508 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700509 Log.e(TAG, "Failed dumping state", e);
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700510 }
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700511 }
Dianne Hackborn8c841092013-06-24 13:46:13 -0700512 pw.flush();
Dianne Hackborn4702a852012-08-17 15:18:29 -0700513 throw ex;
Dianne Hackborn5bf6e1a2012-08-14 18:35:02 -0700514 }
515
Doris Liude9284d2015-05-22 15:41:46 -0700516 static boolean modifiesAlpha(Animator anim) {
517 if (anim == null) {
518 return false;
519 }
520 if (anim instanceof ValueAnimator) {
521 ValueAnimator valueAnim = (ValueAnimator) anim;
522 PropertyValuesHolder[] values = valueAnim.getValues();
523 for (int i = 0; i < values.length; i++) {
524 if (("alpha").equals(values[i].getPropertyName())) {
525 return true;
526 }
527 }
528 } else if (anim instanceof AnimatorSet) {
529 List<Animator> animList = ((AnimatorSet) anim).getChildAnimations();
530 for (int i = 0; i < animList.size(); i++) {
531 if (modifiesAlpha(animList.get(i))) {
532 return true;
533 }
534 }
535 }
536 return false;
537 }
538
539 static boolean shouldRunOnHWLayer(View v, Animator anim) {
540 if (v == null || anim == null) {
541 return false;
542 }
543 return v.getLayerType() == View.LAYER_TYPE_NONE
544 && v.hasOverlappingRendering()
545 && modifiesAlpha(anim);
546 }
547
548 /**
549 * Sets the to be animated view on hardware layer during the animation.
550 */
551 private void setHWLayerAnimListenerIfAlpha(final View v, Animator anim) {
552 if (v == null || anim == null) {
553 return;
554 }
555 if (shouldRunOnHWLayer(v, anim)) {
556 anim.addListener(new AnimateOnHWLayerIfNeededListener(v));
557 }
558 }
559
Dianne Hackborn625ac272010-09-17 18:29:22 -0700560 @Override
Dianne Hackborn48e7b452011-01-17 12:28:35 -0800561 public FragmentTransaction beginTransaction() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700562 return new BackStackRecord(this);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700563 }
564
Dianne Hackborn625ac272010-09-17 18:29:22 -0700565 @Override
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800566 public boolean executePendingTransactions() {
567 return execPendingActions();
568 }
569
570 @Override
571 public void popBackStack() {
George Mounteca8e222016-07-07 13:13:05 -0700572 enqueueAction(new PopBackStackState(null, -1, 0), false);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800573 }
574
575 @Override
576 public boolean popBackStackImmediate() {
577 checkStateLoss();
George Mounteca8e222016-07-07 13:13:05 -0700578 return popBackStackImmediate(null, -1, 0);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700579 }
580
Dianne Hackborn625ac272010-09-17 18:29:22 -0700581 @Override
George Mounteca8e222016-07-07 13:13:05 -0700582 public void popBackStack(String name, int flags) {
583 enqueueAction(new PopBackStackState(name, -1, flags), false);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800584 }
585
586 @Override
587 public boolean popBackStackImmediate(String name, int flags) {
588 checkStateLoss();
George Mounteca8e222016-07-07 13:13:05 -0700589 return popBackStackImmediate(name, -1, flags);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700590 }
591
Dianne Hackborn625ac272010-09-17 18:29:22 -0700592 @Override
George Mounteca8e222016-07-07 13:13:05 -0700593 public void popBackStack(int id, int flags) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800594 if (id < 0) {
595 throw new IllegalArgumentException("Bad id: " + id);
596 }
George Mounteca8e222016-07-07 13:13:05 -0700597 enqueueAction(new PopBackStackState(null, id, flags), false);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -0800598 }
599
600 @Override
601 public boolean popBackStackImmediate(int id, int flags) {
602 checkStateLoss();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700603 if (id < 0) {
604 throw new IllegalArgumentException("Bad id: " + id);
605 }
George Mounteca8e222016-07-07 13:13:05 -0700606 return popBackStackImmediate(null, id, flags);
607 }
608
609 /**
610 * Used by all public popBackStackImmediate methods, this executes pending transactions and
611 * returns true if the pop action did anything, regardless of what other pending
612 * transactions did.
613 *
614 * @return true if the pop operation did anything or false otherwise.
615 */
616 private boolean popBackStackImmediate(String name, int id, int flags) {
617 executePendingTransactions();
618 ensureExecReady(true);
619
620 boolean executePop = popBackStackState(mTmpRecords, mTmpIsPop, name, id, flags);
621 if (executePop) {
622 mExecutingActions = true;
623 try {
624 optimizeAndExecuteOps(mTmpRecords, mTmpIsPop);
625 } finally {
626 cleanupExec();
627 }
628 }
629
630 doPendingDeferredStart();
631 return executePop;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700632 }
633
Dianne Hackborn625ac272010-09-17 18:29:22 -0700634 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800635 public int getBackStackEntryCount() {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700636 return mBackStack != null ? mBackStack.size() : 0;
637 }
638
Dianne Hackborn625ac272010-09-17 18:29:22 -0700639 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800640 public BackStackEntry getBackStackEntryAt(int index) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700641 return mBackStack.get(index);
642 }
643
Dianne Hackborn625ac272010-09-17 18:29:22 -0700644 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700645 public void addOnBackStackChangedListener(OnBackStackChangedListener listener) {
646 if (mBackStackChangeListeners == null) {
647 mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>();
648 }
649 mBackStackChangeListeners.add(listener);
650 }
651
Dianne Hackborn625ac272010-09-17 18:29:22 -0700652 @Override
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700653 public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) {
654 if (mBackStackChangeListeners != null) {
655 mBackStackChangeListeners.remove(listener);
656 }
657 }
658
Dianne Hackborn625ac272010-09-17 18:29:22 -0700659 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700660 public void putFragment(Bundle bundle, String key, Fragment fragment) {
661 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700662 throwException(new IllegalStateException("Fragment " + fragment
663 + " is not currently in the FragmentManager"));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700664 }
665 bundle.putInt(key, fragment.mIndex);
666 }
667
Dianne Hackborn625ac272010-09-17 18:29:22 -0700668 @Override
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700669 public Fragment getFragment(Bundle bundle, String key) {
Dianne Hackborndef15372010-08-15 12:43:52 -0700670 int index = bundle.getInt(key, -1);
671 if (index == -1) {
672 return null;
673 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700674 if (index >= mActive.size()) {
Cyril Mottier2de50822013-09-30 22:42:26 +0200675 throwException(new IllegalStateException("Fragment no longer exists for key "
Dianne Hackborn4702a852012-08-17 15:18:29 -0700676 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700677 }
678 Fragment f = mActive.get(index);
679 if (f == null) {
Cyril Mottier2de50822013-09-30 22:42:26 +0200680 throwException(new IllegalStateException("Fragment no longer exists for key "
Dianne Hackborn4702a852012-08-17 15:18:29 -0700681 + key + ": index " + index));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700682 }
683 return f;
684 }
685
Dianne Hackborn625ac272010-09-17 18:29:22 -0700686 @Override
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700687 public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
688 if (fragment.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -0700689 throwException(new IllegalStateException("Fragment " + fragment
690 + " is not currently in the FragmentManager"));
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700691 }
692 if (fragment.mState > Fragment.INITIALIZING) {
693 Bundle result = saveFragmentBasicState(fragment);
694 return result != null ? new Fragment.SavedState(result) : null;
695 }
696 return null;
697 }
698
699 @Override
Dianne Hackborn6d9dcbc2012-10-02 17:51:13 -0700700 public boolean isDestroyed() {
701 return mDestroyed;
702 }
703
704 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800705 public String toString() {
706 StringBuilder sb = new StringBuilder(128);
707 sb.append("FragmentManager{");
708 sb.append(Integer.toHexString(System.identityHashCode(this)));
709 sb.append(" in ");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700710 if (mParent != null) {
711 DebugUtils.buildShortClassTag(mParent, sb);
712 } else {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700713 DebugUtils.buildShortClassTag(mHost, sb);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700714 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800715 sb.append("}}");
716 return sb.toString();
717 }
718
719 @Override
Dianne Hackborn625ac272010-09-17 18:29:22 -0700720 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700721 String innerPrefix = prefix + " ";
722
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800723 int N;
724 if (mActive != null) {
725 N = mActive.size();
726 if (N > 0) {
727 writer.print(prefix); writer.print("Active Fragments in ");
728 writer.print(Integer.toHexString(System.identityHashCode(this)));
729 writer.println(":");
730 for (int i=0; i<N; i++) {
731 Fragment f = mActive.get(i);
732 writer.print(prefix); writer.print(" #"); writer.print(i);
733 writer.print(": "); writer.println(f);
734 if (f != null) {
735 f.dump(innerPrefix, fd, writer, args);
736 }
737 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700738 }
739 }
740
741 if (mAdded != null) {
742 N = mAdded.size();
743 if (N > 0) {
744 writer.print(prefix); writer.println("Added Fragments:");
745 for (int i=0; i<N; i++) {
746 Fragment f = mAdded.get(i);
747 writer.print(prefix); writer.print(" #"); writer.print(i);
748 writer.print(": "); writer.println(f.toString());
749 }
750 }
751 }
752
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800753 if (mCreatedMenus != null) {
754 N = mCreatedMenus.size();
755 if (N > 0) {
756 writer.print(prefix); writer.println("Fragments Created Menus:");
757 for (int i=0; i<N; i++) {
758 Fragment f = mCreatedMenus.get(i);
759 writer.print(prefix); writer.print(" #"); writer.print(i);
760 writer.print(": "); writer.println(f.toString());
761 }
762 }
763 }
764
Dianne Hackborn625ac272010-09-17 18:29:22 -0700765 if (mBackStack != null) {
766 N = mBackStack.size();
767 if (N > 0) {
768 writer.print(prefix); writer.println("Back Stack:");
769 for (int i=0; i<N; i++) {
770 BackStackRecord bs = mBackStack.get(i);
771 writer.print(prefix); writer.print(" #"); writer.print(i);
772 writer.print(": "); writer.println(bs.toString());
Dianne Hackborn30d71892010-12-11 10:37:55 -0800773 bs.dump(innerPrefix, fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700774 }
775 }
776 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800777
778 synchronized (this) {
779 if (mBackStackIndices != null) {
780 N = mBackStackIndices.size();
781 if (N > 0) {
782 writer.print(prefix); writer.println("Back Stack Indices:");
783 for (int i=0; i<N; i++) {
784 BackStackRecord bs = mBackStackIndices.get(i);
785 writer.print(prefix); writer.print(" #"); writer.print(i);
786 writer.print(": "); writer.println(bs);
787 }
788 }
789 }
790
791 if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) {
792 writer.print(prefix); writer.print("mAvailBackStackIndices: ");
793 writer.println(Arrays.toString(mAvailBackStackIndices.toArray()));
794 }
795 }
796
797 if (mPendingActions != null) {
798 N = mPendingActions.size();
799 if (N > 0) {
800 writer.print(prefix); writer.println("Pending Actions:");
801 for (int i=0; i<N; i++) {
George Mounteca8e222016-07-07 13:13:05 -0700802 OpGenerator r = mPendingActions.get(i);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800803 writer.print(prefix); writer.print(" #"); writer.print(i);
804 writer.print(": "); writer.println(r);
805 }
806 }
807 }
808
809 writer.print(prefix); writer.println("FragmentManager misc state:");
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700810 writer.print(prefix); writer.print(" mHost="); writer.println(mHost);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700811 writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer);
812 if (mParent != null) {
813 writer.print(prefix); writer.print(" mParent="); writer.println(mParent);
814 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800815 writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState);
816 writer.print(" mStateSaved="); writer.print(mStateSaved);
817 writer.print(" mDestroyed="); writer.println(mDestroyed);
818 if (mNeedMenuInvalidate) {
819 writer.print(prefix); writer.print(" mNeedMenuInvalidate=");
820 writer.println(mNeedMenuInvalidate);
821 }
822 if (mNoTransactionsBecause != null) {
823 writer.print(prefix); writer.print(" mNoTransactionsBecause=");
824 writer.println(mNoTransactionsBecause);
825 }
826 if (mAvailIndices != null && mAvailIndices.size() > 0) {
827 writer.print(prefix); writer.print(" mAvailIndices: ");
828 writer.println(Arrays.toString(mAvailIndices.toArray()));
829 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700830 }
831
Chet Haasea18a86b2010-09-07 13:20:00 -0700832 Animator loadAnimator(Fragment fragment, int transit, boolean enter,
Dianne Hackbornf121be72010-05-06 14:10:32 -0700833 int transitionStyle) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700834 Animator animObj = fragment.onCreateAnimator(transit, enter,
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700835 fragment.mNextAnim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700836 if (animObj != null) {
837 return animObj;
838 }
839
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700840 if (fragment.mNextAnim != 0) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700841 Animator anim = AnimatorInflater.loadAnimator(mHost.getContext(), fragment.mNextAnim);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700842 if (anim != null) {
843 return anim;
844 }
845 }
846
Dianne Hackbornf121be72010-05-06 14:10:32 -0700847 if (transit == 0) {
848 return null;
849 }
850
851 int styleIndex = transitToStyleIndex(transit, enter);
852 if (styleIndex < 0) {
853 return null;
854 }
855
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700856 if (transitionStyle == 0 && mHost.onHasWindowAnimations()) {
857 transitionStyle = mHost.onGetWindowAnimations();
Dianne Hackbornf121be72010-05-06 14:10:32 -0700858 }
859 if (transitionStyle == 0) {
860 return null;
861 }
862
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700863 TypedArray attrs = mHost.getContext().obtainStyledAttributes(transitionStyle,
Chet Haase811ed1062010-08-06 10:38:15 -0700864 com.android.internal.R.styleable.FragmentAnimation);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700865 int anim = attrs.getResourceId(styleIndex, 0);
866 attrs.recycle();
867
868 if (anim == 0) {
869 return null;
870 }
871
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700872 return AnimatorInflater.loadAnimator(mHost.getContext(), anim);
Dianne Hackbornf121be72010-05-06 14:10:32 -0700873 }
874
Adam Powell635c60a2011-10-26 10:22:16 -0700875 public void performPendingDeferredStart(Fragment f) {
876 if (f.mDeferStart) {
Adam Powell78fed9b2011-11-07 10:45:34 -0800877 if (mExecutingActions) {
878 // Wait until we're done executing our pending transactions
879 mHavePendingDeferredStart = true;
880 return;
881 }
Adam Powell635c60a2011-10-26 10:22:16 -0700882 f.mDeferStart = false;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700883 moveToState(f, mCurState, 0, 0, false);
Adam Powell635c60a2011-10-26 10:22:16 -0700884 }
885 }
886
Adam Powell467cc6f2016-05-11 13:45:33 -0700887 boolean isStateAtLeast(int state) {
888 return mCurState >= state;
889 }
890
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700891 void moveToState(Fragment f, int newState, int transit, int transitionStyle,
892 boolean keepActive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -0700893 if (DEBUG && false) Log.v(TAG, "moveToState: " + f
894 + " oldState=" + f.mState + " newState=" + newState
895 + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
Craig Mautner1c437192012-08-01 10:01:16 -0700896
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700897 // Fragments that are not currently added will sit in the onCreate() state.
Dianne Hackborne181bd92012-09-25 14:15:15 -0700898 if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700899 newState = Fragment.CREATED;
900 }
Dianne Hackbornf9302322011-06-14 18:36:14 -0700901 if (f.mRemoving && newState > f.mState) {
902 // While removing a fragment, we can't change it to a higher state.
903 newState = f.mState;
904 }
Adam Powell2db4e4b2011-11-02 14:30:47 -0700905 // Defer start if requested; don't allow it to move to STARTED or higher
906 // if it's not already started.
907 if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
Adam Powell635c60a2011-10-26 10:22:16 -0700908 newState = Fragment.STOPPED;
909 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700910 if (f.mState < newState) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -0800911 // For fragments that are created from a layout, when restoring from
912 // state we don't want to allow them to be created until they are
913 // being reloaded from the layout.
914 if (f.mFromLayout && !f.mInLayout) {
915 return;
916 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800917 if (f.mAnimatingAway != null) {
918 // The fragment is currently being animated... but! Now we
919 // want to move our state back up. Give up on waiting for the
920 // animation, move to whatever the final state should be once
921 // the animation is done, and then we can proceed from there.
922 f.mAnimatingAway = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -0700923 moveToState(f, f.mStateAfterAnimating, 0, 0, true);
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800924 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700925 switch (f.mState) {
926 case Fragment.INITIALIZING:
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700927 if (DEBUG) Log.v(TAG, "moveto CREATED: " + f);
Dianne Hackborndef15372010-08-15 12:43:52 -0700928 if (f.mSavedFragmentState != null) {
929 f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray(
930 FragmentManagerImpl.VIEW_STATE_TAG);
931 f.mTarget = getFragment(f.mSavedFragmentState,
932 FragmentManagerImpl.TARGET_STATE_TAG);
933 if (f.mTarget != null) {
934 f.mTargetRequestCode = f.mSavedFragmentState.getInt(
935 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
936 }
Adam Powell78fed9b2011-11-07 10:45:34 -0800937 f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
938 FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
939 if (!f.mUserVisibleHint) {
940 f.mDeferStart = true;
941 if (newState > Fragment.STOPPED) {
942 newState = Fragment.STOPPED;
943 }
944 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700945 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700946 f.mHost = mHost;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700947 f.mParentFragment = mParent;
948 f.mFragmentManager = mParent != null
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700949 ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700950 f.mCalled = false;
Todd Kennedy434bd652015-05-04 12:29:50 -0700951 f.onAttach(mHost.getContext());
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700952 if (!f.mCalled) {
953 throw new SuperNotCalledException("Fragment " + f
954 + " did not call through to super.onAttach()");
955 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700956 if (f.mParentFragment == null) {
Todd Kennedy434bd652015-05-04 12:29:50 -0700957 mHost.onAttachFragment(f);
Adam Powelle30299f2016-05-13 11:04:39 -0700958 } else {
959 f.mParentFragment.onAttachFragment(f);
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700960 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700961
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700962 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700963 f.performCreate(f.mSavedFragmentState);
Adam Powelleacacb52016-03-23 13:07:27 -0700964 } else {
965 f.restoreChildFragmentState(f.mSavedFragmentState, true);
966 f.mState = Fragment.CREATED;
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700967 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700968 f.mRetaining = false;
969 if (f.mFromLayout) {
970 // For fragments that are part of the content view
971 // layout, we need to instantiate the view immediately
972 // and the inflater will take care of adding it.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700973 f.mView = f.performCreateView(f.getLayoutInflater(
974 f.mSavedFragmentState), null, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700975 if (f.mView != null) {
976 f.mView.setSaveFromParentEnabled(false);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700977 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700978 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700979 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700980 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700981 case Fragment.CREATED:
982 if (newState > Fragment.CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700983 if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700984 if (!f.mFromLayout) {
985 ViewGroup container = null;
986 if (f.mContainerId != 0) {
Adam Powelle01f5952016-02-23 15:25:42 -0800987 if (f.mContainerId == View.NO_ID) {
988 throwException(new IllegalArgumentException(
989 "Cannot create fragment "
990 + f
991 + " for a container view with no id"));
992 }
993 container = (ViewGroup) mContainer.onFindViewById(f.mContainerId);
Dianne Hackborn352cc982011-01-04 11:34:18 -0800994 if (container == null && !f.mRestored) {
Adam Powelle01f5952016-02-23 15:25:42 -0800995 String resName;
996 try {
997 resName = f.getResources().getResourceName(f.mContainerId);
998 } catch (NotFoundException e) {
999 resName = "unknown";
1000 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07001001 throwException(new IllegalArgumentException(
1002 "No view found for id 0x"
1003 + Integer.toHexString(f.mContainerId) + " ("
Adam Powelle01f5952016-02-23 15:25:42 -08001004 + resName
Dianne Hackborn4702a852012-08-17 15:18:29 -07001005 + ") for fragment " + f));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001006 }
1007 }
1008 f.mContainer = container;
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001009 f.mView = f.performCreateView(f.getLayoutInflater(
1010 f.mSavedFragmentState), container, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001011 if (f.mView != null) {
1012 f.mView.setSaveFromParentEnabled(false);
1013 if (container != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -07001014 Animator anim = loadAnimator(f, transit, true,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001015 transitionStyle);
1016 if (anim != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001017 anim.setTarget(f.mView);
Doris Liude9284d2015-05-22 15:41:46 -07001018 setHWLayerAnimListenerIfAlpha(f.mView, anim);
Chet Haase811ed1062010-08-06 10:38:15 -07001019 anim.start();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001020 }
1021 container.addView(f.mView);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001022 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001023 if (f.mHidden) f.mView.setVisibility(View.GONE);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001024 f.onViewCreated(f.mView, f.mSavedFragmentState);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001025 }
1026 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001027
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001028 f.performActivityCreated(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001029 if (f.mView != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001030 f.restoreViewState(f.mSavedFragmentState);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001031 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001032 f.mSavedFragmentState = null;
1033 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001034 case Fragment.ACTIVITY_CREATED:
Adam Powelleacacb52016-03-23 13:07:27 -07001035 if (newState > Fragment.ACTIVITY_CREATED) {
1036 f.mState = Fragment.STOPPED;
1037 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001038 case Fragment.STOPPED:
1039 if (newState > Fragment.STOPPED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001040 if (DEBUG) Log.v(TAG, "moveto STARTED: " + f);
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001041 f.performStart();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001042 }
1043 case Fragment.STARTED:
1044 if (newState > Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001045 if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001046 f.performResume();
Adam Powell95202512011-08-07 17:20:17 -07001047 // Get rid of this in case we saved it and never needed it.
1048 f.mSavedFragmentState = null;
1049 f.mSavedViewState = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001050 }
1051 }
1052 } else if (f.mState > newState) {
1053 switch (f.mState) {
1054 case Fragment.RESUMED:
1055 if (newState < Fragment.RESUMED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001056 if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001057 f.performPause();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001058 }
1059 case Fragment.STARTED:
1060 if (newState < Fragment.STARTED) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001061 if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f);
Dianne Hackborn2707d602010-07-09 18:01:20 -07001062 f.performStop();
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001063 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001064 case Fragment.STOPPED:
Dianne Hackbornc8017682010-07-06 13:34:38 -07001065 case Fragment.ACTIVITY_CREATED:
1066 if (newState < Fragment.ACTIVITY_CREATED) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001067 if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001068 if (f.mView != null) {
1069 // Need to save the current view state if not
1070 // done already.
Todd Kennedy46d168f2015-05-13 11:13:58 -07001071 if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001072 saveFragmentViewState(f);
Dianne Hackbornf121be72010-05-06 14:10:32 -07001073 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001074 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001075 f.performDestroyView();
Dianne Hackborndef15372010-08-15 12:43:52 -07001076 if (f.mView != null && f.mContainer != null) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001077 Animator anim = null;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001078 if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
Chet Haaseb20db3e2010-09-10 13:07:30 -07001079 anim = loadAnimator(f, transit, false,
Dianne Hackborndef15372010-08-15 12:43:52 -07001080 transitionStyle);
Chet Haaseb20db3e2010-09-10 13:07:30 -07001081 }
1082 if (anim != null) {
1083 final ViewGroup container = f.mContainer;
1084 final View view = f.mView;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001085 final Fragment fragment = f;
Chet Haaseb20db3e2010-09-10 13:07:30 -07001086 container.startViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001087 f.mAnimatingAway = anim;
1088 f.mStateAfterAnimating = newState;
Chet Haaseb20db3e2010-09-10 13:07:30 -07001089 anim.addListener(new AnimatorListenerAdapter() {
1090 @Override
1091 public void onAnimationEnd(Animator anim) {
1092 container.endViewTransition(view);
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001093 if (fragment.mAnimatingAway != null) {
1094 fragment.mAnimatingAway = null;
1095 moveToState(fragment, fragment.mStateAfterAnimating,
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001096 0, 0, false);
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001097 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001098 }
Chet Haaseb20db3e2010-09-10 13:07:30 -07001099 });
1100 anim.setTarget(f.mView);
Doris Liude9284d2015-05-22 15:41:46 -07001101 setHWLayerAnimListenerIfAlpha(f.mView, anim);
Chet Haaseb20db3e2010-09-10 13:07:30 -07001102 anim.start();
1103
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001104 }
Dianne Hackborndef15372010-08-15 12:43:52 -07001105 f.mContainer.removeView(f.mView);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001106 }
1107 f.mContainer = null;
1108 f.mView = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001109 }
1110 case Fragment.CREATED:
1111 if (newState < Fragment.CREATED) {
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001112 if (mDestroyed) {
1113 if (f.mAnimatingAway != null) {
1114 // The fragment's containing activity is
1115 // being destroyed, but this fragment is
1116 // currently animating away. Stop the
1117 // animation right now -- it is not needed,
1118 // and we can't wait any more on destroying
1119 // the fragment.
Dianne Hackborn1b39e222010-12-28 14:17:18 -08001120 Animator anim = f.mAnimatingAway;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001121 f.mAnimatingAway = null;
Dianne Hackborn1b39e222010-12-28 14:17:18 -08001122 anim.cancel();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001123 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001124 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001125 if (f.mAnimatingAway != null) {
1126 // We are waiting for the fragment's view to finish
1127 // animating away. Just make a note of the state
1128 // the fragment now should move to once the animation
1129 // is done.
1130 f.mStateAfterAnimating = newState;
Dianne Hackbornf9302322011-06-14 18:36:14 -07001131 newState = Fragment.CREATED;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001132 } else {
1133 if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f);
1134 if (!f.mRetaining) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001135 f.performDestroy();
Adam Powelld1d4d9c2016-01-12 10:11:42 -08001136 } else {
Adam Powellcbade7f2016-04-15 11:14:37 -07001137 f.mState = Fragment.INITIALIZING;
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001138 }
1139
Adam Powella9bab982016-04-21 11:04:41 -07001140 f.performDetach();
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001141 if (!keepActive) {
1142 if (!f.mRetaining) {
1143 makeInactive(f);
1144 } else {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001145 f.mHost = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001146 f.mParentFragment = null;
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001147 f.mFragmentManager = null;
1148 }
Dianne Hackbornf9302322011-06-14 18:36:14 -07001149 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001150 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001151 }
1152 }
1153 }
1154
Adam Powelld1d4d9c2016-01-12 10:11:42 -08001155 if (f.mState != newState) {
1156 Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; "
1157 + "expected state " + newState + " found " + f.mState);
1158 f.mState = newState;
1159 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001160 }
1161
Dianne Hackborn625ac272010-09-17 18:29:22 -07001162 void moveToState(Fragment f) {
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001163 moveToState(f, mCurState, 0, 0, false);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001164 }
1165
George Mounteca8e222016-07-07 13:13:05 -07001166 /**
1167 * Fragments that have been shown or hidden don't have their visibility changed or
1168 * animations run during the {@link #showFragment(Fragment)} or {@link #hideFragment(Fragment)}
1169 * calls. After fragments are brought to their final state in
1170 * {@link #moveFragmentToExpectedState(Fragment)} the fragments that have been shown or
1171 * hidden must have their visibility changed and their animations started here.
1172 *
1173 * @param fragment The fragment with mHiddenChanged = true that should change its View's
1174 * visibility and start the show or hide animation.
1175 */
1176 void completeShowHideFragment(final Fragment fragment) {
1177 if (fragment.mView != null) {
1178 Animator anim = loadAnimator(fragment, fragment.mNextTransition, !fragment.mHidden,
1179 fragment.mNextTransitionStyle);
1180 if (anim != null) {
1181 anim.setTarget(fragment.mView);
1182 if (fragment.mHidden) {
1183 // Delay the actual hide operation until the animation finishes, otherwise
1184 // the fragment will just immediately disappear
1185 anim.addListener(new AnimatorListenerAdapter() {
1186 @Override
1187 public void onAnimationEnd(Animator animation) {
1188 animation.removeListener(this);
1189 if (fragment.mView != null) {
1190 fragment.mView.setVisibility(View.GONE);
1191 }
1192 }
1193 });
1194 }
1195 setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
1196 anim.start();
1197 } else {
1198 final int visibility = fragment.mHidden ? View.GONE : View.VISIBLE;
1199 fragment.mView.setVisibility(visibility);
1200 }
1201 }
1202 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
1203 mNeedMenuInvalidate = true;
1204 }
1205 fragment.mHiddenChanged = false;
1206 fragment.onHiddenChanged(fragment.mHidden);
Dianne Hackbornf121be72010-05-06 14:10:32 -07001207 }
George Mounteca8e222016-07-07 13:13:05 -07001208
1209 /**
1210 * Moves a fragment to its expected final state or the fragment manager's state, depending
1211 * on whether the fragment manager's state is raised properly.
1212 *
1213 * @param f The fragment to change.
1214 */
1215 void moveFragmentToExpectedState(Fragment f) {
1216 if (f == null) {
1217 return;
1218 }
1219 int nextState = mCurState;
1220 if (f.mRemoving) {
1221 if (f.isInBackStack()) {
1222 nextState = Fragment.CREATED;
1223 } else {
1224 nextState = Fragment.INITIALIZING;
1225 }
1226 }
1227 moveToState(f, nextState, f.mNextTransition, f.mNextTransitionStyle, false);
1228
1229 if (f.mView != null) {
1230 Fragment underFragment = findFragmentUnder(f);
1231 if (underFragment != null) {
1232 final View underView = underFragment.mView;
1233 // make sure this fragment is in the right order.
1234 final ViewGroup container = f.mContainer;
1235 int underIndex = container.indexOfChild(underView);
1236 int viewIndex = container.indexOfChild(f.mView);
1237 if (viewIndex < underIndex) {
1238 container.removeViewAt(viewIndex);
1239 container.addView(f.mView, underIndex);
1240 }
1241 }
1242 }
1243 if (f.mHiddenChanged) {
1244 completeShowHideFragment(f);
1245 }
1246 }
1247
1248 void moveToState(int newState) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001249 if (mHost == null && newState != Fragment.INITIALIZING) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001250 throw new IllegalStateException("No activity");
1251 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001252
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001253 mCurState = newState;
George Mounteca8e222016-07-07 13:13:05 -07001254
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001255 if (mActive != null) {
Adam Powell635c60a2011-10-26 10:22:16 -07001256 boolean loadersRunning = false;
George Mounteca8e222016-07-07 13:13:05 -07001257
1258 // Must add them in the proper order. mActive fragments may be out of order
1259 final int numAdded = mAdded.size();
1260 for (int i = 0; i < numAdded; i++) {
1261 Fragment f = mAdded.get(i);
1262 moveFragmentToExpectedState(f);
1263 if (f.mLoaderManager != null) {
1264 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1265 }
1266 }
1267
1268 // Now iterate through all active fragments. These will include those that are removed
1269 // and detached.
1270 final int numActive = mActive.size();
1271 for (int i = 0; i < numActive; i++) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001272 Fragment f = mActive.get(i);
George Mounteca8e222016-07-07 13:13:05 -07001273 if (f != null && (f.mRemoving || f.mDetached)) {
1274 moveFragmentToExpectedState(f);
Adam Powell635c60a2011-10-26 10:22:16 -07001275 if (f.mLoaderManager != null) {
1276 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1277 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001278 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001279 }
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001280
Adam Powell635c60a2011-10-26 10:22:16 -07001281 if (!loadersRunning) {
1282 startPendingDeferredFragments();
1283 }
1284
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001285 if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) {
1286 mHost.onInvalidateOptionsMenu();
Dianne Hackborn5f36c962010-08-26 15:54:17 -07001287 mNeedMenuInvalidate = false;
1288 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001289 }
1290 }
1291
Adam Powell635c60a2011-10-26 10:22:16 -07001292 void startPendingDeferredFragments() {
Adam Powell37510902011-10-31 11:48:24 -07001293 if (mActive == null) return;
1294
Adam Powell635c60a2011-10-26 10:22:16 -07001295 for (int i=0; i<mActive.size(); i++) {
1296 Fragment f = mActive.get(i);
1297 if (f != null) {
1298 performPendingDeferredStart(f);
1299 }
1300 }
1301 }
1302
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001303 void makeActive(Fragment f) {
1304 if (f.mIndex >= 0) {
1305 return;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001306 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001307
1308 if (mAvailIndices == null || mAvailIndices.size() <= 0) {
1309 if (mActive == null) {
1310 mActive = new ArrayList<Fragment>();
1311 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001312 f.setIndex(mActive.size(), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001313 mActive.add(f);
1314
1315 } else {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001316 f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001317 mActive.set(f.mIndex, f);
1318 }
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001319 if (DEBUG) Log.v(TAG, "Allocated fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001320 }
1321
1322 void makeInactive(Fragment f) {
1323 if (f.mIndex < 0) {
1324 return;
1325 }
1326
Dianne Hackborn03fcc332012-05-15 12:49:40 -07001327 if (DEBUG) Log.v(TAG, "Freeing fragment index " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001328 mActive.set(f.mIndex, null);
1329 if (mAvailIndices == null) {
1330 mAvailIndices = new ArrayList<Integer>();
1331 }
1332 mAvailIndices.add(f.mIndex);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001333 mHost.inactivateFragment(f.mWho);
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001334 f.initState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001335 }
1336
1337 public void addFragment(Fragment fragment, boolean moveToStateNow) {
1338 if (mAdded == null) {
1339 mAdded = new ArrayList<Fragment>();
1340 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001341 if (DEBUG) Log.v(TAG, "add: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001342 makeActive(fragment);
1343 if (!fragment.mDetached) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001344 if (mAdded.contains(fragment)) {
1345 throw new IllegalStateException("Fragment already added: " + fragment);
1346 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001347 mAdded.add(fragment);
1348 fragment.mAdded = true;
1349 fragment.mRemoving = false;
George Mounteca8e222016-07-07 13:13:05 -07001350 if (fragment.mView == null) {
1351 fragment.mHiddenChanged = false;
1352 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001353 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001354 mNeedMenuInvalidate = true;
1355 }
1356 if (moveToStateNow) {
1357 moveToState(fragment);
1358 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001359 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001360 }
George Mounteca8e222016-07-07 13:13:05 -07001361
1362 public void removeFragment(Fragment fragment) {
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001363 if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001364 final boolean inactive = !fragment.isInBackStack();
1365 if (!fragment.mDetached || inactive) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001366 if (false) {
1367 // Would be nice to catch a bad remove here, but we need
1368 // time to test this to make sure we aren't crashes cases
1369 // where it is not a problem.
1370 if (!mAdded.contains(fragment)) {
1371 throw new IllegalStateException("Fragment not added: " + fragment);
1372 }
1373 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001374 if (mAdded != null) {
1375 mAdded.remove(fragment);
1376 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001377 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001378 mNeedMenuInvalidate = true;
1379 }
1380 fragment.mAdded = false;
1381 fragment.mRemoving = true;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001382 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07001383 }
George Mounteca8e222016-07-07 13:13:05 -07001384
1385 /**
1386 * Marks a fragment as hidden to be later animated in with
1387 * {@link #completeShowHideFragment(Fragment)}.
1388 *
1389 * @param fragment The fragment to be shown.
1390 */
1391 public void hideFragment(Fragment fragment) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001392 if (DEBUG) Log.v(TAG, "hide: " + fragment);
1393 if (!fragment.mHidden) {
1394 fragment.mHidden = true;
George Mounteca8e222016-07-07 13:13:05 -07001395 // Toggle hidden changed so that if a fragment goes through show/hide/show
1396 // it doesn't go through the animation.
1397 fragment.mHiddenChanged = !fragment.mHiddenChanged;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001398 }
1399 }
George Mounteca8e222016-07-07 13:13:05 -07001400
1401 /**
1402 * Marks a fragment as shown to be later animated in with
1403 * {@link #completeShowHideFragment(Fragment)}.
1404 *
1405 * @param fragment The fragment to be shown.
1406 */
1407 public void showFragment(Fragment fragment) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001408 if (DEBUG) Log.v(TAG, "show: " + fragment);
1409 if (fragment.mHidden) {
1410 fragment.mHidden = false;
George Mounteca8e222016-07-07 13:13:05 -07001411 // Toggle hidden changed so that if a fragment goes through show/hide/show
1412 // it doesn't go through the animation.
1413 fragment.mHiddenChanged = !fragment.mHiddenChanged;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001414 }
1415 }
George Mounteca8e222016-07-07 13:13:05 -07001416
1417 public void detachFragment(Fragment fragment) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001418 if (DEBUG) Log.v(TAG, "detach: " + fragment);
1419 if (!fragment.mDetached) {
1420 fragment.mDetached = true;
1421 if (fragment.mAdded) {
1422 // We are not already in back stack, so need to remove the fragment.
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001423 if (mAdded != null) {
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001424 if (DEBUG) Log.v(TAG, "remove from detach: " + fragment);
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001425 mAdded.remove(fragment);
1426 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001427 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001428 mNeedMenuInvalidate = true;
1429 }
1430 fragment.mAdded = false;
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001431 }
1432 }
1433 }
1434
George Mounteca8e222016-07-07 13:13:05 -07001435 public void attachFragment(Fragment fragment) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001436 if (DEBUG) Log.v(TAG, "attach: " + fragment);
1437 if (fragment.mDetached) {
1438 fragment.mDetached = false;
1439 if (!fragment.mAdded) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001440 if (mAdded == null) {
1441 mAdded = new ArrayList<Fragment>();
1442 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07001443 if (mAdded.contains(fragment)) {
1444 throw new IllegalStateException("Fragment already added: " + fragment);
1445 }
1446 if (DEBUG) Log.v(TAG, "add from attach: " + fragment);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001447 mAdded.add(fragment);
1448 fragment.mAdded = true;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001449 if (fragment.mHasMenu && fragment.mMenuVisible) {
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001450 mNeedMenuInvalidate = true;
1451 }
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001452 }
1453 }
1454 }
1455
Dianne Hackbornf121be72010-05-06 14:10:32 -07001456 public Fragment findFragmentById(int id) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001457 if (mAdded != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001458 // First look through added fragments.
1459 for (int i=mAdded.size()-1; i>=0; i--) {
1460 Fragment f = mAdded.get(i);
1461 if (f != null && f.mFragmentId == id) {
1462 return f;
1463 }
1464 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001465 }
1466 if (mActive != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001467 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001468 for (int i=mActive.size()-1; i>=0; i--) {
1469 Fragment f = mActive.get(i);
1470 if (f != null && f.mFragmentId == id) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001471 return f;
1472 }
1473 }
1474 }
1475 return null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001476 }
1477
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001478 public Fragment findFragmentByTag(String tag) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001479 if (mAdded != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001480 // First look through added fragments.
1481 for (int i=mAdded.size()-1; i>=0; i--) {
1482 Fragment f = mAdded.get(i);
1483 if (f != null && tag.equals(f.mTag)) {
1484 return f;
1485 }
1486 }
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07001487 }
1488 if (mActive != null && tag != null) {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001489 // Now for any known fragment.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001490 for (int i=mActive.size()-1; i>=0; i--) {
1491 Fragment f = mActive.get(i);
1492 if (f != null && tag.equals(f.mTag)) {
1493 return f;
1494 }
1495 }
1496 }
1497 return null;
1498 }
1499
1500 public Fragment findFragmentByWho(String who) {
1501 if (mActive != null && who != null) {
1502 for (int i=mActive.size()-1; i>=0; i--) {
1503 Fragment f = mActive.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001504 if (f != null && (f=f.findFragmentByWho(who)) != null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001505 return f;
1506 }
1507 }
1508 }
1509 return null;
1510 }
1511
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001512 private void checkStateLoss() {
1513 if (mStateSaved) {
1514 throw new IllegalStateException(
1515 "Can not perform this action after onSaveInstanceState");
1516 }
1517 if (mNoTransactionsBecause != null) {
1518 throw new IllegalStateException(
1519 "Can not perform this action inside of " + mNoTransactionsBecause);
1520 }
1521 }
1522
Alan Viverette95a46092013-08-14 11:17:25 -07001523 /**
1524 * Adds an action to the queue of pending actions.
1525 *
1526 * @param action the action to add
1527 * @param allowStateLoss whether to allow loss of state information
1528 * @throws IllegalStateException if the activity has been destroyed
1529 */
George Mounteca8e222016-07-07 13:13:05 -07001530 public void enqueueAction(OpGenerator action, boolean allowStateLoss) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001531 if (!allowStateLoss) {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001532 checkStateLoss();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001533 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001534 synchronized (this) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001535 if (mDestroyed || mHost == null) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -08001536 throw new IllegalStateException("Activity has been destroyed");
1537 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001538 if (mPendingActions == null) {
George Mounteca8e222016-07-07 13:13:05 -07001539 mPendingActions = new ArrayList<>();
Dianne Hackborn445646c2010-06-25 15:52:59 -07001540 }
1541 mPendingActions.add(action);
1542 if (mPendingActions.size() == 1) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001543 mHost.getHandler().removeCallbacks(mExecCommit);
1544 mHost.getHandler().post(mExecCommit);
Dianne Hackborn445646c2010-06-25 15:52:59 -07001545 }
1546 }
1547 }
1548
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001549 public int allocBackStackIndex(BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001550 synchronized (this) {
1551 if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) {
1552 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001553 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001554 }
1555 int index = mBackStackIndices.size();
1556 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1557 mBackStackIndices.add(bse);
1558 return index;
1559
1560 } else {
1561 int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1);
1562 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1563 mBackStackIndices.set(index, bse);
1564 return index;
1565 }
1566 }
1567 }
1568
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001569 public void setBackStackIndex(int index, BackStackRecord bse) {
Dianne Hackborndd913a52010-07-22 12:17:04 -07001570 synchronized (this) {
1571 if (mBackStackIndices == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001572 mBackStackIndices = new ArrayList<BackStackRecord>();
Dianne Hackborndd913a52010-07-22 12:17:04 -07001573 }
1574 int N = mBackStackIndices.size();
1575 if (index < N) {
1576 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse);
1577 mBackStackIndices.set(index, bse);
1578 } else {
1579 while (N < index) {
1580 mBackStackIndices.add(null);
1581 if (mAvailBackStackIndices == null) {
1582 mAvailBackStackIndices = new ArrayList<Integer>();
1583 }
1584 if (DEBUG) Log.v(TAG, "Adding available back stack index " + N);
1585 mAvailBackStackIndices.add(N);
1586 N++;
1587 }
1588 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse);
1589 mBackStackIndices.add(bse);
1590 }
1591 }
1592 }
1593
1594 public void freeBackStackIndex(int index) {
1595 synchronized (this) {
1596 mBackStackIndices.set(index, null);
1597 if (mAvailBackStackIndices == null) {
1598 mAvailBackStackIndices = new ArrayList<Integer>();
1599 }
1600 if (DEBUG) Log.v(TAG, "Freeing back stack index " + index);
1601 mAvailBackStackIndices.add(index);
1602 }
1603 }
1604
George Mounteca8e222016-07-07 13:13:05 -07001605 /**
1606 * Broken out from exec*, this prepares for gathering and executing operations.
1607 *
1608 * @param allowStateLoss true if state loss should be ignored or false if it should be
1609 * checked.
1610 */
1611 private void ensureExecReady(boolean allowStateLoss) {
Adam Powell8585ed62016-02-04 15:38:20 -08001612 if (mExecutingActions) {
1613 throw new IllegalStateException("FragmentManager is already executing transactions");
1614 }
1615
1616 if (Looper.myLooper() != mHost.getHandler().getLooper()) {
1617 throw new IllegalStateException("Must be called from main thread of fragment host");
1618 }
1619
Adam Powell3518fed2016-03-01 09:07:44 -08001620 if (!allowStateLoss) {
Adam Powell8585ed62016-02-04 15:38:20 -08001621 checkStateLoss();
1622 }
1623
George Mounteca8e222016-07-07 13:13:05 -07001624 if (mTmpRecords == null) {
1625 mTmpRecords = new ArrayList<>();
1626 mTmpIsPop = new ArrayList<>();
1627 }
1628 }
1629
1630 public void execSingleAction(OpGenerator action, boolean allowStateLoss) {
1631 ensureExecReady(allowStateLoss);
1632 if (action.generateOps(mTmpRecords, mTmpIsPop)) {
1633 mExecutingActions = true;
1634 try {
1635 optimizeAndExecuteOps(mTmpRecords, mTmpIsPop);
1636 } finally {
1637 cleanupExec();
1638 }
George Mountc8c2bdf2016-06-30 17:08:27 -07001639 }
Adam Powell8585ed62016-02-04 15:38:20 -08001640
1641 doPendingDeferredStart();
1642 }
1643
Dianne Hackborn445646c2010-06-25 15:52:59 -07001644 /**
George Mounteca8e222016-07-07 13:13:05 -07001645 * Broken out of exec*, this cleans up the mExecutingActions and the temporary structures
1646 * used in executing operations.
1647 */
1648 private void cleanupExec() {
1649 mExecutingActions = false;
1650 mTmpIsPop.clear();
1651 mTmpRecords.clear();
1652 }
1653
1654 /**
Dianne Hackborn445646c2010-06-25 15:52:59 -07001655 * Only call from main thread!
1656 */
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001657 public boolean execPendingActions() {
George Mounteca8e222016-07-07 13:13:05 -07001658 ensureExecReady(true);
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001659
1660 boolean didSomething = false;
George Mounteca8e222016-07-07 13:13:05 -07001661 while (generateOpsForPendingActions(mTmpRecords, mTmpIsPop)) {
Dianne Hackborn445646c2010-06-25 15:52:59 -07001662 mExecutingActions = true;
George Mountc8c2bdf2016-06-30 17:08:27 -07001663 try {
George Mounteca8e222016-07-07 13:13:05 -07001664 optimizeAndExecuteOps(mTmpRecords, mTmpIsPop);
George Mountc8c2bdf2016-06-30 17:08:27 -07001665 } finally {
George Mounteca8e222016-07-07 13:13:05 -07001666 cleanupExec();
Dianne Hackborn445646c2010-06-25 15:52:59 -07001667 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001668 didSomething = true;
Dianne Hackborn445646c2010-06-25 15:52:59 -07001669 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001670
Adam Powell8585ed62016-02-04 15:38:20 -08001671 doPendingDeferredStart();
1672
1673 return didSomething;
1674 }
1675
George Mounteca8e222016-07-07 13:13:05 -07001676 /**
1677 * Optimizes BackStackRecord operations. This method merges operations of proximate records
1678 * that allow optimization. See {@link FragmentTransaction#setAllowOptimization(boolean)}.
1679 * <p>
1680 * For example, a transaction that adds to the back stack and then another that pops that
1681 * back stack record will be optimized.
1682 * <p>
1683 * Likewise, two transactions committed that are executed at the same time will be optimized
1684 * as well as two pop operations executed together.
1685 *
1686 * @param records The records pending execution
1687 * @param isRecordPop The direction that these records are being run.
1688 */
1689 private void optimizeAndExecuteOps(ArrayList<BackStackRecord> records,
1690 ArrayList<Boolean> isRecordPop) {
1691 if (records == null || records.isEmpty()) {
1692 return;
1693 }
1694
1695 if (isRecordPop == null || records.size() != isRecordPop.size()) {
1696 throw new IllegalStateException("Internal error with the back stack records");
1697 }
1698
1699 final int numRecords = records.size();
1700 int startIndex = 0;
1701 for (int recordNum = 0; recordNum < numRecords; recordNum++) {
1702 final boolean canOptimize = records.get(recordNum).mAllowOptimization;
1703 if (!canOptimize) {
1704 // execute all previous transactions
1705 if (startIndex != recordNum) {
1706 executeOpsTogether(records, isRecordPop, startIndex, recordNum);
1707 }
1708 // execute all unoptimized together
1709 int optimizeEnd;
1710 for (optimizeEnd = recordNum + 1; optimizeEnd < numRecords; optimizeEnd++) {
1711 if (records.get(optimizeEnd).mAllowOptimization) {
1712 break;
1713 }
1714 }
1715 executeOpsTogether(records, isRecordPop, recordNum, optimizeEnd);
1716 startIndex = optimizeEnd;
1717 recordNum = optimizeEnd - 1;
1718 }
1719 }
1720 if (startIndex != numRecords) {
1721 executeOpsTogether(records, isRecordPop, startIndex, numRecords);
1722 }
1723 }
1724
1725 /**
1726 * Optimizes a subset of a list of BackStackRecords, all of which either allow optimization or
1727 * do not allow optimization.
1728 * @param records A list of BackStackRecords that are to be optimized
1729 * @param isRecordPop The direction that these records are being run.
1730 * @param startIndex The index of the first record in <code>records</code> to be optimized
1731 * @param endIndex One more than the final record index in <code>records</code> to optimize.
1732 */
1733 private void executeOpsTogether(ArrayList<BackStackRecord> records,
1734 ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) {
1735 final boolean allowOptimization = records.get(startIndex).mAllowOptimization;
1736 boolean addToBackStack = false;
1737 if (mTmpAddedFragments == null) {
1738 mTmpAddedFragments = new ArrayList<>();
1739 mTmpFragmentsContainerTransitions = new SparseArray<>();
1740 } else {
1741 mTmpAddedFragments.clear();
1742 mTmpFragmentsContainerTransitions.clear();
1743 }
1744 if (mAdded != null) {
1745 mTmpAddedFragments.addAll(mAdded);
1746 }
1747 for (int recordNum = startIndex; recordNum < endIndex; recordNum++) {
1748 final BackStackRecord record = records.get(recordNum);
1749 final boolean isPop = isRecordPop.get(recordNum);
1750 if (!isPop) {
1751 record.expandReplaceOps(mTmpAddedFragments);
1752 }
1753 final int bumpAmount = isPop ? -1 : 1;
1754 record.bumpBackStackNesting(bumpAmount);
1755 addToBackStack = addToBackStack || record.mAddToBackStack;
1756
1757 if (mCurState >= Fragment.CREATED) {
1758 if (isPop) {
1759 record.calculatePopFragments(mTmpFragmentsContainerTransitions);
1760 } else {
1761 record.calculateFragments(mTmpFragmentsContainerTransitions);
1762 }
1763 }
1764 }
1765 mTmpAddedFragments.clear();
1766
1767 if (!allowOptimization) {
1768 startTransitions(records, isRecordPop, startIndex, endIndex);
1769 }
1770 executeOps(records, isRecordPop, startIndex, endIndex);
1771
1772 if (allowOptimization) {
1773 moveFragmentsToAtLeastCreated();
1774 startTransitions(records, isRecordPop, startIndex, endIndex);
1775 moveToState(mCurState);
1776 }
1777
1778 for (int recordNum = startIndex; recordNum < endIndex; recordNum++) {
1779 final BackStackRecord record = records.get(recordNum);
1780 final boolean isPop = isRecordPop.get(recordNum);
1781 if (isPop && record.mIndex >= 0) {
1782 freeBackStackIndex(record.mIndex);
1783 record.mIndex = -1;
1784 }
1785 }
1786 if (addToBackStack) {
1787 reportBackStackChanged();
1788 }
1789 }
1790
1791 /**
1792 * Find a fragment within the fragment's container whose View should be below the passed
1793 * fragment. {@code null} is returned when the fragment has no View or if there should be
1794 * no fragment with a View below the given fragment.
1795 *
1796 * As an example, if mAdded has two Fragments with Views sharing the same container:
1797 * FragmentA
1798 * FragmentB
1799 *
1800 * Then, when processing FragmentB, FragmentA will be returned. If, however, FragmentA
1801 * had no View, null would be returned.
1802 *
1803 * @param f The fragment that may be on top of another fragment.
1804 * @return The fragment with a View under f, if one exists or null if f has no View or
1805 * there are no fragments with Views in the same container.
1806 */
1807 private Fragment findFragmentUnder(Fragment f) {
1808 final ViewGroup container = f.mContainer;
1809 final View view = f.mView;
1810
1811 if (container == null || view == null) {
1812 return null;
1813 }
1814
1815 final int fragmentIndex = mAdded.indexOf(f);
1816 for (int i = fragmentIndex - 1; i >= 0; i--) {
1817 Fragment underFragment = mAdded.get(i);
1818 if (underFragment.mContainer == container && underFragment.mView != null) {
1819 // Found the fragment under this one
1820 return underFragment;
1821 }
1822 }
1823 return null;
1824 }
1825
1826 /**
1827 * Run the operations in the BackStackRecords, either to push or pop.
1828 *
1829 * @param records The list of records whose operations should be run.
1830 * @param isRecordPop The direction that these records are being run.
1831 * @param startIndex The index of the first entry in records to run.
1832 * @param endIndex One past the index of the final entry in records to run.
1833 */
1834 private static void executeOps(ArrayList<BackStackRecord> records,
1835 ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) {
1836 for (int i = startIndex; i < endIndex; i++) {
1837 final BackStackRecord record = records.get(i);
1838 final boolean isPop = isRecordPop.get(i);
1839 if (isPop) {
1840 record.executePopOps();
1841 } else {
1842 record.executeOps();
1843 }
1844 }
1845 }
1846
1847 /**
1848 * Prepares the fragments for Transitions and starts it. If the FragmentManager is not
1849 * at Fragment.CREATED, no transition will be run.
1850 *
1851 * This is explicitly for {@link Fragment#setEnterTransition(Transition)} and its siblings,
1852 * not for {@link FragmentTransaction#setTransition(int)}.
1853 *
1854 * @param records The entries to examine for transitions.
1855 * @param isRecordPop The direction that these records are being run.
1856 * @param startIndex The index of the first entry in records to run transitions for.
1857 * @param endIndex One past the index of the final entry in records to run transitions for.
1858 */
1859 private void startTransitions(ArrayList<BackStackRecord> records,
1860 ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) {
1861 if (mCurState >= Fragment.CREATED) {
1862 if (mTmpFragmentsContainerTransitions.size() != 0) {
1863 BackStackRecord record = records.get(0);
1864 BackStackRecord.TransitionState state =
1865 record.beginTransition(mTmpFragmentsContainerTransitions);
1866 if (state != null) {
1867 for (int i = startIndex + 1; i < endIndex - 1; i++) {
1868 final BackStackRecord nameRecord = records.get(i);
1869 final boolean isPop = isRecordPop.get(i);
1870 ArrayList<String> sourceNames = isPop
1871 ? nameRecord.mSharedElementTargetNames
1872 : record.mSharedElementSourceNames;
1873 ArrayList<String> targetNames = isPop
1874 ? nameRecord.mSharedElementSourceNames
1875 : record.mSharedElementTargetNames;
1876 BackStackRecord.setNameOverrides(state, sourceNames, targetNames);
1877 }
1878 }
1879 mTmpFragmentsContainerTransitions.clear();
1880 }
1881 }
1882 }
1883
1884 /**
1885 * Ensure that fragments that are added are at least at the CREATED state
1886 * so that they may load Transitions using TransitionInflater. When the transaction
1887 * cannot be optimized, this is executed in
1888 * {@link BackStackRecord#setLastIn(SparseArray, SparseArray, Fragment)} instead. Prior to
1889 * N, this wasn't supported, so no out-of-order creation can be done for compatibility.
1890 * <p>
1891 * This won't change the state of the fragment manager, nor will it change the fragment's
1892 * state if the fragment manager isn't at least at the CREATED state.
1893 */
1894 private void moveFragmentsToAtLeastCreated() {
1895 if (mCurState < Fragment.CREATED) {
1896 return;
1897 }
1898 final int numAdded = mAdded == null ? 0 : mAdded.size();
1899 for (int i = 0; i < numAdded; i++) {
1900 Fragment fragment = mAdded.get(i);
1901 if (fragment.mState < Fragment.CREATED) {
1902 moveToState(fragment, Fragment.CREATED, 0, 0, false);
1903 }
1904 }
1905 }
1906
1907 /**
1908 * Adds all records in the pending actions to records and whether they are add or pop
1909 * operations to isPop. After executing, the pending actions will be empty.
1910 *
1911 * @param records All pending actions will generate BackStackRecords added to this.
1912 * This contains the transactions, in order, to execute.
1913 * @param isPop All pending actions will generate booleans to add to this. This contains
1914 * an entry for each entry in records to indicate whether or not it is a
1915 * pop action.
1916 */
1917 private boolean generateOpsForPendingActions(ArrayList<BackStackRecord> records,
1918 ArrayList<Boolean> isPop) {
1919 int numActions;
1920 synchronized (this) {
1921 if (mPendingActions == null || mPendingActions.size() == 0) {
1922 return false;
1923 }
1924
1925 numActions = mPendingActions.size();
1926 for (int i = 0; i < numActions; i++) {
1927 mPendingActions.get(i).generateOps(records, isPop);
1928 }
1929 mPendingActions.clear();
1930 mHost.getHandler().removeCallbacks(mExecCommit);
1931 }
1932 return numActions > 0;
1933 }
1934
Adam Powell8585ed62016-02-04 15:38:20 -08001935 void doPendingDeferredStart() {
Adam Powell78fed9b2011-11-07 10:45:34 -08001936 if (mHavePendingDeferredStart) {
1937 boolean loadersRunning = false;
1938 for (int i=0; i<mActive.size(); i++) {
1939 Fragment f = mActive.get(i);
1940 if (f != null && f.mLoaderManager != null) {
1941 loadersRunning |= f.mLoaderManager.hasRunningLoaders();
1942 }
1943 }
1944 if (!loadersRunning) {
1945 mHavePendingDeferredStart = false;
1946 startPendingDeferredFragments();
1947 }
1948 }
Dianne Hackborn445646c2010-06-25 15:52:59 -07001949 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001950
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001951 void reportBackStackChanged() {
1952 if (mBackStackChangeListeners != null) {
1953 for (int i=0; i<mBackStackChangeListeners.size(); i++) {
1954 mBackStackChangeListeners.get(i).onBackStackChanged();
1955 }
1956 }
1957 }
1958
1959 void addBackStackState(BackStackRecord state) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001960 if (mBackStack == null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001961 mBackStack = new ArrayList<BackStackRecord>();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001962 }
1963 mBackStack.add(state);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001964 reportBackStackChanged();
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001965 }
George Mounteca8e222016-07-07 13:13:05 -07001966
1967 boolean popBackStackState(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop,
1968 String name, int id, int flags) {
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001969 if (mBackStack == null) {
1970 return false;
1971 }
George Mounteca8e222016-07-07 13:13:05 -07001972 if (name == null && id < 0 && (flags & POP_BACK_STACK_INCLUSIVE) == 0) {
1973 int last = mBackStack.size() - 1;
Dianne Hackbornf121be72010-05-06 14:10:32 -07001974 if (last < 0) {
1975 return false;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001976 }
George Mounteca8e222016-07-07 13:13:05 -07001977 records.add(mBackStack.remove(last));
1978 isRecordPop.add(true);
Dianne Hackbornf121be72010-05-06 14:10:32 -07001979 } else {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001980 int index = -1;
1981 if (name != null || id >= 0) {
1982 // If a name or ID is specified, look for that place in
1983 // the stack.
1984 index = mBackStack.size()-1;
1985 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001986 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001987 if (name != null && name.equals(bss.getName())) {
1988 break;
1989 }
1990 if (id >= 0 && id == bss.mIndex) {
1991 break;
1992 }
1993 index--;
Dianne Hackbornf121be72010-05-06 14:10:32 -07001994 }
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001995 if (index < 0) {
1996 return false;
Dianne Hackborndd913a52010-07-22 12:17:04 -07001997 }
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08001998 if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001999 index--;
2000 // Consume all following entries that match.
2001 while (index >= 0) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07002002 BackStackRecord bss = mBackStack.get(index);
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07002003 if ((name != null && name.equals(bss.getName()))
2004 || (id >= 0 && id == bss.mIndex)) {
2005 index--;
2006 continue;
2007 }
2008 break;
2009 }
2010 }
Dianne Hackborndd913a52010-07-22 12:17:04 -07002011 }
2012 if (index == mBackStack.size()-1) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07002013 return false;
2014 }
George Mounteca8e222016-07-07 13:13:05 -07002015 for (int i = mBackStack.size() - 1; i > index; i--) {
2016 records.add(mBackStack.remove(i));
2017 isRecordPop.add(true);
Dianne Hackbornf121be72010-05-06 14:10:32 -07002018 }
Dianne Hackbornf121be72010-05-06 14:10:32 -07002019 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07002020 return true;
2021 }
Adam Powell44ba79e2016-02-04 16:20:37 -08002022
2023 FragmentManagerNonConfig retainNonConfig() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002024 ArrayList<Fragment> fragments = null;
Adam Powell44ba79e2016-02-04 16:20:37 -08002025 ArrayList<FragmentManagerNonConfig> childFragments = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002026 if (mActive != null) {
2027 for (int i=0; i<mActive.size(); i++) {
2028 Fragment f = mActive.get(i);
Adam Powell44ba79e2016-02-04 16:20:37 -08002029 if (f != null) {
2030 if (f.mRetainInstance) {
2031 if (fragments == null) {
2032 fragments = new ArrayList<>();
2033 }
2034 fragments.add(f);
2035 f.mRetaining = true;
2036 f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1;
2037 if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002038 }
Adam Powell44ba79e2016-02-04 16:20:37 -08002039 boolean addedChild = false;
2040 if (f.mChildFragmentManager != null) {
2041 FragmentManagerNonConfig child = f.mChildFragmentManager.retainNonConfig();
2042 if (child != null) {
2043 if (childFragments == null) {
2044 childFragments = new ArrayList<>();
2045 for (int j = 0; j < i; j++) {
2046 childFragments.add(null);
2047 }
2048 }
2049 childFragments.add(child);
2050 addedChild = true;
2051 }
2052 }
2053 if (childFragments != null && !addedChild) {
2054 childFragments.add(null);
2055 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002056 }
2057 }
2058 }
Adam Powell44ba79e2016-02-04 16:20:37 -08002059 if (fragments == null && childFragments == null) {
2060 return null;
2061 }
2062 return new FragmentManagerNonConfig(fragments, childFragments);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002063 }
2064
2065 void saveFragmentViewState(Fragment f) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002066 if (f.mView == null) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002067 return;
2068 }
2069 if (mStateArray == null) {
2070 mStateArray = new SparseArray<Parcelable>();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002071 } else {
2072 mStateArray.clear();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002073 }
2074 f.mView.saveHierarchyState(mStateArray);
2075 if (mStateArray.size() > 0) {
2076 f.mSavedViewState = mStateArray;
2077 mStateArray = null;
2078 }
2079 }
2080
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002081 Bundle saveFragmentBasicState(Fragment f) {
2082 Bundle result = null;
2083
2084 if (mStateBundle == null) {
2085 mStateBundle = new Bundle();
2086 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002087 f.performSaveInstanceState(mStateBundle);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002088 if (!mStateBundle.isEmpty()) {
2089 result = mStateBundle;
2090 mStateBundle = null;
2091 }
2092
2093 if (f.mView != null) {
2094 saveFragmentViewState(f);
Dianne Hackborn13332762011-06-03 17:34:45 -07002095 }
2096 if (f.mSavedViewState != null) {
2097 if (result == null) {
2098 result = new Bundle();
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002099 }
Dianne Hackborn13332762011-06-03 17:34:45 -07002100 result.putSparseParcelableArray(
2101 FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002102 }
Adam Powell78fed9b2011-11-07 10:45:34 -08002103 if (!f.mUserVisibleHint) {
Jake Wharton258029e2012-04-22 17:17:01 -04002104 if (result == null) {
2105 result = new Bundle();
2106 }
Adam Powell78fed9b2011-11-07 10:45:34 -08002107 // Only add this if it's not the default value
2108 result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
2109 }
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002110
2111 return result;
2112 }
2113
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002114 Parcelable saveAllState() {
Dianne Hackborn3a57fb92010-11-15 17:58:52 -08002115 // Make sure all pending operations have now been executed to get
2116 // our state update-to-date.
2117 execPendingActions();
2118
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002119 mStateSaved = true;
2120
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002121 if (mActive == null || mActive.size() <= 0) {
2122 return null;
2123 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002124
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002125 // First collect all active fragments.
2126 int N = mActive.size();
2127 FragmentState[] active = new FragmentState[N];
2128 boolean haveFragments = false;
2129 for (int i=0; i<N; i++) {
2130 Fragment f = mActive.get(i);
2131 if (f != null) {
Dianne Hackborn61af8a82012-05-30 16:38:30 -07002132 if (f.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002133 throwException(new IllegalStateException(
2134 "Failure saving state: active " + f
2135 + " has cleared index: " + f.mIndex));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07002136 }
2137
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002138 haveFragments = true;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07002139
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002140 FragmentState fs = new FragmentState(f);
2141 active[i] = fs;
2142
Dianne Hackborn625ac272010-09-17 18:29:22 -07002143 if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002144 fs.mSavedFragmentState = saveFragmentBasicState(f);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002145
2146 if (f.mTarget != null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002147 if (f.mTarget.mIndex < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002148 throwException(new IllegalStateException(
2149 "Failure saving state: " + f
2150 + " has target not in fragment manager: " + f.mTarget));
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002151 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002152 if (fs.mSavedFragmentState == null) {
2153 fs.mSavedFragmentState = new Bundle();
2154 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002155 putFragment(fs.mSavedFragmentState,
2156 FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
2157 if (f.mTargetRequestCode != 0) {
2158 fs.mSavedFragmentState.putInt(
2159 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
2160 f.mTargetRequestCode);
2161 }
Dianne Hackborndef15372010-08-15 12:43:52 -07002162 }
Dianne Hackborndef15372010-08-15 12:43:52 -07002163
Dianne Hackborn625ac272010-09-17 18:29:22 -07002164 } else {
2165 fs.mSavedFragmentState = f.mSavedFragmentState;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002166 }
2167
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002168 if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
2169 + fs.mSavedFragmentState);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002170 }
2171 }
2172
2173 if (!haveFragments) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002174 if (DEBUG) Log.v(TAG, "saveAllState: no fragments!");
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002175 return null;
2176 }
2177
2178 int[] added = null;
2179 BackStackState[] backStack = null;
2180
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002181 // Build list of currently added fragments.
Dianne Hackborn625ac272010-09-17 18:29:22 -07002182 if (mAdded != null) {
2183 N = mAdded.size();
2184 if (N > 0) {
2185 added = new int[N];
2186 for (int i=0; i<N; i++) {
2187 added[i] = mAdded.get(i).mIndex;
Dianne Hackborn61af8a82012-05-30 16:38:30 -07002188 if (added[i] < 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002189 throwException(new IllegalStateException(
2190 "Failure saving state: active " + mAdded.get(i)
2191 + " has cleared index: " + added[i]));
Dianne Hackborn61af8a82012-05-30 16:38:30 -07002192 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002193 if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i
2194 + ": " + mAdded.get(i));
2195 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002196 }
2197 }
2198
2199 // Now save back stack.
2200 if (mBackStack != null) {
2201 N = mBackStack.size();
2202 if (N > 0) {
2203 backStack = new BackStackState[N];
2204 for (int i=0; i<N; i++) {
2205 backStack[i] = new BackStackState(this, mBackStack.get(i));
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002206 if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i
2207 + ": " + mBackStack.get(i));
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002208 }
2209 }
2210 }
2211
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002212 FragmentManagerState fms = new FragmentManagerState();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002213 fms.mActive = active;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002214 fms.mAdded = added;
2215 fms.mBackStack = backStack;
2216 return fms;
2217 }
2218
Adam Powell44ba79e2016-02-04 16:20:37 -08002219 void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002220 // If there is no saved state at all, then there can not be
2221 // any nonConfig fragments either, so that is that.
2222 if (state == null) return;
2223 FragmentManagerState fms = (FragmentManagerState)state;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002224 if (fms.mActive == null) return;
Adam Powell44ba79e2016-02-04 16:20:37 -08002225
2226 List<FragmentManagerNonConfig> childNonConfigs = null;
2227
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002228 // First re-attach any non-config instances we are retaining back
2229 // to their saved state, so we don't try to instantiate them again.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002230 if (nonConfig != null) {
Adam Powell44ba79e2016-02-04 16:20:37 -08002231 List<Fragment> nonConfigFragments = nonConfig.getFragments();
2232 childNonConfigs = nonConfig.getChildNonConfigs();
2233 final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2234 for (int i = 0; i < count; i++) {
2235 Fragment f = nonConfigFragments.get(i);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002236 if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002237 FragmentState fs = fms.mActive[f.mIndex];
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002238 fs.mInstance = f;
2239 f.mSavedViewState = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002240 f.mBackStackNesting = 0;
Dianne Hackborn625ac272010-09-17 18:29:22 -07002241 f.mInLayout = false;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002242 f.mAdded = false;
Dianne Hackbornf9302322011-06-14 18:36:14 -07002243 f.mTarget = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002244 if (fs.mSavedFragmentState != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002245 fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader());
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002246 f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
Dianne Hackborndef15372010-08-15 12:43:52 -07002247 FragmentManagerImpl.VIEW_STATE_TAG);
Craig Mautner33acfbe2014-06-28 14:00:53 -07002248 f.mSavedFragmentState = fs.mSavedFragmentState;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002249 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002250 }
2251 }
2252
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002253 // Build the full list of active fragments, instantiating them from
2254 // their saved state.
Adam Powell44ba79e2016-02-04 16:20:37 -08002255 mActive = new ArrayList<>(fms.mActive.length);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002256 if (mAvailIndices != null) {
2257 mAvailIndices.clear();
2258 }
2259 for (int i=0; i<fms.mActive.length; i++) {
2260 FragmentState fs = fms.mActive[i];
2261 if (fs != null) {
Adam Powell44ba79e2016-02-04 16:20:37 -08002262 FragmentManagerNonConfig childNonConfig = null;
2263 if (childNonConfigs != null && i < childNonConfigs.size()) {
2264 childNonConfig = childNonConfigs.get(i);
2265 }
2266 Fragment f = fs.instantiate(mHost, mParent, childNonConfig);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07002267 if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002268 mActive.add(f);
Dianne Hackborn30d71892010-12-11 10:37:55 -08002269 // Now that the fragment is instantiated (or came from being
2270 // retained above), clear mInstance in case we end up re-restoring
2271 // from this FragmentState again.
2272 fs.mInstance = null;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002273 } else {
2274 mActive.add(null);
2275 if (mAvailIndices == null) {
Adam Powell44ba79e2016-02-04 16:20:37 -08002276 mAvailIndices = new ArrayList<>();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002277 }
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07002278 if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002279 mAvailIndices.add(i);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002280 }
2281 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002282
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002283 // Update the target of all retained fragments.
2284 if (nonConfig != null) {
Adam Powell44ba79e2016-02-04 16:20:37 -08002285 List<Fragment> nonConfigFragments = nonConfig.getFragments();
2286 final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0;
2287 for (int i = 0; i < count; i++) {
2288 Fragment f = nonConfigFragments.get(i);
Dianne Hackbornf9302322011-06-14 18:36:14 -07002289 if (f.mTargetIndex >= 0) {
2290 if (f.mTargetIndex < mActive.size()) {
2291 f.mTarget = mActive.get(f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002292 } else {
2293 Log.w(TAG, "Re-attaching retained fragment " + f
Dianne Hackbornf9302322011-06-14 18:36:14 -07002294 + " target no longer exists: " + f.mTargetIndex);
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002295 f.mTarget = null;
2296 }
2297 }
2298 }
2299 }
2300
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002301 // Build the list of currently added fragments.
2302 if (fms.mAdded != null) {
2303 mAdded = new ArrayList<Fragment>(fms.mAdded.length);
2304 for (int i=0; i<fms.mAdded.length; i++) {
2305 Fragment f = mActive.get(fms.mAdded[i]);
2306 if (f == null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002307 throwException(new IllegalStateException(
2308 "No instantiated fragment for index #" + fms.mAdded[i]));
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002309 }
2310 f.mAdded = true;
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07002311 if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f);
2312 if (mAdded.contains(f)) {
2313 throw new IllegalStateException("Already added!");
2314 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002315 mAdded.add(f);
2316 }
2317 } else {
2318 mAdded = null;
2319 }
2320
2321 // Build the back stack.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002322 if (fms.mBackStack != null) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07002323 mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002324 for (int i=0; i<fms.mBackStack.length; i++) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07002325 BackStackRecord bse = fms.mBackStack[i].instantiate(this);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07002326 if (DEBUG) {
2327 Log.v(TAG, "restoreAllState: back stack #" + i
Dianne Hackbornb7a2e472010-08-12 16:20:42 -07002328 + " (index " + bse.mIndex + "): " + bse);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07002329 LogWriter logw = new LogWriter(Log.VERBOSE, TAG);
Dianne Hackborn8c841092013-06-24 13:46:13 -07002330 PrintWriter pw = new FastPrintWriter(logw, false, 1024);
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07002331 bse.dump(" ", pw, false);
Dianne Hackborn8c841092013-06-24 13:46:13 -07002332 pw.flush();
Dianne Hackbornf43a33c2012-09-27 00:48:11 -07002333 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002334 mBackStack.add(bse);
Dianne Hackborndd913a52010-07-22 12:17:04 -07002335 if (bse.mIndex >= 0) {
2336 setBackStackIndex(bse.mIndex, bse);
2337 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002338 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07002339 } else {
2340 mBackStack = null;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002341 }
2342 }
2343
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002344 public void attachController(FragmentHostCallback<?> host, FragmentContainer container,
2345 Fragment parent) {
2346 if (mHost != null) throw new IllegalStateException("Already attached");
2347 mHost = host;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002348 mContainer = container;
2349 mParent = parent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002350 }
2351
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002352 public void noteStateNotSaved() {
2353 mStateSaved = false;
2354 }
2355
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002356 public void dispatchCreate() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002357 mStateSaved = false;
George Mounteca8e222016-07-07 13:13:05 -07002358 moveToState(Fragment.CREATED);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002359 }
2360
Dianne Hackbornc8017682010-07-06 13:34:38 -07002361 public void dispatchActivityCreated() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002362 mStateSaved = false;
George Mounteca8e222016-07-07 13:13:05 -07002363 moveToState(Fragment.ACTIVITY_CREATED);
Dianne Hackbornc8017682010-07-06 13:34:38 -07002364 }
2365
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002366 public void dispatchStart() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002367 mStateSaved = false;
George Mounteca8e222016-07-07 13:13:05 -07002368 moveToState(Fragment.STARTED);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002369 }
2370
2371 public void dispatchResume() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -07002372 mStateSaved = false;
George Mounteca8e222016-07-07 13:13:05 -07002373 moveToState(Fragment.RESUMED);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002374 }
2375
2376 public void dispatchPause() {
George Mounteca8e222016-07-07 13:13:05 -07002377 moveToState(Fragment.STARTED);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002378 }
2379
2380 public void dispatchStop() {
George Mounteca8e222016-07-07 13:13:05 -07002381 moveToState(Fragment.STOPPED);
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002382 }
2383
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002384 public void dispatchDestroyView() {
George Mounteca8e222016-07-07 13:13:05 -07002385 moveToState(Fragment.CREATED);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002386 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002387
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002388 public void dispatchDestroy() {
Dianne Hackbornd173fa32010-12-23 13:58:22 -08002389 mDestroyed = true;
Dianne Hackbornc6938232011-07-21 16:25:26 -07002390 execPendingActions();
George Mounteca8e222016-07-07 13:13:05 -07002391 moveToState(Fragment.INITIALIZING);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002392 mHost = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002393 mContainer = null;
2394 mParent = null;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002395 }
Wale Ogunwale7c796812016-01-29 21:13:50 -08002396
Andrii Kulian933076d2016-03-29 17:04:42 -07002397 public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) {
Wale Ogunwale7c796812016-01-29 21:13:50 -08002398 if (mAdded == null) {
2399 return;
2400 }
2401 for (int i = mAdded.size() - 1; i >= 0; --i) {
2402 final Fragment f = mAdded.get(i);
2403 if (f != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002404 f.performMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002405 }
2406 }
2407 }
2408
Andrii Kulian933076d2016-03-29 17:04:42 -07002409 public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Wale Ogunwale7c796812016-01-29 21:13:50 -08002410 if (mAdded == null) {
2411 return;
2412 }
2413 for (int i = mAdded.size() - 1; i >= 0; --i) {
2414 final Fragment f = mAdded.get(i);
2415 if (f != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002416 f.performPictureInPictureModeChanged(isInPictureInPictureMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002417 }
2418 }
2419 }
2420
Dianne Hackborn9d071802010-12-08 14:49:15 -08002421 public void dispatchConfigurationChanged(Configuration newConfig) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002422 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08002423 for (int i=0; i<mAdded.size(); i++) {
2424 Fragment f = mAdded.get(i);
2425 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002426 f.performConfigurationChanged(newConfig);
Dianne Hackborn9d071802010-12-08 14:49:15 -08002427 }
2428 }
2429 }
2430 }
2431
2432 public void dispatchLowMemory() {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002433 if (mAdded != null) {
Dianne Hackborn9d071802010-12-08 14:49:15 -08002434 for (int i=0; i<mAdded.size(); i++) {
2435 Fragment f = mAdded.get(i);
2436 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002437 f.performLowMemory();
Dianne Hackborn9d071802010-12-08 14:49:15 -08002438 }
2439 }
2440 }
2441 }
2442
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002443 public void dispatchTrimMemory(int level) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002444 if (mAdded != null) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002445 for (int i=0; i<mAdded.size(); i++) {
2446 Fragment f = mAdded.get(i);
2447 if (f != null) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002448 f.performTrimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07002449 }
2450 }
2451 }
2452 }
2453
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002454 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2455 boolean show = false;
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07002456 ArrayList<Fragment> newMenus = null;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002457 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002458 for (int i=0; i<mAdded.size(); i++) {
2459 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002460 if (f != null) {
2461 if (f.performCreateOptionsMenu(menu, inflater)) {
2462 show = true;
2463 if (newMenus == null) {
2464 newMenus = new ArrayList<Fragment>();
2465 }
2466 newMenus.add(f);
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07002467 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002468 }
2469 }
2470 }
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07002471
2472 if (mCreatedMenus != null) {
2473 for (int i=0; i<mCreatedMenus.size(); i++) {
2474 Fragment f = mCreatedMenus.get(i);
2475 if (newMenus == null || !newMenus.contains(f)) {
2476 f.onDestroyOptionsMenu();
2477 }
2478 }
2479 }
2480
2481 mCreatedMenus = newMenus;
2482
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002483 return show;
2484 }
2485
2486 public boolean dispatchPrepareOptionsMenu(Menu menu) {
2487 boolean show = false;
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002488 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002489 for (int i=0; i<mAdded.size(); i++) {
2490 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002491 if (f != null) {
2492 if (f.performPrepareOptionsMenu(menu)) {
2493 show = true;
2494 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002495 }
2496 }
2497 }
2498 return show;
2499 }
2500
2501 public boolean dispatchOptionsItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002502 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002503 for (int i=0; i<mAdded.size(); i++) {
2504 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002505 if (f != null) {
2506 if (f.performOptionsItemSelected(item)) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002507 return true;
2508 }
2509 }
2510 }
2511 }
2512 return false;
2513 }
2514
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07002515 public boolean dispatchContextItemSelected(MenuItem item) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002516 if (mAdded != null) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07002517 for (int i=0; i<mAdded.size(); i++) {
2518 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002519 if (f != null) {
2520 if (f.performContextItemSelected(item)) {
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07002521 return true;
2522 }
2523 }
2524 }
2525 }
2526 return false;
2527 }
2528
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002529 public void dispatchOptionsMenuClosed(Menu menu) {
Dianne Hackbornacdfbcc2012-06-19 15:07:05 -07002530 if (mAdded != null) {
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002531 for (int i=0; i<mAdded.size(); i++) {
2532 Fragment f = mAdded.get(i);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002533 if (f != null) {
2534 f.performOptionsMenuClosed(menu);
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07002535 }
2536 }
2537 }
2538 }
Adam Powellf0f5fff2011-08-01 13:42:50 -07002539
2540 @Override
2541 public void invalidateOptionsMenu() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002542 if (mHost != null && mCurState == Fragment.RESUMED) {
2543 mHost.onInvalidateOptionsMenu();
Adam Powellf0f5fff2011-08-01 13:42:50 -07002544 } else {
2545 mNeedMenuInvalidate = true;
2546 }
2547 }
2548
Dianne Hackbornf121be72010-05-06 14:10:32 -07002549 public static int reverseTransit(int transit) {
2550 int rev = 0;
2551 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002552 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
2553 rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002554 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002555 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
2556 rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002557 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002558 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
2559 rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002560 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002561 }
2562 return rev;
2563
2564 }
2565
2566 public static int transitToStyleIndex(int transit, boolean enter) {
2567 int animAttr = -1;
2568 switch (transit) {
Chet Haase811ed1062010-08-06 10:38:15 -07002569 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002570 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002571 ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation
2572 : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002573 break;
Chet Haase811ed1062010-08-06 10:38:15 -07002574 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE:
Dianne Hackbornf121be72010-05-06 14:10:32 -07002575 animAttr = enter
Chet Haase811ed1062010-08-06 10:38:15 -07002576 ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation
2577 : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002578 break;
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002579 case FragmentTransaction.TRANSIT_FRAGMENT_FADE:
Chet Haase9ff82bf2010-10-05 14:30:51 -07002580 animAttr = enter
Dianne Hackborn327fbd22011-01-17 14:38:50 -08002581 ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation
2582 : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation;
Chet Haase9ff82bf2010-10-05 14:30:51 -07002583 break;
Dianne Hackbornf121be72010-05-06 14:10:32 -07002584 }
2585 return animAttr;
2586 }
Adam Powell371a8092014-06-20 12:51:12 -07002587
2588 @Override
2589 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
2590 if (!"fragment".equals(name)) {
2591 return null;
2592 }
2593
2594 String fname = attrs.getAttributeValue(null, "class");
2595 TypedArray a =
2596 context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
2597 if (fname == null) {
2598 fname = a.getString(com.android.internal.R.styleable.Fragment_name);
2599 }
2600 int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID);
2601 String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
2602 a.recycle();
2603
2604 int containerId = parent != null ? parent.getId() : 0;
2605 if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
2606 throw new IllegalArgumentException(attrs.getPositionDescription()
2607 + ": Must specify unique android:id, android:tag, or have a parent with"
2608 + " an id for " + fname);
2609 }
2610
2611 // If we restored from a previous state, we may already have
2612 // instantiated this fragment from the state and should use
2613 // that instance instead of making a new one.
2614 Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
2615 if (fragment == null && tag != null) {
2616 fragment = findFragmentByTag(tag);
2617 }
2618 if (fragment == null && containerId != View.NO_ID) {
2619 fragment = findFragmentById(containerId);
2620 }
2621
2622 if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
2623 + Integer.toHexString(id) + " fname=" + fname
2624 + " existing=" + fragment);
2625 if (fragment == null) {
2626 fragment = Fragment.instantiate(context, fname);
2627 fragment.mFromLayout = true;
2628 fragment.mFragmentId = id != 0 ? id : containerId;
2629 fragment.mContainerId = containerId;
2630 fragment.mTag = tag;
2631 fragment.mInLayout = true;
2632 fragment.mFragmentManager = this;
Todd Kennedy0aa69b72015-08-31 14:10:04 -07002633 fragment.mHost = mHost;
Todd Kennedy434bd652015-05-04 12:29:50 -07002634 fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
Adam Powell371a8092014-06-20 12:51:12 -07002635 addFragment(fragment, true);
2636 } else if (fragment.mInLayout) {
2637 // A fragment already exists and it is not one we restored from
2638 // previous state.
2639 throw new IllegalArgumentException(attrs.getPositionDescription()
2640 + ": Duplicate id 0x" + Integer.toHexString(id)
2641 + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
2642 + " with another fragment for " + fname);
2643 } else {
2644 // This fragment was retained from a previous instance; get it
2645 // going now.
2646 fragment.mInLayout = true;
Todd Kennedyac0e6ca2015-10-19 12:48:55 -07002647 fragment.mHost = mHost;
Adam Powell371a8092014-06-20 12:51:12 -07002648 // If this fragment is newly instantiated (either right now, or
2649 // from last saved state), then give it the attributes to
2650 // initialize itself.
2651 if (!fragment.mRetaining) {
Todd Kennedy434bd652015-05-04 12:29:50 -07002652 fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState);
Adam Powell371a8092014-06-20 12:51:12 -07002653 }
2654 }
2655
2656 // If we haven't finished entering the CREATED state ourselves yet,
2657 // push the inflated child fragment along.
2658 if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
2659 moveToState(fragment, Fragment.CREATED, 0, 0, false);
2660 } else {
2661 moveToState(fragment);
2662 }
2663
2664 if (fragment.mView == null) {
2665 throw new IllegalStateException("Fragment " + fname
2666 + " did not create a view.");
2667 }
2668 if (id != 0) {
2669 fragment.mView.setId(id);
2670 }
2671 if (fragment.mView.getTag() == null) {
2672 fragment.mView.setTag(tag);
2673 }
2674 return fragment.mView;
2675 }
2676
2677 @Override
2678 public View onCreateView(String name, Context context, AttributeSet attrs) {
2679 return null;
2680 }
2681
2682 LayoutInflater.Factory2 getLayoutInflaterFactory() {
2683 return this;
2684 }
George Mounteca8e222016-07-07 13:13:05 -07002685
2686 /**
2687 * An add or pop transaction to be scheduled for the UI thread.
2688 */
2689 interface OpGenerator {
2690 /**
2691 * Generate transactions to add to {@code records} and whether or not the transaction is
2692 * an add or pop to {@code isRecordPop}.
2693 *
2694 * records and isRecordPop must be added equally so that each transaction in records
2695 * matches the boolean for whether or not it is a pop in isRecordPop.
2696 *
2697 * @param records A list to add transactions to.
2698 * @param isRecordPop A list to add whether or not the transactions added to records is
2699 * a pop transaction.
2700 * @return true if something was added or false otherwise.
2701 */
2702 boolean generateOps(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop);
2703 }
2704
2705 /**
2706 * A pop operation OpGenerator. This will be run on the UI thread and will generate the
2707 * transactions that will be popped if anything can be popped.
2708 */
2709 private class PopBackStackState implements OpGenerator {
2710 final String mName;
2711 final int mId;
2712 final int mFlags;
2713
2714 public PopBackStackState(String name, int id, int flags) {
2715 mName = name;
2716 mId = id;
2717 mFlags = flags;
2718 }
2719
2720 @Override
2721 public boolean generateOps(ArrayList<BackStackRecord> records,
2722 ArrayList<Boolean> isRecordPop) {
2723 return popBackStackState(records, isRecordPop, mName, mId, mFlags);
2724 }
2725 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002726}