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