FragmentManager.java revision f7bdb2b37497128b8a9108eea43c17ea544c957a
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 android.content.Context; 20import android.content.res.Configuration; 21import android.content.res.Resources.NotFoundException; 22import android.content.res.TypedArray; 23import android.os.Build; 24import android.os.Bundle; 25import android.os.Handler; 26import android.os.Looper; 27import android.os.Parcel; 28import android.os.Parcelable; 29import android.support.annotation.CallSuper; 30import android.support.annotation.IdRes; 31import android.support.annotation.StringRes; 32import android.support.v4.util.DebugUtils; 33import android.support.v4.util.LogWriter; 34import android.support.v4.view.LayoutInflaterFactory; 35import android.support.v4.view.ViewCompat; 36import android.util.AttributeSet; 37import android.util.Log; 38import android.util.SparseArray; 39import android.view.animation.AccelerateInterpolator; 40import android.view.animation.AlphaAnimation; 41import android.view.animation.Animation; 42import android.view.animation.AnimationSet; 43import android.view.animation.AnimationUtils; 44import android.view.animation.DecelerateInterpolator; 45import android.view.animation.Interpolator; 46import android.view.animation.ScaleAnimation; 47import android.view.animation.Animation.AnimationListener; 48import android.view.Menu; 49import android.view.MenuInflater; 50import android.view.MenuItem; 51import android.view.View; 52import android.view.ViewGroup; 53 54import java.io.FileDescriptor; 55import java.io.PrintWriter; 56import java.lang.reflect.Field; 57import java.util.ArrayList; 58import java.util.Arrays; 59import java.util.List; 60 61/** 62 * Static library support version of the framework's {@link android.app.FragmentManager}. 63 * Used to write apps that run on platforms prior to Android 3.0. When running 64 * on Android 3.0 or above, this implementation is still used; it does not try 65 * to switch to the framework's implementation. See the framework {@link FragmentManager} 66 * documentation for a class overview. 67 * 68 * <p>Your activity must derive from {@link FragmentActivity} to use this. From such an activity, 69 * you can acquire the {@link FragmentManager} by calling 70 * {@link FragmentActivity#getSupportFragmentManager}. 71 */ 72public abstract class FragmentManager { 73 /** 74 * Representation of an entry on the fragment back stack, as created 75 * with {@link FragmentTransaction#addToBackStack(String) 76 * FragmentTransaction.addToBackStack()}. Entries can later be 77 * retrieved with {@link FragmentManager#getBackStackEntryAt(int) 78 * FragmentManager.getBackStackEntry()}. 79 * 80 * <p>Note that you should never hold on to a BackStackEntry object; 81 * the identifier as returned by {@link #getId} is the only thing that 82 * will be persisted across activity instances. 83 */ 84 public interface BackStackEntry { 85 /** 86 * Return the unique identifier for the entry. This is the only 87 * representation of the entry that will persist across activity 88 * instances. 89 */ 90 public int getId(); 91 92 /** 93 * Get the name that was supplied to 94 * {@link FragmentTransaction#addToBackStack(String) 95 * FragmentTransaction.addToBackStack(String)} when creating this entry. 96 */ 97 public String getName(); 98 99 /** 100 * Return the full bread crumb title resource identifier for the entry, 101 * or 0 if it does not have one. 102 */ 103 @StringRes 104 public int getBreadCrumbTitleRes(); 105 106 /** 107 * Return the short bread crumb title resource identifier for the entry, 108 * or 0 if it does not have one. 109 */ 110 @StringRes 111 public int getBreadCrumbShortTitleRes(); 112 113 /** 114 * Return the full bread crumb title for the entry, or null if it 115 * does not have one. 116 */ 117 public CharSequence getBreadCrumbTitle(); 118 119 /** 120 * Return the short bread crumb title for the entry, or null if it 121 * does not have one. 122 */ 123 public CharSequence getBreadCrumbShortTitle(); 124 } 125 126 /** 127 * Interface to watch for changes to the back stack. 128 */ 129 public interface OnBackStackChangedListener { 130 /** 131 * Called whenever the contents of the back stack change. 132 */ 133 public void onBackStackChanged(); 134 } 135 136 /** 137 * Start a series of edit operations on the Fragments associated with 138 * this FragmentManager. 139 * 140 * <p>Note: A fragment transaction can only be created/committed prior 141 * to an activity saving its state. If you try to commit a transaction 142 * after {@link FragmentActivity#onSaveInstanceState FragmentActivity.onSaveInstanceState()} 143 * (and prior to a following {@link FragmentActivity#onStart FragmentActivity.onStart} 144 * or {@link FragmentActivity#onResume FragmentActivity.onResume()}, you will get an error. 145 * This is because the framework takes care of saving your current fragments 146 * in the state, and if changes are made after the state is saved then they 147 * will be lost.</p> 148 */ 149 public abstract FragmentTransaction beginTransaction(); 150 151 /** 152 * @hide -- remove once prebuilts are in. 153 * @deprecated 154 */ 155 @Deprecated 156 public FragmentTransaction openTransaction() { 157 return beginTransaction(); 158 } 159 160 /** 161 * After a {@link FragmentTransaction} is committed with 162 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it 163 * is scheduled to be executed asynchronously on the process's main thread. 164 * If you want to immediately executing any such pending operations, you 165 * can call this function (only from the main thread) to do so. Note that 166 * all callbacks and other related behavior will be done from within this 167 * call, so be careful about where this is called from. 168 * 169 * <p>If you are committing a single transaction that does not modify the 170 * fragment back stack, strongly consider using 171 * {@link FragmentTransaction#commitNow()} instead. This can help avoid 172 * unwanted side effects when other code in your app has pending committed 173 * transactions that expect different timing.</p> 174 * 175 * @return Returns true if there were any pending transactions to be 176 * executed. 177 */ 178 public abstract boolean executePendingTransactions(); 179 180 /** 181 * Finds a fragment that was identified by the given id either when inflated 182 * from XML or as the container ID when added in a transaction. This first 183 * searches through fragments that are currently added to the manager's 184 * activity; if no such fragment is found, then all fragments currently 185 * on the back stack associated with this ID are searched. 186 * @return The fragment if found or null otherwise. 187 */ 188 public abstract Fragment findFragmentById(@IdRes int id); 189 190 /** 191 * Finds a fragment that was identified by the given tag either when inflated 192 * from XML or as supplied 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 are searched. 196 * @return The fragment if found or null otherwise. 197 */ 198 public abstract Fragment findFragmentByTag(String tag); 199 200 /** 201 * Flag for {@link #popBackStack(String, int)} 202 * and {@link #popBackStack(int, int)}: If set, and the name or ID of 203 * a back stack entry has been supplied, then all matching entries will 204 * be consumed until one that doesn't match is found or the bottom of 205 * the stack is reached. Otherwise, all entries up to but not including that entry 206 * will be removed. 207 */ 208 public static final int POP_BACK_STACK_INCLUSIVE = 1<<0; 209 210 /** 211 * Pop the top state off the back stack. Returns true if there was one 212 * to pop, else false. This function is asynchronous -- it enqueues the 213 * request to pop, but the action will not be performed until the application 214 * returns to its event loop. 215 */ 216 public abstract void popBackStack(); 217 218 /** 219 * Like {@link #popBackStack()}, but performs the operation immediately 220 * inside of the call. This is like calling {@link #executePendingTransactions()} 221 * afterwards. 222 * @return Returns true if there was something popped, else false. 223 */ 224 public abstract boolean popBackStackImmediate(); 225 226 /** 227 * Pop the last fragment transition from the manager's fragment 228 * back stack. If there is nothing to pop, false is returned. 229 * This function is asynchronous -- it enqueues the 230 * request to pop, but the action will not be performed until the application 231 * returns to its event loop. 232 * 233 * @param name If non-null, this is the name of a previous back state 234 * to look for; if found, all states up to that state will be popped. The 235 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 236 * the named state itself is popped. If null, only the top state is popped. 237 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 238 */ 239 public abstract void popBackStack(String name, int flags); 240 241 /** 242 * Like {@link #popBackStack(String, int)}, but performs the operation immediately 243 * inside of the call. This is like calling {@link #executePendingTransactions()} 244 * afterwards. 245 * @return Returns true if there was something popped, else false. 246 */ 247 public abstract boolean popBackStackImmediate(String name, int flags); 248 249 /** 250 * Pop all back stack states up to the one with the given identifier. 251 * This function is asynchronous -- it enqueues the 252 * request to pop, but the action will not be performed until the application 253 * returns to its event loop. 254 * 255 * @param id Identifier of the stated to be popped. If no identifier exists, 256 * false is returned. 257 * The identifier is the number returned by 258 * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The 259 * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 260 * the named state itself is popped. 261 * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 262 */ 263 public abstract void popBackStack(int id, int flags); 264 265 /** 266 * Like {@link #popBackStack(int, int)}, but performs the operation immediately 267 * inside of the call. This is like calling {@link #executePendingTransactions()} 268 * afterwards. 269 * @return Returns true if there was something popped, else false. 270 */ 271 public abstract boolean popBackStackImmediate(int id, int flags); 272 273 /** 274 * Return the number of entries currently in the back stack. 275 */ 276 public abstract int getBackStackEntryCount(); 277 278 /** 279 * Return the BackStackEntry at index <var>index</var> in the back stack; 280 * entries start index 0 being the bottom of the stack. 281 */ 282 public abstract BackStackEntry getBackStackEntryAt(int index); 283 284 /** 285 * Add a new listener for changes to the fragment back stack. 286 */ 287 public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener); 288 289 /** 290 * Remove a listener that was previously added with 291 * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}. 292 */ 293 public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener); 294 295 /** 296 * Put a reference to a fragment in a Bundle. This Bundle can be 297 * persisted as saved state, and when later restoring 298 * {@link #getFragment(Bundle, String)} will return the current 299 * instance of the same fragment. 300 * 301 * @param bundle The bundle in which to put the fragment reference. 302 * @param key The name of the entry in the bundle. 303 * @param fragment The Fragment whose reference is to be stored. 304 */ 305 public abstract void putFragment(Bundle bundle, String key, Fragment fragment); 306 307 /** 308 * Retrieve the current Fragment instance for a reference previously 309 * placed with {@link #putFragment(Bundle, String, Fragment)}. 310 * 311 * @param bundle The bundle from which to retrieve the fragment reference. 312 * @param key The name of the entry in the bundle. 313 * @return Returns the current Fragment instance that is associated with 314 * the given reference. 315 */ 316 public abstract Fragment getFragment(Bundle bundle, String key); 317 318 /** 319 * Get a list of all fragments that have been added to the fragment manager. 320 * 321 * @return The list of all fragments or null if none. 322 * @hide 323 */ 324 public abstract List<Fragment> getFragments(); 325 326 /** 327 * Save the current instance state of the given Fragment. This can be 328 * used later when creating a new instance of the Fragment and adding 329 * it to the fragment manager, to have it create itself to match the 330 * current state returned here. Note that there are limits on how 331 * this can be used: 332 * 333 * <ul> 334 * <li>The Fragment must currently be attached to the FragmentManager. 335 * <li>A new Fragment created using this saved state must be the same class 336 * type as the Fragment it was created from. 337 * <li>The saved state can not contain dependencies on other fragments -- 338 * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to 339 * store a fragment reference because that reference may not be valid when 340 * this saved state is later used. Likewise the Fragment's target and 341 * result code are not included in this state. 342 * </ul> 343 * 344 * @param f The Fragment whose state is to be saved. 345 * @return The generated state. This will be null if there was no 346 * interesting state created by the fragment. 347 */ 348 public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f); 349 350 /** 351 * Returns true if the final {@link android.app.Activity#onDestroy() Activity.onDestroy()} 352 * call has been made on the FragmentManager's Activity, so this instance is now dead. 353 */ 354 public abstract boolean isDestroyed(); 355 356 /** 357 * Print the FragmentManager's state into the given stream. 358 * 359 * @param prefix Text to print at the front of each line. 360 * @param fd The raw file descriptor that the dump is being sent to. 361 * @param writer A PrintWriter to which the dump is to be set. 362 * @param args Additional arguments to the dump request. 363 */ 364 public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); 365 366 /** 367 * Control whether the framework's internal fragment manager debugging 368 * logs are turned on. If enabled, you will see output in logcat as 369 * the framework performs fragment operations. 370 */ 371 public static void enableDebugLogging(boolean enabled) { 372 FragmentManagerImpl.DEBUG = enabled; 373 } 374} 375 376final class FragmentManagerState implements Parcelable { 377 FragmentState[] mActive; 378 int[] mAdded; 379 BackStackState[] mBackStack; 380 381 public FragmentManagerState() { 382 } 383 384 public FragmentManagerState(Parcel in) { 385 mActive = in.createTypedArray(FragmentState.CREATOR); 386 mAdded = in.createIntArray(); 387 mBackStack = in.createTypedArray(BackStackState.CREATOR); 388 } 389 390 public int describeContents() { 391 return 0; 392 } 393 394 public void writeToParcel(Parcel dest, int flags) { 395 dest.writeTypedArray(mActive, flags); 396 dest.writeIntArray(mAdded); 397 dest.writeTypedArray(mBackStack, flags); 398 } 399 400 public static final Parcelable.Creator<FragmentManagerState> CREATOR 401 = new Parcelable.Creator<FragmentManagerState>() { 402 public FragmentManagerState createFromParcel(Parcel in) { 403 return new FragmentManagerState(in); 404 } 405 406 public FragmentManagerState[] newArray(int size) { 407 return new FragmentManagerState[size]; 408 } 409 }; 410} 411 412/** 413 * Container for fragments associated with an activity. 414 */ 415final class FragmentManagerImpl extends FragmentManager implements LayoutInflaterFactory { 416 static boolean DEBUG = false; 417 static final String TAG = "FragmentManager"; 418 419 static final boolean HONEYCOMB = android.os.Build.VERSION.SDK_INT >= 11; 420 421 static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state"; 422 static final String TARGET_STATE_TAG = "android:target_state"; 423 static final String VIEW_STATE_TAG = "android:view_state"; 424 static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint"; 425 426 427 static class AnimateOnHWLayerIfNeededListener implements AnimationListener { 428 private AnimationListener mOrignalListener = null; 429 private boolean mShouldRunOnHWLayer = false; 430 private View mView = null; 431 public AnimateOnHWLayerIfNeededListener(final View v, Animation anim) { 432 if (v == null || anim == null) { 433 return; 434 } 435 mView = v; 436 } 437 438 public AnimateOnHWLayerIfNeededListener(final View v, Animation anim, 439 AnimationListener listener) { 440 if (v == null || anim == null) { 441 return; 442 } 443 mOrignalListener = listener; 444 mView = v; 445 } 446 447 @Override 448 @CallSuper 449 public void onAnimationStart(Animation animation) { 450 if (mView != null) { 451 mShouldRunOnHWLayer = shouldRunOnHWLayer(mView, animation); 452 if (mShouldRunOnHWLayer) { 453 mView.post(new Runnable() { 454 @Override 455 public void run() { 456 ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_HARDWARE, null); 457 } 458 }); 459 } 460 } 461 if (mOrignalListener != null) { 462 mOrignalListener.onAnimationStart(animation); 463 } 464 } 465 466 @Override 467 @CallSuper 468 public void onAnimationEnd(Animation animation) { 469 if (mView != null && mShouldRunOnHWLayer) { 470 mView.post(new Runnable() { 471 @Override 472 public void run() { 473 ViewCompat.setLayerType(mView, ViewCompat.LAYER_TYPE_NONE, null); 474 } 475 }); 476 } 477 if (mOrignalListener != null) { 478 mOrignalListener.onAnimationEnd(animation); 479 } 480 } 481 482 @Override 483 public void onAnimationRepeat(Animation animation) { 484 if (mOrignalListener != null) { 485 mOrignalListener.onAnimationRepeat(animation); 486 } 487 } 488 } 489 490 ArrayList<Runnable> mPendingActions; 491 Runnable[] mTmpActions; 492 boolean mExecutingActions; 493 494 ArrayList<Fragment> mActive; 495 ArrayList<Fragment> mAdded; 496 ArrayList<Integer> mAvailIndices; 497 ArrayList<BackStackRecord> mBackStack; 498 ArrayList<Fragment> mCreatedMenus; 499 500 // Must be accessed while locked. 501 ArrayList<BackStackRecord> mBackStackIndices; 502 ArrayList<Integer> mAvailBackStackIndices; 503 504 ArrayList<OnBackStackChangedListener> mBackStackChangeListeners; 505 506 int mCurState = Fragment.INITIALIZING; 507 FragmentHostCallback mHost; 508 FragmentController mController; 509 FragmentContainer mContainer; 510 Fragment mParent; 511 512 static Field sAnimationListenerField = null; 513 514 boolean mNeedMenuInvalidate; 515 boolean mStateSaved; 516 boolean mDestroyed; 517 String mNoTransactionsBecause; 518 boolean mHavePendingDeferredStart; 519 520 // Temporary vars for state save and restore. 521 Bundle mStateBundle = null; 522 SparseArray<Parcelable> mStateArray = null; 523 524 Runnable mExecCommit = new Runnable() { 525 @Override 526 public void run() { 527 execPendingActions(); 528 } 529 }; 530 531 static boolean modifiesAlpha(Animation anim) { 532 if (anim instanceof AlphaAnimation) { 533 return true; 534 } else if (anim instanceof AnimationSet) { 535 List<Animation> anims = ((AnimationSet) anim).getAnimations(); 536 for (int i = 0; i < anims.size(); i++) { 537 if (anims.get(i) instanceof AlphaAnimation) { 538 return true; 539 } 540 } 541 } 542 return false; 543 } 544 545 static boolean shouldRunOnHWLayer(View v, Animation anim) { 546 return Build.VERSION.SDK_INT >= 19 547 && ViewCompat.getLayerType(v) == ViewCompat.LAYER_TYPE_NONE 548 && ViewCompat.hasOverlappingRendering(v) 549 && modifiesAlpha(anim); 550 } 551 552 private void throwException(RuntimeException ex) { 553 Log.e(TAG, ex.getMessage()); 554 Log.e(TAG, "Activity state:"); 555 LogWriter logw = new LogWriter(TAG); 556 PrintWriter pw = new PrintWriter(logw); 557 if (mHost != null) { 558 try { 559 mHost.onDump(" ", null, pw, new String[] { }); 560 } catch (Exception e) { 561 Log.e(TAG, "Failed dumping state", e); 562 } 563 } else { 564 try { 565 dump(" ", null, pw, new String[] { }); 566 } catch (Exception e) { 567 Log.e(TAG, "Failed dumping state", e); 568 } 569 } 570 throw ex; 571 } 572 573 @Override 574 public FragmentTransaction beginTransaction() { 575 return new BackStackRecord(this); 576 } 577 578 @Override 579 public boolean executePendingTransactions() { 580 return execPendingActions(); 581 } 582 583 @Override 584 public void popBackStack() { 585 enqueueAction(new Runnable() { 586 @Override public void run() { 587 popBackStackState(mHost.getHandler(), null, -1, 0); 588 } 589 }, false); 590 } 591 592 @Override 593 public boolean popBackStackImmediate() { 594 checkStateLoss(); 595 executePendingTransactions(); 596 return popBackStackState(mHost.getHandler(), null, -1, 0); 597 } 598 599 @Override 600 public void popBackStack(final String name, final int flags) { 601 enqueueAction(new Runnable() { 602 @Override public void run() { 603 popBackStackState(mHost.getHandler(), name, -1, flags); 604 } 605 }, false); 606 } 607 608 @Override 609 public boolean popBackStackImmediate(String name, int flags) { 610 checkStateLoss(); 611 executePendingTransactions(); 612 return popBackStackState(mHost.getHandler(), name, -1, flags); 613 } 614 615 @Override 616 public void popBackStack(final int id, final int flags) { 617 if (id < 0) { 618 throw new IllegalArgumentException("Bad id: " + id); 619 } 620 enqueueAction(new Runnable() { 621 @Override public void run() { 622 popBackStackState(mHost.getHandler(), null, id, flags); 623 } 624 }, false); 625 } 626 627 @Override 628 public boolean popBackStackImmediate(int id, int flags) { 629 checkStateLoss(); 630 executePendingTransactions(); 631 if (id < 0) { 632 throw new IllegalArgumentException("Bad id: " + id); 633 } 634 return popBackStackState(mHost.getHandler(), null, id, flags); 635 } 636 637 @Override 638 public int getBackStackEntryCount() { 639 return mBackStack != null ? mBackStack.size() : 0; 640 } 641 642 @Override 643 public BackStackEntry getBackStackEntryAt(int index) { 644 return mBackStack.get(index); 645 } 646 647 @Override 648 public void addOnBackStackChangedListener(OnBackStackChangedListener listener) { 649 if (mBackStackChangeListeners == null) { 650 mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>(); 651 } 652 mBackStackChangeListeners.add(listener); 653 } 654 655 @Override 656 public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) { 657 if (mBackStackChangeListeners != null) { 658 mBackStackChangeListeners.remove(listener); 659 } 660 } 661 662 @Override 663 public void putFragment(Bundle bundle, String key, Fragment fragment) { 664 if (fragment.mIndex < 0) { 665 throwException(new IllegalStateException("Fragment " + fragment 666 + " is not currently in the FragmentManager")); 667 } 668 bundle.putInt(key, fragment.mIndex); 669 } 670 671 @Override 672 public Fragment getFragment(Bundle bundle, String key) { 673 int index = bundle.getInt(key, -1); 674 if (index == -1) { 675 return null; 676 } 677 if (index >= mActive.size()) { 678 throwException(new IllegalStateException("Fragment no longer exists for key " 679 + key + ": index " + index)); 680 } 681 Fragment f = mActive.get(index); 682 if (f == null) { 683 throwException(new IllegalStateException("Fragment no longer exists for key " 684 + key + ": index " + index)); 685 } 686 return f; 687 } 688 689 @Override 690 public List<Fragment> getFragments() { 691 return mActive; 692 } 693 694 @Override 695 public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) { 696 if (fragment.mIndex < 0) { 697 throwException( new IllegalStateException("Fragment " + fragment 698 + " is not currently in the FragmentManager")); 699 } 700 if (fragment.mState > Fragment.INITIALIZING) { 701 Bundle result = saveFragmentBasicState(fragment); 702 return result != null ? new Fragment.SavedState(result) : null; 703 } 704 return null; 705 } 706 707 @Override 708 public boolean isDestroyed() { 709 return mDestroyed; 710 } 711 712 @Override 713 public String toString() { 714 StringBuilder sb = new StringBuilder(128); 715 sb.append("FragmentManager{"); 716 sb.append(Integer.toHexString(System.identityHashCode(this))); 717 sb.append(" in "); 718 if (mParent != null) { 719 DebugUtils.buildShortClassTag(mParent, sb); 720 } else { 721 DebugUtils.buildShortClassTag(mHost, sb); 722 } 723 sb.append("}}"); 724 return sb.toString(); 725 } 726 727 @Override 728 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 729 String innerPrefix = prefix + " "; 730 731 int N; 732 if (mActive != null) { 733 N = mActive.size(); 734 if (N > 0) { 735 writer.print(prefix); writer.print("Active Fragments in "); 736 writer.print(Integer.toHexString(System.identityHashCode(this))); 737 writer.println(":"); 738 for (int i=0; i<N; i++) { 739 Fragment f = mActive.get(i); 740 writer.print(prefix); writer.print(" #"); writer.print(i); 741 writer.print(": "); writer.println(f); 742 if (f != null) { 743 f.dump(innerPrefix, fd, writer, args); 744 } 745 } 746 } 747 } 748 749 if (mAdded != null) { 750 N = mAdded.size(); 751 if (N > 0) { 752 writer.print(prefix); writer.println("Added Fragments:"); 753 for (int i=0; i<N; i++) { 754 Fragment f = mAdded.get(i); 755 writer.print(prefix); writer.print(" #"); writer.print(i); 756 writer.print(": "); writer.println(f.toString()); 757 } 758 } 759 } 760 761 if (mCreatedMenus != null) { 762 N = mCreatedMenus.size(); 763 if (N > 0) { 764 writer.print(prefix); writer.println("Fragments Created Menus:"); 765 for (int i=0; i<N; i++) { 766 Fragment f = mCreatedMenus.get(i); 767 writer.print(prefix); writer.print(" #"); writer.print(i); 768 writer.print(": "); writer.println(f.toString()); 769 } 770 } 771 } 772 773 if (mBackStack != null) { 774 N = mBackStack.size(); 775 if (N > 0) { 776 writer.print(prefix); writer.println("Back Stack:"); 777 for (int i=0; i<N; i++) { 778 BackStackRecord bs = mBackStack.get(i); 779 writer.print(prefix); writer.print(" #"); writer.print(i); 780 writer.print(": "); writer.println(bs.toString()); 781 bs.dump(innerPrefix, fd, writer, args); 782 } 783 } 784 } 785 786 synchronized (this) { 787 if (mBackStackIndices != null) { 788 N = mBackStackIndices.size(); 789 if (N > 0) { 790 writer.print(prefix); writer.println("Back Stack Indices:"); 791 for (int i=0; i<N; i++) { 792 BackStackRecord bs = mBackStackIndices.get(i); 793 writer.print(prefix); writer.print(" #"); writer.print(i); 794 writer.print(": "); writer.println(bs); 795 } 796 } 797 } 798 799 if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) { 800 writer.print(prefix); writer.print("mAvailBackStackIndices: "); 801 writer.println(Arrays.toString(mAvailBackStackIndices.toArray())); 802 } 803 } 804 805 if (mPendingActions != null) { 806 N = mPendingActions.size(); 807 if (N > 0) { 808 writer.print(prefix); writer.println("Pending Actions:"); 809 for (int i=0; i<N; i++) { 810 Runnable r = mPendingActions.get(i); 811 writer.print(prefix); writer.print(" #"); writer.print(i); 812 writer.print(": "); writer.println(r); 813 } 814 } 815 } 816 817 writer.print(prefix); writer.println("FragmentManager misc state:"); 818 writer.print(prefix); writer.print(" mHost="); writer.println(mHost); 819 writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer); 820 if (mParent != null) { 821 writer.print(prefix); writer.print(" mParent="); writer.println(mParent); 822 } 823 writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState); 824 writer.print(" mStateSaved="); writer.print(mStateSaved); 825 writer.print(" mDestroyed="); writer.println(mDestroyed); 826 if (mNeedMenuInvalidate) { 827 writer.print(prefix); writer.print(" mNeedMenuInvalidate="); 828 writer.println(mNeedMenuInvalidate); 829 } 830 if (mNoTransactionsBecause != null) { 831 writer.print(prefix); writer.print(" mNoTransactionsBecause="); 832 writer.println(mNoTransactionsBecause); 833 } 834 if (mAvailIndices != null && mAvailIndices.size() > 0) { 835 writer.print(prefix); writer.print(" mAvailIndices: "); 836 writer.println(Arrays.toString(mAvailIndices.toArray())); 837 } 838 } 839 840 static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f); 841 static final Interpolator DECELERATE_CUBIC = new DecelerateInterpolator(1.5f); 842 static final Interpolator ACCELERATE_QUINT = new AccelerateInterpolator(2.5f); 843 static final Interpolator ACCELERATE_CUBIC = new AccelerateInterpolator(1.5f); 844 845 static final int ANIM_DUR = 220; 846 847 static Animation makeOpenCloseAnimation(Context context, float startScale, 848 float endScale, float startAlpha, float endAlpha) { 849 AnimationSet set = new AnimationSet(false); 850 ScaleAnimation scale = new ScaleAnimation(startScale, endScale, startScale, endScale, 851 Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f); 852 scale.setInterpolator(DECELERATE_QUINT); 853 scale.setDuration(ANIM_DUR); 854 set.addAnimation(scale); 855 AlphaAnimation alpha = new AlphaAnimation(startAlpha, endAlpha); 856 alpha.setInterpolator(DECELERATE_CUBIC); 857 alpha.setDuration(ANIM_DUR); 858 set.addAnimation(alpha); 859 return set; 860 } 861 862 static Animation makeFadeAnimation(Context context, float start, float end) { 863 AlphaAnimation anim = new AlphaAnimation(start, end); 864 anim.setInterpolator(DECELERATE_CUBIC); 865 anim.setDuration(ANIM_DUR); 866 return anim; 867 } 868 869 Animation loadAnimation(Fragment fragment, int transit, boolean enter, 870 int transitionStyle) { 871 Animation animObj = fragment.onCreateAnimation(transit, enter, 872 fragment.mNextAnim); 873 if (animObj != null) { 874 return animObj; 875 } 876 877 if (fragment.mNextAnim != 0) { 878 Animation anim = AnimationUtils.loadAnimation(mHost.getContext(), fragment.mNextAnim); 879 if (anim != null) { 880 return anim; 881 } 882 } 883 884 if (transit == 0) { 885 return null; 886 } 887 888 int styleIndex = transitToStyleIndex(transit, enter); 889 if (styleIndex < 0) { 890 return null; 891 } 892 893 switch (styleIndex) { 894 case ANIM_STYLE_OPEN_ENTER: 895 return makeOpenCloseAnimation(mHost.getContext(), 1.125f, 1.0f, 0, 1); 896 case ANIM_STYLE_OPEN_EXIT: 897 return makeOpenCloseAnimation(mHost.getContext(), 1.0f, .975f, 1, 0); 898 case ANIM_STYLE_CLOSE_ENTER: 899 return makeOpenCloseAnimation(mHost.getContext(), .975f, 1.0f, 0, 1); 900 case ANIM_STYLE_CLOSE_EXIT: 901 return makeOpenCloseAnimation(mHost.getContext(), 1.0f, 1.075f, 1, 0); 902 case ANIM_STYLE_FADE_ENTER: 903 return makeFadeAnimation(mHost.getContext(), 0, 1); 904 case ANIM_STYLE_FADE_EXIT: 905 return makeFadeAnimation(mHost.getContext(), 1, 0); 906 } 907 908 if (transitionStyle == 0 && mHost.onHasWindowAnimations()) { 909 transitionStyle = mHost.onGetWindowAnimations(); 910 } 911 if (transitionStyle == 0) { 912 return null; 913 } 914 915 //TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle, 916 // com.android.internal.R.styleable.FragmentAnimation); 917 //int anim = attrs.getResourceId(styleIndex, 0); 918 //attrs.recycle(); 919 920 //if (anim == 0) { 921 // return null; 922 //} 923 924 //return AnimatorInflater.loadAnimator(mActivity, anim); 925 return null; 926 } 927 928 public void performPendingDeferredStart(Fragment f) { 929 if (f.mDeferStart) { 930 if (mExecutingActions) { 931 // Wait until we're done executing our pending transactions 932 mHavePendingDeferredStart = true; 933 return; 934 } 935 f.mDeferStart = false; 936 moveToState(f, mCurState, 0, 0, false); 937 } 938 } 939 940 /** 941 * Sets the to be animated view on hardware layer during the animation. Note 942 * that calling this will replace any existing animation listener on the animation 943 * with a new one, as animations do not support more than one listeners. Therefore, 944 * animations that already have listeners should do the layer change operations 945 * in their existing listeners, rather than calling this function. 946 */ 947 private void setHWLayerAnimListenerIfAlpha(final View v, Animation anim) { 948 if (v == null || anim == null) { 949 return; 950 } 951 if (shouldRunOnHWLayer(v, anim)) { 952 AnimationListener originalListener = null; 953 try { 954 if (sAnimationListenerField == null) { 955 sAnimationListenerField = Animation.class.getDeclaredField("mListener"); 956 sAnimationListenerField.setAccessible(true); 957 } 958 originalListener = (AnimationListener) sAnimationListenerField.get(anim); 959 } catch (NoSuchFieldException e) { 960 Log.e(TAG, "No field with the name mListener is found in Animation class", e); 961 } catch (IllegalAccessException e) { 962 Log.e(TAG, "Cannot access Animation's mListener field", e); 963 } 964 // If there's already a listener set on the animation, we need wrap the new listener 965 // around the existing listener, so that they will both get animation listener 966 // callbacks. 967 anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener(v, anim, 968 originalListener)); 969 } 970 } 971 972 void moveToState(Fragment f, int newState, int transit, int transitionStyle, 973 boolean keepActive) { 974 // Fragments that are not currently added will sit in the onCreate() state. 975 if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) { 976 newState = Fragment.CREATED; 977 } 978 if (f.mRemoving && newState > f.mState) { 979 // While removing a fragment, we can't change it to a higher state. 980 newState = f.mState; 981 } 982 // Defer start if requested; don't allow it to move to STARTED or higher 983 // if it's not already started. 984 if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) { 985 newState = Fragment.STOPPED; 986 } 987 if (f.mState < newState) { 988 // For fragments that are created from a layout, when restoring from 989 // state we don't want to allow them to be created until they are 990 // being reloaded from the layout. 991 if (f.mFromLayout && !f.mInLayout) { 992 return; 993 } 994 if (f.mAnimatingAway != null) { 995 // The fragment is currently being animated... but! Now we 996 // want to move our state back up. Give up on waiting for the 997 // animation, move to whatever the final state should be once 998 // the animation is done, and then we can proceed from there. 999 f.mAnimatingAway = null; 1000 moveToState(f, f.mStateAfterAnimating, 0, 0, true); 1001 } 1002 switch (f.mState) { 1003 case Fragment.INITIALIZING: 1004 if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); 1005 if (f.mSavedFragmentState != null) { 1006 f.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); 1007 f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray( 1008 FragmentManagerImpl.VIEW_STATE_TAG); 1009 f.mTarget = getFragment(f.mSavedFragmentState, 1010 FragmentManagerImpl.TARGET_STATE_TAG); 1011 if (f.mTarget != null) { 1012 f.mTargetRequestCode = f.mSavedFragmentState.getInt( 1013 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0); 1014 } 1015 f.mUserVisibleHint = f.mSavedFragmentState.getBoolean( 1016 FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true); 1017 if (!f.mUserVisibleHint) { 1018 f.mDeferStart = true; 1019 if (newState > Fragment.STOPPED) { 1020 newState = Fragment.STOPPED; 1021 } 1022 } 1023 } 1024 f.mHost = mHost; 1025 f.mParentFragment = mParent; 1026 f.mFragmentManager = mParent != null 1027 ? mParent.mChildFragmentManager : mHost.getFragmentManagerImpl(); 1028 f.mCalled = false; 1029 f.onAttach(mHost.getContext()); 1030 if (!f.mCalled) { 1031 throw new SuperNotCalledException("Fragment " + f 1032 + " did not call through to super.onAttach()"); 1033 } 1034 if (f.mParentFragment == null) { 1035 mHost.onAttachFragment(f); 1036 } 1037 1038 if (!f.mRetaining) { 1039 f.performCreate(f.mSavedFragmentState); 1040 } else { 1041 f.restoreChildFragmentState(f.mSavedFragmentState); 1042 f.mState = Fragment.CREATED; 1043 } 1044 f.mRetaining = false; 1045 if (f.mFromLayout) { 1046 // For fragments that are part of the content view 1047 // layout, we need to instantiate the view immediately 1048 // and the inflater will take care of adding it. 1049 f.mView = f.performCreateView(f.getLayoutInflater( 1050 f.mSavedFragmentState), null, f.mSavedFragmentState); 1051 if (f.mView != null) { 1052 f.mInnerView = f.mView; 1053 if (Build.VERSION.SDK_INT >= 11) { 1054 ViewCompat.setSaveFromParentEnabled(f.mView, false); 1055 } else { 1056 f.mView = NoSaveStateFrameLayout.wrap(f.mView); 1057 } 1058 if (f.mHidden) f.mView.setVisibility(View.GONE); 1059 f.onViewCreated(f.mView, f.mSavedFragmentState); 1060 } else { 1061 f.mInnerView = null; 1062 } 1063 } 1064 case Fragment.CREATED: 1065 if (newState > Fragment.CREATED) { 1066 if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f); 1067 if (!f.mFromLayout) { 1068 ViewGroup container = null; 1069 if (f.mContainerId != 0) { 1070 if (f.mContainerId == View.NO_ID) { 1071 throwException(new IllegalArgumentException( 1072 "Cannot create fragment " 1073 + f 1074 + " for a container view with no id")); 1075 } 1076 container = (ViewGroup) mContainer.onFindViewById(f.mContainerId); 1077 if (container == null && !f.mRestored) { 1078 String resName; 1079 try { 1080 resName = f.getResources().getResourceName(f.mContainerId); 1081 } catch (NotFoundException e) { 1082 resName = "unknown"; 1083 } 1084 throwException(new IllegalArgumentException( 1085 "No view found for id 0x" 1086 + Integer.toHexString(f.mContainerId) + " (" 1087 + resName 1088 + ") for fragment " + f)); 1089 } 1090 } 1091 f.mContainer = container; 1092 f.mView = f.performCreateView(f.getLayoutInflater( 1093 f.mSavedFragmentState), container, f.mSavedFragmentState); 1094 if (f.mView != null) { 1095 f.mInnerView = f.mView; 1096 if (Build.VERSION.SDK_INT >= 11) { 1097 ViewCompat.setSaveFromParentEnabled(f.mView, false); 1098 } else { 1099 f.mView = NoSaveStateFrameLayout.wrap(f.mView); 1100 } 1101 if (container != null) { 1102 Animation anim = loadAnimation(f, transit, true, 1103 transitionStyle); 1104 if (anim != null) { 1105 setHWLayerAnimListenerIfAlpha(f.mView, anim); 1106 f.mView.startAnimation(anim); 1107 } 1108 container.addView(f.mView); 1109 } 1110 if (f.mHidden) f.mView.setVisibility(View.GONE); 1111 f.onViewCreated(f.mView, f.mSavedFragmentState); 1112 } else { 1113 f.mInnerView = null; 1114 } 1115 } 1116 1117 f.performActivityCreated(f.mSavedFragmentState); 1118 if (f.mView != null) { 1119 f.restoreViewState(f.mSavedFragmentState); 1120 } 1121 f.mSavedFragmentState = null; 1122 } 1123 case Fragment.ACTIVITY_CREATED: 1124 if (newState > Fragment.ACTIVITY_CREATED) { 1125 f.mState = Fragment.STOPPED; 1126 } 1127 case Fragment.STOPPED: 1128 if (newState > Fragment.STOPPED) { 1129 if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); 1130 f.performStart(); 1131 } 1132 case Fragment.STARTED: 1133 if (newState > Fragment.STARTED) { 1134 if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); 1135 f.performResume(); 1136 f.mSavedFragmentState = null; 1137 f.mSavedViewState = null; 1138 } 1139 } 1140 } else if (f.mState > newState) { 1141 switch (f.mState) { 1142 case Fragment.RESUMED: 1143 if (newState < Fragment.RESUMED) { 1144 if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); 1145 f.performPause(); 1146 } 1147 case Fragment.STARTED: 1148 if (newState < Fragment.STARTED) { 1149 if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); 1150 f.performStop(); 1151 } 1152 case Fragment.STOPPED: 1153 if (newState < Fragment.STOPPED) { 1154 if (DEBUG) Log.v(TAG, "movefrom STOPPED: " + f); 1155 f.performReallyStop(); 1156 } 1157 case Fragment.ACTIVITY_CREATED: 1158 if (newState < Fragment.ACTIVITY_CREATED) { 1159 if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f); 1160 if (f.mView != null) { 1161 // Need to save the current view state if not 1162 // done already. 1163 if (mHost.onShouldSaveFragmentState(f) && f.mSavedViewState == null) { 1164 saveFragmentViewState(f); 1165 } 1166 } 1167 f.performDestroyView(); 1168 if (f.mView != null && f.mContainer != null) { 1169 Animation anim = null; 1170 if (mCurState > Fragment.INITIALIZING && !mDestroyed) { 1171 anim = loadAnimation(f, transit, false, 1172 transitionStyle); 1173 } 1174 if (anim != null) { 1175 final Fragment fragment = f; 1176 f.mAnimatingAway = f.mView; 1177 f.mStateAfterAnimating = newState; 1178 final View viewToAnimate = f.mView; 1179 anim.setAnimationListener(new AnimateOnHWLayerIfNeededListener( 1180 viewToAnimate, anim) { 1181 @Override 1182 public void onAnimationEnd(Animation animation) { 1183 super.onAnimationEnd(animation); 1184 if (fragment.mAnimatingAway != null) { 1185 fragment.mAnimatingAway = null; 1186 moveToState(fragment, fragment.mStateAfterAnimating, 1187 0, 0, false); 1188 } 1189 } 1190 }); 1191 f.mView.startAnimation(anim); 1192 } 1193 f.mContainer.removeView(f.mView); 1194 } 1195 f.mContainer = null; 1196 f.mView = null; 1197 f.mInnerView = null; 1198 } 1199 case Fragment.CREATED: 1200 if (newState < Fragment.CREATED) { 1201 if (mDestroyed) { 1202 if (f.mAnimatingAway != null) { 1203 // The fragment's containing activity is 1204 // being destroyed, but this fragment is 1205 // currently animating away. Stop the 1206 // animation right now -- it is not needed, 1207 // and we can't wait any more on destroying 1208 // the fragment. 1209 View v = f.mAnimatingAway; 1210 f.mAnimatingAway = null; 1211 v.clearAnimation(); 1212 } 1213 } 1214 if (f.mAnimatingAway != null) { 1215 // We are waiting for the fragment's view to finish 1216 // animating away. Just make a note of the state 1217 // the fragment now should move to once the animation 1218 // is done. 1219 f.mStateAfterAnimating = newState; 1220 newState = Fragment.CREATED; 1221 } else { 1222 if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); 1223 if (!f.mRetaining) { 1224 f.performDestroy(); 1225 } else { 1226 f.mState = Fragment.INITIALIZING; 1227 } 1228 1229 f.performDetach(); 1230 if (!keepActive) { 1231 if (!f.mRetaining) { 1232 makeInactive(f); 1233 } else { 1234 f.mHost = null; 1235 f.mParentFragment = null; 1236 f.mFragmentManager = null; 1237 } 1238 } 1239 } 1240 } 1241 } 1242 } 1243 1244 if (f.mState != newState) { 1245 Log.w(TAG, "moveToState: Fragment state for " + f + " not updated inline; " 1246 + "expected state " + newState + " found " + f.mState); 1247 f.mState = newState; 1248 } 1249 } 1250 1251 void moveToState(Fragment f) { 1252 moveToState(f, mCurState, 0, 0, false); 1253 } 1254 1255 void moveToState(int newState, boolean always) { 1256 moveToState(newState, 0, 0, always); 1257 } 1258 1259 void moveToState(int newState, int transit, int transitStyle, boolean always) { 1260 if (mHost == null && newState != Fragment.INITIALIZING) { 1261 throw new IllegalStateException("No host"); 1262 } 1263 1264 if (!always && mCurState == newState) { 1265 return; 1266 } 1267 1268 mCurState = newState; 1269 if (mActive != null) { 1270 boolean loadersRunning = false; 1271 for (int i=0; i<mActive.size(); i++) { 1272 Fragment f = mActive.get(i); 1273 if (f != null) { 1274 moveToState(f, newState, transit, transitStyle, false); 1275 if (f.mLoaderManager != null) { 1276 loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 1277 } 1278 } 1279 } 1280 1281 if (!loadersRunning) { 1282 startPendingDeferredFragments(); 1283 } 1284 1285 if (mNeedMenuInvalidate && mHost != null && mCurState == Fragment.RESUMED) { 1286 mHost.onSupportInvalidateOptionsMenu(); 1287 mNeedMenuInvalidate = false; 1288 } 1289 } 1290 } 1291 1292 void startPendingDeferredFragments() { 1293 if (mActive == null) return; 1294 1295 for (int i=0; i<mActive.size(); i++) { 1296 Fragment f = mActive.get(i); 1297 if (f != null) { 1298 performPendingDeferredStart(f); 1299 } 1300 } 1301 } 1302 1303 void makeActive(Fragment f) { 1304 if (f.mIndex >= 0) { 1305 return; 1306 } 1307 1308 if (mAvailIndices == null || mAvailIndices.size() <= 0) { 1309 if (mActive == null) { 1310 mActive = new ArrayList<Fragment>(); 1311 } 1312 f.setIndex(mActive.size(), mParent); 1313 mActive.add(f); 1314 1315 } else { 1316 f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent); 1317 mActive.set(f.mIndex, f); 1318 } 1319 if (DEBUG) Log.v(TAG, "Allocated fragment index " + f); 1320 } 1321 1322 void makeInactive(Fragment f) { 1323 if (f.mIndex < 0) { 1324 return; 1325 } 1326 1327 if (DEBUG) Log.v(TAG, "Freeing fragment index " + f); 1328 mActive.set(f.mIndex, null); 1329 if (mAvailIndices == null) { 1330 mAvailIndices = new ArrayList<Integer>(); 1331 } 1332 mAvailIndices.add(f.mIndex); 1333 mHost.inactivateFragment(f.mWho); 1334 f.initState(); 1335 } 1336 1337 public void addFragment(Fragment fragment, boolean moveToStateNow) { 1338 if (mAdded == null) { 1339 mAdded = new ArrayList<Fragment>(); 1340 } 1341 if (DEBUG) Log.v(TAG, "add: " + fragment); 1342 makeActive(fragment); 1343 if (!fragment.mDetached) { 1344 if (mAdded.contains(fragment)) { 1345 throw new IllegalStateException("Fragment already added: " + fragment); 1346 } 1347 mAdded.add(fragment); 1348 fragment.mAdded = true; 1349 fragment.mRemoving = false; 1350 if (fragment.mHasMenu && fragment.mMenuVisible) { 1351 mNeedMenuInvalidate = true; 1352 } 1353 if (moveToStateNow) { 1354 moveToState(fragment); 1355 } 1356 } 1357 } 1358 1359 public void removeFragment(Fragment fragment, int transition, int transitionStyle) { 1360 if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting); 1361 final boolean inactive = !fragment.isInBackStack(); 1362 if (!fragment.mDetached || inactive) { 1363 if (mAdded != null) { 1364 mAdded.remove(fragment); 1365 } 1366 if (fragment.mHasMenu && fragment.mMenuVisible) { 1367 mNeedMenuInvalidate = true; 1368 } 1369 fragment.mAdded = false; 1370 fragment.mRemoving = true; 1371 moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, 1372 transition, transitionStyle, false); 1373 } 1374 } 1375 1376 public void hideFragment(Fragment fragment, int transition, int transitionStyle) { 1377 if (DEBUG) Log.v(TAG, "hide: " + fragment); 1378 if (!fragment.mHidden) { 1379 fragment.mHidden = true; 1380 if (fragment.mView != null) { 1381 Animation anim = loadAnimation(fragment, transition, false, 1382 transitionStyle); 1383 if (anim != null) { 1384 setHWLayerAnimListenerIfAlpha(fragment.mView, anim); 1385 fragment.mView.startAnimation(anim); 1386 } 1387 fragment.mView.setVisibility(View.GONE); 1388 } 1389 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { 1390 mNeedMenuInvalidate = true; 1391 } 1392 fragment.onHiddenChanged(true); 1393 } 1394 } 1395 1396 public void showFragment(Fragment fragment, int transition, int transitionStyle) { 1397 if (DEBUG) Log.v(TAG, "show: " + fragment); 1398 if (fragment.mHidden) { 1399 fragment.mHidden = false; 1400 if (fragment.mView != null) { 1401 Animation anim = loadAnimation(fragment, transition, true, 1402 transitionStyle); 1403 if (anim != null) { 1404 setHWLayerAnimListenerIfAlpha(fragment.mView, anim); 1405 fragment.mView.startAnimation(anim); 1406 } 1407 fragment.mView.setVisibility(View.VISIBLE); 1408 } 1409 if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { 1410 mNeedMenuInvalidate = true; 1411 } 1412 fragment.onHiddenChanged(false); 1413 } 1414 } 1415 1416 public void detachFragment(Fragment fragment, int transition, int transitionStyle) { 1417 if (DEBUG) Log.v(TAG, "detach: " + fragment); 1418 if (!fragment.mDetached) { 1419 fragment.mDetached = true; 1420 if (fragment.mAdded) { 1421 // We are not already in back stack, so need to remove the fragment. 1422 if (mAdded != null) { 1423 if (DEBUG) Log.v(TAG, "remove from detach: " + fragment); 1424 mAdded.remove(fragment); 1425 } 1426 if (fragment.mHasMenu && fragment.mMenuVisible) { 1427 mNeedMenuInvalidate = true; 1428 } 1429 fragment.mAdded = false; 1430 moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false); 1431 } 1432 } 1433 } 1434 1435 public void attachFragment(Fragment fragment, int transition, int transitionStyle) { 1436 if (DEBUG) Log.v(TAG, "attach: " + fragment); 1437 if (fragment.mDetached) { 1438 fragment.mDetached = false; 1439 if (!fragment.mAdded) { 1440 if (mAdded == null) { 1441 mAdded = new ArrayList<Fragment>(); 1442 } 1443 if (mAdded.contains(fragment)) { 1444 throw new IllegalStateException("Fragment already added: " + fragment); 1445 } 1446 if (DEBUG) Log.v(TAG, "add from attach: " + fragment); 1447 mAdded.add(fragment); 1448 fragment.mAdded = true; 1449 if (fragment.mHasMenu && fragment.mMenuVisible) { 1450 mNeedMenuInvalidate = true; 1451 } 1452 moveToState(fragment, mCurState, transition, transitionStyle, false); 1453 } 1454 } 1455 } 1456 1457 public Fragment findFragmentById(int id) { 1458 if (mAdded != null) { 1459 // First look through added fragments. 1460 for (int i=mAdded.size()-1; i>=0; i--) { 1461 Fragment f = mAdded.get(i); 1462 if (f != null && f.mFragmentId == id) { 1463 return f; 1464 } 1465 } 1466 } 1467 if (mActive != null) { 1468 // Now for any known fragment. 1469 for (int i=mActive.size()-1; i>=0; i--) { 1470 Fragment f = mActive.get(i); 1471 if (f != null && f.mFragmentId == id) { 1472 return f; 1473 } 1474 } 1475 } 1476 return null; 1477 } 1478 1479 public Fragment findFragmentByTag(String tag) { 1480 if (mAdded != null && tag != null) { 1481 // First look through added fragments. 1482 for (int i=mAdded.size()-1; i>=0; i--) { 1483 Fragment f = mAdded.get(i); 1484 if (f != null && tag.equals(f.mTag)) { 1485 return f; 1486 } 1487 } 1488 } 1489 if (mActive != null && tag != null) { 1490 // Now for any known fragment. 1491 for (int i=mActive.size()-1; i>=0; i--) { 1492 Fragment f = mActive.get(i); 1493 if (f != null && tag.equals(f.mTag)) { 1494 return f; 1495 } 1496 } 1497 } 1498 return null; 1499 } 1500 1501 public Fragment findFragmentByWho(String who) { 1502 if (mActive != null && who != null) { 1503 for (int i=mActive.size()-1; i>=0; i--) { 1504 Fragment f = mActive.get(i); 1505 if (f != null && (f=f.findFragmentByWho(who)) != null) { 1506 return f; 1507 } 1508 } 1509 } 1510 return null; 1511 } 1512 1513 private void checkStateLoss() { 1514 if (mStateSaved) { 1515 throw new IllegalStateException( 1516 "Can not perform this action after onSaveInstanceState"); 1517 } 1518 if (mNoTransactionsBecause != null) { 1519 throw new IllegalStateException( 1520 "Can not perform this action inside of " + mNoTransactionsBecause); 1521 } 1522 } 1523 1524 /** 1525 * Adds an action to the queue of pending actions. 1526 * 1527 * @param action the action to add 1528 * @param allowStateLoss whether to allow loss of state information 1529 * @throws IllegalStateException if the activity has been destroyed 1530 */ 1531 public void enqueueAction(Runnable action, boolean allowStateLoss) { 1532 if (!allowStateLoss) { 1533 checkStateLoss(); 1534 } 1535 synchronized (this) { 1536 if (mDestroyed || mHost == null) { 1537 throw new IllegalStateException("Activity has been destroyed"); 1538 } 1539 if (mPendingActions == null) { 1540 mPendingActions = new ArrayList<Runnable>(); 1541 } 1542 mPendingActions.add(action); 1543 if (mPendingActions.size() == 1) { 1544 mHost.getHandler().removeCallbacks(mExecCommit); 1545 mHost.getHandler().post(mExecCommit); 1546 } 1547 } 1548 } 1549 1550 public int allocBackStackIndex(BackStackRecord bse) { 1551 synchronized (this) { 1552 if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { 1553 if (mBackStackIndices == null) { 1554 mBackStackIndices = new ArrayList<BackStackRecord>(); 1555 } 1556 int index = mBackStackIndices.size(); 1557 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1558 mBackStackIndices.add(bse); 1559 return index; 1560 1561 } else { 1562 int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); 1563 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1564 mBackStackIndices.set(index, bse); 1565 return index; 1566 } 1567 } 1568 } 1569 1570 public void setBackStackIndex(int index, BackStackRecord bse) { 1571 synchronized (this) { 1572 if (mBackStackIndices == null) { 1573 mBackStackIndices = new ArrayList<BackStackRecord>(); 1574 } 1575 int N = mBackStackIndices.size(); 1576 if (index < N) { 1577 if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); 1578 mBackStackIndices.set(index, bse); 1579 } else { 1580 while (N < index) { 1581 mBackStackIndices.add(null); 1582 if (mAvailBackStackIndices == null) { 1583 mAvailBackStackIndices = new ArrayList<Integer>(); 1584 } 1585 if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); 1586 mAvailBackStackIndices.add(N); 1587 N++; 1588 } 1589 if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); 1590 mBackStackIndices.add(bse); 1591 } 1592 } 1593 } 1594 1595 public void freeBackStackIndex(int index) { 1596 synchronized (this) { 1597 mBackStackIndices.set(index, null); 1598 if (mAvailBackStackIndices == null) { 1599 mAvailBackStackIndices = new ArrayList<Integer>(); 1600 } 1601 if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); 1602 mAvailBackStackIndices.add(index); 1603 } 1604 } 1605 1606 public void execSingleAction(Runnable action, boolean allowStateLoss) { 1607 if (mExecutingActions) { 1608 throw new IllegalStateException("FragmentManager is already executing transactions"); 1609 } 1610 1611 if (Looper.myLooper() != mHost.getHandler().getLooper()) { 1612 throw new IllegalStateException("Must be called from main thread of fragment host"); 1613 } 1614 1615 if (!allowStateLoss) { 1616 checkStateLoss(); 1617 } 1618 1619 mExecutingActions = true; 1620 action.run(); 1621 mExecutingActions = false; 1622 1623 doPendingDeferredStart(); 1624 } 1625 1626 /** 1627 * Only call from main thread! 1628 */ 1629 public boolean execPendingActions() { 1630 if (mExecutingActions) { 1631 throw new IllegalStateException("FragmentManager is already executing transactions"); 1632 } 1633 1634 if (Looper.myLooper() != mHost.getHandler().getLooper()) { 1635 throw new IllegalStateException("Must be called from main thread of fragment host"); 1636 } 1637 1638 boolean didSomething = false; 1639 1640 while (true) { 1641 int numActions; 1642 1643 synchronized (this) { 1644 if (mPendingActions == null || mPendingActions.size() == 0) { 1645 break; 1646 } 1647 1648 numActions = mPendingActions.size(); 1649 if (mTmpActions == null || mTmpActions.length < numActions) { 1650 mTmpActions = new Runnable[numActions]; 1651 } 1652 mPendingActions.toArray(mTmpActions); 1653 mPendingActions.clear(); 1654 mHost.getHandler().removeCallbacks(mExecCommit); 1655 } 1656 1657 mExecutingActions = true; 1658 for (int i=0; i<numActions; i++) { 1659 mTmpActions[i].run(); 1660 mTmpActions[i] = null; 1661 } 1662 mExecutingActions = false; 1663 didSomething = true; 1664 } 1665 1666 doPendingDeferredStart(); 1667 1668 return didSomething; 1669 } 1670 1671 void doPendingDeferredStart() { 1672 if (mHavePendingDeferredStart) { 1673 boolean loadersRunning = false; 1674 for (int i = 0; i < mActive.size(); i++) { 1675 Fragment f = mActive.get(i); 1676 if (f != null && f.mLoaderManager != null) { 1677 loadersRunning |= f.mLoaderManager.hasRunningLoaders(); 1678 } 1679 } 1680 if (!loadersRunning) { 1681 mHavePendingDeferredStart = false; 1682 startPendingDeferredFragments(); 1683 } 1684 } 1685 } 1686 1687 void reportBackStackChanged() { 1688 if (mBackStackChangeListeners != null) { 1689 for (int i=0; i<mBackStackChangeListeners.size(); i++) { 1690 mBackStackChangeListeners.get(i).onBackStackChanged(); 1691 } 1692 } 1693 } 1694 1695 void addBackStackState(BackStackRecord state) { 1696 if (mBackStack == null) { 1697 mBackStack = new ArrayList<BackStackRecord>(); 1698 } 1699 mBackStack.add(state); 1700 reportBackStackChanged(); 1701 } 1702 1703 @SuppressWarnings("unused") 1704 boolean popBackStackState(Handler handler, String name, int id, int flags) { 1705 if (mBackStack == null) { 1706 return false; 1707 } 1708 if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) { 1709 int last = mBackStack.size()-1; 1710 if (last < 0) { 1711 return false; 1712 } 1713 final BackStackRecord bss = mBackStack.remove(last); 1714 SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); 1715 SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); 1716 if (mCurState >= Fragment.CREATED) { 1717 bss.calculateBackFragments(firstOutFragments, lastInFragments); 1718 } 1719 bss.popFromBackStack(true, null, firstOutFragments, lastInFragments); 1720 reportBackStackChanged(); 1721 } else { 1722 int index = -1; 1723 if (name != null || id >= 0) { 1724 // If a name or ID is specified, look for that place in 1725 // the stack. 1726 index = mBackStack.size()-1; 1727 while (index >= 0) { 1728 BackStackRecord bss = mBackStack.get(index); 1729 if (name != null && name.equals(bss.getName())) { 1730 break; 1731 } 1732 if (id >= 0 && id == bss.mIndex) { 1733 break; 1734 } 1735 index--; 1736 } 1737 if (index < 0) { 1738 return false; 1739 } 1740 if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) { 1741 index--; 1742 // Consume all following entries that match. 1743 while (index >= 0) { 1744 BackStackRecord bss = mBackStack.get(index); 1745 if ((name != null && name.equals(bss.getName())) 1746 || (id >= 0 && id == bss.mIndex)) { 1747 index--; 1748 continue; 1749 } 1750 break; 1751 } 1752 } 1753 } 1754 if (index == mBackStack.size()-1) { 1755 return false; 1756 } 1757 final ArrayList<BackStackRecord> states 1758 = new ArrayList<BackStackRecord>(); 1759 for (int i=mBackStack.size()-1; i>index; i--) { 1760 states.add(mBackStack.remove(i)); 1761 } 1762 final int LAST = states.size()-1; 1763 SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); 1764 SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); 1765 if (mCurState >= Fragment.CREATED) { 1766 for (int i = 0; i <= LAST; i++) { 1767 states.get(i).calculateBackFragments(firstOutFragments, lastInFragments); 1768 } 1769 } 1770 BackStackRecord.TransitionState state = null; 1771 for (int i=0; i<=LAST; i++) { 1772 if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i)); 1773 state = states.get(i).popFromBackStack(i == LAST, state, 1774 firstOutFragments, lastInFragments); 1775 } 1776 reportBackStackChanged(); 1777 } 1778 return true; 1779 } 1780 1781 FragmentManagerNonConfig retainNonConfig() { 1782 ArrayList<Fragment> fragments = null; 1783 ArrayList<FragmentManagerNonConfig> childFragments = null; 1784 if (mActive != null) { 1785 for (int i=0; i<mActive.size(); i++) { 1786 Fragment f = mActive.get(i); 1787 if (f != null) { 1788 if (f.mRetainInstance) { 1789 if (fragments == null) { 1790 fragments = new ArrayList<Fragment>(); 1791 } 1792 fragments.add(f); 1793 f.mRetaining = true; 1794 f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1; 1795 if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f); 1796 } 1797 boolean addedChild = false; 1798 if (f.mChildFragmentManager != null) { 1799 FragmentManagerNonConfig child = f.mChildFragmentManager.retainNonConfig(); 1800 if (child != null) { 1801 if (childFragments == null) { 1802 childFragments = new ArrayList<FragmentManagerNonConfig>(); 1803 for (int j = 0; j < i; j++) { 1804 childFragments.add(null); 1805 } 1806 } 1807 childFragments.add(child); 1808 addedChild = true; 1809 } 1810 } 1811 if (childFragments != null && !addedChild) { 1812 childFragments.add(null); 1813 } 1814 } 1815 } 1816 } 1817 if (fragments == null && childFragments == null) { 1818 return null; 1819 } 1820 return new FragmentManagerNonConfig(fragments, childFragments); 1821 } 1822 1823 void saveFragmentViewState(Fragment f) { 1824 if (f.mInnerView == null) { 1825 return; 1826 } 1827 if (mStateArray == null) { 1828 mStateArray = new SparseArray<Parcelable>(); 1829 } else { 1830 mStateArray.clear(); 1831 } 1832 f.mInnerView.saveHierarchyState(mStateArray); 1833 if (mStateArray.size() > 0) { 1834 f.mSavedViewState = mStateArray; 1835 mStateArray = null; 1836 } 1837 } 1838 1839 Bundle saveFragmentBasicState(Fragment f) { 1840 Bundle result = null; 1841 1842 if (mStateBundle == null) { 1843 mStateBundle = new Bundle(); 1844 } 1845 f.performSaveInstanceState(mStateBundle); 1846 if (!mStateBundle.isEmpty()) { 1847 result = mStateBundle; 1848 mStateBundle = null; 1849 } 1850 1851 if (f.mView != null) { 1852 saveFragmentViewState(f); 1853 } 1854 if (f.mSavedViewState != null) { 1855 if (result == null) { 1856 result = new Bundle(); 1857 } 1858 result.putSparseParcelableArray( 1859 FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); 1860 } 1861 if (!f.mUserVisibleHint) { 1862 if (result == null) { 1863 result = new Bundle(); 1864 } 1865 // Only add this if it's not the default value 1866 result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); 1867 } 1868 1869 return result; 1870 } 1871 1872 Parcelable saveAllState() { 1873 // Make sure all pending operations have now been executed to get 1874 // our state update-to-date. 1875 execPendingActions(); 1876 1877 if (HONEYCOMB) { 1878 // As of Honeycomb, we save state after pausing. Prior to that 1879 // it is before pausing. With fragments this is an issue, since 1880 // there are many things you may do after pausing but before 1881 // stopping that change the fragment state. For those older 1882 // devices, we will not at this point say that we have saved 1883 // the state, so we will allow them to continue doing fragment 1884 // transactions. This retains the same semantics as Honeycomb, 1885 // though you do have the risk of losing the very most recent state 1886 // if the process is killed... we'll live with that. 1887 mStateSaved = true; 1888 } 1889 1890 if (mActive == null || mActive.size() <= 0) { 1891 return null; 1892 } 1893 1894 // First collect all active fragments. 1895 int N = mActive.size(); 1896 FragmentState[] active = new FragmentState[N]; 1897 boolean haveFragments = false; 1898 for (int i=0; i<N; i++) { 1899 Fragment f = mActive.get(i); 1900 if (f != null) { 1901 if (f.mIndex < 0) { 1902 throwException(new IllegalStateException( 1903 "Failure saving state: active " + f 1904 + " has cleared index: " + f.mIndex)); 1905 } 1906 1907 haveFragments = true; 1908 1909 FragmentState fs = new FragmentState(f); 1910 active[i] = fs; 1911 1912 if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) { 1913 fs.mSavedFragmentState = saveFragmentBasicState(f); 1914 1915 if (f.mTarget != null) { 1916 if (f.mTarget.mIndex < 0) { 1917 throwException(new IllegalStateException( 1918 "Failure saving state: " + f 1919 + " has target not in fragment manager: " + f.mTarget)); 1920 } 1921 if (fs.mSavedFragmentState == null) { 1922 fs.mSavedFragmentState = new Bundle(); 1923 } 1924 putFragment(fs.mSavedFragmentState, 1925 FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget); 1926 if (f.mTargetRequestCode != 0) { 1927 fs.mSavedFragmentState.putInt( 1928 FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 1929 f.mTargetRequestCode); 1930 } 1931 } 1932 1933 } else { 1934 fs.mSavedFragmentState = f.mSavedFragmentState; 1935 } 1936 1937 if (DEBUG) Log.v(TAG, "Saved state of " + f + ": " 1938 + fs.mSavedFragmentState); 1939 } 1940 } 1941 1942 if (!haveFragments) { 1943 if (DEBUG) Log.v(TAG, "saveAllState: no fragments!"); 1944 return null; 1945 } 1946 1947 int[] added = null; 1948 BackStackState[] backStack = null; 1949 1950 // Build list of currently added fragments. 1951 if (mAdded != null) { 1952 N = mAdded.size(); 1953 if (N > 0) { 1954 added = new int[N]; 1955 for (int i=0; i<N; i++) { 1956 added[i] = mAdded.get(i).mIndex; 1957 if (added[i] < 0) { 1958 throwException(new IllegalStateException( 1959 "Failure saving state: active " + mAdded.get(i) 1960 + " has cleared index: " + added[i])); 1961 } 1962 if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i 1963 + ": " + mAdded.get(i)); 1964 } 1965 } 1966 } 1967 1968 // Now save back stack. 1969 if (mBackStack != null) { 1970 N = mBackStack.size(); 1971 if (N > 0) { 1972 backStack = new BackStackState[N]; 1973 for (int i=0; i<N; i++) { 1974 backStack[i] = new BackStackState(mBackStack.get(i)); 1975 if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i 1976 + ": " + mBackStack.get(i)); 1977 } 1978 } 1979 } 1980 1981 FragmentManagerState fms = new FragmentManagerState(); 1982 fms.mActive = active; 1983 fms.mAdded = added; 1984 fms.mBackStack = backStack; 1985 return fms; 1986 } 1987 1988 void restoreAllState(Parcelable state, FragmentManagerNonConfig nonConfig) { 1989 // If there is no saved state at all, then there can not be 1990 // any nonConfig fragments either, so that is that. 1991 if (state == null) return; 1992 FragmentManagerState fms = (FragmentManagerState)state; 1993 if (fms.mActive == null) return; 1994 1995 List<FragmentManagerNonConfig> childNonConfigs = null; 1996 1997 // First re-attach any non-config instances we are retaining back 1998 // to their saved state, so we don't try to instantiate them again. 1999 if (nonConfig != null) { 2000 List<Fragment> nonConfigFragments = nonConfig.getFragments(); 2001 childNonConfigs = nonConfig.getChildNonConfigs(); 2002 final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; 2003 for (int i = 0; i < count; i++) { 2004 Fragment f = nonConfigFragments.get(i); 2005 if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f); 2006 FragmentState fs = fms.mActive[f.mIndex]; 2007 fs.mInstance = f; 2008 f.mSavedViewState = null; 2009 f.mBackStackNesting = 0; 2010 f.mInLayout = false; 2011 f.mAdded = false; 2012 f.mTarget = null; 2013 if (fs.mSavedFragmentState != null) { 2014 fs.mSavedFragmentState.setClassLoader(mHost.getContext().getClassLoader()); 2015 f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( 2016 FragmentManagerImpl.VIEW_STATE_TAG); 2017 f.mSavedFragmentState = fs.mSavedFragmentState; 2018 } 2019 } 2020 } 2021 2022 // Build the full list of active fragments, instantiating them from 2023 // their saved state. 2024 mActive = new ArrayList<>(fms.mActive.length); 2025 if (mAvailIndices != null) { 2026 mAvailIndices.clear(); 2027 } 2028 for (int i=0; i<fms.mActive.length; i++) { 2029 FragmentState fs = fms.mActive[i]; 2030 if (fs != null) { 2031 FragmentManagerNonConfig childNonConfig = null; 2032 if (childNonConfigs != null && i < childNonConfigs.size()) { 2033 childNonConfig = childNonConfigs.get(i); 2034 } 2035 Fragment f = fs.instantiate(mHost, mParent, childNonConfig); 2036 if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f); 2037 mActive.add(f); 2038 // Now that the fragment is instantiated (or came from being 2039 // retained above), clear mInstance in case we end up re-restoring 2040 // from this FragmentState again. 2041 fs.mInstance = null; 2042 } else { 2043 mActive.add(null); 2044 if (mAvailIndices == null) { 2045 mAvailIndices = new ArrayList<Integer>(); 2046 } 2047 if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i); 2048 mAvailIndices.add(i); 2049 } 2050 } 2051 2052 // Update the target of all retained fragments. 2053 if (nonConfig != null) { 2054 List<Fragment> nonConfigFragments = nonConfig.getFragments(); 2055 final int count = nonConfigFragments != null ? nonConfigFragments.size() : 0; 2056 for (int i = 0; i < count; i++) { 2057 Fragment f = nonConfigFragments.get(i); 2058 if (f.mTargetIndex >= 0) { 2059 if (f.mTargetIndex < mActive.size()) { 2060 f.mTarget = mActive.get(f.mTargetIndex); 2061 } else { 2062 Log.w(TAG, "Re-attaching retained fragment " + f 2063 + " target no longer exists: " + f.mTargetIndex); 2064 f.mTarget = null; 2065 } 2066 } 2067 } 2068 } 2069 2070 // Build the list of currently added fragments. 2071 if (fms.mAdded != null) { 2072 mAdded = new ArrayList<Fragment>(fms.mAdded.length); 2073 for (int i=0; i<fms.mAdded.length; i++) { 2074 Fragment f = mActive.get(fms.mAdded[i]); 2075 if (f == null) { 2076 throwException(new IllegalStateException( 2077 "No instantiated fragment for index #" + fms.mAdded[i])); 2078 } 2079 f.mAdded = true; 2080 if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f); 2081 if (mAdded.contains(f)) { 2082 throw new IllegalStateException("Already added!"); 2083 } 2084 mAdded.add(f); 2085 } 2086 } else { 2087 mAdded = null; 2088 } 2089 2090 // Build the back stack. 2091 if (fms.mBackStack != null) { 2092 mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length); 2093 for (int i=0; i<fms.mBackStack.length; i++) { 2094 BackStackRecord bse = fms.mBackStack[i].instantiate(this); 2095 if (DEBUG) { 2096 Log.v(TAG, "restoreAllState: back stack #" + i 2097 + " (index " + bse.mIndex + "): " + bse); 2098 LogWriter logw = new LogWriter(TAG); 2099 PrintWriter pw = new PrintWriter(logw); 2100 bse.dump(" ", pw, false); 2101 } 2102 mBackStack.add(bse); 2103 if (bse.mIndex >= 0) { 2104 setBackStackIndex(bse.mIndex, bse); 2105 } 2106 } 2107 } else { 2108 mBackStack = null; 2109 } 2110 } 2111 2112 public void attachController(FragmentHostCallback host, 2113 FragmentContainer container, Fragment parent) { 2114 if (mHost != null) throw new IllegalStateException("Already attached"); 2115 mHost = host; 2116 mContainer = container; 2117 mParent = parent; 2118 } 2119 2120 public void noteStateNotSaved() { 2121 mStateSaved = false; 2122 } 2123 2124 public void dispatchCreate() { 2125 mStateSaved = false; 2126 moveToState(Fragment.CREATED, false); 2127 } 2128 2129 public void dispatchActivityCreated() { 2130 mStateSaved = false; 2131 moveToState(Fragment.ACTIVITY_CREATED, false); 2132 } 2133 2134 public void dispatchStart() { 2135 mStateSaved = false; 2136 moveToState(Fragment.STARTED, false); 2137 } 2138 2139 public void dispatchResume() { 2140 mStateSaved = false; 2141 moveToState(Fragment.RESUMED, false); 2142 } 2143 2144 public void dispatchPause() { 2145 moveToState(Fragment.STARTED, false); 2146 } 2147 2148 public void dispatchStop() { 2149 // See saveAllState() for the explanation of this. We do this for 2150 // all platform versions, to keep our behavior more consistent between 2151 // them. 2152 mStateSaved = true; 2153 2154 moveToState(Fragment.STOPPED, false); 2155 } 2156 2157 public void dispatchReallyStop() { 2158 moveToState(Fragment.ACTIVITY_CREATED, false); 2159 } 2160 2161 public void dispatchDestroyView() { 2162 moveToState(Fragment.CREATED, false); 2163 } 2164 2165 public void dispatchDestroy() { 2166 mDestroyed = true; 2167 execPendingActions(); 2168 moveToState(Fragment.INITIALIZING, false); 2169 mHost = null; 2170 mContainer = null; 2171 mParent = null; 2172 } 2173 2174 public void dispatchMultiWindowModeChanged(boolean isInMultiWindowMode) { 2175 if (mAdded == null) { 2176 return; 2177 } 2178 for (int i = mAdded.size() - 1; i >= 0; --i) { 2179 final android.support.v4.app.Fragment f = mAdded.get(i); 2180 if (f != null) { 2181 f.performMultiWindowModeChanged(isInMultiWindowMode); 2182 } 2183 } 2184 } 2185 2186 public void dispatchPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 2187 if (mAdded == null) { 2188 return; 2189 } 2190 for (int i = mAdded.size() - 1; i >= 0; --i) { 2191 final android.support.v4.app.Fragment f = mAdded.get(i); 2192 if (f != null) { 2193 f.performPictureInPictureModeChanged(isInPictureInPictureMode); 2194 } 2195 } 2196 } 2197 2198 public void dispatchConfigurationChanged(Configuration newConfig) { 2199 if (mAdded != null) { 2200 for (int i=0; i<mAdded.size(); i++) { 2201 Fragment f = mAdded.get(i); 2202 if (f != null) { 2203 f.performConfigurationChanged(newConfig); 2204 } 2205 } 2206 } 2207 } 2208 2209 public void dispatchLowMemory() { 2210 if (mAdded != null) { 2211 for (int i=0; i<mAdded.size(); i++) { 2212 Fragment f = mAdded.get(i); 2213 if (f != null) { 2214 f.performLowMemory(); 2215 } 2216 } 2217 } 2218 } 2219 2220 public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { 2221 boolean show = false; 2222 ArrayList<Fragment> newMenus = null; 2223 if (mAdded != null) { 2224 for (int i=0; i<mAdded.size(); i++) { 2225 Fragment f = mAdded.get(i); 2226 if (f != null) { 2227 if (f.performCreateOptionsMenu(menu, inflater)) { 2228 show = true; 2229 if (newMenus == null) { 2230 newMenus = new ArrayList<Fragment>(); 2231 } 2232 newMenus.add(f); 2233 } 2234 } 2235 } 2236 } 2237 2238 if (mCreatedMenus != null) { 2239 for (int i=0; i<mCreatedMenus.size(); i++) { 2240 Fragment f = mCreatedMenus.get(i); 2241 if (newMenus == null || !newMenus.contains(f)) { 2242 f.onDestroyOptionsMenu(); 2243 } 2244 } 2245 } 2246 2247 mCreatedMenus = newMenus; 2248 2249 return show; 2250 } 2251 2252 public boolean dispatchPrepareOptionsMenu(Menu menu) { 2253 boolean show = false; 2254 if (mAdded != null) { 2255 for (int i=0; i<mAdded.size(); i++) { 2256 Fragment f = mAdded.get(i); 2257 if (f != null) { 2258 if (f.performPrepareOptionsMenu(menu)) { 2259 show = true; 2260 } 2261 } 2262 } 2263 } 2264 return show; 2265 } 2266 2267 public boolean dispatchOptionsItemSelected(MenuItem item) { 2268 if (mAdded != null) { 2269 for (int i=0; i<mAdded.size(); i++) { 2270 Fragment f = mAdded.get(i); 2271 if (f != null) { 2272 if (f.performOptionsItemSelected(item)) { 2273 return true; 2274 } 2275 } 2276 } 2277 } 2278 return false; 2279 } 2280 2281 public boolean dispatchContextItemSelected(MenuItem item) { 2282 if (mAdded != null) { 2283 for (int i=0; i<mAdded.size(); i++) { 2284 Fragment f = mAdded.get(i); 2285 if (f != null) { 2286 if (f.performContextItemSelected(item)) { 2287 return true; 2288 } 2289 } 2290 } 2291 } 2292 return false; 2293 } 2294 2295 public void dispatchOptionsMenuClosed(Menu menu) { 2296 if (mAdded != null) { 2297 for (int i=0; i<mAdded.size(); i++) { 2298 Fragment f = mAdded.get(i); 2299 if (f != null) { 2300 f.performOptionsMenuClosed(menu); 2301 } 2302 } 2303 } 2304 } 2305 2306 public static int reverseTransit(int transit) { 2307 int rev = 0; 2308 switch (transit) { 2309 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 2310 rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE; 2311 break; 2312 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 2313 rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN; 2314 break; 2315 case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 2316 rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE; 2317 break; 2318 } 2319 return rev; 2320 2321 } 2322 2323 public static final int ANIM_STYLE_OPEN_ENTER = 1; 2324 public static final int ANIM_STYLE_OPEN_EXIT = 2; 2325 public static final int ANIM_STYLE_CLOSE_ENTER = 3; 2326 public static final int ANIM_STYLE_CLOSE_EXIT = 4; 2327 public static final int ANIM_STYLE_FADE_ENTER = 5; 2328 public static final int ANIM_STYLE_FADE_EXIT = 6; 2329 2330 public static int transitToStyleIndex(int transit, boolean enter) { 2331 int animAttr = -1; 2332 switch (transit) { 2333 case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: 2334 animAttr = enter ? ANIM_STYLE_OPEN_ENTER : ANIM_STYLE_OPEN_EXIT; 2335 break; 2336 case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: 2337 animAttr = enter ? ANIM_STYLE_CLOSE_ENTER : ANIM_STYLE_CLOSE_EXIT; 2338 break; 2339 case FragmentTransaction.TRANSIT_FRAGMENT_FADE: 2340 animAttr = enter ? ANIM_STYLE_FADE_ENTER : ANIM_STYLE_FADE_EXIT; 2341 break; 2342 } 2343 return animAttr; 2344 } 2345 2346 @Override 2347 public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 2348 if (!"fragment".equals(name)) { 2349 return null; 2350 } 2351 2352 String fname = attrs.getAttributeValue(null, "class"); 2353 TypedArray a = context.obtainStyledAttributes(attrs, FragmentTag.Fragment); 2354 if (fname == null) { 2355 fname = a.getString(FragmentTag.Fragment_name); 2356 } 2357 int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID); 2358 String tag = a.getString(FragmentTag.Fragment_tag); 2359 a.recycle(); 2360 2361 if (!Fragment.isSupportFragmentClass(mHost.getContext(), fname)) { 2362 // Invalid support lib fragment; let the device's framework handle it. 2363 // This will allow android.app.Fragments to do the right thing. 2364 return null; 2365 } 2366 2367 int containerId = parent != null ? parent.getId() : 0; 2368 if (containerId == View.NO_ID && id == View.NO_ID && tag == null) { 2369 throw new IllegalArgumentException(attrs.getPositionDescription() 2370 + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname); 2371 } 2372 2373 // If we restored from a previous state, we may already have 2374 // instantiated this fragment from the state and should use 2375 // that instance instead of making a new one. 2376 Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null; 2377 if (fragment == null && tag != null) { 2378 fragment = findFragmentByTag(tag); 2379 } 2380 if (fragment == null && containerId != View.NO_ID) { 2381 fragment = findFragmentById(containerId); 2382 } 2383 2384 if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x" 2385 + Integer.toHexString(id) + " fname=" + fname 2386 + " existing=" + fragment); 2387 if (fragment == null) { 2388 fragment = Fragment.instantiate(context, fname); 2389 fragment.mFromLayout = true; 2390 fragment.mFragmentId = id != 0 ? id : containerId; 2391 fragment.mContainerId = containerId; 2392 fragment.mTag = tag; 2393 fragment.mInLayout = true; 2394 fragment.mFragmentManager = this; 2395 fragment.mHost = mHost; 2396 fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); 2397 addFragment(fragment, true); 2398 2399 } else if (fragment.mInLayout) { 2400 // A fragment already exists and it is not one we restored from 2401 // previous state. 2402 throw new IllegalArgumentException(attrs.getPositionDescription() 2403 + ": Duplicate id 0x" + Integer.toHexString(id) 2404 + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId) 2405 + " with another fragment for " + fname); 2406 } else { 2407 // This fragment was retained from a previous instance; get it 2408 // going now. 2409 fragment.mInLayout = true; 2410 fragment.mHost = mHost; 2411 // If this fragment is newly instantiated (either right now, or 2412 // from last saved state), then give it the attributes to 2413 // initialize itself. 2414 if (!fragment.mRetaining) { 2415 fragment.onInflate(mHost.getContext(), attrs, fragment.mSavedFragmentState); 2416 } 2417 } 2418 2419 // If we haven't finished entering the CREATED state ourselves yet, 2420 // push the inflated child fragment along. 2421 if (mCurState < Fragment.CREATED && fragment.mFromLayout) { 2422 moveToState(fragment, Fragment.CREATED, 0, 0, false); 2423 } else { 2424 moveToState(fragment); 2425 } 2426 2427 if (fragment.mView == null) { 2428 throw new IllegalStateException("Fragment " + fname 2429 + " did not create a view."); 2430 } 2431 if (id != 0) { 2432 fragment.mView.setId(id); 2433 } 2434 if (fragment.mView.getTag() == null) { 2435 fragment.mView.setTag(tag); 2436 } 2437 return fragment.mView; 2438 } 2439 2440 LayoutInflaterFactory getLayoutInflaterFactory() { 2441 return this; 2442 } 2443 2444 static class FragmentTag { 2445 public static final int[] Fragment = { 2446 0x01010003, 0x010100d0, 0x010100d1 2447 }; 2448 public static final int Fragment_id = 1; 2449 public static final int Fragment_name = 0; 2450 public static final int Fragment_tag = 2; 2451 } 2452} 2453