FragmentManager.java revision 42435e007921e83d45bbb8532f4e2a6b460d42da
1/* 2 * Copyright (C) 2011 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.support.v4.app; 18 19import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21import android.content.Context; 22import android.content.res.Configuration; 23import android.content.res.Resources.NotFoundException; 24import android.content.res.TypedArray; 25import android.os.Build; 26import android.os.Bundle; 27import android.os.Looper; 28import android.os.Parcel; 29import android.os.Parcelable; 30import android.support.annotation.CallSuper; 31import android.support.annotation.IdRes; 32import android.support.annotation.RestrictTo; 33import android.support.annotation.StringRes; 34import android.support.v4.os.BuildCompat; 35import android.support.v4.util.DebugUtils; 36import android.support.v4.util.LogWriter; 37import android.support.v4.util.Pair; 38import android.support.v4.view.LayoutInflaterFactory; 39import android.support.v4.view.ViewCompat; 40import android.util.AttributeSet; 41import android.util.Log; 42import android.util.SparseArray; 43import android.view.LayoutInflater; 44import android.view.Menu; 45import android.view.MenuInflater; 46import android.view.MenuItem; 47import android.view.View; 48import android.view.ViewGroup; 49import android.view.animation.AccelerateInterpolator; 50import android.view.animation.AlphaAnimation; 51import android.view.animation.Animation; 52import android.view.animation.Animation.AnimationListener; 53import android.view.animation.AnimationSet; 54import android.view.animation.AnimationUtils; 55import android.view.animation.DecelerateInterpolator; 56import android.view.animation.Interpolator; 57import android.view.animation.ScaleAnimation; 58 59import java.io.FileDescriptor; 60import java.io.PrintWriter; 61import java.lang.reflect.Field; 62import java.util.ArrayList; 63import java.util.Arrays; 64import java.util.List; 65import java.util.concurrent.CopyOnWriteArrayList; 66 67/** 68 * Static library support version of the framework's {@link android.app.FragmentManager}. 69 * Used to write apps that run on platforms prior to Android 3.0. When running 70 * on Android 3.0 or above, this implementation is still used; it does not try 71 * to switch to the framework's implementation. See the framework {@link FragmentManager} 72 * documentation for a class overview. 73 * 74 * <p>Your activity must derive from {@link FragmentActivity} to use this. From such an activity, 75 * you can acquire the {@link FragmentManager} by calling 76 * {@link FragmentActivity#getSupportFragmentManager}. 77 */ 78public abstract class FragmentManager { 79 /** 80 * Representation of an entry on the fragment back stack, as created 81 * with {@link FragmentTransaction#addToBackStack(String) 82 * FragmentTransaction.addToBackStack()}. Entries can later be 83 * retrieved with {@link FragmentManager#getBackStackEntryAt(int) 84 * FragmentManager.getBackStackEntryAt()}. 85 * 86 * <p>Note that you should never hold on to a BackStackEntry object; 87 * the identifier as returned by {@link #getId} is the only thing that 88 * will be persisted across activity instances. 89 */ 90 public interface BackStackEntry { 91 /** 92 * Return the unique identifier for the entry. This is the only 93 * representation of the entry that will persist across activity 94 * instances. 95 */ 96 public int getId(); 97 98 /** 99 * Get the name that was supplied to 100 * {@link FragmentTransaction#addToBackStack(String) 101 * FragmentTransaction.addToBackStack(String)} when creating this entry. 102 */ 103 public String getName(); 104 105 /** 106 * Return the full bread crumb title resource identifier for the entry, 107 * or 0 if it does not have one. 108 */ 109 @StringRes 110 public int getBreadCrumbTitleRes(); 111 112 /** 113 * Return the short bread crumb title resource identifier for the entry, 114 * or 0 if it does not have one. 115 */ 116 @StringRes 117 public int getBreadCrumbShortTitleRes(); 118 119 /** 120 * Return the full bread crumb title for the entry, or null if it 121 * does not have one. 122 */ 123 public CharSequence getBreadCrumbTitle(); 124 125 /** 126 * Return the short bread crumb title for the entry, or null if it 127 * does not have one. 128 */ 129 public CharSequence getBreadCrumbShortTitle(); 130 } 131 132 /** 133 * Interface to watch for changes to the back stack. 134 */ 135 public interface OnBackStackChangedListener { 136 /** 137 * Called whenever the contents of the back stack change. 138 */ 139 public void onBackStackChanged(); 140 } 141 142 /** 143 * Start a series of edit operations on the Fragments associated with 144 * this FragmentManager. 145 * 146 * <p>Note: A fragment transaction can only be created/committed prior 147 * to an activity saving its state. If you try to commit a transaction 148 * after {@link FragmentActivity#onSaveInstanceState FragmentActivity.onSaveInstanceState()} 149 * (and prior to a following {@link FragmentActivity#onStart FragmentActivity.onStart} 150 * or {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will get an error. 151 * This is because the framework takes care of saving your current fragments 152 * in the state, and if changes are made after the state is saved then they 153 * will be lost.</p> 154 */ 155 public abstract FragmentTransaction beginTransaction(); 156 157 /** 158 * @hide -- remove once prebuilts are in. 159 * @deprecated 160 */ 161 @RestrictTo(LIBRARY_GROUP) 162 @Deprecated 163 public FragmentTransaction openTransaction() { 164 return beginTransaction(); 165 } 166 167 /** 168 * After a {@link FragmentTransaction} is committed with 169 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it 170 * is scheduled to be executed asynchronously on the process's main thread. 171 * If you want to immediately executing any such pending operations, you 172 * can call this function (only from the main thread) to do so. Note that 173 * all callbacks and other related behavior will be done from within this 174 * call, so be careful about where this is called from. 175 * 176 * <p>If you are committing a single transaction that does not modify the 177 * fragment back stack, strongly consider using 178 * {@link FragmentTransaction#commitNow()} instead. This can help avoid 179 * unwanted side effects when other code in your app has pending committed 180 * transactions that expect different timing.</p> 181 * <p> 182 * This also forces the start of any postponed Transactions where 183 * {@link Fragment#postponeEnterTransition()} has been called. 184 * 185 * @return Returns true if there were any pending transactions to be 186 * executed. 187 */ 188 public abstract boolean executePendingTransactions(); 189 190 /** 191 * Finds a fragment that was identified by the given id either when inflated 192 * from XML or as the container ID when added in a transaction. This first 193 * searches through fragments that are currently added to the manager's 194 * activity; if no such fragment is found, then all fragments currently 195 * on the back stack associated with this ID are searched. 196 * @return The fragment if found or null otherwise. 197 */ 198 public abstract Fragment findFragmentById(@IdRes int id); 199 200 /** 201 * Finds a fragment that was identified by the given tag either when inflated 202 * from XML or as supplied when added in a transaction. This first 203 * searches through fragments that are currently added to the manager's 204 * activity; if no such fragment is found, then all fragments currently 205 * on the back stack are searched. 206 * @return The fragment if found or null otherwise. 207 */ 208 public abstract Fragment findFragmentByTag(String tag); 209 210 /** 211 * Flag for {@link #popBackStack(String, int)} 212 * and {@link #popBackStack(int, int)}: If set, and the name or ID of 213 * a back stack entry has been supplied, then all matching entries will 214 * be consumed until one that doesn't match is found or the bottom of 215 * the stack is reached. Otherwise, all entries up to but not including that entry 216 * will be removed. 217 */ 218 public static final int POP_BACK_STACK_INCLUSIVE = 1<<0; 219 220 /** 221 * Pop the top state off the back stack. Returns true if there was one 222 * to pop, else false. This function is asynchronous -- it enqueues the 223 * request to pop, but the action will not be performed until the application 224 * returns to its event loop. 225 */ 226 public abstract void popBackStack(); 227 228 /** 229 * Like {@link #popBackStack()}, but performs the operation immediately 230 * inside of the call. This is like calling {@link #executePendingTransactions()} 231 * afterwards without forcing the start of postponed Transactions. 232 * @return Returns true if there was something popped, else false. 233 */ 234 public abstract boolean popBackStackImmediate(); 235 236 /** 237 * Pop the last fragment transition from the manager's fragment 238 * back stack. If there is nothing to pop, false is returned. 239 * 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 * 243 * @param name If non-null, this is the name of a previous back state 244 * to look for; if found, all states up to that state will be popped. The 245 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 246 * the named state itself is popped. If null, only the top state is popped. 247 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 248 */ 249 public abstract void popBackStack(String name, int flags); 250 251 /** 252 * Like {@link #popBackStack(String, int)}, but performs the operation immediately 253 * inside of the call. This is like calling {@link #executePendingTransactions()} 254 * afterwards without forcing the start of postponed Transactions. 255 * @return Returns true if there was something popped, else false. 256 */ 257 public abstract boolean popBackStackImmediate(String name, int flags); 258 259 /** 260 * Pop all back stack states up to the one with the given identifier. 261 * This function is asynchronous -- it enqueues the 262 * request to pop, but the action will not be performed until the application 263 * returns to its event loop. 264 * 265 * @param id Identifier of the stated to be popped. If no identifier exists, 266 * false is returned. 267 * The identifier is the number returned by 268 * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The 269 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 270 * the named state itself is popped. 271 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 272 */ 273 public abstract void popBackStack(int id, int flags); 274 275 /** 276 * Like {@link #popBackStack(int, int)}, but performs the operation immediately 277 * inside of the call. This is like calling {@link #executePendingTransactions()} 278 * afterwards without forcing the start of postponed Transactions. 279 * @return Returns true if there was something popped, else false. 280 */ 281 public abstract boolean popBackStackImmediate(int id, int flags); 282 283 /** 284 * Return the number of entries currently in the back stack. 285 */ 286 public abstract int getBackStackEntryCount(); 287 288 /** 289 * Return the BackStackEntry at index <var>index</var> in the back stack; 290 * entries start index 0 being the bottom of the stack. 291 */ 292 public abstract BackStackEntry getBackStackEntryAt(int index); 293 294 /** 295 * Add a new listener for changes to the fragment back stack. 296 */ 297 public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener); 298 299 /** 300 * Remove a listener that was previously added with 301 * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}. 302 */ 303 public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener); 304 305 /** 306 * Put a reference to a fragment in a Bundle. This Bundle can be 307 * persisted as saved state, and when later restoring 308 * {@link #getFragment(Bundle, String)} will return the current 309 * instance of the same fragment. 310 * 311 * @param bundle The bundle in which to put the fragment reference. 312 * @param key The name of the entry in the bundle. 313 * @param fragment The Fragment whose reference is to be stored. 314 */ 315 public abstract void putFragment(Bundle bundle, String key, Fragment fragment); 316 317 /** 318 * Retrieve the current Fragment instance for a reference previously 319 * placed with {@link #putFragment(Bundle, String, Fragment)}. 320 * 321 * @param bundle The bundle from which to retrieve the fragment reference. 322 * @param key The name of the entry in the bundle. 323 * @return Returns the current Fragment instance that is associated with 324 * the given reference. 325 */ 326 public abstract Fragment getFragment(Bundle bundle, String key); 327 328 /** 329 * Get a list of all fragments that have been added to the fragment manager. 330 * 331 * @return The list of all fragments or null if none. 332 * @hide 333 */ 334 @RestrictTo(LIBRARY_GROUP) 335 public abstract List<Fragment> getFragments(); 336 337 /** 338 * Save the current instance state of the given Fragment. This can be 339 * used later when creating a new instance of the Fragment and adding 340 * it to the fragment manager, to have it create itself to match the 341 * current state returned here. Note that there are limits on how 342 * this can be used: 343 * 344 * <ul> 345 * <li>The Fragment must currently be attached to the FragmentManager. 346 * <li>A new Fragment created using this saved state must be the same class 347 * type as the Fragment it was created from. 348 * <li>The saved state can not contain dependencies on other fragments -- 349 * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to 350 * store a fragment reference because that reference may not be valid when 351 * this saved state is later used. Likewise the Fragment's target and 352 * result code are not included in this state. 353 * </ul> 354 * 355 * @param f The Fragment whose state is to be saved. 356 * @return The generated state. This will be null if there was no 357 * interesting state created by the fragment. 358 */ 359 public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f); 360 361 /** 362 * Returns true if the final {@link android.app.Activity#onDestroy() Activity.onDestroy()} 363 * call has been made on the FragmentManager's Activity, so this instance is now dead. 364 */ 365 public abstract boolean isDestroyed(); 366 367 /** 368 * Registers a {@link FragmentLifecycleCallbacks} to listen to fragment lifecycle events 369 * happening in this FragmentManager. All registered callbacks will be automatically 370 * unregistered when this FragmentManager is destroyed. 371 * 372 * @param cb Callbacks to register 373 * @param recursive true to automatically register this callback for all child FragmentManagers 374 */ 375 public abstract void registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb, 376 boolean recursive); 377 378 /** 379 * Unregisters a previously registered {@link FragmentLifecycleCallbacks}. If the callback 380 * was not previously registered this call has no effect. All registered callbacks will be 381 * automatically unregistered when this FragmentManager is destroyed. 382 * 383 * @param cb Callbacks to unregister 384 */ 385 public abstract void unregisterFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb); 386 387 /** 388 * Print the FragmentManager's state into the given stream. 389 * 390 * @param prefix Text to print at the front of each line. 391 * @param fd The raw file descriptor that the dump is being sent to. 392 * @param writer A PrintWriter to which the dump is to be set. 393 * @param args Additional arguments to the dump request. 394 */ 395 public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); 396 397 /** 398 * Control whether the framework's internal fragment manager debugging 399 * logs are turned on. If enabled, you will see output in logcat as 400 * the framework performs fragment operations. 401 */ 402 public static void enableDebugLogging(boolean enabled) { 403 FragmentManagerImpl.DEBUG = enabled; 404 } 405 406 /** 407 * Callback interface for listening to fragment state changes that happen 408 * within a given FragmentManager. 409 */ 410 public abstract static class FragmentLifecycleCallbacks { 411 /** 412 * Called right before the fragment's {@link Fragment#onAttach(Context)} method is called. 413 * This is a good time to inject any required dependencies for the fragment before any of 414 * the fragment's lifecycle methods are invoked. 415 * 416 * @param fm Host FragmentManager 417 * @param f Fragment changing state 418 * @param context Context that the Fragment is being attached to 419 */ 420 public void onFragmentPreAttached(FragmentManager fm, Fragment f, Context context) {} 421 422 /** 423 * Called after the fragment has been attached to its host. Its host will have had 424 * <code>onAttachFragment</code> called before this call happens. 425 * 426 * @param fm Host FragmentManager 427 * @param f Fragment changing state 428 * @param context Context that the Fragment was attached to 429 */ 430 public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {} 431 432 /** 433 * Called after the fragment has returned from the FragmentManager's call to 434 * {@link Fragment#onCreate(Bundle)}. This will only happen once for any given 435 * fragment instance, though the fragment may be attached and detached multiple times. 436 * 437 * @param fm Host FragmentManager 438 * @param f Fragment changing state 439 * @param savedInstanceState Saved instance bundle from a previous instance 440 */ 441 public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {} 442 443 /** 444 * Called after the fragment has returned from the FragmentManager's call to 445 * {@link Fragment#onActivityCreated(Bundle)}. This will only happen once for any given 446 * fragment instance, though the fragment may be attached and detached multiple times. 447 * 448 * @param fm Host FragmentManager 449 * @param f Fragment changing state 450 * @param savedInstanceState Saved instance bundle from a previous instance 451 */ 452 public void onFragmentActivityCreated(FragmentManager fm, Fragment f, 453 Bundle savedInstanceState) {} 454 455 /** 456 * Called after the fragment has returned a non-null view from the FragmentManager's 457 * request to {@link Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)}. 458 * 459 * @param fm Host FragmentManager 460 * @param f Fragment that created and owns the view 461 * @param v View returned by the fragment 462 * @param savedInstanceState Saved instance bundle from a previous instance 463 */ 464 public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v, 465 Bundle savedInstanceState) {} 466 467 /** 468 * Called after the fragment has returned from the FragmentManager's call to 469 * {@link Fragment#onStart()}. 470 * 471 * @param fm Host FragmentManager 472 * @param f Fragment changing state 473 */ 474 public void onFragmentStarted(FragmentManager fm, Fragment f) {} 475 476 /** 477 * Called after the fragment has returned from the FragmentManager's call to 478 * {@link Fragment#onResume()}. 479 * 480 * @param fm Host FragmentManager 481 * @param f Fragment changing state 482 */ 483 public void onFragmentResumed(FragmentManager fm, Fragment f) {} 484 485 /** 486 * Called after the fragment has returned from the FragmentManager's call to 487 * {@link Fragment#onPause()}. 488 * 489 * @param fm Host FragmentManager 490 * @param f Fragment changing state 491 */ 492 public void onFragmentPaused(FragmentManager fm, Fragment f) {} 493 494 /** 495 * Called after the fragment has returned from the FragmentManager's call to 496 * {@link Fragment#onStop()}. 497 * 498 * @param fm Host FragmentManager 499 * @param f Fragment changing state 500 */ 501 public void onFragmentStopped(FragmentManager fm, Fragment f) {} 502 503 /** 504 * Called after the fragment has returned from the FragmentManager's call to 505 * {@link Fragment#onSaveInstanceState(Bundle)}. 506 * 507 * @param fm Host FragmentManager 508 * @param f Fragment changing state 509 * @param outState Saved state bundle for the fragment 510 */ 511 public void onFragmentSaveInstanceState(FragmentManager fm, Fragment f, Bundle outState) {} 512 513 /** 514 * Called after the fragment has returned from the FragmentManager's call to 515 * {@link Fragment#onDestroyView()}. 516 * 517 * @param fm Host FragmentManager 518 * @param f Fragment changing state 519 */ 520 public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {} 521 522 /** 523 * Called after the fragment has returned from the FragmentManager's call to 524 * {@link Fragment#onDestroy()}. 525 * 526 * @param fm Host FragmentManager 527 * @param f Fragment changing state 528 */ 529 public void onFragmentDestroyed(FragmentManager fm, Fragment f) {} 530 531 /** 532 * Called after the fragment has returned from the FragmentManager's call to 533 * {@link Fragment#onDetach()}. 534 * 535 * @param fm Host FragmentManager 536 * @param f Fragment changing state 537 */ 538 public void onFragmentDetached(FragmentManager fm, Fragment f) {} 539 } 540} 541 542final class FragmentManagerState implements Parcelable { 543 FragmentState[] mActive; 544 int[] mAdded; 545 BackStackState[] mBackStack; 546 547 public FragmentManagerState() { 548 } 549 550 public FragmentManagerState(Parcel in) { 551 mActive = in.createTypedArray(FragmentState.CREATOR); 552 mAdded = in.createIntArray(); 553 mBackStack = in.createTypedArray(BackStackState.CREATOR); 554 } 555 556 @Override 557 public int describeContents() { 558 return 0; 559 } 560 561 @Override 562 public void writeToParcel(Parcel dest, int flags) { 563 dest.writeTypedArray(mActive, flags); 564 dest.writeIntArray(mAdded); 565 dest.writeTypedArray(mBackStack, flags); 566 } 567 568 public static final Parcelable.Creator<FragmentManagerState> CREATOR 569 = new Parcelable.Creator<FragmentManagerState>() { 570 @Override 571 public FragmentManagerState createFromParcel(Parcel in) { 572 return new FragmentManagerState(in); 573 } 574 575 @Override 576 public FragmentManagerState[] newArray(int size) { 577 return new FragmentManagerState[size]; 578 } 579 }; 580} 581 582/** 583 * Container for fragments associated with an activity. 584 */ 585final class FragmentManagerImpl extends FragmentManager implements LayoutInflaterFactory { 586 static boolean DEBUG = false; 587 static final String TAG = "FragmentManager"; 588 589 static final boolean HONEYCOMB = android.os.Build.VERSION.SDK_INT >= 11; 590 591 static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state"; 592 static final String TARGET_STATE_TAG = "android:target_state"; 593 static final String VIEW_STATE_TAG = "android:view_state"; 594 static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint"; 595 596 static class AnimateOnHWLayerIfNeededListener implements AnimationListener { 597 private AnimationListener mOriginalListener; 598 private boolean mShouldRunOnHWLayer; 599 View mView; 600 601 public AnimateOnHWLayerIfNeededListener(final View v, Animation anim) { 602 if (v == null || anim == null) { 603 return; 604 } 605 mView = v; 606 } 607 608 public AnimateOnHWLayerIfNeededListener(final View v, Animation anim, 609 AnimationListener listener) { 610 if (v == null || anim == null) { 611 return; 612 } 613 mOriginalListener = listener; 614 mView = v; 615 mShouldRunOnHWLayer = true; 616 } 617 618 @Override 619 @CallSuper 620 public void onAnimationStart(Animation animation) { 621 if (mOriginalListener != null) { 622 mOriginalListener.onAnimationStart(animation); 623 } 624 } 625 626 @Override 627 @CallSuper 628 public void onAnimationEnd(Animation animation) { 629 if (mView != null && mShouldRunOnHWLayer) { 630 // If we're attached to a window, assume we're in the normal performTraversals 631 // drawing path for Animations running. It's not safe to change the layer type 632 // during drawing, so post it to the View to run later. If we're not attached 633 // or we're running on N and above, post it to the view. If we're not on N and 634 // not attached, do it right now since existing platform versions don't run the 635 // hwui renderer for detached views off the UI thread making changing layer type 636 // safe, but posting may not be. 637 // Prior to N posting to a detached view from a non-Looper thread could cause 638 // leaks, since the thread-local run queue on a non-Looper thread would never 639 // be flushed. 640 if (ViewCompat.isAttachedToWindow(mView) || BuildCompat.isAtLeastN()) { 641 mView.post(new Runnable() { 642 @Override 643 public void run() { 644 ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null); 645 } 646 }); 647 } else { 648 ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null); 649 } 650 } 651 if (mOriginalListener != null) { 652 mOriginalListener.onAnimationEnd(animation); 653 } 654 } 655 656 @Override 657 public void onAnimationRepeat(Animation animation) { 658 if (mOriginalListener != null) { 659 mOriginalListener.onAnimationRepeat(animation); 660 } 661 } 662 } 663 664 ArrayList<OpGenerator> mPendingActions; 665 Runnable[] mTmpActions; 666 boolean mExecutingActions; 667 668 ArrayList<Fragment> mActive; 669 ArrayList<Fragment> mAdded; 670 ArrayList<Integer> mAvailIndices; 671 ArrayList<BackStackRecord> mBackStack; 672 ArrayList<Fragment> mCreatedMenus; 673 674 // Must be accessed while locked. 675 ArrayList<BackStackRecord> mBackStackIndices; 676 ArrayList<Integer> mAvailBackStackIndices; 677 678 ArrayList<OnBackStackChangedListener> mBackStackChangeListeners; 679 private CopyOnWriteArrayList<Pair<FragmentLifecycleCallbacks, Boolean>> mLifecycleCallbacks; 680 681 int mCurState = Fragment.INITIALIZING; 682 FragmentHostCallback mHost; 683 FragmentContainer mContainer; 684 Fragment mParent; 685 686 static Field sAnimationListenerField = null; 687 688 boolean mNeedMenuInvalidate; 689 boolean mStateSaved; 690 boolean mDestroyed; 691 String mNoTransactionsBecause; 692 boolean mHavePendingDeferredStart; 693 694 // Temporary vars for optimizing execution of BackStackRecords: 695 ArrayList<BackStackRecord> mTmpRecords; 696 ArrayList<Boolean> mTmpIsPop; 697 ArrayList<Fragment> mTmpAddedFragments; 698 699 // Temporary vars for state save and restore. 700 Bundle mStateBundle = null; 701 SparseArray<Parcelable> mStateArray = null; 702 703 // Postponed transactions. 704 ArrayList<StartEnterTransitionListener> mPostponedTransactions; 705 706 Runnable mExecCommit = new Runnable() { 707 @Override 708 public void run() { 709 execPendingActions(); 710 } 711 }; 712 713 static boolean modifiesAlpha(Animation anim) { 714 if (anim instanceof AlphaAnimation) { 715 return true; 716 } else if (anim instanceof AnimationSet) { 717 List<Animation> anims = ((AnimationSet) anim).getAnimations(); 718 for (int i = 0; i < anims.size(); i++) { 719 if (anims.get(i) instanceof AlphaAnimation) { 720 return true; 721 } 722 } 723 } 724 return false; 725 } 726 727 static boolean shouldRunOnHWLayer(View v, Animation anim) { 728 return Build.VERSION.SDK_INT >= 19 729 && ViewCompat.getLayerType(v) == ViewCompat.LAYER_TYPE_NONE 730 && ViewCompat.hasOverlappingRendering(v) 731 && modifiesAlpha(anim); 732 } 733 734 private void throwException(RuntimeException ex) { 735 Log.e(TAG, ex.getMessage()); 736 Log.e(TAG, "Activity state:"); 737 LogWriter logw = new LogWriter(TAG); 738 PrintWriter pw = new PrintWriter(logw); 739 if (mHost != null) { 740 try { 741 mHost.onDump(" ", null, pw, new String[] { }); 742 } catch (Exception e) { 743 Log.e(TAG, "Failed dumping state", e); 744 } 745 } else { 746 try { 747 dump(" ", null, pw, new String[] { }); 748 } catch (Exception e) { 749 Log.e(TAG, "Failed dumping state", e); 750 } 751 } 752 throw ex; 753 } 754 755 @Override 756 public FragmentTransaction beginTransaction() { 757 return new BackStackRecord(this); 758 } 759 760 @Override 761 public boolean executePendingTransactions() { 762 boolean updates = execPendingActions(); 763 forcePostponedTransactions(); 764 return updates; 765 } 766 767 @Override 768 public void popBackStack() { 769 enqueueAction(new PopBackStackState(null, -1, 0), false); 770 } 771 772 @Override 773 public boolean popBackStackImmediate() { 774 checkStateLoss(); 775 return popBackStackImmediate(null, -1, 0); 776 } 777 778 @Override 779 public void popBackStack(final String name, final int flags) { 780 enqueueAction(new PopBackStackState(name, -1, flags), false); 781 } 782 783 @Override 784 public boolean popBackStackImmediate(String name, int flags) { 785 checkStateLoss(); 786 return popBackStackImmediate(name, -1, flags); 787 } 788 789 @Override 790 public void popBackStack(final int id, final int flags) { 791 if (id < 0) { 792 throw new IllegalArgumentException("Bad id: " + id); 793 } 794 enqueueAction(new PopBackStackState(null, id, flags), false); 795 } 796 797 @Override 798 public boolean popBackStackImmediate(int id, int flags) { 799 checkStateLoss(); 800 execPendingActions(); 801 if (id < 0) { 802 throw new IllegalArgumentException("Bad id: " + id); 803 } 804 return popBackStackImmediate(null, id, flags); 805 } 806 807 /** 808 * Used by all public popBackStackImmediate methods, this executes pending transactions and 809 * returns true if the pop action did anything, regardless of what other pending 810 * transactions did. 811 * 812 * @return true if the pop operation did anything or false otherwise. 813 */ 814 private boolean popBackStackImmediate(String name, int id, int flags) { 815 execPendingActions(); 816 ensureExecReady(true); 817 818 boolean executePop = popBackStackState(mTmpRecords, mTmpIsPop, name, id, flags); 819 if (executePop) { 820 mExecutingActions = true; 821 try { 822 optimizeAndExecuteOps(mTmpRecords, mTmpIsPop); 823 } finally { 824 cleanupExec(); 825 } 826 } 827 828 doPendingDeferredStart(); 829 return executePop; 830 } 831 832 @Override 833 public int getBackStackEntryCount() { 834 return mBackStack != null ? mBackStack.size() : 0; 835 } 836 837 @Override 838 public BackStackEntry getBackStackEntryAt(int index) { 839 return mBackStack.get(index); 840 } 841 842 @Override 843 public void addOnBackStackChangedListener(OnBackStackChangedListener listener) { 844 if (mBackStackChangeListeners == null) { 845 mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>(); 846 } 847 mBackStackChangeListeners.add(listener); 848 } 849 850 @Override 851 public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) { 852 if (mBackStackChangeListeners != null) { 853 mBackStackChangeListeners.remove(listener); 854 } 855 } 856 857 @Override 858 public void putFragment(Bundle bundle, String key, Fragment fragment) { 859 if (fragment.mIndex < 0) { 860 throwException(new IllegalStateException("Fragment " + fragment 861 + " is not currently in the FragmentManager")); 862 } 863 bundle.putInt(key, fragment.mIndex); 864 } 865 866 @Override 867 public Fragment getFragment(Bundle bundle, String key) { 868 int index = bundle.getInt(key, -1); 869 if (index == -1) { 870 return null; 871 } 872 if (index >= mActive.size()) { 873 throwException(new IllegalStateException("Fragment no longer exists for key " 874 + key + ": index " + index)); 875 } 876 Fragment f = mActive.get(index); 877 if (f == null) { 878 throwException(new IllegalStateException("Fragment no longer exists for key " 879 + key + ": index " + index)); 880 } 881 return f; 882 } 883 884 @Override 885 public List<Fragment> getFragments() { 886 return mActive; 887 } 888 889 @Override 890 public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) { 891 if (fragment.mIndex < 0) { 892 throwException( new IllegalStateException("Fragment " + fragment 893 + " is not currently in the FragmentManager")); 894 } 895 if (fragment.mState > Fragment.INITIALIZING) { 896 Bundle result = saveFragmentBasicState(fragment); 897 return result != null ? new Fragment.SavedState(result) : null; 898 } 899 return null; 900 } 901 902 @Override 903 public boolean isDestroyed() { 904 return mDestroyed; 905 } 906 907 @Override 908 public String toString() { 909 StringBuilder sb = new StringBuilder(128); 910 sb.append("FragmentManager{"); 911 sb.append(Integer.toHexString(System.identityHashCode(this))); 912 sb.append(" in "); 913 if (mParent != null) { 914 DebugUtils.buildShortClassTag(mParent, sb); 915 } else { 916 DebugUtils.buildShortClassTag(mHost, sb); 917 } 918 sb.append("}}"); 919 return sb.toString(); 920 } 921 922 @Override 923 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 924 String innerPrefix = prefix + " "; 925 926 int N; 927 if (mActive != null) { 928 N = mActive.size(); 929 if (N > 0) { 930 writer.print(prefix); writer.print("Active Fragments in "); 931 writer.print(Integer.toHexString(System.identityHashCode(this))); 932 writer.println(":"); 933 for (int i=0; i<N; i++) { 934 Fragment f = mActive.get(i); 935 writer.print(prefix); writer.print(" #"); writer.print(i); 936 writer.print(": "); writer.println(f); 937 if (f != null) { 938 f.dump(innerPrefix, fd, writer, args); 939 } 940 } 941 } 942 } 943 944 if (mAdded != null) { 945 N = mAdded.size(); 946 if (N > 0) { 947 writer.print(prefix); writer.println("Added Fragments:"); 948 for (int i=0; i<N; i++) { 949 Fragment f = mAdded.get(i); 950 writer.print(prefix); writer.print(" #"); writer.print(i); 951 writer.print(": "); writer.println(f.toString()); 952 } 953 } 954 } 955 956 if (mCreatedMenus != null) { 957 N = mCreatedMenus.size(); 958 if (N > 0) { 959 writer.print(prefix); writer.println("Fragments Created Menus:"); 960 for (int i=0; i<N; i++) { 961 Fragment f = mCreatedMenus.get(i); 962 writer.print(prefix); writer.print(" #"); writer.print(i); 963 writer.print(": "); writer.println(f.toString()); 964 } 965 } 966 } 967 968 if (mBackStack != null) { 969 N = mBackStack.size(); 970 if (N > 0) { 971 writer.print(prefix); writer.println("Back Stack:"); 972 for (int i=0; i<N; i++) { 973 BackStackRecord bs = mBackStack.get(i); 974 writer.print(prefix); writer.print(" #"); writer.print(i); 975 writer.print(": "); writer.println(bs.toString()); 976 bs.dump(innerPrefix, fd, writer, args); 977 } 978 } 979 } 980 981 synchronized (this) { 982 if (mBackStackIndices != null) { 983 N = mBackStackIndices.size(); 984 if (N > 0) { 985 writer.print(prefix); writer.println("Back Stack Indices:"); 986 for (int i=0; i<N; i++) { 987 BackStackRecord bs = mBackStackIndices.get(i); 988 writer.print(prefix); writer.print(" #"); writer.print(i); 989 writer.print(": "); writer.println(bs); 990 } 991 } 992 } 993 994 if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) { 995 writer.print(prefix); writer.print("mAvailBackStackIndices: "); 996 writer.println(Arrays.toString(mAvailBackStackIndices.toArray())); 997 } 998 } 999 1000 if (mPendingActions != null) { 1001 N = mPendingActions.size(); 1002 if (N > 0) { 1003 writer.print(prefix); writer.println("Pending Actions:"); 1004 for (int i=0; i<N; i++) { 1005 OpGenerator r = mPendingActions.get(i); 1006 writer.print(prefix); writer.print(" #"); writer.print(i); 1007 writer.print(": "); writer.println(r); 1008 } 1009 } 1010 } 1011 1012 writer.print(prefix); writer.println("FragmentManager misc state:"); 1013 writer.print(prefix); writer.print(" mHost="); writer.println(mHost); 1014 writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer); 1015 if (mParent != null) { 1016 writer.print(prefix); writer.print(" mParent="); writer.println(mParent); 1017 } 1018 writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState); 1019 writer.print(" mStateSaved="); writer.print(mStateSaved); 1020 writer.print(" mDestroyed="); writer.println(mDestroyed); 1021 if (mNeedMenuInvalidate) { 1022 writer.print(prefix); writer.print(" mNeedMenuInvalidate="); 1023 writer.println(mNeedMenuInvalidate); 1024 } 1025 if (mNoTransactionsBecause != null) { 1026 writer.print(prefix); writer.print(" mNoTransactionsBecause="); 1027 writer.println(mNoTransactionsBecause); 1028 } 1029 if (mAvailIndices != null && mAvailIndices.size() > 0) { 1030 writer.print(prefix); writer.print(" mAvailIndices: "); 1031 writer.println(Arrays.toString(mAvailIndices.toArray())); 1032 } 1033 } 1034 1035 static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f); 1036 static final Interpolator DECELERATE_CUBIC = new DecelerateInterpolator(1.5f); 1037 static final Interpolator ACCELERATE_QUINT = new AccelerateInterpolator(2.5f); 1038 static final Interpolator ACCELERATE_CUBIC = new AccelerateInterpolator(1.5f); 1039 1040 static final int ANIM_DUR = 220; 1041 1042 static Animation makeOpenCloseAnimation(Context context, float startScale, 1043 float endScale, float startAlpha, float endAlpha) { 1044 AnimationSet set = new AnimationSet(false); 1045 ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale, 1046 Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f); 1047 scale.setInterpolator(DECELERATE_QUINT); 1048 scale.setDuration(ANIM_DUR); 1049 set.addAnimation(scale); 1050 AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha); 1051 alpha.setInterpolator(DECELERATE_CUBIC); 1052 alpha.setDuration(ANIM_DUR); 1053 set.addAnimation(alpha); 1054 return set; 1055 } 1056 1057 static Animation makeFadeAnimation(Context context, float start, float end) { 1058 AlphaAnimation anim = new AlphaAnimation(start, end); 1059 anim.setInterpolator(DECELERATE_CUBIC); 1060 anim.setDuration(ANIM_DUR); 1061 return anim; 1062 } 1063 1064 Animation loadAnimation(Fragment fragment, int transit, boolean enter, 1065 int transitionStyle) { 1066 Animation animObj = fragment.onCreateAnimation(transit, enter, fragment.getNextAnim()); 1067 if (animObj != null) { 1068 return animObj; 1069 } 1070 1071 if (fragment.getNextAnim() != 0) { 1072 Animation anim = AnimationUtils.loadAnimation(mHost.getContext(), 1073 fragment.getNextAnim()); 1074 if (anim != null) { 1075 return anim; 1076 } 1077 } 1078 1079 if (transit == 0) { 1080 return null; 1081 } 1082 1083 int styleIndex = transitToStyleIndex(transit, enter); 1084 if (styleIndex < 0) { 1085 return null; 1086 } 1087 1088 switch (styleIndex) { 1089 case ANIM_STYLE_OPEN_ENTER: 1090 return makeOpenCloseAnimation(mHost.getContext(), 1.125f, 1.0f, 0, 1); 1091 case ANIM_STYLE_OPEN_EXIT: 1092 return makeOpenCloseAnimation(mHost.getContext(), 1.0f, .975f, 1, 0); 1093 case ANIM_STYLE_CLOSE_ENTER: 1094 return makeOpenCloseAnimation(mHost.getContext(), .975f, 1.0f, 0, 1); 1095 case ANIM_STYLE_CLOSE_EXIT: 1096 return makeOpenCloseAnimation(mHost.getContext(), 1.0f, 1.075f, 1, 0); 1097 case ANIM_STYLE_FADE_ENTER: 1098 return makeFadeAnimation(mHost.getContext(), 0, 1); 1099 case ANIM_STYLE_FADE_EXIT: 1100 return makeFadeAnimation(mHost.getContext(), 1, 0); 1101 } 1102 1103 if (transitionStyle == 0 && mHost.onHasWindowAnimations()) { 1104 transitionStyle = mHost.onGetWindowAnimations(); 1105 } 1106 if (transitionStyle == 0) { 1107 return null; 1108 } 1109 1110 //TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle, 1111 // com.android.internal.R.styleable.FragmentAnimation); 1112 //int anim = attrs.getResourceId(styleIndex, 0); 1113 //attrs.recycle(); 1114 1115 //if (anim == 0) { 1116 // return null; 1117 //} 1118 1119 //return AnimatorInflater.loadAnimator(mActivity, anim); 1120 return null; 1121 } 1122 1123 public void performPendingDeferredStart(Fragment f) { 1124 if (f.mDeferStart) { 1125 if (mExecutingActions) { 1126 // Wait until we're done executing our pending transactions 1127 mHavePendingDeferredStart = true; 1128 return; 1129 } 1130 f.mDeferStart = false; 1131 moveToState(f, mCurState, 0, 0, false); 1132 } 1133 } 1134 1135 /** 1136 * Sets the to be animated view on hardware layer during the animation. Note 1137 * that calling this will replace any existing animation listener on the animation 1138 * with a new one, as animations do not support more than one listeners. Therefore, 1139 * animations that already have listeners should do the layer change operations 1140 * in their existing listeners, rather than calling this function. 1141 */ 1142 private void setHWLayerAnimListenerIfAlpha(final View v, Animation anim) { 1143 if (v == null || anim == null) { 1144 return; 1145 } 1146 if (shouldRunOnHWLayer(v, anim)) { 1147 AnimationListener originalListener = null; 1148 try { 1149 if (sAnimationListenerField == null) { 1150 sAnimationListenerField = Animation.class.getDeclaredField("mListener"); 1151 sAnimationListenerField.setAccessible(true); 1152 } 1153 originalListener = (AnimationListener) sAnimationListenerField.get(anim); 1154 } catch (NoSuchFieldException e) { 1155 Log.e(TAG, "No field with the name mListener is found in Animation class", e); 1156 } catch (IllegalAccessException e) { 1157 Log.e(TAG, "Cannot access Animation's mListener field", e); 1158 } 1159 // If there's already a listener set on the animation, we need wrap the new listener 1160 // around the existing listener, so that they will both get animation listener 1161 // callbacks. 1162 ViewCompat.setLayerType(v, ViewCompat.LAYER_TYPE_HARDWARE, null); 1163 anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener(v, anim, 1164 originalListener)); 1165 } 1166 } 1167 1168 boolean isStateAtLeast(int state) { 1169 return mCurState >= state; 1170 } 1171 1172 void moveToState(Fragment f, int newState, int transit, int transitionStyle, 1173 boolean keepActive) { 1174 // Fragments that are not currently added will sit in the onCreate() state. 1175 if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) { 1176 newState = Fragment.CREATED; 1177 } 1178 if (f.mRemoving && newState > f.mState) { 1179 // While removing a fragment, we can't change it to a higher state. 1180 newState = f.mState; 1181 } 1182 // Defer start if requested; don't allow it to move to STARTED or higher 1183 // if it's not already started. 1184 if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) { 1185 newState = Fragment.STOPPED; 1186 } 1187 if (f.mState <= newState) { 1188 // For fragments that are created from a layout, when restoring from 1189 // state we don't want to allow them to be created until they are 1190 // being reloaded from the layout. 1191 if (f.mFromLayout && !f.mInLayout) { 1192 return; 1193 } 1194 if (f.getAnimatingAway() != null) { 1195 // The fragment is currently being animated... but! Now we 1196 // want to move our state back up. Give up on waiting for the 1197 // animation, move to whatever the final state should be once 1198 // the animation is done, and then we can proceed from there. 1199 f.setAnimatingAway(null); 1200 moveToState(f, f.getStateAfterAnimating(), 0, 0, true); 1201 } 1202 switch (f.mState) { 1203 case Fragment.INITIALIZING: 1204 if (newState > Fragment.INITIALIZING) { 1205 if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); 1206 if (f.mSavedFragmentState != null) { 1207 f.mSavedFragmentState.setClassLoader(mHost.getContext() 1208 .getClassLoader()); 1209 f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray( 1210 FragmentManagerImpl.VIEW_STATE_TAG); 1211 f.mTarget = getFragment(f.mSavedFragmentState, 1212 FragmentManagerImpl.TARGET_STATE_TAG); 1213 if (f.mTarget != null) { 1214 f.mTargetRequestCode = f.mSavedFragmentState.getInt( 1215 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0); 1216 } 1217 f.mUserVisibleHint = f.mSavedFragmentState.getBoolean( 1218 FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true); 1219 if (!f.mUserVisibleHint) { 1220 f.mDeferStart = true; 1221 if (newState > Fragment.STOPPED) { 1222 newState = Fragment.STOPPED; 1223 } 1224 } 1225 } 1226 f.mHost = mHost; 1227 f.mParentFragment = mParent; 1228 f.mFragmentManager = mParent != null 1229 ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl(); 1230 dispatchOnFragmentPreAttached(f, mHost.getContext(), false); 1231 f.mCalled = false; 1232 f.onAttach(mHost.getContext()); 1233 if (!f.mCalled) { 1234 throw new SuperNotCalledException("Fragment " + f 1235 + " did not call through to super.onAttach()"); 1236 } 1237 if (f.mParentFragment == null) { 1238 mHost.onAttachFragment(f); 1239 } else { 1240 f.mParentFragment.onAttachFragment(f); 1241 } 1242 dispatchOnFragmentAttached(f, mHost.getContext(), false); 1243 1244 if (!f.mRetaining) { 1245 f.performCreate(f.mSavedFragmentState); 1246 dispatchOnFragmentCreated(f, f.mSavedFragmentState, false); 1247 } else { 1248 f.restoreChildFragmentState(f.mSavedFragmentState); 1249 f.mState = Fragment.CREATED; 1250 } 1251 f.mRetaining = false; 1252 } 1253 1254 case Fragment.CREATED: 1255 // This is outside the if statement below on purpose; we want this to run 1256 // even if we do a moveToState from CREATED => *, CREATED => CREATED, and 1257 // * => CREATED as part of the case fallthrough above. 1258 ensureInflatedFragmentView(f); 1259 1260 if (newState > Fragment.CREATED) { 1261 if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f); 1262 if (!f.mFromLayout) { 1263 ViewGroup container = null; 1264 if (f.mContainerId != 0) { 1265 if (f.mContainerId == View.NO_ID) { 1266 throwException(new IllegalArgumentException( 1267 "Cannot create fragment " 1268 + f 1269 + " for a container view with no id")); 1270 } 1271 container = (ViewGroup) mContainer.onFindViewById(f.mContainerId); 1272 if (container == null && !f.mRestored) { 1273 String resName; 1274 try { 1275 resName = f.getResources().getResourceName(f.mContainerId); 1276 } catch (NotFoundException e) { 1277 resName = "unknown"; 1278 } 1279 throwException(new IllegalArgumentException( 1280 "No view found for id 0x" 1281 + Integer.toHexString(f.mContainerId) + " (" 1282 + resName 1283 + ") for fragment " + f)); 1284 } 1285 } 1286 f.mContainer = container; 1287 f.mView = f.performCreateView(f.getLayoutInflater( 1288 f.mSavedFragmentState), container, f.mSavedFragmentState); 1289 if (f.mView != null) { 1290 f.mInnerView = f.mView; 1291 if (Build.VERSION.SDK_INT >= 11) { 1292 ViewCompat.setSaveFromParentEnabled(f.mView, false); 1293 } else { 1294 f.mView = NoSaveStateFrameLayout.wrap(f.mView); 1295 } 1296 if (container != null) { 1297 container.addView(f.mView); 1298 f.mIsNewlyAdded = true; 1299 } 1300 if (f.mHidden) { 1301 f.mView.setVisibility(View.GONE); 1302 f.mIsNewlyAdded = false; // No animation 1303 } 1304 f.onViewCreated(f.mView, f.mSavedFragmentState); 1305 dispatchOnFragmentViewCreated(f, f.mView, f.mSavedFragmentState, 1306 false); 1307 } else { 1308 f.mInnerView = null; 1309 } 1310 } 1311 1312 f.performActivityCreated(f.mSavedFragmentState); 1313 dispatchOnFragmentActivityCreated(f, f.mSavedFragmentState, false); 1314 if (f.mView != null) { 1315 f.restoreViewState(f.mSavedFragmentState); 1316 } 1317 f.mSavedFragmentState = null; 1318 } 1319 case Fragment.ACTIVITY_CREATED: 1320 if (newState > Fragment.ACTIVITY_CREATED) { 1321 f.mState = Fragment.STOPPED; 1322 } 1323 case Fragment.STOPPED: 1324 if (newState > Fragment.STOPPED) { 1325 if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); 1326 f.performStart(); 1327 dispatchOnFragmentStarted(f, false); 1328 } 1329 case Fragment.STARTED: 1330 if (newState > Fragment.STARTED) { 1331 if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); 1332 f.performResume(); 1333 dispatchOnFragmentResumed(f, false); 1334 f.mSavedFragmentState = null; 1335 f.mSavedViewState = null; 1336 } 1337 } 1338 } else if (f.mState > newState) { 1339 switch (f.mState) { 1340 case Fragment.RESUMED: 1341 if (newState < Fragment.RESUMED) { 1342 if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); 1343 f.performPause(); 1344 dispatchOnFragmentPaused(f, false); 1345 } 1346 case Fragment.STARTED: 1347 if (newState < Fragment.STARTED) { 1348 if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); 1349 f.performStop(); 1350 dispatchOnFragmentStopped(f, false); 1351 } 1352 case Fragment.STOPPED: 1353 if (newState < Fragment.STOPPED) { 1354 if (DEBUG) Log.v(TAG, "movefrom STOPPED: " + f); 1355 f.performReallyStop(); 1356 } 1357 case Fragment.ACTIVITY_CREATED: 1358 if (newState < Fragment.ACTIVITY_CREATED) { 1359 if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f); 1360 if (f.mView != null) { 1361 // Need to save the current view state if not 1362 // done already. 1363 if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) { 1364 saveFragmentViewState(f); 1365 } 1366 } 1367 f.performDestroyView(); 1368 dispatchOnFragmentViewDestroyed(f, false); 1369 if (f.mView != null && f.mContainer != null) { 1370 Animation anim = null; 1371 if (mCurState > Fragment.INITIALIZING && !mDestroyed 1372 && f.mView.getVisibility() == View.VISIBLE) { 1373 anim = loadAnimation(f, transit, false, 1374 transitionStyle); 1375 } 1376 if (anim != null) { 1377 final Fragment fragment = f; 1378 f.setAnimatingAway(f.mView); 1379 f.setStateAfterAnimating(newState); 1380 final View viewToAnimate = f.mView; 1381 anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener( 1382 viewToAnimate, anim) { 1383 @Override 1384 public void onAnimationEnd(Animation animation) { 1385 super.onAnimationEnd(animation); 1386 if (fragment.getAnimatingAway() != null) { 1387 fragment.setAnimatingAway(null); 1388 moveToState(fragment, fragment.getStateAfterAnimating(), 1389 0, 0, false); 1390 } 1391 } 1392 }); 1393 f.mView.startAnimation(anim); 1394 } 1395 f.mContainer.removeView(f.mView); 1396 } 1397 f.mContainer = null; 1398 f.mView = null; 1399 f.mInnerView = null; 1400 f.mInLayout = false; 1401 } 1402 case Fragment.CREATED: 1403 if (newState < Fragment.CREATED) { 1404 if (mDestroyed) { 1405 if (f.getAnimatingAway() != null) { 1406 // The fragment's containing activity is 1407 // being destroyed, but this fragment is 1408 // currently animating away. Stop the 1409 // animation right now -- it is not needed, 1410 // and we can't wait any more on destroying 1411 // the fragment. 1412 View v = f.getAnimatingAway(); 1413 f.setAnimatingAway(null); 1414 v.clearAnimation(); 1415 } 1416 } 1417 if (f.getAnimatingAway() != null) { 1418 // We are waiting for the fragment's view to finish 1419 // animating away. Just make a note of the state 1420 // the fragment now should move to once the animation 1421 // is done. 1422 f.setStateAfterAnimating(newState); 1423 newState = Fragment.CREATED; 1424 } else { 1425 if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); 1426 if (!f.mRetaining) { 1427 f.performDestroy(); 1428 dispatchOnFragmentDestroyed(f, false); 1429 } else { 1430 f.mState = Fragment.INITIALIZING; 1431 } 1432 1433 f.performDetach(); 1434 dispatchOnFragmentDetached(f, false); 1435 if (!keepActive) { 1436 if (!f.mRetaining) { 1437 makeInactive(f); 1438 } else { 1439 f.mHost = null; 1440 f.mParentFragment = null; 1441 f.mFragmentManager = null; 1442 } 1443 } 1444 } 1445 } 1446 } 1447 } 1448 1449 if (f.mState != newState) { 1450 Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; " 1451 + "expected state " + newState + " found " + f.mState); 1452 f.mState = newState; 1453 } 1454 } 1455 1456 void moveToState(Fragment f) { 1457 moveToState(f, mCurState, 0, 0, false); 1458 } 1459 1460 void ensureInflatedFragmentView(Fragment f) { 1461 if (f.mFromLayout && !f.mPerformedCreateView) { 1462 f.mView = f.performCreateView(f.getLayoutInflater( 1463 f.mSavedFragmentState), null, f.mSavedFragmentState); 1464 if (f.mView != null) { 1465 f.mInnerView = f.mView; 1466 if (Build.VERSION.SDK_INT >= 11) { 1467 ViewCompat.setSaveFromParentEnabled(f.mView, false); 1468 } else { 1469 f.mView = NoSaveStateFrameLayout.wrap(f.mView); 1470 } 1471 if (f.mHidden) f.mView.setVisibility(View.GONE); 1472 f.onViewCreated(f.mView, f.mSavedFragmentState); 1473 dispatchOnFragmentViewCreated(f, f.mView, f.mSavedFragmentState, false); 1474 } else { 1475 f.mInnerView = null; 1476 } 1477 } 1478 } 1479 1480 /** 1481 * Fragments that have been shown or hidden don't have their visibility changed or 1482 * animations run during the {@link #showFragment(Fragment)} or {@link #hideFragment(Fragment)} 1483 * calls. After fragments are brought to their final state in 1484 * {@link #moveFragmentToExpectedState(Fragment)} the fragments that have been shown or 1485 * hidden must have their visibility changed and their animations started here. 1486 * 1487 * @param fragment The fragment with mHiddenChanged = true that should change its View's 1488 * visibility and start the show or hide animation. 1489 */ 1490 void completeShowHideFragment(final Fragment fragment) { 1491 if (fragment.mView != null) { 1492 Animation anim = loadAnimation(fragment, fragment.getNextTransition(), 1493 !fragment.mHidden, fragment.getNextTransitionStyle()); 1494 if (anim != null) { 1495 setHWLayerAnimListenerIfAlpha(fragment.mView, anim); 1496 fragment.mView.startAnimation(anim); 1497 setHWLayerAnimListenerIfAlpha(fragment.mView, anim); 1498 anim.start(); 1499 } 1500 final int visibility = fragment.mHidden && !fragment.isHideReplaced() 1501 ? View.GONE 1502 : View.VISIBLE; 1503 fragment.mView.setVisibility(visibility); 1504 if (fragment.isHideReplaced()) { 1505 fragment.setHideReplaced(false); 1506 } 1507 } 1508 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { 1509 mNeedMenuInvalidate = true; 1510 } 1511 fragment.mHiddenChanged = false; 1512 fragment.onHiddenChanged(fragment.mHidden); 1513 } 1514 1515 /** 1516 * Moves a fragment to its expected final state or the fragment manager's state, depending 1517 * on whether the fragment manager's state is raised properly. 1518 * 1519 * @param f The fragment to change. 1520 */ 1521 void moveFragmentToExpectedState(Fragment f) { 1522 if (f == null) { 1523 return; 1524 } 1525 int nextState = mCurState; 1526 if (f.mRemoving) { 1527 if (f.isInBackStack()) { 1528 nextState = Math.min(nextState, Fragment.CREATED); 1529 } else { 1530 nextState = Math.min(nextState, Fragment.INITIALIZING); 1531 } 1532 } 1533 moveToState(f, nextState, f.getNextTransition(), f.getNextTransitionStyle(), false); 1534 1535 if (f.mView != null) { 1536 // Move the view if it is out of order 1537 Fragment underFragment = findFragmentUnder(f); 1538 if (underFragment != null) { 1539 final View underView = underFragment.mView; 1540 // make sure this fragment is in the right order. 1541 final ViewGroup container = f.mContainer; 1542 int underIndex = container.indexOfChild(underView); 1543 int viewIndex = container.indexOfChild(f.mView); 1544 if (viewIndex < underIndex) { 1545 container.removeViewAt(viewIndex); 1546 container.addView(f.mView, underIndex); 1547 } 1548 } 1549 if (f.mIsNewlyAdded && f.mContainer != null) { 1550 // Make it visible and run the animations 1551 f.mView.setVisibility(View.VISIBLE); 1552 f.mIsNewlyAdded = false; 1553 // run animations: 1554 Animation anim = loadAnimation(f, f.getNextTransition(), true, 1555 f.getNextTransitionStyle()); 1556 if (anim != null) { 1557 setHWLayerAnimListenerIfAlpha(f.mView, anim); 1558 f.mView.startAnimation(anim); 1559 } 1560 } 1561 } 1562 if (f.mHiddenChanged) { 1563 completeShowHideFragment(f); 1564 } 1565 } 1566 1567 /** 1568 * Changes the state of the fragment manager to {@code newState}. If the fragment manager 1569 * changes state or {@code always} is {@code true}, any fragments within it have their 1570 * states updated as well. 1571 * 1572 * @param newState The new state for the fragment manager 1573 * @param always If {@code true}, all fragments update their state, even 1574 * if {@code newState} matches the current fragment manager's state. 1575 */ 1576 void moveToState(int newState, boolean always) { 1577 if (mHost == null && newState != Fragment.INITIALIZING) { 1578 throw new IllegalStateException("No activity"); 1579 } 1580 1581 if (!always && newState == mCurState) { 1582 return; 1583 } 1584 1585 mCurState = newState; 1586 1587 if (mActive != null) { 1588 boolean loadersRunning = false; 1589 1590 // Must add them in the proper order. mActive fragments may be out of order 1591 if (mAdded != null) { 1592 final int numAdded = mAdded.size(); 1593 for (int i = 0; i < numAdded; i++) { 1594 Fragment f = mAdded.get(i); 1595 moveFragmentToExpectedState(f); 1596 if (f.mLoaderManager != null) { 1597 loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 1598 } 1599 } 1600 } 1601 1602 // Now iterate through all active fragments. These will include those that are removed 1603 // and detached. 1604 final int numActive = mActive.size(); 1605 for (int i = 0; i < numActive; i++) { 1606 Fragment f = mActive.get(i); 1607 if (f != null && (f.mRemoving || f.mDetached) && !f.mIsNewlyAdded) { 1608 moveFragmentToExpectedState(f); 1609 if (f.mLoaderManager != null) { 1610 loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 1611 } 1612 } 1613 } 1614 1615 if (!loadersRunning) { 1616 startPendingDeferredFragments(); 1617 } 1618 1619 if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) { 1620 mHost.onSupportInvalidateOptionsMenu(); 1621 mNeedMenuInvalidate = false; 1622 } 1623 } 1624 } 1625 1626 void startPendingDeferredFragments() { 1627 if (mActive == null) return; 1628 1629 for (int i=0; i<mActive.size(); i++) { 1630 Fragment f = mActive.get(i); 1631 if (f != null) { 1632 performPendingDeferredStart(f); 1633 } 1634 } 1635 } 1636 1637 void makeActive(Fragment f) { 1638 if (f.mIndex >= 0) { 1639 return; 1640 } 1641 1642 if (mAvailIndices == null || mAvailIndices.size() <= 0) { 1643 if (mActive == null) { 1644 mActive = new ArrayList<Fragment>(); 1645 } 1646 f.setIndex(mActive.size(), mParent); 1647 mActive.add(f); 1648 1649 } else { 1650 f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent); 1651 mActive.set(f.mIndex, f); 1652 } 1653 if (DEBUG) Log.v(TAG, "Allocated fragment index " + f); 1654 } 1655 1656 void makeInactive(Fragment f) { 1657 if (f.mIndex < 0) { 1658 return; 1659 } 1660 1661 if (DEBUG) Log.v(TAG, "Freeing fragment index " + f); 1662 mActive.set(f.mIndex, null); 1663 if (mAvailIndices == null) { 1664 mAvailIndices = new ArrayList<Integer>(); 1665 } 1666 mAvailIndices.add(f.mIndex); 1667 mHost.inactivateFragment(f.mWho); 1668 f.initState(); 1669 } 1670 1671 public void addFragment(Fragment fragment, boolean moveToStateNow) { 1672 if (mAdded == null) { 1673 mAdded = new ArrayList<Fragment>(); 1674 } 1675 if (DEBUG) Log.v(TAG, "add: " + fragment); 1676 makeActive(fragment); 1677 if (!fragment.mDetached) { 1678 if (mAdded.contains(fragment)) { 1679 throw new IllegalStateException("Fragment already added: " + fragment); 1680 } 1681 mAdded.add(fragment); 1682 fragment.mAdded = true; 1683 fragment.mRemoving = false; 1684 if (fragment.mView == null) { 1685 fragment.mHiddenChanged = false; 1686 } 1687 if (fragment.mHasMenu && fragment.mMenuVisible) { 1688 mNeedMenuInvalidate = true; 1689 } 1690 if (moveToStateNow) { 1691 moveToState(fragment); 1692 } 1693 } 1694 } 1695 1696 public void removeFragment(Fragment fragment) { 1697 if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting); 1698 final boolean inactive = !fragment.isInBackStack(); 1699 if (!fragment.mDetached || inactive) { 1700 if (mAdded != null) { 1701 mAdded.remove(fragment); 1702 } 1703 if (fragment.mHasMenu && fragment.mMenuVisible) { 1704 mNeedMenuInvalidate = true; 1705 } 1706 fragment.mAdded = false; 1707 fragment.mRemoving = true; 1708 } 1709 } 1710 1711 /** 1712 * Marks a fragment as hidden to be later animated in with 1713 * {@link #completeShowHideFragment(Fragment)}. 1714 * 1715 * @param fragment The fragment to be shown. 1716 */ 1717 public void hideFragment(Fragment fragment) { 1718 if (DEBUG) Log.v(TAG, "hide: " + fragment); 1719 if (!fragment.mHidden) { 1720 fragment.mHidden = true; 1721 // Toggle hidden changed so that if a fragment goes through show/hide/show 1722 // it doesn't go through the animation. 1723 fragment.mHiddenChanged = !fragment.mHiddenChanged; 1724 } 1725 } 1726 1727 /** 1728 * Marks a fragment as shown to be later animated in with 1729 * {@link #completeShowHideFragment(Fragment)}. 1730 * 1731 * @param fragment The fragment to be shown. 1732 */ 1733 public void showFragment(Fragment fragment) { 1734 if (DEBUG) Log.v(TAG, "show: " + fragment); 1735 if (fragment.mHidden) { 1736 fragment.mHidden = false; 1737 // Toggle hidden changed so that if a fragment goes through show/hide/show 1738 // it doesn't go through the animation. 1739 fragment.mHiddenChanged = !fragment.mHiddenChanged; 1740 } 1741 } 1742 1743 public void detachFragment(Fragment fragment) { 1744 if (DEBUG) Log.v(TAG, "detach: " + fragment); 1745 if (!fragment.mDetached) { 1746 fragment.mDetached = true; 1747 if (fragment.mAdded) { 1748 // We are not already in back stack, so need to remove the fragment. 1749 if (mAdded != null) { 1750 if (DEBUG) Log.v(TAG, "remove from detach: " + fragment); 1751 mAdded.remove(fragment); 1752 } 1753 if (fragment.mHasMenu && fragment.mMenuVisible) { 1754 mNeedMenuInvalidate = true; 1755 } 1756 fragment.mAdded = false; 1757 } 1758 } 1759 } 1760 1761 public void attachFragment(Fragment fragment) { 1762 if (DEBUG) Log.v(TAG, "attach: " + fragment); 1763 if (fragment.mDetached) { 1764 fragment.mDetached = false; 1765 if (!fragment.mAdded) { 1766 if (mAdded == null) { 1767 mAdded = new ArrayList<Fragment>(); 1768 } 1769 if (mAdded.contains(fragment)) { 1770 throw new IllegalStateException("Fragment already added: " + fragment); 1771 } 1772 if (DEBUG) Log.v(TAG, "add from attach: " + fragment); 1773 mAdded.add(fragment); 1774 fragment.mAdded = true; 1775 if (fragment.mHasMenu && fragment.mMenuVisible) { 1776 mNeedMenuInvalidate = true; 1777 } 1778 } 1779 } 1780 } 1781 1782 @Override 1783 public Fragment findFragmentById(int id) { 1784 if (mAdded != null) { 1785 // First look through added fragments. 1786 for (int i=mAdded.size()-1; i>=0; i--) { 1787 Fragment f = mAdded.get(i); 1788 if (f != null && f.mFragmentId == id) { 1789 return f; 1790 } 1791 } 1792 } 1793 if (mActive != null) { 1794 // Now for any known fragment. 1795 for (int i=mActive.size()-1; i>=0; i--) { 1796 Fragment f = mActive.get(i); 1797 if (f != null && f.mFragmentId == id) { 1798 return f; 1799 } 1800 } 1801 } 1802 return null; 1803 } 1804 1805 @Override 1806 public Fragment findFragmentByTag(String tag) { 1807 if (mAdded != null && tag != null) { 1808 // First look through added fragments. 1809 for (int i=mAdded.size()-1; i>=0; i--) { 1810 Fragment f = mAdded.get(i); 1811 if (f != null && tag.equals(f.mTag)) { 1812 return f; 1813 } 1814 } 1815 } 1816 if (mActive != null && tag != null) { 1817 // Now for any known fragment. 1818 for (int i=mActive.size()-1; i>=0; i--) { 1819 Fragment f = mActive.get(i); 1820 if (f != null && tag.equals(f.mTag)) { 1821 return f; 1822 } 1823 } 1824 } 1825 return null; 1826 } 1827 1828 public Fragment findFragmentByWho(String who) { 1829 if (mActive != null && who != null) { 1830 for (int i=mActive.size()-1; i>=0; i--) { 1831 Fragment f = mActive.get(i); 1832 if (f != null && (f=f.findFragmentByWho(who)) != null) { 1833 return f; 1834 } 1835 } 1836 } 1837 return null; 1838 } 1839 1840 private void checkStateLoss() { 1841 if (mStateSaved) { 1842 throw new IllegalStateException( 1843 "Can not perform this action after onSaveInstanceState"); 1844 } 1845 if (mNoTransactionsBecause != null) { 1846 throw new IllegalStateException( 1847 "Can not perform this action inside of " + mNoTransactionsBecause); 1848 } 1849 } 1850 1851 /** 1852 * Adds an action to the queue of pending actions. 1853 * 1854 * @param action the action to add 1855 * @param allowStateLoss whether to allow loss of state information 1856 * @throws IllegalStateException if the activity has been destroyed 1857 */ 1858 public void enqueueAction(OpGenerator action, boolean allowStateLoss) { 1859 if (!allowStateLoss) { 1860 checkStateLoss(); 1861 } 1862 synchronized (this) { 1863 if (mDestroyed || mHost == null) { 1864 throw new IllegalStateException("Activity has been destroyed"); 1865 } 1866 if (mPendingActions == null) { 1867 mPendingActions = new ArrayList<>(); 1868 } 1869 mPendingActions.add(action); 1870 scheduleCommit(); 1871 } 1872 } 1873 1874 /** 1875 * Schedules the execution when one hasn't been scheduled already. This should happen 1876 * the first time {@link #enqueueAction(OpGenerator, boolean)} is called or when 1877 * a postponed transaction has been started with 1878 * {@link Fragment#startPostponedEnterTransition()} 1879 */ 1880 private void scheduleCommit() { 1881 synchronized (this) { 1882 boolean postponeReady = 1883 mPostponedTransactions != null && !mPostponedTransactions.isEmpty(); 1884 boolean pendingReady = mPendingActions != null && mPendingActions.size() == 1; 1885 if (postponeReady || pendingReady) { 1886 mHost.getHandler().removeCallbacks(mExecCommit); 1887 mHost.getHandler().post(mExecCommit); 1888 } 1889 } 1890 } 1891 1892 public int allocBackStackIndex(BackStackRecord bse) { 1893 synchronized (this) { 1894 if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { 1895 if (mBackStackIndices == null) { 1896 mBackStackIndices = new ArrayList<BackStackRecord>(); 1897 } 1898 int index = mBackStackIndices.size(); 1899 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1900 mBackStackIndices.add(bse); 1901 return index; 1902 1903 } else { 1904 int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); 1905 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1906 mBackStackIndices.set(index, bse); 1907 return index; 1908 } 1909 } 1910 } 1911 1912 public void setBackStackIndex(int index, BackStackRecord bse) { 1913 synchronized (this) { 1914 if (mBackStackIndices == null) { 1915 mBackStackIndices = new ArrayList<BackStackRecord>(); 1916 } 1917 int N = mBackStackIndices.size(); 1918 if (index < N) { 1919 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1920 mBackStackIndices.set(index, bse); 1921 } else { 1922 while (N < index) { 1923 mBackStackIndices.add(null); 1924 if (mAvailBackStackIndices == null) { 1925 mAvailBackStackIndices = new ArrayList<Integer>(); 1926 } 1927 if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); 1928 mAvailBackStackIndices.add(N); 1929 N++; 1930 } 1931 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1932 mBackStackIndices.add(bse); 1933 } 1934 } 1935 } 1936 1937 public void freeBackStackIndex(int index) { 1938 synchronized (this) { 1939 mBackStackIndices.set(index, null); 1940 if (mAvailBackStackIndices == null) { 1941 mAvailBackStackIndices = new ArrayList<Integer>(); 1942 } 1943 if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); 1944 mAvailBackStackIndices.add(index); 1945 } 1946 } 1947 1948 /** 1949 * Broken out from exec*, this prepares for gathering and executing operations. 1950 * 1951 * @param allowStateLoss true if state loss should be ignored or false if it should be 1952 * checked. 1953 */ 1954 private void ensureExecReady(boolean allowStateLoss) { 1955 if (mExecutingActions) { 1956 throw new IllegalStateException("FragmentManager is already executing transactions"); 1957 } 1958 1959 if (Looper.myLooper() != mHost.getHandler().getLooper()) { 1960 throw new IllegalStateException("Must be called from main thread of fragment host"); 1961 } 1962 1963 if (!allowStateLoss) { 1964 checkStateLoss(); 1965 } 1966 1967 if (mTmpRecords == null) { 1968 mTmpRecords = new ArrayList<>(); 1969 mTmpIsPop = new ArrayList<>(); 1970 } 1971 executePostponedTransaction(null, null); 1972 } 1973 1974 public void execSingleAction(OpGenerator action, boolean allowStateLoss) { 1975 ensureExecReady(allowStateLoss); 1976 if (action.generateOps(mTmpRecords, mTmpIsPop)) { 1977 mExecutingActions = true; 1978 try { 1979 optimizeAndExecuteOps(mTmpRecords, mTmpIsPop); 1980 } finally { 1981 cleanupExec(); 1982 } 1983 } 1984 1985 doPendingDeferredStart(); 1986 } 1987 1988 /** 1989 * Broken out of exec*, this cleans up the mExecutingActions and the temporary structures 1990 * used in executing operations. 1991 */ 1992 private void cleanupExec() { 1993 mExecutingActions = false; 1994 mTmpIsPop.clear(); 1995 mTmpRecords.clear(); 1996 } 1997 1998 /** 1999 * Only call from main thread! 2000 */ 2001 public boolean execPendingActions() { 2002 ensureExecReady(true); 2003 2004 boolean didSomething = false; 2005 while (generateOpsForPendingActions(mTmpRecords, mTmpIsPop)) { 2006 mExecutingActions = true; 2007 try { 2008 optimizeAndExecuteOps(mTmpRecords, mTmpIsPop); 2009 } finally { 2010 cleanupExec(); 2011 } 2012 didSomething = true; 2013 } 2014 2015 doPendingDeferredStart(); 2016 2017 return didSomething; 2018 } 2019 2020 /** 2021 * Complete the execution of transactions that have previously been postponed, but are 2022 * now ready. 2023 */ 2024 private void executePostponedTransaction(ArrayList<BackStackRecord> records, 2025 ArrayList<Boolean> isRecordPop) { 2026 int numPostponed = mPostponedTransactions == null ? 0 : mPostponedTransactions.size(); 2027 for (int i = 0; i < numPostponed; i++) { 2028 StartEnterTransitionListener listener = mPostponedTransactions.get(i); 2029 if (records != null && !listener.mIsBack) { 2030 int index = records.indexOf(listener.mRecord); 2031 if (index != -1 && isRecordPop.get(index)) { 2032 listener.cancelTransaction(); 2033 continue; 2034 } 2035 } 2036 if (listener.isReady() || (records != null 2037 && listener.mRecord.interactsWith(records, 0, records.size()))) { 2038 mPostponedTransactions.remove(i); 2039 i--; 2040 numPostponed--; 2041 int index; 2042 if (records != null && !listener.mIsBack 2043 && (index = records.indexOf(listener.mRecord)) != -1 2044 && isRecordPop.get(index)) { 2045 // This is popping a postponed transaction 2046 listener.cancelTransaction(); 2047 } else { 2048 listener.completeTransaction(); 2049 } 2050 } 2051 } 2052 } 2053 2054 /** 2055 * Optimizes BackStackRecord operations. This method merges operations of proximate records 2056 * that allow optimization. See {@link FragmentTransaction#setAllowOptimization(boolean)}. 2057 * <p> 2058 * For example, a transaction that adds to the back stack and then another that pops that 2059 * back stack record will be optimized. 2060 * <p> 2061 * Likewise, two transactions committed that are executed at the same time will be optimized 2062 * as well as two pop operations executed together. 2063 * 2064 * @param records The records pending execution 2065 * @param isRecordPop The direction that these records are being run. 2066 */ 2067 private void optimizeAndExecuteOps(ArrayList<BackStackRecord> records, 2068 ArrayList<Boolean> isRecordPop) { 2069 if (records == null || records.isEmpty()) { 2070 return; 2071 } 2072 2073 if (isRecordPop == null || records.size() != isRecordPop.size()) { 2074 throw new IllegalStateException("Internal error with the back stack records"); 2075 } 2076 2077 // Force start of any postponed transactions that interact with scheduled transactions: 2078 executePostponedTransaction(records, isRecordPop); 2079 2080 final int numRecords = records.size(); 2081 int startIndex = 0; 2082 for (int recordNum = 0; recordNum < numRecords; recordNum++) { 2083 final boolean canOptimize = records.get(recordNum).mAllowOptimization; 2084 if (!canOptimize) { 2085 // execute all previous transactions 2086 if (startIndex != recordNum) { 2087 executeOpsTogether(records, isRecordPop, startIndex, recordNum); 2088 } 2089 // execute all unoptimized together 2090 int optimizeEnd; 2091 for (optimizeEnd = recordNum + 1; optimizeEnd < numRecords; optimizeEnd++) { 2092 if (records.get(optimizeEnd).mAllowOptimization) { 2093 break; 2094 } 2095 } 2096 executeOpsTogether(records, isRecordPop, recordNum, optimizeEnd); 2097 startIndex = optimizeEnd; 2098 recordNum = optimizeEnd - 1; 2099 } 2100 } 2101 if (startIndex != numRecords) { 2102 executeOpsTogether(records, isRecordPop, startIndex, numRecords); 2103 } 2104 } 2105 2106 /** 2107 * Optimizes a subset of a list of BackStackRecords, all of which either allow optimization or 2108 * do not allow optimization. 2109 * @param records A list of BackStackRecords that are to be optimized 2110 * @param isRecordPop The direction that these records are being run. 2111 * @param startIndex The index of the first record in <code>records</code> to be optimized 2112 * @param endIndex One more than the final record index in <code>records</code> to optimize. 2113 */ 2114 private void executeOpsTogether(ArrayList<BackStackRecord> records, 2115 ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) { 2116 final boolean allowOptimization = records.get(startIndex).mAllowOptimization; 2117 boolean addToBackStack = false; 2118 if (mTmpAddedFragments == null) { 2119 mTmpAddedFragments = new ArrayList<>(); 2120 } else { 2121 mTmpAddedFragments.clear(); 2122 } 2123 if (mAdded != null) { 2124 mTmpAddedFragments.addAll(mAdded); 2125 } 2126 for (int recordNum = startIndex; recordNum < endIndex; recordNum++) { 2127 final BackStackRecord record = records.get(recordNum); 2128 final boolean isPop = isRecordPop.get(recordNum); 2129 if (!isPop) { 2130 record.expandReplaceOps(mTmpAddedFragments); 2131 } 2132 final int bumpAmount = isPop ? -1 : 1; 2133 record.bumpBackStackNesting(bumpAmount); 2134 addToBackStack = addToBackStack || record.mAddToBackStack; 2135 } 2136 mTmpAddedFragments.clear(); 2137 2138 if (!allowOptimization) { 2139 FragmentTransition.startTransitions(this, records, isRecordPop, startIndex, endIndex, 2140 false); 2141 } 2142 executeOps(records, isRecordPop, startIndex, endIndex); 2143 2144 int postponeIndex = endIndex; 2145 if (allowOptimization) { 2146 moveFragmentsToInvisible(); 2147 postponeIndex = postponePostponableTransactions(records, isRecordPop, 2148 startIndex, endIndex); 2149 } 2150 2151 if (postponeIndex != startIndex && allowOptimization) { 2152 // need to run something now 2153 FragmentTransition.startTransitions(this, records, isRecordPop, startIndex, 2154 postponeIndex, true); 2155 moveToState(mCurState, true); 2156 } 2157 2158 for (int recordNum = startIndex; recordNum < endIndex; recordNum++) { 2159 final BackStackRecord record = records.get(recordNum); 2160 final boolean isPop = isRecordPop.get(recordNum); 2161 if (isPop && record.mIndex >= 0) { 2162 freeBackStackIndex(record.mIndex); 2163 record.mIndex = -1; 2164 } 2165 } 2166 if (addToBackStack) { 2167 reportBackStackChanged(); 2168 } 2169 } 2170 2171 /** 2172 * Examine all transactions and determine which ones are marked as postponed. Those will 2173 * have their operations rolled back and moved to the end of the record list (up to endIndex). 2174 * It will also add the postponed transaction to the queue. 2175 * 2176 * @param records A list of BackStackRecords that should be checked. 2177 * @param isRecordPop The direction that these records are being run. 2178 * @param startIndex The index of the first record in <code>records</code> to be checked 2179 * @param endIndex One more than the final record index in <code>records</code> to be checked. 2180 * @return The index of the first postponed transaction or endIndex if no transaction was 2181 * postponed. 2182 */ 2183 private int postponePostponableTransactions(ArrayList<BackStackRecord> records, 2184 ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) { 2185 int postponeIndex = endIndex; 2186 for (int i = endIndex - 1; i >= startIndex; i--) { 2187 final BackStackRecord record = records.get(i); 2188 final boolean isPop = isRecordPop.get(i); 2189 boolean isPostponed = record.isPostponed() 2190 && !record.interactsWith(records, i + 1, endIndex); 2191 if (isPostponed) { 2192 if (mPostponedTransactions == null) { 2193 mPostponedTransactions = new ArrayList<>(); 2194 } 2195 StartEnterTransitionListener listener = 2196 new StartEnterTransitionListener(record, isPop); 2197 mPostponedTransactions.add(listener); 2198 record.setOnStartPostponedListener(listener); 2199 2200 // roll back the transaction 2201 if (isPop) { 2202 record.executeOps(); 2203 } else { 2204 record.executePopOps(); 2205 } 2206 2207 // move to the end 2208 postponeIndex--; 2209 if (i != postponeIndex) { 2210 records.remove(i); 2211 records.add(postponeIndex, record); 2212 } 2213 2214 // different views may be visible now 2215 moveFragmentsToInvisible(); 2216 } 2217 } 2218 return postponeIndex; 2219 } 2220 2221 /** 2222 * When a postponed transaction is ready to be started, this completes the transaction, 2223 * removing, hiding, or showing views as well as starting the animations and transitions. 2224 * <p> 2225 * {@code runtransitions} is set to false when the transaction postponement was interrupted 2226 * abnormally -- normally by a new transaction being started that affects the postponed 2227 * transaction. 2228 * 2229 * @param record The transaction to run 2230 * @param isPop true if record is popping or false if it is adding 2231 * @param runTransitions true if the fragment transition should be run or false otherwise. 2232 * @param moveToState true if the state should be changed after executing the operations. 2233 * This is false when the transaction is canceled when a postponed 2234 * transaction is popped. 2235 */ 2236 private void completeExecute(BackStackRecord record, boolean isPop, boolean runTransitions, 2237 boolean moveToState) { 2238 ArrayList<BackStackRecord> records = new ArrayList<>(1); 2239 ArrayList<Boolean> isRecordPop = new ArrayList<>(1); 2240 records.add(record); 2241 isRecordPop.add(isPop); 2242 executeOps(records, isRecordPop, 0, 1); 2243 if (runTransitions) { 2244 FragmentTransition.startTransitions(this, records, isRecordPop, 0, 1, true); 2245 } 2246 if (moveToState) { 2247 moveToState(mCurState, true); 2248 } else if (mActive != null) { 2249 final int numActive = mActive.size(); 2250 for (int i = 0; i < numActive; i++) { 2251 // Allow added fragments to be removed during the pop since we aren't going 2252 // to move them to the final state with moveToState(mCurState). 2253 Fragment fragment = mActive.get(i); 2254 if (fragment.mView != null && fragment.mIsNewlyAdded 2255 && record.interactsWith(fragment.mContainerId)) { 2256 fragment.mIsNewlyAdded = false; 2257 } 2258 } 2259 } 2260 } 2261 2262 /** 2263 * Find a fragment within the fragment's container whose View should be below the passed 2264 * fragment. {@code null} is returned when the fragment has no View or if there should be 2265 * no fragment with a View below the given fragment. 2266 * 2267 * As an example, if mAdded has two Fragments with Views sharing the same container: 2268 * FragmentA 2269 * FragmentB 2270 * 2271 * Then, when processing FragmentB, FragmentA will be returned. If, however, FragmentA 2272 * had no View, null would be returned. 2273 * 2274 * @param f The fragment that may be on top of another fragment. 2275 * @return The fragment with a View under f, if one exists or null if f has no View or 2276 * there are no fragments with Views in the same container. 2277 */ 2278 private Fragment findFragmentUnder(Fragment f) { 2279 final ViewGroup container = f.mContainer; 2280 final View view = f.mView; 2281 2282 if (container == null || view == null) { 2283 return null; 2284 } 2285 2286 final int fragmentIndex = mAdded.indexOf(f); 2287 for (int i = fragmentIndex - 1; i >= 0; i--) { 2288 Fragment underFragment = mAdded.get(i); 2289 if (underFragment.mContainer == container && underFragment.mView != null) { 2290 // Found the fragment under this one 2291 return underFragment; 2292 } 2293 } 2294 return null; 2295 } 2296 2297 /** 2298 * Run the operations in the BackStackRecords, either to push or pop. 2299 * 2300 * @param records The list of records whose operations should be run. 2301 * @param isRecordPop The direction that these records are being run. 2302 * @param startIndex The index of the first entry in records to run. 2303 * @param endIndex One past the index of the final entry in records to run. 2304 */ 2305 private static void executeOps(ArrayList<BackStackRecord> records, 2306 ArrayList<Boolean> isRecordPop, int startIndex, int endIndex) { 2307 for (int i = startIndex; i < endIndex; i++) { 2308 final BackStackRecord record = records.get(i); 2309 final boolean isPop = isRecordPop.get(i); 2310 if (isPop) { 2311 record.executePopOps(); 2312 } else { 2313 record.executeOps(); 2314 } 2315 } 2316 } 2317 2318 /** 2319 * Ensure that fragments that are added are moved to at least the CREATED state. 2320 * Any newly-added Views are made INVISIBLE so that the Transaction can be postponed 2321 * with {@link Fragment#postponeEnterTransition()}. 2322 */ 2323 private void moveFragmentsToInvisible() { 2324 if (mCurState < Fragment.CREATED) { 2325 return; 2326 } 2327 // We want to leave the fragment in the started state 2328 final int state = Math.min(mCurState, Fragment.STARTED); 2329 final int numAdded = mAdded == null ? 0 : mAdded.size(); 2330 for (int i = 0; i < numAdded; i++) { 2331 Fragment fragment = mAdded.get(i); 2332 if (fragment.mState < state) { 2333 moveToState(fragment, state, fragment.getNextAnim(), fragment.getNextTransition(), 2334 false); 2335 if (fragment.mView != null && !fragment.mHidden && fragment.mIsNewlyAdded) { 2336 fragment.mView.setVisibility(View.INVISIBLE); 2337 } 2338 } 2339 } 2340 } 2341 2342 /** 2343 * Starts all postponed transactions regardless of whether they are ready or not. 2344 */ 2345 private void forcePostponedTransactions() { 2346 if (mPostponedTransactions != null) { 2347 while (!mPostponedTransactions.isEmpty()) { 2348 mPostponedTransactions.remove(0).completeTransaction(); 2349 } 2350 } 2351 } 2352 2353 /** 2354 * Ends the animations of fragments so that they immediately reach the end state. 2355 * This is used prior to saving the state so that the correct state is saved. 2356 */ 2357 private void endAnimatingAwayFragments() { 2358 final int numFragments = mActive == null ? 0 : mActive.size(); 2359 for (int i = 0; i < numFragments; i++) { 2360 Fragment fragment = mActive.get(i); 2361 if (fragment != null && fragment.getAnimatingAway() != null) { 2362 // Give up waiting for the animation and just end it. 2363 final int stateAfterAnimating = fragment.getStateAfterAnimating(); 2364 final View animatingAway = fragment.getAnimatingAway(); 2365 fragment.setAnimatingAway(null); 2366 animatingAway.clearAnimation(); 2367 moveToState(fragment, stateAfterAnimating, 0, 0, false); 2368 } 2369 } 2370 } 2371 2372 /** 2373 * Adds all records in the pending actions to records and whether they are add or pop 2374 * operations to isPop. After executing, the pending actions will be empty. 2375 * 2376 * @param records All pending actions will generate BackStackRecords added to this. 2377 * This contains the transactions, in order, to execute. 2378 * @param isPop All pending actions will generate booleans to add to this. This contains 2379 * an entry for each entry in records to indicate whether or not it is a 2380 * pop action. 2381 */ 2382 private boolean generateOpsForPendingActions(ArrayList<BackStackRecord> records, 2383 ArrayList<Boolean> isPop) { 2384 int numActions; 2385 synchronized (this) { 2386 if (mPendingActions == null || mPendingActions.size() == 0) { 2387 return false; 2388 } 2389 2390 numActions = mPendingActions.size(); 2391 for (int i = 0; i < numActions; i++) { 2392 mPendingActions.get(i).generateOps(records, isPop); 2393 } 2394 mPendingActions.clear(); 2395 mHost.getHandler().removeCallbacks(mExecCommit); 2396 } 2397 return numActions > 0; 2398 } 2399 2400 void doPendingDeferredStart() { 2401 if (mHavePendingDeferredStart) { 2402 boolean loadersRunning = false; 2403 for (int i = 0; i < mActive.size(); i++) { 2404 Fragment f = mActive.get(i); 2405 if (f != null && f.mLoaderManager != null) { 2406 loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 2407 } 2408 } 2409 if (!loadersRunning) { 2410 mHavePendingDeferredStart = false; 2411 startPendingDeferredFragments(); 2412 } 2413 } 2414 } 2415 2416 void reportBackStackChanged() { 2417 if (mBackStackChangeListeners != null) { 2418 for (int i=0; i<mBackStackChangeListeners.size(); i++) { 2419 mBackStackChangeListeners.get(i).onBackStackChanged(); 2420 } 2421 } 2422 } 2423 2424 void addBackStackState(BackStackRecord state) { 2425 if (mBackStack == null) { 2426 mBackStack = new ArrayList<BackStackRecord>(); 2427 } 2428 mBackStack.add(state); 2429 reportBackStackChanged(); 2430 } 2431 2432 @SuppressWarnings("unused") 2433 boolean popBackStackState(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop, 2434 String name, int id, int flags) { 2435 if (mBackStack == null) { 2436 return false; 2437 } 2438 if (name == null && id < 0 && (flags & POP_BACK_STACK_INCLUSIVE) == 0) { 2439 int last = mBackStack.size() - 1; 2440 if (last < 0) { 2441 return false; 2442 } 2443 records.add(mBackStack.remove(last)); 2444 isRecordPop.add(true); 2445 } else { 2446 int index = -1; 2447 if (name != null || id >= 0) { 2448 // If a name or ID is specified, look for that place in 2449 // the stack. 2450 index = mBackStack.size()-1; 2451 while (index >= 0) { 2452 BackStackRecord bss = mBackStack.get(index); 2453 if (name != null && name.equals(bss.getName())) { 2454 break; 2455 } 2456 if (id >= 0 && id == bss.mIndex) { 2457 break; 2458 } 2459 index--; 2460 } 2461 if (index < 0) { 2462 return false; 2463 } 2464 if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) { 2465 index--; 2466 // Consume all following entries that match. 2467 while (index >= 0) { 2468 BackStackRecord bss = mBackStack.get(index); 2469 if ((name != null && name.equals(bss.getName())) 2470 || (id >= 0 && id == bss.mIndex)) { 2471 index--; 2472 continue; 2473 } 2474 break; 2475 } 2476 } 2477 } 2478 if (index == mBackStack.size()-1) { 2479 return false; 2480 } 2481 for (int i = mBackStack.size() - 1; i > index; i--) { 2482 records.add(mBackStack.remove(i)); 2483 isRecordPop.add(true); 2484 } 2485 } 2486 return true; 2487 } 2488 2489 FragmentManagerNonConfig retainNonConfig() { 2490 ArrayList<Fragment> fragments = null; 2491 ArrayList<FragmentManagerNonConfig> childFragments = null; 2492 if (mActive != null) { 2493 for (int i=0; i<mActive.size(); i++) { 2494 Fragment f = mActive.get(i); 2495 if (f != null) { 2496 if (f.mRetainInstance) { 2497 if (fragments == null) { 2498 fragments = new ArrayList<Fragment>(); 2499 } 2500 fragments.add(f); 2501 f.mRetaining = true; 2502 f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1; 2503 if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f); 2504 } 2505 boolean addedChild = false; 2506 if (f.mChildFragmentManager != null) { 2507 FragmentManagerNonConfig child = f.mChildFragmentManager.retainNonConfig(); 2508 if (child != null) { 2509 if (childFragments == null) { 2510 childFragments = new ArrayList<FragmentManagerNonConfig>(); 2511 for (int j = 0; j < i; j++) { 2512 childFragments.add(null); 2513 } 2514 } 2515 childFragments.add(child); 2516 addedChild = true; 2517 } 2518 } 2519 if (childFragments != null && !addedChild) { 2520 childFragments.add(null); 2521 } 2522 } 2523 } 2524 } 2525 if (fragments == null && childFragments == null) { 2526 return null; 2527 } 2528 return new FragmentManagerNonConfig(fragments, childFragments); 2529 } 2530 2531 void saveFragmentViewState(Fragment f) { 2532 if (f.mInnerView == null) { 2533 return; 2534 } 2535 if (mStateArray == null) { 2536 mStateArray = new SparseArray<Parcelable>(); 2537 } else { 2538 mStateArray.clear(); 2539 } 2540 f.mInnerView.saveHierarchyState(mStateArray); 2541 if (mStateArray.size() > 0) { 2542 f.mSavedViewState = mStateArray; 2543 mStateArray = null; 2544 } 2545 } 2546 2547 Bundle saveFragmentBasicState(Fragment f) { 2548 Bundle result = null; 2549 2550 if (mStateBundle == null) { 2551 mStateBundle = new Bundle(); 2552 } 2553 f.performSaveInstanceState(mStateBundle); 2554 dispatchOnFragmentSaveInstanceState(f, mStateBundle, false); 2555 if (!mStateBundle.isEmpty()) { 2556 result = mStateBundle; 2557 mStateBundle = null; 2558 } 2559 2560 if (f.mView != null) { 2561 saveFragmentViewState(f); 2562 } 2563 if (f.mSavedViewState != null) { 2564 if (result == null) { 2565 result = new Bundle(); 2566 } 2567 result.putSparseParcelableArray( 2568 FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); 2569 } 2570 if (!f.mUserVisibleHint) { 2571 if (result == null) { 2572 result = new Bundle(); 2573 } 2574 // Only add this if it's not the default value 2575 result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); 2576 } 2577 2578 return result; 2579 } 2580 2581 Parcelable saveAllState() { 2582 // Make sure all pending operations have now been executed to get 2583 // our state update-to-date. 2584 forcePostponedTransactions(); 2585 endAnimatingAwayFragments(); 2586 execPendingActions(); 2587 2588 if (HONEYCOMB) { 2589 // As of Honeycomb, we save state after pausing. Prior to that 2590 // it is before pausing. With fragments this is an issue, since 2591 // there are many things you may do after pausing but before 2592 // stopping that change the fragment state. For those older 2593 // devices, we will not at this point say that we have saved 2594 // the state, so we will allow them to continue doing fragment 2595 // transactions. This retains the same semantics as Honeycomb, 2596 // though you do have the risk of losing the very most recent state 2597 // if the process is killed... we'll live with that. 2598 mStateSaved = true; 2599 } 2600 2601 if (mActive == null || mActive.size() <= 0) { 2602 return null; 2603 } 2604 2605 // First collect all active fragments. 2606 int N = mActive.size(); 2607 FragmentState[] active = new FragmentState[N]; 2608 boolean haveFragments = false; 2609 for (int i=0; i<N; i++) { 2610 Fragment f = mActive.get(i); 2611 if (f != null) { 2612 if (f.mIndex < 0) { 2613 throwException(new IllegalStateException( 2614 "Failure saving state: active " + f 2615 + " has cleared index: " + f.mIndex)); 2616 } 2617 2618 haveFragments = true; 2619 2620 FragmentState fs = new FragmentState(f); 2621 active[i] = fs; 2622 2623 if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) { 2624 fs.mSavedFragmentState = saveFragmentBasicState(f); 2625 2626 if (f.mTarget != null) { 2627 if (f.mTarget.mIndex < 0) { 2628 throwException(new IllegalStateException( 2629 "Failure saving state: " + f 2630 + " has target not in fragment manager: " + f.mTarget)); 2631 } 2632 if (fs.mSavedFragmentState == null) { 2633 fs.mSavedFragmentState = new Bundle(); 2634 } 2635 putFragment(fs.mSavedFragmentState, 2636 FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget); 2637 if (f.mTargetRequestCode != 0) { 2638 fs.mSavedFragmentState.putInt( 2639 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 2640 f.mTargetRequestCode); 2641 } 2642 } 2643 2644 } else { 2645 fs.mSavedFragmentState = f.mSavedFragmentState; 2646 } 2647 2648 if (DEBUG) Log.v(TAG, "Saved state of " + f + ": " 2649 + fs.mSavedFragmentState); 2650 } 2651 } 2652 2653 if (!haveFragments) { 2654 if (DEBUG) Log.v(TAG, "saveAllState: no fragments!"); 2655 return null; 2656 } 2657 2658 int[] added = null; 2659 BackStackState[] backStack = null; 2660 2661 // Build list of currently added fragments. 2662 if (mAdded != null) { 2663 N = mAdded.size(); 2664 if (N > 0) { 2665 added = new int[N]; 2666 for (int i=0; i<N; i++) { 2667 added[i] = mAdded.get(i).mIndex; 2668 if (added[i] < 0) { 2669 throwException(new IllegalStateException( 2670 "Failure saving state: active " + mAdded.get(i) 2671 + " has cleared index: " + added[i])); 2672 } 2673 if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i 2674 + ": " + mAdded.get(i)); 2675 } 2676 } 2677 } 2678 2679 // Now save back stack. 2680 if (mBackStack != null) { 2681 N = mBackStack.size(); 2682 if (N > 0) { 2683 backStack = new BackStackState[N]; 2684 for (int i=0; i<N; i++) { 2685 backStack[i] = new BackStackState(mBackStack.get(i)); 2686 if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i 2687 + ": " + mBackStack.get(i)); 2688 } 2689 } 2690 } 2691 2692 FragmentManagerState fms = new FragmentManagerState(); 2693 fms.mActive = active; 2694 fms.mAdded = added; 2695 fms.mBackStack = backStack; 2696 return fms; 2697 } 2698 2699 void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) { 2700 // If there is no saved state at all, then there can not be 2701 // any nonConfig fragments either, so that is that. 2702 if (state == null) return; 2703 FragmentManagerState fms = (FragmentManagerState)state; 2704 if (fms.mActive == null) return; 2705 2706 List<FragmentManagerNonConfig> childNonConfigs = null; 2707 2708 // First re-attach any non-config instances we are retaining back 2709 // to their saved state, so we don't try to instantiate them again. 2710 if (nonConfig != null) { 2711 List<Fragment> nonConfigFragments = nonConfig.getFragments(); 2712 childNonConfigs = nonConfig.getChildNonConfigs(); 2713 final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; 2714 for (int i = 0; i < count; i++) { 2715 Fragment f = nonConfigFragments.get(i); 2716 if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f); 2717 FragmentState fs = fms.mActive[f.mIndex]; 2718 fs.mInstance = f; 2719 f.mSavedViewState = null; 2720 f.mBackStackNesting = 0; 2721 f.mInLayout = false; 2722 f.mAdded = false; 2723 f.mTarget = null; 2724 if (fs.mSavedFragmentState != null) { 2725 fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); 2726 f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( 2727 FragmentManagerImpl.VIEW_STATE_TAG); 2728 f.mSavedFragmentState = fs.mSavedFragmentState; 2729 } 2730 } 2731 } 2732 2733 // Build the full list of active fragments, instantiating them from 2734 // their saved state. 2735 mActive = new ArrayList<>(fms.mActive.length); 2736 if (mAvailIndices != null) { 2737 mAvailIndices.clear(); 2738 } 2739 for (int i=0; i<fms.mActive.length; i++) { 2740 FragmentState fs = fms.mActive[i]; 2741 if (fs != null) { 2742 FragmentManagerNonConfig childNonConfig = null; 2743 if (childNonConfigs != null && i < childNonConfigs.size()) { 2744 childNonConfig = childNonConfigs.get(i); 2745 } 2746 Fragment f = fs.instantiate(mHost, mParent, childNonConfig); 2747 if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f); 2748 mActive.add(f); 2749 // Now that the fragment is instantiated (or came from being 2750 // retained above), clear mInstance in case we end up re-restoring 2751 // from this FragmentState again. 2752 fs.mInstance = null; 2753 } else { 2754 mActive.add(null); 2755 if (mAvailIndices == null) { 2756 mAvailIndices = new ArrayList<Integer>(); 2757 } 2758 if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i); 2759 mAvailIndices.add(i); 2760 } 2761 } 2762 2763 // Update the target of all retained fragments. 2764 if (nonConfig != null) { 2765 List<Fragment> nonConfigFragments = nonConfig.getFragments(); 2766 final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; 2767 for (int i = 0; i < count; i++) { 2768 Fragment f = nonConfigFragments.get(i); 2769 if (f.mTargetIndex >= 0) { 2770 if (f.mTargetIndex < mActive.size()) { 2771 f.mTarget = mActive.get(f.mTargetIndex); 2772 } else { 2773 Log.w(TAG, "Re-attaching retained fragment " + f 2774 + " target no longer exists: " + f.mTargetIndex); 2775 f.mTarget = null; 2776 } 2777 } 2778 } 2779 } 2780 2781 // Build the list of currently added fragments. 2782 if (fms.mAdded != null) { 2783 mAdded = new ArrayList<Fragment>(fms.mAdded.length); 2784 for (int i=0; i<fms.mAdded.length; i++) { 2785 Fragment f = mActive.get(fms.mAdded[i]); 2786 if (f == null) { 2787 throwException(new IllegalStateException( 2788 "No instantiated fragment for index #" + fms.mAdded[i])); 2789 } 2790 f.mAdded = true; 2791 if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f); 2792 if (mAdded.contains(f)) { 2793 throw new IllegalStateException("Already added!"); 2794 } 2795 mAdded.add(f); 2796 } 2797 } else { 2798 mAdded = null; 2799 } 2800 2801 // Build the back stack. 2802 if (fms.mBackStack != null) { 2803 mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length); 2804 for (int i=0; i<fms.mBackStack.length; i++) { 2805 BackStackRecord bse = fms.mBackStack[i].instantiate(this); 2806 if (DEBUG) { 2807 Log.v(TAG, "restoreAllState: back stack #" + i 2808 + " (index " + bse.mIndex + "): " + bse); 2809 LogWriter logw = new LogWriter(TAG); 2810 PrintWriter pw = new PrintWriter(logw); 2811 bse.dump(" ", pw, false); 2812 } 2813 mBackStack.add(bse); 2814 if (bse.mIndex >= 0) { 2815 setBackStackIndex(bse.mIndex, bse); 2816 } 2817 } 2818 } else { 2819 mBackStack = null; 2820 } 2821 } 2822 2823 public void attachController(FragmentHostCallback host, 2824 FragmentContainer container, Fragment parent) { 2825 if (mHost != null) throw new IllegalStateException("Already attached"); 2826 mHost = host; 2827 mContainer = container; 2828 mParent = parent; 2829 } 2830 2831 public void noteStateNotSaved() { 2832 mStateSaved = false; 2833 } 2834 2835 public void dispatchCreate() { 2836 mStateSaved = false; 2837 moveToState(Fragment.CREATED, false); 2838 } 2839 2840 public void dispatchActivityCreated() { 2841 mStateSaved = false; 2842 moveToState(Fragment.ACTIVITY_CREATED, false); 2843 } 2844 2845 public void dispatchStart() { 2846 mStateSaved = false; 2847 moveToState(Fragment.STARTED, false); 2848 } 2849 2850 public void dispatchResume() { 2851 mStateSaved = false; 2852 moveToState(Fragment.RESUMED, false); 2853 } 2854 2855 public void dispatchPause() { 2856 moveToState(Fragment.STARTED, false); 2857 } 2858 2859 public void dispatchStop() { 2860 // See saveAllState() for the explanation of this. We do this for 2861 // all platform versions, to keep our behavior more consistent between 2862 // them. 2863 mStateSaved = true; 2864 2865 moveToState(Fragment.STOPPED, false); 2866 } 2867 2868 public void dispatchReallyStop() { 2869 moveToState(Fragment.ACTIVITY_CREATED, false); 2870 } 2871 2872 public void dispatchDestroyView() { 2873 moveToState(Fragment.CREATED, false); 2874 } 2875 2876 public void dispatchDestroy() { 2877 mDestroyed = true; 2878 execPendingActions(); 2879 moveToState(Fragment.INITIALIZING, false); 2880 mHost = null; 2881 mContainer = null; 2882 mParent = null; 2883 } 2884 2885 public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) { 2886 if (mAdded == null) { 2887 return; 2888 } 2889 for (int i = mAdded.size() - 1; i >= 0; --i) { 2890 final android.support.v4.app.Fragment f = mAdded.get(i); 2891 if (f != null) { 2892 f.performMultiWindowModeChanged(isInMultiWindowMode); 2893 } 2894 } 2895 } 2896 2897 public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 2898 if (mAdded == null) { 2899 return; 2900 } 2901 for (int i = mAdded.size() - 1; i >= 0; --i) { 2902 final android.support.v4.app.Fragment f = mAdded.get(i); 2903 if (f != null) { 2904 f.performPictureInPictureModeChanged(isInPictureInPictureMode); 2905 } 2906 } 2907 } 2908 2909 public void dispatchConfigurationChanged(Configuration newConfig) { 2910 if (mAdded != null) { 2911 for (int i=0; i<mAdded.size(); i++) { 2912 Fragment f = mAdded.get(i); 2913 if (f != null) { 2914 f.performConfigurationChanged(newConfig); 2915 } 2916 } 2917 } 2918 } 2919 2920 public void dispatchLowMemory() { 2921 if (mAdded != null) { 2922 for (int i=0; i<mAdded.size(); i++) { 2923 Fragment f = mAdded.get(i); 2924 if (f != null) { 2925 f.performLowMemory(); 2926 } 2927 } 2928 } 2929 } 2930 2931 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { 2932 boolean show = false; 2933 ArrayList<Fragment> newMenus = null; 2934 if (mAdded != null) { 2935 for (int i=0; i<mAdded.size(); i++) { 2936 Fragment f = mAdded.get(i); 2937 if (f != null) { 2938 if (f.performCreateOptionsMenu(menu, inflater)) { 2939 show = true; 2940 if (newMenus == null) { 2941 newMenus = new ArrayList<Fragment>(); 2942 } 2943 newMenus.add(f); 2944 } 2945 } 2946 } 2947 } 2948 2949 if (mCreatedMenus != null) { 2950 for (int i=0; i<mCreatedMenus.size(); i++) { 2951 Fragment f = mCreatedMenus.get(i); 2952 if (newMenus == null || !newMenus.contains(f)) { 2953 f.onDestroyOptionsMenu(); 2954 } 2955 } 2956 } 2957 2958 mCreatedMenus = newMenus; 2959 2960 return show; 2961 } 2962 2963 public boolean dispatchPrepareOptionsMenu(Menu menu) { 2964 boolean show = false; 2965 if (mAdded != null) { 2966 for (int i=0; i<mAdded.size(); i++) { 2967 Fragment f = mAdded.get(i); 2968 if (f != null) { 2969 if (f.performPrepareOptionsMenu(menu)) { 2970 show = true; 2971 } 2972 } 2973 } 2974 } 2975 return show; 2976 } 2977 2978 public boolean dispatchOptionsItemSelected(MenuItem item) { 2979 if (mAdded != null) { 2980 for (int i=0; i<mAdded.size(); i++) { 2981 Fragment f = mAdded.get(i); 2982 if (f != null) { 2983 if (f.performOptionsItemSelected(item)) { 2984 return true; 2985 } 2986 } 2987 } 2988 } 2989 return false; 2990 } 2991 2992 public boolean dispatchContextItemSelected(MenuItem item) { 2993 if (mAdded != null) { 2994 for (int i=0; i<mAdded.size(); i++) { 2995 Fragment f = mAdded.get(i); 2996 if (f != null) { 2997 if (f.performContextItemSelected(item)) { 2998 return true; 2999 } 3000 } 3001 } 3002 } 3003 return false; 3004 } 3005 3006 public void dispatchOptionsMenuClosed(Menu menu) { 3007 if (mAdded != null) { 3008 for (int i=0; i<mAdded.size(); i++) { 3009 Fragment f = mAdded.get(i); 3010 if (f != null) { 3011 f.performOptionsMenuClosed(menu); 3012 } 3013 } 3014 } 3015 } 3016 3017 public void registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb, 3018 boolean recursive) { 3019 if (mLifecycleCallbacks == null) { 3020 mLifecycleCallbacks = new CopyOnWriteArrayList<>(); 3021 } 3022 mLifecycleCallbacks.add(new Pair(cb, recursive)); 3023 } 3024 3025 public void unregisterFragmentLifecycleCallbacks(FragmentLifecycleCallbacks cb) { 3026 if (mLifecycleCallbacks == null) { 3027 return; 3028 } 3029 3030 synchronized (mLifecycleCallbacks) { 3031 for (int i = 0, N = mLifecycleCallbacks.size(); i < N; i++) { 3032 if (mLifecycleCallbacks.get(i).first == cb) { 3033 mLifecycleCallbacks.remove(i); 3034 break; 3035 } 3036 } 3037 } 3038 } 3039 3040 void dispatchOnFragmentPreAttached(Fragment f, Context context, boolean onlyRecursive) { 3041 if (mParent != null) { 3042 FragmentManager parentManager = mParent.getFragmentManager(); 3043 if (parentManager instanceof FragmentManagerImpl) { 3044 ((FragmentManagerImpl) parentManager) 3045 .dispatchOnFragmentPreAttached(f, context, true); 3046 } 3047 } 3048 if (mLifecycleCallbacks == null) { 3049 return; 3050 } 3051 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3052 if (!onlyRecursive || p.second) { 3053 p.first.onFragmentPreAttached(this, f, context); 3054 } 3055 } 3056 } 3057 3058 void dispatchOnFragmentAttached(Fragment f, Context context, boolean onlyRecursive) { 3059 if (mParent != null) { 3060 FragmentManager parentManager = mParent.getFragmentManager(); 3061 if (parentManager instanceof FragmentManagerImpl) { 3062 ((FragmentManagerImpl) parentManager) 3063 .dispatchOnFragmentAttached(f, context, true); 3064 } 3065 } 3066 if (mLifecycleCallbacks == null) { 3067 return; 3068 } 3069 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3070 if (!onlyRecursive || p.second) { 3071 p.first.onFragmentAttached(this, f, context); 3072 } 3073 } 3074 } 3075 3076 void dispatchOnFragmentCreated(Fragment f, Bundle savedInstanceState, boolean onlyRecursive) { 3077 if (mParent != null) { 3078 FragmentManager parentManager = mParent.getFragmentManager(); 3079 if (parentManager instanceof FragmentManagerImpl) { 3080 ((FragmentManagerImpl) parentManager) 3081 .dispatchOnFragmentCreated(f, savedInstanceState, true); 3082 } 3083 } 3084 if (mLifecycleCallbacks == null) { 3085 return; 3086 } 3087 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3088 if (!onlyRecursive || p.second) { 3089 p.first.onFragmentCreated(this, f, savedInstanceState); 3090 } 3091 } 3092 } 3093 3094 void dispatchOnFragmentActivityCreated(Fragment f, Bundle savedInstanceState, 3095 boolean onlyRecursive) { 3096 if (mParent != null) { 3097 FragmentManager parentManager = mParent.getFragmentManager(); 3098 if (parentManager instanceof FragmentManagerImpl) { 3099 ((FragmentManagerImpl) parentManager) 3100 .dispatchOnFragmentActivityCreated(f, savedInstanceState, true); 3101 } 3102 } 3103 if (mLifecycleCallbacks == null) { 3104 return; 3105 } 3106 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3107 if (!onlyRecursive || p.second) { 3108 p.first.onFragmentActivityCreated(this, f, savedInstanceState); 3109 } 3110 } 3111 } 3112 3113 void dispatchOnFragmentViewCreated(Fragment f, View v, Bundle savedInstanceState, 3114 boolean onlyRecursive) { 3115 if (mParent != null) { 3116 FragmentManager parentManager = mParent.getFragmentManager(); 3117 if (parentManager instanceof FragmentManagerImpl) { 3118 ((FragmentManagerImpl) parentManager) 3119 .dispatchOnFragmentViewCreated(f, v, savedInstanceState, true); 3120 } 3121 } 3122 if (mLifecycleCallbacks == null) { 3123 return; 3124 } 3125 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3126 if (!onlyRecursive || p.second) { 3127 p.first.onFragmentViewCreated(this, f, v, savedInstanceState); 3128 } 3129 } 3130 } 3131 3132 void dispatchOnFragmentStarted(Fragment f, boolean onlyRecursive) { 3133 if (mParent != null) { 3134 FragmentManager parentManager = mParent.getFragmentManager(); 3135 if (parentManager instanceof FragmentManagerImpl) { 3136 ((FragmentManagerImpl) parentManager) 3137 .dispatchOnFragmentStarted(f, true); 3138 } 3139 } 3140 if (mLifecycleCallbacks == null) { 3141 return; 3142 } 3143 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3144 if (!onlyRecursive || p.second) { 3145 p.first.onFragmentStarted(this, f); 3146 } 3147 } 3148 } 3149 3150 void dispatchOnFragmentResumed(Fragment f, boolean onlyRecursive) { 3151 if (mParent != null) { 3152 FragmentManager parentManager = mParent.getFragmentManager(); 3153 if (parentManager instanceof FragmentManagerImpl) { 3154 ((FragmentManagerImpl) parentManager) 3155 .dispatchOnFragmentResumed(f, true); 3156 } 3157 } 3158 if (mLifecycleCallbacks == null) { 3159 return; 3160 } 3161 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3162 if (!onlyRecursive || p.second) { 3163 p.first.onFragmentResumed(this, f); 3164 } 3165 } 3166 } 3167 3168 void dispatchOnFragmentPaused(Fragment f, boolean onlyRecursive) { 3169 if (mParent != null) { 3170 FragmentManager parentManager = mParent.getFragmentManager(); 3171 if (parentManager instanceof FragmentManagerImpl) { 3172 ((FragmentManagerImpl) parentManager) 3173 .dispatchOnFragmentPaused(f, true); 3174 } 3175 } 3176 if (mLifecycleCallbacks == null) { 3177 return; 3178 } 3179 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3180 if (!onlyRecursive || p.second) { 3181 p.first.onFragmentPaused(this, f); 3182 } 3183 } 3184 } 3185 3186 void dispatchOnFragmentStopped(Fragment f, boolean onlyRecursive) { 3187 if (mParent != null) { 3188 FragmentManager parentManager = mParent.getFragmentManager(); 3189 if (parentManager instanceof FragmentManagerImpl) { 3190 ((FragmentManagerImpl) parentManager) 3191 .dispatchOnFragmentStopped(f, true); 3192 } 3193 } 3194 if (mLifecycleCallbacks == null) { 3195 return; 3196 } 3197 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3198 if (!onlyRecursive || p.second) { 3199 p.first.onFragmentStopped(this, f); 3200 } 3201 } 3202 } 3203 3204 void dispatchOnFragmentSaveInstanceState(Fragment f, Bundle outState, boolean onlyRecursive) { 3205 if (mParent != null) { 3206 FragmentManager parentManager = mParent.getFragmentManager(); 3207 if (parentManager instanceof FragmentManagerImpl) { 3208 ((FragmentManagerImpl) parentManager) 3209 .dispatchOnFragmentSaveInstanceState(f, outState, true); 3210 } 3211 } 3212 if (mLifecycleCallbacks == null) { 3213 return; 3214 } 3215 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3216 if (!onlyRecursive || p.second) { 3217 p.first.onFragmentSaveInstanceState(this, f, outState); 3218 } 3219 } 3220 } 3221 3222 void dispatchOnFragmentViewDestroyed(Fragment f, boolean onlyRecursive) { 3223 if (mParent != null) { 3224 FragmentManager parentManager = mParent.getFragmentManager(); 3225 if (parentManager instanceof FragmentManagerImpl) { 3226 ((FragmentManagerImpl) parentManager) 3227 .dispatchOnFragmentViewDestroyed(f, true); 3228 } 3229 } 3230 if (mLifecycleCallbacks == null) { 3231 return; 3232 } 3233 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3234 if (!onlyRecursive || p.second) { 3235 p.first.onFragmentViewDestroyed(this, f); 3236 } 3237 } 3238 } 3239 3240 void dispatchOnFragmentDestroyed(Fragment f, boolean onlyRecursive) { 3241 if (mParent != null) { 3242 FragmentManager parentManager = mParent.getFragmentManager(); 3243 if (parentManager instanceof FragmentManagerImpl) { 3244 ((FragmentManagerImpl) parentManager) 3245 .dispatchOnFragmentDestroyed(f, true); 3246 } 3247 } 3248 if (mLifecycleCallbacks == null) { 3249 return; 3250 } 3251 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3252 if (!onlyRecursive || p.second) { 3253 p.first.onFragmentDestroyed(this, f); 3254 } 3255 } 3256 } 3257 3258 void dispatchOnFragmentDetached(Fragment f, boolean onlyRecursive) { 3259 if (mParent != null) { 3260 FragmentManager parentManager = mParent.getFragmentManager(); 3261 if (parentManager instanceof FragmentManagerImpl) { 3262 ((FragmentManagerImpl) parentManager) 3263 .dispatchOnFragmentDetached(f, true); 3264 } 3265 } 3266 if (mLifecycleCallbacks == null) { 3267 return; 3268 } 3269 for (Pair<FragmentLifecycleCallbacks, Boolean> p : mLifecycleCallbacks) { 3270 if (!onlyRecursive || p.second) { 3271 p.first.onFragmentDetached(this, f); 3272 } 3273 } 3274 } 3275 3276 public static int reverseTransit(int transit) { 3277 int rev = 0; 3278 switch (transit) { 3279 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 3280 rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE; 3281 break; 3282 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 3283 rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN; 3284 break; 3285 case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 3286 rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE; 3287 break; 3288 } 3289 return rev; 3290 3291 } 3292 3293 public static final int ANIM_STYLE_OPEN_ENTER = 1; 3294 public static final int ANIM_STYLE_OPEN_EXIT = 2; 3295 public static final int ANIM_STYLE_CLOSE_ENTER = 3; 3296 public static final int ANIM_STYLE_CLOSE_EXIT = 4; 3297 public static final int ANIM_STYLE_FADE_ENTER = 5; 3298 public static final int ANIM_STYLE_FADE_EXIT = 6; 3299 3300 public static int transitToStyleIndex(int transit, boolean enter) { 3301 int animAttr = -1; 3302 switch (transit) { 3303 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 3304 animAttr = enter ? ANIM_STYLE_OPEN_ENTER : ANIM_STYLE_OPEN_EXIT; 3305 break; 3306 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 3307 animAttr = enter ? ANIM_STYLE_CLOSE_ENTER : ANIM_STYLE_CLOSE_EXIT; 3308 break; 3309 case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 3310 animAttr = enter ? ANIM_STYLE_FADE_ENTER : ANIM_STYLE_FADE_EXIT; 3311 break; 3312 } 3313 return animAttr; 3314 } 3315 3316 @Override 3317 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 3318 if (!"fragment".equals(name)) { 3319 return null; 3320 } 3321 3322 String fname = attrs.getAttributeValue(null, "class"); 3323 TypedArray a = context.obtainStyledAttributes(attrs, FragmentTag.Fragment); 3324 if (fname == null) { 3325 fname = a.getString(FragmentTag.Fragment_name); 3326 } 3327 int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID); 3328 String tag = a.getString(FragmentTag.Fragment_tag); 3329 a.recycle(); 3330 3331 if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) { 3332 // Invalid support lib fragment; let the device's framework handle it. 3333 // This will allow android.app.Fragments to do the right thing. 3334 return null; 3335 } 3336 3337 int containerId = parent != null ? parent.getId() : 0; 3338 if (containerId == View.NO_ID && id == View.NO_ID && tag == null) { 3339 throw new IllegalArgumentException(attrs.getPositionDescription() 3340 + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname); 3341 } 3342 3343 // If we restored from a previous state, we may already have 3344 // instantiated this fragment from the state and should use 3345 // that instance instead of making a new one. 3346 Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null; 3347 if (fragment == null && tag != null) { 3348 fragment = findFragmentByTag(tag); 3349 } 3350 if (fragment == null && containerId != View.NO_ID) { 3351 fragment = findFragmentById(containerId); 3352 } 3353 3354 if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x" 3355 + Integer.toHexString(id) + " fname=" + fname 3356 + " existing=" + fragment); 3357 if (fragment == null) { 3358 fragment = Fragment.instantiate(context, fname); 3359 fragment.mFromLayout = true; 3360 fragment.mFragmentId = id != 0 ? id : containerId; 3361 fragment.mContainerId = containerId; 3362 fragment.mTag = tag; 3363 fragment.mInLayout = true; 3364 fragment.mFragmentManager = this; 3365 fragment.mHost = mHost; 3366 fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); 3367 addFragment(fragment, true); 3368 3369 } else if (fragment.mInLayout) { 3370 // A fragment already exists and it is not one we restored from 3371 // previous state. 3372 throw new IllegalArgumentException(attrs.getPositionDescription() 3373 + ": Duplicate id 0x" + Integer.toHexString(id) 3374 + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId) 3375 + " with another fragment for " + fname); 3376 } else { 3377 // This fragment was retained from a previous instance; get it 3378 // going now. 3379 fragment.mInLayout = true; 3380 fragment.mHost = mHost; 3381 // If this fragment is newly instantiated (either right now, or 3382 // from last saved state), then give it the attributes to 3383 // initialize itself. 3384 if (!fragment.mRetaining) { 3385 fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); 3386 } 3387 } 3388 3389 // If we haven't finished entering the CREATED state ourselves yet, 3390 // push the inflated child fragment along. This will ensureInflatedFragmentView 3391 // at the right phase of the lifecycle so that we will have mView populated 3392 // for compliant fragments below. 3393 if (mCurState < Fragment.CREATED && fragment.mFromLayout) { 3394 moveToState(fragment, Fragment.CREATED, 0, 0, false); 3395 } else { 3396 moveToState(fragment); 3397 } 3398 3399 if (fragment.mView == null) { 3400 throw new IllegalStateException("Fragment " + fname 3401 + " did not create a view."); 3402 } 3403 if (id != 0) { 3404 fragment.mView.setId(id); 3405 } 3406 if (fragment.mView.getTag() == null) { 3407 fragment.mView.setTag(tag); 3408 } 3409 return fragment.mView; 3410 } 3411 3412 LayoutInflaterFactory getLayoutInflaterFactory() { 3413 return this; 3414 } 3415 3416 static class FragmentTag { 3417 public static final int[] Fragment = { 3418 0x01010003, 0x010100d0, 0x010100d1 3419 }; 3420 public static final int Fragment_id = 1; 3421 public static final int Fragment_name = 0; 3422 public static final int Fragment_tag = 2; 3423 } 3424 3425 /** 3426 * An add or pop transaction to be scheduled for the UI thread. 3427 */ 3428 interface OpGenerator { 3429 /** 3430 * Generate transactions to add to {@code records} and whether or not the transaction is 3431 * an add or pop to {@code isRecordPop}. 3432 * 3433 * records and isRecordPop must be added equally so that each transaction in records 3434 * matches the boolean for whether or not it is a pop in isRecordPop. 3435 * 3436 * @param records A list to add transactions to. 3437 * @param isRecordPop A list to add whether or not the transactions added to records is 3438 * a pop transaction. 3439 * @return true if something was added or false otherwise. 3440 */ 3441 boolean generateOps(ArrayList<BackStackRecord> records, ArrayList<Boolean> isRecordPop); 3442 } 3443 3444 /** 3445 * A pop operation OpGenerator. This will be run on the UI thread and will generate the 3446 * transactions that will be popped if anything can be popped. 3447 */ 3448 private class PopBackStackState implements OpGenerator { 3449 final String mName; 3450 final int mId; 3451 final int mFlags; 3452 3453 PopBackStackState(String name, int id, int flags) { 3454 mName = name; 3455 mId = id; 3456 mFlags = flags; 3457 } 3458 3459 @Override 3460 public boolean generateOps(ArrayList<BackStackRecord> records, 3461 ArrayList<Boolean> isRecordPop) { 3462 return popBackStackState(records, isRecordPop, mName, mId, mFlags); 3463 } 3464 } 3465 3466 /** 3467 * A listener for a postponed transaction. This waits until 3468 * {@link Fragment#startPostponedEnterTransition()} is called or a transaction is started 3469 * that interacts with this one, based on interactions with the fragment container. 3470 */ 3471 static class StartEnterTransitionListener 3472 implements Fragment.OnStartEnterTransitionListener { 3473 private final boolean mIsBack; 3474 private final BackStackRecord mRecord; 3475 private int mNumPostponed; 3476 3477 StartEnterTransitionListener(BackStackRecord record, boolean isBack) { 3478 mIsBack = isBack; 3479 mRecord = record; 3480 } 3481 3482 /** 3483 * Called from {@link Fragment#startPostponedEnterTransition()}, this decreases the 3484 * number of Fragments that are postponed. This may cause the transaction to schedule 3485 * to finish running and run transitions and animations. 3486 */ 3487 @Override 3488 public void onStartEnterTransition() { 3489 mNumPostponed--; 3490 if (mNumPostponed != 0) { 3491 return; 3492 } 3493 mRecord.mManager.scheduleCommit(); 3494 } 3495 3496 /** 3497 * Called from {@link Fragment# 3498 * setOnStartEnterTransitionListener(Fragment.OnStartEnterTransitionListener)}, this 3499 * increases the number of fragments that are postponed as part of this transaction. 3500 */ 3501 @Override 3502 public void startListening() { 3503 mNumPostponed++; 3504 } 3505 3506 /** 3507 * @return true if there are no more postponed fragments as part of the transaction. 3508 */ 3509 public boolean isReady() { 3510 return mNumPostponed == 0; 3511 } 3512 3513 /** 3514 * Completes the transaction and start the animations and transitions. This may skip 3515 * the transitions if this is called before all fragments have called 3516 * {@link Fragment#startPostponedEnterTransition()}. 3517 */ 3518 public void completeTransaction() { 3519 final boolean canceled; 3520 canceled = mNumPostponed > 0; 3521 FragmentManagerImpl manager = mRecord.mManager; 3522 final int numAdded = manager.mAdded.size(); 3523 for (int i = 0; i < numAdded; i++) { 3524 final Fragment fragment = manager.mAdded.get(i); 3525 fragment.setOnStartEnterTransitionListener(null); 3526 if (canceled && fragment.isPostponed()) { 3527 fragment.startPostponedEnterTransition(); 3528 } 3529 } 3530 mRecord.mManager.completeExecute(mRecord, mIsBack, !canceled, true); 3531 } 3532 3533 /** 3534 * Cancels this transaction instead of completing it. That means that the state isn't 3535 * changed, so the pop results in no change to the state. 3536 */ 3537 public void cancelTransaction() { 3538 mRecord.mManager.completeExecute(mRecord, mIsBack, false, false); 3539 } 3540 } 3541} 3542