Fragment.java revision a0c2dc3cf56bf9c90af517f4cd12ef80d375a85a
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.app; 18 19import android.animation.Animator; 20import android.annotation.NonNull; 21import android.annotation.Nullable; 22import android.annotation.StringRes; 23import android.content.ComponentCallbacks2; 24import android.content.Context; 25import android.content.Intent; 26import android.content.IntentSender; 27import android.content.res.Configuration; 28import android.content.res.Resources; 29import android.content.res.TypedArray; 30import android.os.Build; 31import android.os.Bundle; 32import android.os.Parcel; 33import android.os.Parcelable; 34import android.transition.Transition; 35import android.transition.TransitionInflater; 36import android.transition.TransitionSet; 37import android.util.AndroidRuntimeException; 38import android.util.ArrayMap; 39import android.util.AttributeSet; 40import android.util.DebugUtils; 41import android.util.Log; 42import android.util.SparseArray; 43import android.util.SuperNotCalledException; 44import android.view.ContextMenu; 45import android.view.ContextMenu.ContextMenuInfo; 46import android.view.LayoutInflater; 47import android.view.Menu; 48import android.view.MenuInflater; 49import android.view.MenuItem; 50import android.view.View; 51import android.view.View.OnCreateContextMenuListener; 52import android.view.ViewGroup; 53import android.widget.AdapterView; 54 55import java.io.FileDescriptor; 56import java.io.PrintWriter; 57 58final class FragmentState implements Parcelable { 59 final String mClassName; 60 final int mIndex; 61 final boolean mFromLayout; 62 final int mFragmentId; 63 final int mContainerId; 64 final String mTag; 65 final boolean mRetainInstance; 66 final boolean mDetached; 67 final Bundle mArguments; 68 final boolean mHidden; 69 70 Bundle mSavedFragmentState; 71 72 Fragment mInstance; 73 74 public FragmentState(Fragment frag) { 75 mClassName = frag.getClass().getName(); 76 mIndex = frag.mIndex; 77 mFromLayout = frag.mFromLayout; 78 mFragmentId = frag.mFragmentId; 79 mContainerId = frag.mContainerId; 80 mTag = frag.mTag; 81 mRetainInstance = frag.mRetainInstance; 82 mDetached = frag.mDetached; 83 mArguments = frag.mArguments; 84 mHidden = frag.mHidden; 85 } 86 87 public FragmentState(Parcel in) { 88 mClassName = in.readString(); 89 mIndex = in.readInt(); 90 mFromLayout = in.readInt() != 0; 91 mFragmentId = in.readInt(); 92 mContainerId = in.readInt(); 93 mTag = in.readString(); 94 mRetainInstance = in.readInt() != 0; 95 mDetached = in.readInt() != 0; 96 mArguments = in.readBundle(); 97 mHidden = in.readInt() != 0; 98 mSavedFragmentState = in.readBundle(); 99 } 100 101 public Fragment instantiate(FragmentHostCallback host, Fragment parent, 102 FragmentManagerNonConfig childNonConfig) { 103 if (mInstance == null) { 104 final Context context = host.getContext(); 105 if (mArguments != null) { 106 mArguments.setClassLoader(context.getClassLoader()); 107 } 108 109 mInstance = Fragment.instantiate(context, mClassName, mArguments); 110 111 if (mSavedFragmentState != null) { 112 mSavedFragmentState.setClassLoader(context.getClassLoader()); 113 mInstance.mSavedFragmentState = mSavedFragmentState; 114 } 115 mInstance.setIndex(mIndex, parent); 116 mInstance.mFromLayout = mFromLayout; 117 mInstance.mRestored = true; 118 mInstance.mFragmentId = mFragmentId; 119 mInstance.mContainerId = mContainerId; 120 mInstance.mTag = mTag; 121 mInstance.mRetainInstance = mRetainInstance; 122 mInstance.mDetached = mDetached; 123 mInstance.mHidden = mHidden; 124 mInstance.mFragmentManager = host.mFragmentManager; 125 if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG, 126 "Instantiated fragment " + mInstance); 127 } 128 mInstance.mChildNonConfig = childNonConfig; 129 return mInstance; 130 } 131 132 public int describeContents() { 133 return 0; 134 } 135 136 public void writeToParcel(Parcel dest, int flags) { 137 dest.writeString(mClassName); 138 dest.writeInt(mIndex); 139 dest.writeInt(mFromLayout ? 1 : 0); 140 dest.writeInt(mFragmentId); 141 dest.writeInt(mContainerId); 142 dest.writeString(mTag); 143 dest.writeInt(mRetainInstance ? 1 : 0); 144 dest.writeInt(mDetached ? 1 : 0); 145 dest.writeBundle(mArguments); 146 dest.writeInt(mHidden ? 1 : 0); 147 dest.writeBundle(mSavedFragmentState); 148 } 149 150 public static final Parcelable.Creator<FragmentState> CREATOR 151 = new Parcelable.Creator<FragmentState>() { 152 public FragmentState createFromParcel(Parcel in) { 153 return new FragmentState(in); 154 } 155 156 public FragmentState[] newArray(int size) { 157 return new FragmentState[size]; 158 } 159 }; 160} 161 162/** 163 * A Fragment is a piece of an application's user interface or behavior 164 * that can be placed in an {@link Activity}. Interaction with fragments 165 * is done through {@link FragmentManager}, which can be obtained via 166 * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and 167 * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}. 168 * 169 * <p>The Fragment class can be used many ways to achieve a wide variety of 170 * results. In its core, it represents a particular operation or interface 171 * that is running within a larger {@link Activity}. A Fragment is closely 172 * tied to the Activity it is in, and can not be used apart from one. Though 173 * Fragment defines its own lifecycle, that lifecycle is dependent on its 174 * activity: if the activity is stopped, no fragments inside of it can be 175 * started; when the activity is destroyed, all fragments will be destroyed. 176 * 177 * <p>All subclasses of Fragment must include a public no-argument constructor. 178 * The framework will often re-instantiate a fragment class when needed, 179 * in particular during state restore, and needs to be able to find this 180 * constructor to instantiate it. If the no-argument constructor is not 181 * available, a runtime exception will occur in some cases during state 182 * restore. 183 * 184 * <p>Topics covered here: 185 * <ol> 186 * <li><a href="#OlderPlatforms">Older Platforms</a> 187 * <li><a href="#Lifecycle">Lifecycle</a> 188 * <li><a href="#Layout">Layout</a> 189 * <li><a href="#BackStack">Back Stack</a> 190 * </ol> 191 * 192 * <div class="special reference"> 193 * <h3>Developer Guides</h3> 194 * <p>For more information about using fragments, read the 195 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p> 196 * </div> 197 * 198 * <a name="OlderPlatforms"></a> 199 * <h3>Older Platforms</h3> 200 * 201 * While the Fragment API was introduced in 202 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API 203 * at is also available for use on older platforms through 204 * {@link android.support.v4.app.FragmentActivity}. See the blog post 205 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html"> 206 * Fragments For All</a> for more details. 207 * 208 * <a name="Lifecycle"></a> 209 * <h3>Lifecycle</h3> 210 * 211 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has 212 * its own wrinkle on the standard activity lifecycle. It includes basic 213 * activity lifecycle methods such as {@link #onResume}, but also important 214 * are methods related to interactions with the activity and UI generation. 215 * 216 * <p>The core series of lifecycle methods that are called to bring a fragment 217 * up to resumed state (interacting with the user) are: 218 * 219 * <ol> 220 * <li> {@link #onAttach} called once the fragment is associated with its activity. 221 * <li> {@link #onCreate} called to do initial creation of the fragment. 222 * <li> {@link #onCreateView} creates and returns the view hierarchy associated 223 * with the fragment. 224 * <li> {@link #onActivityCreated} tells the fragment that its activity has 225 * completed its own {@link Activity#onCreate Activity.onCreate()}. 226 * <li> {@link #onViewStateRestored} tells the fragment that all of the saved 227 * state of its view hierarchy has been restored. 228 * <li> {@link #onStart} makes the fragment visible to the user (based on its 229 * containing activity being started). 230 * <li> {@link #onResume} makes the fragment begin interacting with the user 231 * (based on its containing activity being resumed). 232 * </ol> 233 * 234 * <p>As a fragment is no longer being used, it goes through a reverse 235 * series of callbacks: 236 * 237 * <ol> 238 * <li> {@link #onPause} fragment is no longer interacting with the user either 239 * because its activity is being paused or a fragment operation is modifying it 240 * in the activity. 241 * <li> {@link #onStop} fragment is no longer visible to the user either 242 * because its activity is being stopped or a fragment operation is modifying it 243 * in the activity. 244 * <li> {@link #onDestroyView} allows the fragment to clean up resources 245 * associated with its View. 246 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state. 247 * <li> {@link #onDetach} called immediately prior to the fragment no longer 248 * being associated with its activity. 249 * </ol> 250 * 251 * <a name="Layout"></a> 252 * <h3>Layout</h3> 253 * 254 * <p>Fragments can be used as part of your application's layout, allowing 255 * you to better modularize your code and more easily adjust your user 256 * interface to the screen it is running on. As an example, we can look 257 * at a simple program consisting of a list of items, and display of the 258 * details of each item.</p> 259 * 260 * <p>An activity's layout XML can include <code><fragment></code> tags 261 * to embed fragment instances inside of the layout. For example, here is 262 * a simple layout that embeds one fragment:</p> 263 * 264 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout} 265 * 266 * <p>The layout is installed in the activity in the normal way:</p> 267 * 268 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 269 * main} 270 * 271 * <p>The titles fragment, showing a list of titles, is fairly simple, relying 272 * on {@link ListFragment} for most of its work. Note the implementation of 273 * clicking an item: depending on the current activity's layout, it can either 274 * create and display a new fragment to show the details in-place (more about 275 * this later), or start a new activity to show the details.</p> 276 * 277 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 278 * titles} 279 * 280 * <p>The details fragment showing the contents of a selected item just 281 * displays a string of text based on an index of a string array built in to 282 * the app:</p> 283 * 284 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 285 * details} 286 * 287 * <p>In this case when the user clicks on a title, there is no details 288 * container in the current activity, so the titles fragment's click code will 289 * launch a new activity to display the details fragment:</p> 290 * 291 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 292 * details_activity} 293 * 294 * <p>However the screen may be large enough to show both the list of titles 295 * and details about the currently selected title. To use such a layout on 296 * a landscape screen, this alternative layout can be placed under layout-land:</p> 297 * 298 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout} 299 * 300 * <p>Note how the prior code will adjust to this alternative UI flow: the titles 301 * fragment will now embed the details fragment inside of this activity, and the 302 * details activity will finish itself if it is running in a configuration 303 * where the details can be shown in-place. 304 * 305 * <p>When a configuration change causes the activity hosting these fragments 306 * to restart, its new instance may use a different layout that doesn't 307 * include the same fragments as the previous layout. In this case all of 308 * the previous fragments will still be instantiated and running in the new 309 * instance. However, any that are no longer associated with a <fragment> 310 * tag in the view hierarchy will not have their content view created 311 * and will return false from {@link #isInLayout}. (The code here also shows 312 * how you can determine if a fragment placed in a container is no longer 313 * running in a layout with that container and avoid creating its view hierarchy 314 * in that case.) 315 * 316 * <p>The attributes of the <fragment> tag are used to control the 317 * LayoutParams provided when attaching the fragment's view to the parent 318 * container. They can also be parsed by the fragment in {@link #onInflate} 319 * as parameters. 320 * 321 * <p>The fragment being instantiated must have some kind of unique identifier 322 * so that it can be re-associated with a previous instance if the parent 323 * activity needs to be destroyed and recreated. This can be provided these 324 * ways: 325 * 326 * <ul> 327 * <li>If nothing is explicitly supplied, the view ID of the container will 328 * be used. 329 * <li><code>android:tag</code> can be used in <fragment> to provide 330 * a specific tag name for the fragment. 331 * <li><code>android:id</code> can be used in <fragment> to provide 332 * a specific identifier for the fragment. 333 * </ul> 334 * 335 * <a name="BackStack"></a> 336 * <h3>Back Stack</h3> 337 * 338 * <p>The transaction in which fragments are modified can be placed on an 339 * internal back-stack of the owning activity. When the user presses back 340 * in the activity, any transactions on the back stack are popped off before 341 * the activity itself is finished. 342 * 343 * <p>For example, consider this simple fragment that is instantiated with 344 * an integer argument and displays that in a TextView in its UI:</p> 345 * 346 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java 347 * fragment} 348 * 349 * <p>A function that creates a new instance of the fragment, replacing 350 * whatever current fragment instance is being shown and pushing that change 351 * on to the back stack could be written as: 352 * 353 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java 354 * add_stack} 355 * 356 * <p>After each call to this function, a new entry is on the stack, and 357 * pressing back will pop it to return the user to whatever previous state 358 * the activity UI was in. 359 */ 360public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener { 361 private static final ArrayMap<String, Class<?>> sClassMap = 362 new ArrayMap<String, Class<?>>(); 363 364 static final int INVALID_STATE = -1; // Invalid state used as a null value. 365 static final int INITIALIZING = 0; // Not yet created. 366 static final int CREATED = 1; // Created. 367 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation. 368 static final int STOPPED = 3; // Fully created, not started. 369 static final int STARTED = 4; // Created and started, not resumed. 370 static final int RESUMED = 5; // Created started and resumed. 371 372 private static final Transition USE_DEFAULT_TRANSITION = new TransitionSet(); 373 374 int mState = INITIALIZING; 375 376 // Non-null if the fragment's view hierarchy is currently animating away, 377 // meaning we need to wait a bit on completely destroying it. This is the 378 // animation that is running. 379 Animator mAnimatingAway; 380 381 // If mAnimatingAway != null, this is the state we should move to once the 382 // animation is done. 383 int mStateAfterAnimating; 384 385 // When instantiated from saved state, this is the saved state. 386 Bundle mSavedFragmentState; 387 SparseArray<Parcelable> mSavedViewState; 388 389 // Index into active fragment array. 390 int mIndex = -1; 391 392 // Internal unique name for this fragment; 393 String mWho; 394 395 // Construction arguments; 396 Bundle mArguments; 397 398 // Target fragment. 399 Fragment mTarget; 400 401 // For use when retaining a fragment: this is the index of the last mTarget. 402 int mTargetIndex = -1; 403 404 // Target request code. 405 int mTargetRequestCode; 406 407 // True if the fragment is in the list of added fragments. 408 boolean mAdded; 409 410 // If set this fragment is being removed from its activity. 411 boolean mRemoving; 412 413 // Set to true if this fragment was instantiated from a layout file. 414 boolean mFromLayout; 415 416 // Set to true when the view has actually been inflated in its layout. 417 boolean mInLayout; 418 419 // True if this fragment has been restored from previously saved state. 420 boolean mRestored; 421 422 // Number of active back stack entries this fragment is in. 423 int mBackStackNesting; 424 425 // The fragment manager we are associated with. Set as soon as the 426 // fragment is used in a transaction; cleared after it has been removed 427 // from all transactions. 428 FragmentManagerImpl mFragmentManager; 429 430 // Activity this fragment is attached to. 431 FragmentHostCallback mHost; 432 433 // Private fragment manager for child fragments inside of this one. 434 FragmentManagerImpl mChildFragmentManager; 435 436 // For use when restoring fragment state and descendant fragments are retained. 437 // This state is set by FragmentState.instantiate and cleared in onCreate. 438 FragmentManagerNonConfig mChildNonConfig; 439 440 // If this Fragment is contained in another Fragment, this is that container. 441 Fragment mParentFragment; 442 443 // The optional identifier for this fragment -- either the container ID if it 444 // was dynamically added to the view hierarchy, or the ID supplied in 445 // layout. 446 int mFragmentId; 447 448 // When a fragment is being dynamically added to the view hierarchy, this 449 // is the identifier of the parent container it is being added to. 450 int mContainerId; 451 452 // The optional named tag for this fragment -- usually used to find 453 // fragments that are not part of the layout. 454 String mTag; 455 456 // Set to true when the app has requested that this fragment be hidden 457 // from the user. 458 boolean mHidden; 459 460 // Set to true when the app has requested that this fragment be detached. 461 boolean mDetached; 462 463 // If set this fragment would like its instance retained across 464 // configuration changes. 465 boolean mRetainInstance; 466 467 // If set this fragment is being retained across the current config change. 468 boolean mRetaining; 469 470 // If set this fragment has menu items to contribute. 471 boolean mHasMenu; 472 473 // Set to true to allow the fragment's menu to be shown. 474 boolean mMenuVisible = true; 475 476 // Used to verify that subclasses call through to super class. 477 boolean mCalled; 478 479 // If app has requested a specific animation, this is the one to use. 480 int mNextAnim; 481 482 // The parent container of the fragment after dynamically added to UI. 483 ViewGroup mContainer; 484 485 // The View generated for this fragment. 486 View mView; 487 488 // Whether this fragment should defer starting until after other fragments 489 // have been started and their loaders are finished. 490 boolean mDeferStart; 491 492 // Hint provided by the app that this fragment is currently visible to the user. 493 boolean mUserVisibleHint = true; 494 495 LoaderManagerImpl mLoaderManager; 496 boolean mLoadersStarted; 497 boolean mCheckedForLoaderManager; 498 499 private Transition mEnterTransition = null; 500 private Transition mReturnTransition = USE_DEFAULT_TRANSITION; 501 private Transition mExitTransition = null; 502 private Transition mReenterTransition = USE_DEFAULT_TRANSITION; 503 private Transition mSharedElementEnterTransition = null; 504 private Transition mSharedElementReturnTransition = USE_DEFAULT_TRANSITION; 505 private Boolean mAllowReturnTransitionOverlap; 506 private Boolean mAllowEnterTransitionOverlap; 507 508 SharedElementCallback mEnterTransitionCallback = SharedElementCallback.NULL_CALLBACK; 509 SharedElementCallback mExitTransitionCallback = SharedElementCallback.NULL_CALLBACK; 510 511 /** 512 * State information that has been retrieved from a fragment instance 513 * through {@link FragmentManager#saveFragmentInstanceState(Fragment) 514 * FragmentManager.saveFragmentInstanceState}. 515 */ 516 public static class SavedState implements Parcelable { 517 final Bundle mState; 518 519 SavedState(Bundle state) { 520 mState = state; 521 } 522 523 SavedState(Parcel in, ClassLoader loader) { 524 mState = in.readBundle(); 525 if (loader != null && mState != null) { 526 mState.setClassLoader(loader); 527 } 528 } 529 530 @Override 531 public int describeContents() { 532 return 0; 533 } 534 535 @Override 536 public void writeToParcel(Parcel dest, int flags) { 537 dest.writeBundle(mState); 538 } 539 540 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR 541 = new Parcelable.ClassLoaderCreator<SavedState>() { 542 public SavedState createFromParcel(Parcel in) { 543 return new SavedState(in, null); 544 } 545 546 public SavedState createFromParcel(Parcel in, ClassLoader loader) { 547 return new SavedState(in, loader); 548 } 549 550 public SavedState[] newArray(int size) { 551 return new SavedState[size]; 552 } 553 }; 554 } 555 556 /** 557 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when 558 * there is an instantiation failure. 559 */ 560 static public class InstantiationException extends AndroidRuntimeException { 561 public InstantiationException(String msg, Exception cause) { 562 super(msg, cause); 563 } 564 } 565 566 /** 567 * Default constructor. <strong>Every</strong> fragment must have an 568 * empty constructor, so it can be instantiated when restoring its 569 * activity's state. It is strongly recommended that subclasses do not 570 * have other constructors with parameters, since these constructors 571 * will not be called when the fragment is re-instantiated; instead, 572 * arguments can be supplied by the caller with {@link #setArguments} 573 * and later retrieved by the Fragment with {@link #getArguments}. 574 * 575 * <p>Applications should generally not implement a constructor. The 576 * first place application code can run where the fragment is ready to 577 * be used is in {@link #onAttach(Activity)}, the point where the fragment 578 * is actually associated with its activity. Some applications may also 579 * want to implement {@link #onInflate} to retrieve attributes from a 580 * layout resource, though should take care here because this happens for 581 * the fragment is attached to its activity. 582 */ 583 public Fragment() { 584 } 585 586 /** 587 * Like {@link #instantiate(Context, String, Bundle)} but with a null 588 * argument Bundle. 589 */ 590 public static Fragment instantiate(Context context, String fname) { 591 return instantiate(context, fname, null); 592 } 593 594 /** 595 * Create a new instance of a Fragment with the given class name. This is 596 * the same as calling its empty constructor. 597 * 598 * @param context The calling context being used to instantiate the fragment. 599 * This is currently just used to get its ClassLoader. 600 * @param fname The class name of the fragment to instantiate. 601 * @param args Bundle of arguments to supply to the fragment, which it 602 * can retrieve with {@link #getArguments()}. May be null. 603 * @return Returns a new fragment instance. 604 * @throws InstantiationException If there is a failure in instantiating 605 * the given fragment class. This is a runtime exception; it is not 606 * normally expected to happen. 607 */ 608 public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) { 609 try { 610 Class<?> clazz = sClassMap.get(fname); 611 if (clazz == null) { 612 // Class not found in the cache, see if it's real, and try to add it 613 clazz = context.getClassLoader().loadClass(fname); 614 if (!Fragment.class.isAssignableFrom(clazz)) { 615 throw new InstantiationException("Trying to instantiate a class " + fname 616 + " that is not a Fragment", new ClassCastException()); 617 } 618 sClassMap.put(fname, clazz); 619 } 620 Fragment f = (Fragment)clazz.newInstance(); 621 if (args != null) { 622 args.setClassLoader(f.getClass().getClassLoader()); 623 f.mArguments = args; 624 } 625 return f; 626 } catch (ClassNotFoundException e) { 627 throw new InstantiationException("Unable to instantiate fragment " + fname 628 + ": make sure class name exists, is public, and has an" 629 + " empty constructor that is public", e); 630 } catch (java.lang.InstantiationException e) { 631 throw new InstantiationException("Unable to instantiate fragment " + fname 632 + ": make sure class name exists, is public, and has an" 633 + " empty constructor that is public", e); 634 } catch (IllegalAccessException e) { 635 throw new InstantiationException("Unable to instantiate fragment " + fname 636 + ": make sure class name exists, is public, and has an" 637 + " empty constructor that is public", e); 638 } 639 } 640 641 final void restoreViewState(Bundle savedInstanceState) { 642 if (mSavedViewState != null) { 643 mView.restoreHierarchyState(mSavedViewState); 644 mSavedViewState = null; 645 } 646 mCalled = false; 647 onViewStateRestored(savedInstanceState); 648 if (!mCalled) { 649 throw new SuperNotCalledException("Fragment " + this 650 + " did not call through to super.onViewStateRestored()"); 651 } 652 } 653 654 final void setIndex(int index, Fragment parent) { 655 mIndex = index; 656 if (parent != null) { 657 mWho = parent.mWho + ":" + mIndex; 658 } else { 659 mWho = "android:fragment:" + mIndex; 660 } 661 } 662 663 final boolean isInBackStack() { 664 return mBackStackNesting > 0; 665 } 666 667 /** 668 * Subclasses can not override equals(). 669 */ 670 @Override final public boolean equals(Object o) { 671 return super.equals(o); 672 } 673 674 /** 675 * Subclasses can not override hashCode(). 676 */ 677 @Override final public int hashCode() { 678 return super.hashCode(); 679 } 680 681 @Override 682 public String toString() { 683 StringBuilder sb = new StringBuilder(128); 684 DebugUtils.buildShortClassTag(this, sb); 685 if (mIndex >= 0) { 686 sb.append(" #"); 687 sb.append(mIndex); 688 } 689 if (mFragmentId != 0) { 690 sb.append(" id=0x"); 691 sb.append(Integer.toHexString(mFragmentId)); 692 } 693 if (mTag != null) { 694 sb.append(" "); 695 sb.append(mTag); 696 } 697 sb.append('}'); 698 return sb.toString(); 699 } 700 701 /** 702 * Return the identifier this fragment is known by. This is either 703 * the android:id value supplied in a layout or the container view ID 704 * supplied when adding the fragment. 705 */ 706 final public int getId() { 707 return mFragmentId; 708 } 709 710 /** 711 * Get the tag name of the fragment, if specified. 712 */ 713 final public String getTag() { 714 return mTag; 715 } 716 717 /** 718 * Supply the construction arguments for this fragment. This can only 719 * be called before the fragment has been attached to its activity; that 720 * is, you should call it immediately after constructing the fragment. The 721 * arguments supplied here will be retained across fragment destroy and 722 * creation. 723 */ 724 public void setArguments(Bundle args) { 725 if (mIndex >= 0) { 726 throw new IllegalStateException("Fragment already active"); 727 } 728 mArguments = args; 729 } 730 731 /** 732 * Return the arguments supplied to {@link #setArguments}, if any. 733 */ 734 final public Bundle getArguments() { 735 return mArguments; 736 } 737 738 /** 739 * Set the initial saved state that this Fragment should restore itself 740 * from when first being constructed, as returned by 741 * {@link FragmentManager#saveFragmentInstanceState(Fragment) 742 * FragmentManager.saveFragmentInstanceState}. 743 * 744 * @param state The state the fragment should be restored from. 745 */ 746 public void setInitialSavedState(SavedState state) { 747 if (mIndex >= 0) { 748 throw new IllegalStateException("Fragment already active"); 749 } 750 mSavedFragmentState = state != null && state.mState != null 751 ? state.mState : null; 752 } 753 754 /** 755 * Optional target for this fragment. This may be used, for example, 756 * if this fragment is being started by another, and when done wants to 757 * give a result back to the first. The target set here is retained 758 * across instances via {@link FragmentManager#putFragment 759 * FragmentManager.putFragment()}. 760 * 761 * @param fragment The fragment that is the target of this one. 762 * @param requestCode Optional request code, for convenience if you 763 * are going to call back with {@link #onActivityResult(int, int, Intent)}. 764 */ 765 public void setTargetFragment(Fragment fragment, int requestCode) { 766 mTarget = fragment; 767 mTargetRequestCode = requestCode; 768 } 769 770 /** 771 * Return the target fragment set by {@link #setTargetFragment}. 772 */ 773 final public Fragment getTargetFragment() { 774 return mTarget; 775 } 776 777 /** 778 * Return the target request code set by {@link #setTargetFragment}. 779 */ 780 final public int getTargetRequestCode() { 781 return mTargetRequestCode; 782 } 783 784 /** 785 * Return the {@link Context} this fragment is currently associated with. 786 */ 787 public Context getContext() { 788 return mHost == null ? null : mHost.getContext(); 789 } 790 791 /** 792 * Return the Activity this fragment is currently associated with. 793 */ 794 final public Activity getActivity() { 795 return mHost == null ? null : mHost.getActivity(); 796 } 797 798 /** 799 * Return the host object of this fragment. May return {@code null} if the fragment 800 * isn't currently being hosted. 801 */ 802 @Nullable 803 final public Object getHost() { 804 return mHost == null ? null : mHost.onGetHost(); 805 } 806 807 /** 808 * Return <code>getActivity().getResources()</code>. 809 */ 810 final public Resources getResources() { 811 if (mHost == null) { 812 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 813 } 814 return mHost.getContext().getResources(); 815 } 816 817 /** 818 * Return a localized, styled CharSequence from the application's package's 819 * default string table. 820 * 821 * @param resId Resource id for the CharSequence text 822 */ 823 public final CharSequence getText(@StringRes int resId) { 824 return getResources().getText(resId); 825 } 826 827 /** 828 * Return a localized string from the application's package's 829 * default string table. 830 * 831 * @param resId Resource id for the string 832 */ 833 public final String getString(@StringRes int resId) { 834 return getResources().getString(resId); 835 } 836 837 /** 838 * Return a localized formatted string from the application's package's 839 * default string table, substituting the format arguments as defined in 840 * {@link java.util.Formatter} and {@link java.lang.String#format}. 841 * 842 * @param resId Resource id for the format string 843 * @param formatArgs The format arguments that will be used for substitution. 844 */ 845 846 public final String getString(@StringRes int resId, Object... formatArgs) { 847 return getResources().getString(resId, formatArgs); 848 } 849 850 /** 851 * Return the FragmentManager for interacting with fragments associated 852 * with this fragment's activity. Note that this will be non-null slightly 853 * before {@link #getActivity()}, during the time from when the fragment is 854 * placed in a {@link FragmentTransaction} until it is committed and 855 * attached to its activity. 856 * 857 * <p>If this Fragment is a child of another Fragment, the FragmentManager 858 * returned here will be the parent's {@link #getChildFragmentManager()}. 859 */ 860 final public FragmentManager getFragmentManager() { 861 return mFragmentManager; 862 } 863 864 /** 865 * Return a private FragmentManager for placing and managing Fragments 866 * inside of this Fragment. 867 */ 868 final public FragmentManager getChildFragmentManager() { 869 if (mChildFragmentManager == null) { 870 instantiateChildFragmentManager(); 871 if (mState >= RESUMED) { 872 mChildFragmentManager.dispatchResume(); 873 } else if (mState >= STARTED) { 874 mChildFragmentManager.dispatchStart(); 875 } else if (mState >= ACTIVITY_CREATED) { 876 mChildFragmentManager.dispatchActivityCreated(); 877 } else if (mState >= CREATED) { 878 mChildFragmentManager.dispatchCreate(); 879 } 880 } 881 return mChildFragmentManager; 882 } 883 884 /** 885 * Returns the parent Fragment containing this Fragment. If this Fragment 886 * is attached directly to an Activity, returns null. 887 */ 888 final public Fragment getParentFragment() { 889 return mParentFragment; 890 } 891 892 /** 893 * Return true if the fragment is currently added to its activity. 894 */ 895 final public boolean isAdded() { 896 return mHost != null && mAdded; 897 } 898 899 /** 900 * Return true if the fragment has been explicitly detached from the UI. 901 * That is, {@link FragmentTransaction#detach(Fragment) 902 * FragmentTransaction.detach(Fragment)} has been used on it. 903 */ 904 final public boolean isDetached() { 905 return mDetached; 906 } 907 908 /** 909 * Return true if this fragment is currently being removed from its 910 * activity. This is <em>not</em> whether its activity is finishing, but 911 * rather whether it is in the process of being removed from its activity. 912 */ 913 final public boolean isRemoving() { 914 return mRemoving; 915 } 916 917 /** 918 * Return true if the layout is included as part of an activity view 919 * hierarchy via the <fragment> tag. This will always be true when 920 * fragments are created through the <fragment> tag, <em>except</em> 921 * in the case where an old fragment is restored from a previous state and 922 * it does not appear in the layout of the current state. 923 */ 924 final public boolean isInLayout() { 925 return mInLayout; 926 } 927 928 /** 929 * Return true if the fragment is in the resumed state. This is true 930 * for the duration of {@link #onResume()} and {@link #onPause()} as well. 931 */ 932 final public boolean isResumed() { 933 return mState >= RESUMED; 934 } 935 936 /** 937 * Return true if the fragment is currently visible to the user. This means 938 * it: (1) has been added, (2) has its view attached to the window, and 939 * (3) is not hidden. 940 */ 941 final public boolean isVisible() { 942 return isAdded() && !isHidden() && mView != null 943 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE; 944 } 945 946 /** 947 * Return true if the fragment has been hidden. By default fragments 948 * are shown. You can find out about changes to this state with 949 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal 950 * to other states -- that is, to be visible to the user, a fragment 951 * must be both started and not hidden. 952 */ 953 final public boolean isHidden() { 954 return mHidden; 955 } 956 957 /** 958 * Called when the hidden state (as returned by {@link #isHidden()} of 959 * the fragment has changed. Fragments start out not hidden; this will 960 * be called whenever the fragment changes state from that. 961 * @param hidden True if the fragment is now hidden, false if it is not 962 * visible. 963 */ 964 public void onHiddenChanged(boolean hidden) { 965 } 966 967 /** 968 * Control whether a fragment instance is retained across Activity 969 * re-creation (such as from a configuration change). This can only 970 * be used with fragments not in the back stack. If set, the fragment 971 * lifecycle will be slightly different when an activity is recreated: 972 * <ul> 973 * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still 974 * will be, because the fragment is being detached from its current activity). 975 * <li> {@link #onCreate(Bundle)} will not be called since the fragment 976 * is not being re-created. 977 * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b> 978 * still be called. 979 * </ul> 980 */ 981 public void setRetainInstance(boolean retain) { 982 mRetainInstance = retain; 983 } 984 985 final public boolean getRetainInstance() { 986 return mRetainInstance; 987 } 988 989 /** 990 * Report that this fragment would like to participate in populating 991 * the options menu by receiving a call to {@link #onCreateOptionsMenu} 992 * and related methods. 993 * 994 * @param hasMenu If true, the fragment has menu items to contribute. 995 */ 996 public void setHasOptionsMenu(boolean hasMenu) { 997 if (mHasMenu != hasMenu) { 998 mHasMenu = hasMenu; 999 if (isAdded() && !isHidden()) { 1000 mFragmentManager.invalidateOptionsMenu(); 1001 } 1002 } 1003 } 1004 1005 /** 1006 * Set a hint for whether this fragment's menu should be visible. This 1007 * is useful if you know that a fragment has been placed in your view 1008 * hierarchy so that the user can not currently seen it, so any menu items 1009 * it has should also not be shown. 1010 * 1011 * @param menuVisible The default is true, meaning the fragment's menu will 1012 * be shown as usual. If false, the user will not see the menu. 1013 */ 1014 public void setMenuVisibility(boolean menuVisible) { 1015 if (mMenuVisible != menuVisible) { 1016 mMenuVisible = menuVisible; 1017 if (mHasMenu && isAdded() && !isHidden()) { 1018 mFragmentManager.invalidateOptionsMenu(); 1019 } 1020 } 1021 } 1022 1023 /** 1024 * Set a hint to the system about whether this fragment's UI is currently visible 1025 * to the user. This hint defaults to true and is persistent across fragment instance 1026 * state save and restore. 1027 * 1028 * <p>An app may set this to false to indicate that the fragment's UI is 1029 * scrolled out of visibility or is otherwise not directly visible to the user. 1030 * This may be used by the system to prioritize operations such as fragment lifecycle updates 1031 * or loader ordering behavior.</p> 1032 * 1033 * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default), 1034 * false if it is not. 1035 */ 1036 public void setUserVisibleHint(boolean isVisibleToUser) { 1037 if (!mUserVisibleHint && isVisibleToUser && mState < STARTED 1038 && mFragmentManager != null && isAdded()) { 1039 mFragmentManager.performPendingDeferredStart(this); 1040 } 1041 mUserVisibleHint = isVisibleToUser; 1042 mDeferStart = mState < STARTED && !isVisibleToUser; 1043 } 1044 1045 /** 1046 * @return The current value of the user-visible hint on this fragment. 1047 * @see #setUserVisibleHint(boolean) 1048 */ 1049 public boolean getUserVisibleHint() { 1050 return mUserVisibleHint; 1051 } 1052 1053 /** 1054 * Return the LoaderManager for this fragment, creating it if needed. 1055 */ 1056 public LoaderManager getLoaderManager() { 1057 if (mLoaderManager != null) { 1058 return mLoaderManager; 1059 } 1060 if (mHost == null) { 1061 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 1062 } 1063 mCheckedForLoaderManager = true; 1064 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, true); 1065 return mLoaderManager; 1066 } 1067 1068 /** 1069 * Call {@link Activity#startActivity(Intent)} from the fragment's 1070 * containing Activity. 1071 * 1072 * @param intent The intent to start. 1073 */ 1074 public void startActivity(Intent intent) { 1075 startActivity(intent, null); 1076 } 1077 1078 /** 1079 * Call {@link Activity#startActivity(Intent, Bundle)} from the fragment's 1080 * containing Activity. 1081 * 1082 * @param intent The intent to start. 1083 * @param options Additional options for how the Activity should be started. 1084 * See {@link android.content.Context#startActivity(Intent, Bundle) 1085 * Context.startActivity(Intent, Bundle)} for more details. 1086 */ 1087 public void startActivity(Intent intent, Bundle options) { 1088 if (mHost == null) { 1089 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 1090 } 1091 if (options != null) { 1092 mHost.onStartActivityFromFragment(this, intent, -1, options); 1093 } else { 1094 // Note we want to go through this call for compatibility with 1095 // applications that may have overridden the method. 1096 mHost.onStartActivityFromFragment(this, intent, -1, null /*options*/); 1097 } 1098 } 1099 1100 /** 1101 * Call {@link Activity#startActivityForResult(Intent, int)} from the fragment's 1102 * containing Activity. 1103 */ 1104 public void startActivityForResult(Intent intent, int requestCode) { 1105 startActivityForResult(intent, requestCode, null); 1106 } 1107 1108 /** 1109 * Call {@link Activity#startActivityForResult(Intent, int, Bundle)} from the fragment's 1110 * containing Activity. 1111 */ 1112 public void startActivityForResult(Intent intent, int requestCode, Bundle options) { 1113 if (mHost == null) { 1114 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 1115 } 1116 mHost.onStartActivityFromFragment(this, intent, requestCode, options); 1117 } 1118 1119 /** 1120 * Call {@link Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int, 1121 * Bundle)} from the fragment's containing Activity. 1122 */ 1123 public void startIntentSenderForResult(IntentSender intent, int requestCode, 1124 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, 1125 Bundle options) throws IntentSender.SendIntentException { 1126 if (mHost == null) { 1127 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 1128 } 1129 mHost.onStartIntentSenderFromFragment(this, intent, requestCode, fillInIntent, flagsMask, 1130 flagsValues, extraFlags, options); 1131 } 1132 1133 /** 1134 * Receive the result from a previous call to 1135 * {@link #startActivityForResult(Intent, int)}. This follows the 1136 * related Activity API as described there in 1137 * {@link Activity#onActivityResult(int, int, Intent)}. 1138 * 1139 * @param requestCode The integer request code originally supplied to 1140 * startActivityForResult(), allowing you to identify who this 1141 * result came from. 1142 * @param resultCode The integer result code returned by the child activity 1143 * through its setResult(). 1144 * @param data An Intent, which can return result data to the caller 1145 * (various data can be attached to Intent "extras"). 1146 */ 1147 public void onActivityResult(int requestCode, int resultCode, Intent data) { 1148 } 1149 1150 /** 1151 * Requests permissions to be granted to this application. These permissions 1152 * must be requested in your manifest, they should not be granted to your app, 1153 * and they should have protection level {@link android.content.pm.PermissionInfo 1154 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by 1155 * the platform or a third-party app. 1156 * <p> 1157 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL} 1158 * are granted at install time if requested in the manifest. Signature permissions 1159 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at 1160 * install time if requested in the manifest and the signature of your app matches 1161 * the signature of the app declaring the permissions. 1162 * </p> 1163 * <p> 1164 * If your app does not have the requested permissions the user will be presented 1165 * with UI for accepting them. After the user has accepted or rejected the 1166 * requested permissions you will receive a callback on {@link 1167 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the 1168 * permissions were granted or not. 1169 * </p> 1170 * <p> 1171 * Note that requesting a permission does not guarantee it will be granted and 1172 * your app should be able to run without having this permission. 1173 * </p> 1174 * <p> 1175 * This method may start an activity allowing the user to choose which permissions 1176 * to grant and which to reject. Hence, you should be prepared that your activity 1177 * may be paused and resumed. Further, granting some permissions may require 1178 * a restart of you application. In such a case, the system will recreate the 1179 * activity stack before delivering the result to {@link 1180 * #onRequestPermissionsResult(int, String[], int[])}. 1181 * </p> 1182 * <p> 1183 * When checking whether you have a permission you should use {@link 1184 * android.content.Context#checkSelfPermission(String)}. 1185 * </p> 1186 * <p> 1187 * Calling this API for permissions already granted to your app would show UI 1188 * to the user to decide whether the app can still hold these permissions. This 1189 * can be useful if the way your app uses data guarded by the permissions 1190 * changes significantly. 1191 * </p> 1192 * <p> 1193 * You cannot request a permission if your activity sets {@link 1194 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to 1195 * <code>true</code> because in this case the activity would not receive 1196 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}. 1197 * </p> 1198 * <p> 1199 * A sample permissions request looks like this: 1200 * </p> 1201 * <code><pre><p> 1202 * private void showContacts() { 1203 * if (getActivity().checkSelfPermission(Manifest.permission.READ_CONTACTS) 1204 * != PackageManager.PERMISSION_GRANTED) { 1205 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1206 * PERMISSIONS_REQUEST_READ_CONTACTS); 1207 * } else { 1208 * doShowContacts(); 1209 * } 1210 * } 1211 * 1212 * {@literal @}Override 1213 * public void onRequestPermissionsResult(int requestCode, String[] permissions, 1214 * int[] grantResults) { 1215 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS 1216 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 1217 * doShowContacts(); 1218 * } 1219 * } 1220 * </code></pre></p> 1221 * 1222 * @param permissions The requested permissions. Must me non-null and not empty. 1223 * @param requestCode Application specific request code to match with a result 1224 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}. 1225 * Should be >= 0. 1226 * 1227 * @see #onRequestPermissionsResult(int, String[], int[]) 1228 * @see android.content.Context#checkSelfPermission(String) 1229 */ 1230 public final void requestPermissions(@NonNull String[] permissions, int requestCode) { 1231 if (mHost == null) { 1232 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 1233 } 1234 mHost.onRequestPermissionsFromFragment(this, permissions,requestCode); 1235 } 1236 1237 /** 1238 * Callback for the result from requesting permissions. This method 1239 * is invoked for every call on {@link #requestPermissions(String[], int)}. 1240 * <p> 1241 * <strong>Note:</strong> It is possible that the permissions request interaction 1242 * with the user is interrupted. In this case you will receive empty permissions 1243 * and results arrays which should be treated as a cancellation. 1244 * </p> 1245 * 1246 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}. 1247 * @param permissions The requested permissions. Never null. 1248 * @param grantResults The grant results for the corresponding permissions 1249 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED} 1250 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null. 1251 * 1252 * @see #requestPermissions(String[], int) 1253 */ 1254 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 1255 @NonNull int[] grantResults) { 1256 /* callback - do nothing */ 1257 } 1258 1259 /** 1260 * Gets whether you should show UI with rationale for requesting a permission. 1261 * You should do this only if you do not have the permission and the context in 1262 * which the permission is requested does not clearly communicate to the user 1263 * what would be the benefit from granting this permission. 1264 * <p> 1265 * For example, if you write a camera app, requesting the camera permission 1266 * would be expected by the user and no rationale for why it is requested is 1267 * needed. If however, the app needs location for tagging photos then a non-tech 1268 * savvy user may wonder how location is related to taking photos. In this case 1269 * you may choose to show UI with rationale of requesting this permission. 1270 * </p> 1271 * 1272 * @param permission A permission your app wants to request. 1273 * @return Whether you can show permission rationale UI. 1274 * 1275 * @see Context#checkSelfPermission(String) 1276 * @see #requestPermissions(String[], int) 1277 * @see #onRequestPermissionsResult(int, String[], int[]) 1278 */ 1279 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) { 1280 if (mHost != null) { 1281 return mHost.getContext().getPackageManager() 1282 .shouldShowRequestPermissionRationale(permission); 1283 } 1284 return false; 1285 } 1286 1287 /** 1288 * @hide Hack so that DialogFragment can make its Dialog before creating 1289 * its views, and the view construction can use the dialog's context for 1290 * inflation. Maybe this should become a public API. Note sure. 1291 */ 1292 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) { 1293 final LayoutInflater result = mHost.onGetLayoutInflater(); 1294 if (mHost.onUseFragmentManagerInflaterFactory()) { 1295 getChildFragmentManager(); // Init if needed; use raw implementation below. 1296 result.setPrivateFactory(mChildFragmentManager.getLayoutInflaterFactory()); 1297 } 1298 return result; 1299 } 1300 1301 /** 1302 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead. 1303 */ 1304 @Deprecated 1305 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) { 1306 mCalled = true; 1307 } 1308 1309 /** 1310 * Called when a fragment is being created as part of a view layout 1311 * inflation, typically from setting the content view of an activity. This 1312 * may be called immediately after the fragment is created from a <fragment> 1313 * tag in a layout file. Note this is <em>before</em> the fragment's 1314 * {@link #onAttach(Activity)} has been called; all you should do here is 1315 * parse the attributes and save them away. 1316 * 1317 * <p>This is called every time the fragment is inflated, even if it is 1318 * being inflated into a new instance with saved state. It typically makes 1319 * sense to re-parse the parameters each time, to allow them to change with 1320 * different configurations.</p> 1321 * 1322 * <p>Here is a typical implementation of a fragment that can take parameters 1323 * both through attributes supplied here as well from {@link #getArguments()}:</p> 1324 * 1325 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java 1326 * fragment} 1327 * 1328 * <p>Note that parsing the XML attributes uses a "styleable" resource. The 1329 * declaration for the styleable used here is:</p> 1330 * 1331 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments} 1332 * 1333 * <p>The fragment can then be declared within its activity's content layout 1334 * through a tag like this:</p> 1335 * 1336 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes} 1337 * 1338 * <p>This fragment can also be created dynamically from arguments given 1339 * at runtime in the arguments Bundle; here is an example of doing so at 1340 * creation of the containing activity:</p> 1341 * 1342 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java 1343 * create} 1344 * 1345 * @param context The Context that is inflating this fragment. 1346 * @param attrs The attributes at the tag where the fragment is 1347 * being created. 1348 * @param savedInstanceState If the fragment is being re-created from 1349 * a previous saved state, this is the state. 1350 */ 1351 public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) { 1352 onInflate(attrs, savedInstanceState); 1353 mCalled = true; 1354 1355 TypedArray a = context.obtainStyledAttributes(attrs, 1356 com.android.internal.R.styleable.Fragment); 1357 mEnterTransition = loadTransition(context, a, mEnterTransition, null, 1358 com.android.internal.R.styleable.Fragment_fragmentEnterTransition); 1359 mReturnTransition = loadTransition(context, a, mReturnTransition, USE_DEFAULT_TRANSITION, 1360 com.android.internal.R.styleable.Fragment_fragmentReturnTransition); 1361 mExitTransition = loadTransition(context, a, mExitTransition, null, 1362 com.android.internal.R.styleable.Fragment_fragmentExitTransition); 1363 mReenterTransition = loadTransition(context, a, mReenterTransition, USE_DEFAULT_TRANSITION, 1364 com.android.internal.R.styleable.Fragment_fragmentReenterTransition); 1365 mSharedElementEnterTransition = loadTransition(context, a, mSharedElementEnterTransition, 1366 null, com.android.internal.R.styleable.Fragment_fragmentSharedElementEnterTransition); 1367 mSharedElementReturnTransition = loadTransition(context, a, mSharedElementReturnTransition, 1368 USE_DEFAULT_TRANSITION, 1369 com.android.internal.R.styleable.Fragment_fragmentSharedElementReturnTransition); 1370 if (mAllowEnterTransitionOverlap == null) { 1371 mAllowEnterTransitionOverlap = a.getBoolean( 1372 com.android.internal.R.styleable.Fragment_fragmentAllowEnterTransitionOverlap, true); 1373 } 1374 if (mAllowReturnTransitionOverlap == null) { 1375 mAllowReturnTransitionOverlap = a.getBoolean( 1376 com.android.internal.R.styleable.Fragment_fragmentAllowReturnTransitionOverlap, true); 1377 } 1378 a.recycle(); 1379 1380 final Activity hostActivity = mHost == null ? null : mHost.getActivity(); 1381 if (hostActivity != null) { 1382 mCalled = false; 1383 onInflate(hostActivity, attrs, savedInstanceState); 1384 } 1385 } 1386 1387 /** 1388 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead. 1389 */ 1390 @Deprecated 1391 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { 1392 mCalled = true; 1393 } 1394 1395 /** 1396 * Called when a fragment is first attached to its context. 1397 * {@link #onCreate(Bundle)} will be called after this. 1398 */ 1399 public void onAttach(Context context) { 1400 mCalled = true; 1401 final Activity hostActivity = mHost == null ? null : mHost.getActivity(); 1402 if (hostActivity != null) { 1403 mCalled = false; 1404 onAttach(hostActivity); 1405 } 1406 } 1407 1408 /** 1409 * @deprecated Use {@link #onAttach(Context)} instead. 1410 */ 1411 @Deprecated 1412 public void onAttach(Activity activity) { 1413 mCalled = true; 1414 } 1415 1416 /** 1417 * Called when a fragment loads an animation. 1418 */ 1419 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { 1420 return null; 1421 } 1422 1423 /** 1424 * Called to do initial creation of a fragment. This is called after 1425 * {@link #onAttach(Activity)} and before 1426 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, but is not called if the fragment 1427 * instance is retained across Activity re-creation (see {@link #setRetainInstance(boolean)}). 1428 * 1429 * <p>Note that this can be called while the fragment's activity is 1430 * still in the process of being created. As such, you can not rely 1431 * on things like the activity's content view hierarchy being initialized 1432 * at this point. If you want to do work once the activity itself is 1433 * created, see {@link #onActivityCreated(Bundle)}. 1434 * 1435 * <p>If your app's <code>targetSdkVersion</code> is 23 or lower, child fragments 1436 * being restored from the savedInstanceState are restored after <code>onCreate</code> 1437 * returns. When targeting N or above and running on an N or newer platform version 1438 * they are restored by <code>Fragment.onCreate</code>.</p> 1439 * 1440 * @param savedInstanceState If the fragment is being re-created from 1441 * a previous saved state, this is the state. 1442 */ 1443 public void onCreate(@Nullable Bundle savedInstanceState) { 1444 mCalled = true; 1445 final Context context = getContext(); 1446 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0; 1447 if (version >= Build.VERSION_CODES.N) { 1448 restoreChildFragmentState(savedInstanceState, true); 1449 } 1450 } 1451 1452 void restoreChildFragmentState(@Nullable Bundle savedInstanceState, boolean provideNonConfig) { 1453 if (savedInstanceState != null) { 1454 Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG); 1455 if (p != null) { 1456 if (mChildFragmentManager == null) { 1457 instantiateChildFragmentManager(); 1458 } 1459 mChildFragmentManager.restoreAllState(p, provideNonConfig ? mChildNonConfig : null); 1460 mChildNonConfig = null; 1461 mChildFragmentManager.dispatchCreate(); 1462 } 1463 } 1464 } 1465 1466 /** 1467 * Called to have the fragment instantiate its user interface view. 1468 * This is optional, and non-graphical fragments can return null (which 1469 * is the default implementation). This will be called between 1470 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}. 1471 * 1472 * <p>If you return a View from here, you will later be called in 1473 * {@link #onDestroyView} when the view is being released. 1474 * 1475 * @param inflater The LayoutInflater object that can be used to inflate 1476 * any views in the fragment, 1477 * @param container If non-null, this is the parent view that the fragment's 1478 * UI should be attached to. The fragment should not add the view itself, 1479 * but this can be used to generate the LayoutParams of the view. 1480 * @param savedInstanceState If non-null, this fragment is being re-constructed 1481 * from a previous saved state as given here. 1482 * 1483 * @return Return the View for the fragment's UI, or null. 1484 */ 1485 @Nullable 1486 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 1487 Bundle savedInstanceState) { 1488 return null; 1489 } 1490 1491 /** 1492 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} 1493 * has returned, but before any saved state has been restored in to the view. 1494 * This gives subclasses a chance to initialize themselves once 1495 * they know their view hierarchy has been completely created. The fragment's 1496 * view hierarchy is not however attached to its parent at this point. 1497 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. 1498 * @param savedInstanceState If non-null, this fragment is being re-constructed 1499 * from a previous saved state as given here. 1500 */ 1501 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 1502 } 1503 1504 /** 1505 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}), 1506 * if provided. 1507 * 1508 * @return The fragment's root view, or null if it has no layout. 1509 */ 1510 @Nullable 1511 public View getView() { 1512 return mView; 1513 } 1514 1515 /** 1516 * Called when the fragment's activity has been created and this 1517 * fragment's view hierarchy instantiated. It can be used to do final 1518 * initialization once these pieces are in place, such as retrieving 1519 * views or restoring state. It is also useful for fragments that use 1520 * {@link #setRetainInstance(boolean)} to retain their instance, 1521 * as this callback tells the fragment when it is fully associated with 1522 * the new activity instance. This is called after {@link #onCreateView} 1523 * and before {@link #onViewStateRestored(Bundle)}. 1524 * 1525 * @param savedInstanceState If the fragment is being re-created from 1526 * a previous saved state, this is the state. 1527 */ 1528 public void onActivityCreated(@Nullable Bundle savedInstanceState) { 1529 mCalled = true; 1530 } 1531 1532 /** 1533 * Called when all saved state has been restored into the view hierarchy 1534 * of the fragment. This can be used to do initialization based on saved 1535 * state that you are letting the view hierarchy track itself, such as 1536 * whether check box widgets are currently checked. This is called 1537 * after {@link #onActivityCreated(Bundle)} and before 1538 * {@link #onStart()}. 1539 * 1540 * @param savedInstanceState If the fragment is being re-created from 1541 * a previous saved state, this is the state. 1542 */ 1543 public void onViewStateRestored(Bundle savedInstanceState) { 1544 mCalled = true; 1545 } 1546 1547 /** 1548 * Called when the Fragment is visible to the user. This is generally 1549 * tied to {@link Activity#onStart() Activity.onStart} of the containing 1550 * Activity's lifecycle. 1551 */ 1552 public void onStart() { 1553 mCalled = true; 1554 1555 if (!mLoadersStarted) { 1556 mLoadersStarted = true; 1557 if (!mCheckedForLoaderManager) { 1558 mCheckedForLoaderManager = true; 1559 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false); 1560 } 1561 if (mLoaderManager != null) { 1562 mLoaderManager.doStart(); 1563 } 1564 } 1565 } 1566 1567 /** 1568 * Called when the fragment is visible to the user and actively running. 1569 * This is generally 1570 * tied to {@link Activity#onResume() Activity.onResume} of the containing 1571 * Activity's lifecycle. 1572 */ 1573 public void onResume() { 1574 mCalled = true; 1575 } 1576 1577 /** 1578 * Called to ask the fragment to save its current dynamic state, so it 1579 * can later be reconstructed in a new instance of its process is 1580 * restarted. If a new instance of the fragment later needs to be 1581 * created, the data you place in the Bundle here will be available 1582 * in the Bundle given to {@link #onCreate(Bundle)}, 1583 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and 1584 * {@link #onActivityCreated(Bundle)}. 1585 * 1586 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle) 1587 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there 1588 * applies here as well. Note however: <em>this method may be called 1589 * at any time before {@link #onDestroy()}</em>. There are many situations 1590 * where a fragment may be mostly torn down (such as when placed on the 1591 * back stack with no UI showing), but its state will not be saved until 1592 * its owning activity actually needs to save its state. 1593 * 1594 * @param outState Bundle in which to place your saved state. 1595 */ 1596 public void onSaveInstanceState(Bundle outState) { 1597 } 1598 1599 /** 1600 * Called when the Fragment's activity changes from fullscreen mode to multi-window mode and 1601 * visa-versa. This is generally tied to {@link Activity#onMultiWindowModeChanged} of the 1602 * containing Activity. 1603 * 1604 * @param isInMultiWindowMode True if the activity is in multi-window mode. 1605 */ 1606 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { 1607 } 1608 1609 /** 1610 * Called by the system when the activity changes to and from picture-in-picture mode. This is 1611 * generally tied to {@link Activity#onPictureInPictureModeChanged} of the containing Activity. 1612 * 1613 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode. 1614 */ 1615 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 1616 } 1617 1618 public void onConfigurationChanged(Configuration newConfig) { 1619 mCalled = true; 1620 } 1621 1622 /** 1623 * Called when the Fragment is no longer resumed. This is generally 1624 * tied to {@link Activity#onPause() Activity.onPause} of the containing 1625 * Activity's lifecycle. 1626 */ 1627 public void onPause() { 1628 mCalled = true; 1629 } 1630 1631 /** 1632 * Called when the Fragment is no longer started. This is generally 1633 * tied to {@link Activity#onStop() Activity.onStop} of the containing 1634 * Activity's lifecycle. 1635 */ 1636 public void onStop() { 1637 mCalled = true; 1638 } 1639 1640 public void onLowMemory() { 1641 mCalled = true; 1642 } 1643 1644 public void onTrimMemory(int level) { 1645 mCalled = true; 1646 } 1647 1648 /** 1649 * Called when the view previously created by {@link #onCreateView} has 1650 * been detached from the fragment. The next time the fragment needs 1651 * to be displayed, a new view will be created. This is called 1652 * after {@link #onStop()} and before {@link #onDestroy()}. It is called 1653 * <em>regardless</em> of whether {@link #onCreateView} returned a 1654 * non-null view. Internally it is called after the view's state has 1655 * been saved but before it has been removed from its parent. 1656 */ 1657 public void onDestroyView() { 1658 mCalled = true; 1659 } 1660 1661 /** 1662 * Called when the fragment is no longer in use. This is called 1663 * after {@link #onStop()} and before {@link #onDetach()}. 1664 */ 1665 public void onDestroy() { 1666 mCalled = true; 1667 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager 1668 // + " mLoaderManager=" + mLoaderManager); 1669 if (!mCheckedForLoaderManager) { 1670 mCheckedForLoaderManager = true; 1671 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false); 1672 } 1673 if (mLoaderManager != null) { 1674 mLoaderManager.doDestroy(); 1675 } 1676 } 1677 1678 /** 1679 * Called by the fragment manager once this fragment has been removed, 1680 * so that we don't have any left-over state if the application decides 1681 * to re-use the instance. This only clears state that the framework 1682 * internally manages, not things the application sets. 1683 */ 1684 void initState() { 1685 mIndex = -1; 1686 mWho = null; 1687 mAdded = false; 1688 mRemoving = false; 1689 mFromLayout = false; 1690 mInLayout = false; 1691 mRestored = false; 1692 mBackStackNesting = 0; 1693 mFragmentManager = null; 1694 mChildFragmentManager = null; 1695 mHost = null; 1696 mFragmentId = 0; 1697 mContainerId = 0; 1698 mTag = null; 1699 mHidden = false; 1700 mDetached = false; 1701 mRetaining = false; 1702 mLoaderManager = null; 1703 mLoadersStarted = false; 1704 mCheckedForLoaderManager = false; 1705 } 1706 1707 /** 1708 * Called when the fragment is no longer attached to its activity. This is called after 1709 * {@link #onDestroy()}, except in the cases where the fragment instance is retained across 1710 * Activity re-creation (see {@link #setRetainInstance(boolean)}), in which case it is called 1711 * after {@link #onStop()}. 1712 */ 1713 public void onDetach() { 1714 mCalled = true; 1715 1716 // Destroy the child FragmentManager if we still have it here. 1717 // We won't unless we're retaining our instance and if we do, 1718 // our child FragmentManager instance state will have already been saved. 1719 if (mChildFragmentManager != null) { 1720 if (!mRetaining) { 1721 throw new IllegalStateException("Child FragmentManager of " + this + " was not " 1722 + " destroyed and this fragment is not retaining instance"); 1723 } 1724 mChildFragmentManager.dispatchDestroy(); 1725 mChildFragmentManager = null; 1726 } 1727 } 1728 1729 /** 1730 * Initialize the contents of the Activity's standard options menu. You 1731 * should place your menu items in to <var>menu</var>. For this method 1732 * to be called, you must have first called {@link #setHasOptionsMenu}. See 1733 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu} 1734 * for more information. 1735 * 1736 * @param menu The options menu in which you place your items. 1737 * 1738 * @see #setHasOptionsMenu 1739 * @see #onPrepareOptionsMenu 1740 * @see #onOptionsItemSelected 1741 */ 1742 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 1743 } 1744 1745 /** 1746 * Prepare the Screen's standard options menu to be displayed. This is 1747 * called right before the menu is shown, every time it is shown. You can 1748 * use this method to efficiently enable/disable items or otherwise 1749 * dynamically modify the contents. See 1750 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu} 1751 * for more information. 1752 * 1753 * @param menu The options menu as last shown or first initialized by 1754 * onCreateOptionsMenu(). 1755 * 1756 * @see #setHasOptionsMenu 1757 * @see #onCreateOptionsMenu 1758 */ 1759 public void onPrepareOptionsMenu(Menu menu) { 1760 } 1761 1762 /** 1763 * Called when this fragment's option menu items are no longer being 1764 * included in the overall options menu. Receiving this call means that 1765 * the menu needed to be rebuilt, but this fragment's items were not 1766 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)} 1767 * was not called). 1768 */ 1769 public void onDestroyOptionsMenu() { 1770 } 1771 1772 /** 1773 * This hook is called whenever an item in your options menu is selected. 1774 * The default implementation simply returns false to have the normal 1775 * processing happen (calling the item's Runnable or sending a message to 1776 * its Handler as appropriate). You can use this method for any items 1777 * for which you would like to do processing without those other 1778 * facilities. 1779 * 1780 * <p>Derived classes should call through to the base class for it to 1781 * perform the default menu handling. 1782 * 1783 * @param item The menu item that was selected. 1784 * 1785 * @return boolean Return false to allow normal menu processing to 1786 * proceed, true to consume it here. 1787 * 1788 * @see #onCreateOptionsMenu 1789 */ 1790 public boolean onOptionsItemSelected(MenuItem item) { 1791 return false; 1792 } 1793 1794 /** 1795 * This hook is called whenever the options menu is being closed (either by the user canceling 1796 * the menu with the back/menu button, or when an item is selected). 1797 * 1798 * @param menu The options menu as last shown or first initialized by 1799 * onCreateOptionsMenu(). 1800 */ 1801 public void onOptionsMenuClosed(Menu menu) { 1802 } 1803 1804 /** 1805 * Called when a context menu for the {@code view} is about to be shown. 1806 * Unlike {@link #onCreateOptionsMenu}, this will be called every 1807 * time the context menu is about to be shown and should be populated for 1808 * the view (or item inside the view for {@link AdapterView} subclasses, 1809 * this can be found in the {@code menuInfo})). 1810 * <p> 1811 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an 1812 * item has been selected. 1813 * <p> 1814 * The default implementation calls up to 1815 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though 1816 * you can not call this implementation if you don't want that behavior. 1817 * <p> 1818 * It is not safe to hold onto the context menu after this method returns. 1819 * {@inheritDoc} 1820 */ 1821 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 1822 getActivity().onCreateContextMenu(menu, v, menuInfo); 1823 } 1824 1825 /** 1826 * Registers a context menu to be shown for the given view (multiple views 1827 * can show the context menu). This method will set the 1828 * {@link OnCreateContextMenuListener} on the view to this fragment, so 1829 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be 1830 * called when it is time to show the context menu. 1831 * 1832 * @see #unregisterForContextMenu(View) 1833 * @param view The view that should show a context menu. 1834 */ 1835 public void registerForContextMenu(View view) { 1836 view.setOnCreateContextMenuListener(this); 1837 } 1838 1839 /** 1840 * Prevents a context menu to be shown for the given view. This method will 1841 * remove the {@link OnCreateContextMenuListener} on the view. 1842 * 1843 * @see #registerForContextMenu(View) 1844 * @param view The view that should stop showing a context menu. 1845 */ 1846 public void unregisterForContextMenu(View view) { 1847 view.setOnCreateContextMenuListener(null); 1848 } 1849 1850 /** 1851 * This hook is called whenever an item in a context menu is selected. The 1852 * default implementation simply returns false to have the normal processing 1853 * happen (calling the item's Runnable or sending a message to its Handler 1854 * as appropriate). You can use this method for any items for which you 1855 * would like to do processing without those other facilities. 1856 * <p> 1857 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the 1858 * View that added this menu item. 1859 * <p> 1860 * Derived classes should call through to the base class for it to perform 1861 * the default menu handling. 1862 * 1863 * @param item The context menu item that was selected. 1864 * @return boolean Return false to allow normal context menu processing to 1865 * proceed, true to consume it here. 1866 */ 1867 public boolean onContextItemSelected(MenuItem item) { 1868 return false; 1869 } 1870 1871 /** 1872 * When custom transitions are used with Fragments, the enter transition callback 1873 * is called when this Fragment is attached or detached when not popping the back stack. 1874 * 1875 * @param callback Used to manipulate the shared element transitions on this Fragment 1876 * when added not as a pop from the back stack. 1877 */ 1878 public void setEnterSharedElementCallback(SharedElementCallback callback) { 1879 if (callback == null) { 1880 callback = SharedElementCallback.NULL_CALLBACK; 1881 } 1882 mEnterTransitionCallback = callback; 1883 } 1884 1885 /** 1886 * @hide 1887 */ 1888 public void setEnterSharedElementTransitionCallback(SharedElementCallback callback) { 1889 setEnterSharedElementCallback(callback); 1890 } 1891 1892 /** 1893 * When custom transitions are used with Fragments, the exit transition callback 1894 * is called when this Fragment is attached or detached when popping the back stack. 1895 * 1896 * @param callback Used to manipulate the shared element transitions on this Fragment 1897 * when added as a pop from the back stack. 1898 */ 1899 public void setExitSharedElementCallback(SharedElementCallback callback) { 1900 if (callback == null) { 1901 callback = SharedElementCallback.NULL_CALLBACK; 1902 } 1903 mExitTransitionCallback = callback; 1904 } 1905 1906 /** 1907 * @hide 1908 */ 1909 public void setExitSharedElementTransitionCallback(SharedElementCallback callback) { 1910 setExitSharedElementCallback(callback); 1911 } 1912 1913 /** 1914 * Sets the Transition that will be used to move Views into the initial scene. The entering 1915 * Views will be those that are regular Views or ViewGroups that have 1916 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend 1917 * {@link android.transition.Visibility} as entering is governed by changing visibility from 1918 * {@link View#INVISIBLE} to {@link View#VISIBLE}. If <code>transition</code> is null, 1919 * entering Views will remain unaffected. 1920 * 1921 * @param transition The Transition to use to move Views into the initial Scene. 1922 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition 1923 */ 1924 public void setEnterTransition(Transition transition) { 1925 mEnterTransition = transition; 1926 } 1927 1928 /** 1929 * Returns the Transition that will be used to move Views into the initial scene. The entering 1930 * Views will be those that are regular Views or ViewGroups that have 1931 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend 1932 * {@link android.transition.Visibility} as entering is governed by changing visibility from 1933 * {@link View#INVISIBLE} to {@link View#VISIBLE}. 1934 * 1935 * @return the Transition to use to move Views into the initial Scene. 1936 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition 1937 */ 1938 public Transition getEnterTransition() { 1939 return mEnterTransition; 1940 } 1941 1942 /** 1943 * Sets the Transition that will be used to move Views out of the scene when the Fragment is 1944 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting 1945 * Views will be those that are regular Views or ViewGroups that have 1946 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend 1947 * {@link android.transition.Visibility} as entering is governed by changing visibility from 1948 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null, 1949 * entering Views will remain unaffected. If nothing is set, the default will be to 1950 * use the same value as set in {@link #setEnterTransition(android.transition.Transition)}. 1951 * 1952 * @param transition The Transition to use to move Views out of the Scene when the Fragment 1953 * is preparing to close. 1954 * @attr ref android.R.styleable#Fragment_fragmentExitTransition 1955 */ 1956 public void setReturnTransition(Transition transition) { 1957 mReturnTransition = transition; 1958 } 1959 1960 /** 1961 * Returns the Transition that will be used to move Views out of the scene when the Fragment is 1962 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting 1963 * Views will be those that are regular Views or ViewGroups that have 1964 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend 1965 * {@link android.transition.Visibility} as entering is governed by changing visibility from 1966 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null, 1967 * entering Views will remain unaffected. 1968 * 1969 * @return the Transition to use to move Views out of the Scene when the Fragment 1970 * is preparing to close. 1971 * @attr ref android.R.styleable#Fragment_fragmentExitTransition 1972 */ 1973 public Transition getReturnTransition() { 1974 return mReturnTransition == USE_DEFAULT_TRANSITION ? getEnterTransition() 1975 : mReturnTransition; 1976 } 1977 1978 /** 1979 * Sets the Transition that will be used to move Views out of the scene when the 1980 * fragment is removed, hidden, or detached when not popping the back stack. 1981 * The exiting Views will be those that are regular Views or ViewGroups that 1982 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend 1983 * {@link android.transition.Visibility} as exiting is governed by changing visibility 1984 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will 1985 * remain unaffected. 1986 * 1987 * @param transition The Transition to use to move Views out of the Scene when the Fragment 1988 * is being closed not due to popping the back stack. 1989 * @attr ref android.R.styleable#Fragment_fragmentExitTransition 1990 */ 1991 public void setExitTransition(Transition transition) { 1992 mExitTransition = transition; 1993 } 1994 1995 /** 1996 * Returns the Transition that will be used to move Views out of the scene when the 1997 * fragment is removed, hidden, or detached when not popping the back stack. 1998 * The exiting Views will be those that are regular Views or ViewGroups that 1999 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend 2000 * {@link android.transition.Visibility} as exiting is governed by changing visibility 2001 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will 2002 * remain unaffected. 2003 * 2004 * @return the Transition to use to move Views out of the Scene when the Fragment 2005 * is being closed not due to popping the back stack. 2006 * @attr ref android.R.styleable#Fragment_fragmentExitTransition 2007 */ 2008 public Transition getExitTransition() { 2009 return mExitTransition; 2010 } 2011 2012 /** 2013 * Sets the Transition that will be used to move Views in to the scene when returning due 2014 * to popping a back stack. The entering Views will be those that are regular Views 2015 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions 2016 * will extend {@link android.transition.Visibility} as exiting is governed by changing 2017 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, 2018 * the views will remain unaffected. If nothing is set, the default will be to use the same 2019 * transition as {@link #setExitTransition(android.transition.Transition)}. 2020 * 2021 * @param transition The Transition to use to move Views into the scene when reentering from a 2022 * previously-started Activity. 2023 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition 2024 */ 2025 public void setReenterTransition(Transition transition) { 2026 mReenterTransition = transition; 2027 } 2028 2029 /** 2030 * Returns the Transition that will be used to move Views in to the scene when returning due 2031 * to popping a back stack. The entering Views will be those that are regular Views 2032 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions 2033 * will extend {@link android.transition.Visibility} as exiting is governed by changing 2034 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, 2035 * the views will remain unaffected. If nothing is set, the default will be to use the same 2036 * transition as {@link #setExitTransition(android.transition.Transition)}. 2037 * 2038 * @return the Transition to use to move Views into the scene when reentering from a 2039 * previously-started Activity. 2040 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition 2041 */ 2042 public Transition getReenterTransition() { 2043 return mReenterTransition == USE_DEFAULT_TRANSITION ? getExitTransition() 2044 : mReenterTransition; 2045 } 2046 2047 /** 2048 * Sets the Transition that will be used for shared elements transferred into the content 2049 * Scene. Typical Transitions will affect size and location, such as 2050 * {@link android.transition.ChangeBounds}. A null 2051 * value will cause transferred shared elements to blink to the final position. 2052 * 2053 * @param transition The Transition to use for shared elements transferred into the content 2054 * Scene. 2055 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition 2056 */ 2057 public void setSharedElementEnterTransition(Transition transition) { 2058 mSharedElementEnterTransition = transition; 2059 } 2060 2061 /** 2062 * Returns the Transition that will be used for shared elements transferred into the content 2063 * Scene. Typical Transitions will affect size and location, such as 2064 * {@link android.transition.ChangeBounds}. A null 2065 * value will cause transferred shared elements to blink to the final position. 2066 * 2067 * @return The Transition to use for shared elements transferred into the content 2068 * Scene. 2069 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition 2070 */ 2071 public Transition getSharedElementEnterTransition() { 2072 return mSharedElementEnterTransition; 2073 } 2074 2075 /** 2076 * Sets the Transition that will be used for shared elements transferred back during a 2077 * pop of the back stack. This Transition acts in the leaving Fragment. 2078 * Typical Transitions will affect size and location, such as 2079 * {@link android.transition.ChangeBounds}. A null 2080 * value will cause transferred shared elements to blink to the final position. 2081 * If no value is set, the default will be to use the same value as 2082 * {@link #setSharedElementEnterTransition(android.transition.Transition)}. 2083 * 2084 * @param transition The Transition to use for shared elements transferred out of the content 2085 * Scene. 2086 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition 2087 */ 2088 public void setSharedElementReturnTransition(Transition transition) { 2089 mSharedElementReturnTransition = transition; 2090 } 2091 2092 /** 2093 * Return the Transition that will be used for shared elements transferred back during a 2094 * pop of the back stack. This Transition acts in the leaving Fragment. 2095 * Typical Transitions will affect size and location, such as 2096 * {@link android.transition.ChangeBounds}. A null 2097 * value will cause transferred shared elements to blink to the final position. 2098 * If no value is set, the default will be to use the same value as 2099 * {@link #setSharedElementEnterTransition(android.transition.Transition)}. 2100 * 2101 * @return The Transition to use for shared elements transferred out of the content 2102 * Scene. 2103 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition 2104 */ 2105 public Transition getSharedElementReturnTransition() { 2106 return mSharedElementReturnTransition == USE_DEFAULT_TRANSITION ? 2107 getSharedElementEnterTransition() : mSharedElementReturnTransition; 2108 } 2109 2110 /** 2111 * Sets whether the the exit transition and enter transition overlap or not. 2112 * When true, the enter transition will start as soon as possible. When false, the 2113 * enter transition will wait until the exit transition completes before starting. 2114 * 2115 * @param allow true to start the enter transition when possible or false to 2116 * wait until the exiting transition completes. 2117 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap 2118 */ 2119 public void setAllowEnterTransitionOverlap(boolean allow) { 2120 mAllowEnterTransitionOverlap = allow; 2121 } 2122 2123 /** 2124 * Returns whether the the exit transition and enter transition overlap or not. 2125 * When true, the enter transition will start as soon as possible. When false, the 2126 * enter transition will wait until the exit transition completes before starting. 2127 * 2128 * @return true when the enter transition should start as soon as possible or false to 2129 * when it should wait until the exiting transition completes. 2130 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap 2131 */ 2132 public boolean getAllowEnterTransitionOverlap() { 2133 return (mAllowEnterTransitionOverlap == null) ? true : mAllowEnterTransitionOverlap; 2134 } 2135 2136 /** 2137 * Sets whether the the return transition and reenter transition overlap or not. 2138 * When true, the reenter transition will start as soon as possible. When false, the 2139 * reenter transition will wait until the return transition completes before starting. 2140 * 2141 * @param allow true to start the reenter transition when possible or false to wait until the 2142 * return transition completes. 2143 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap 2144 */ 2145 public void setAllowReturnTransitionOverlap(boolean allow) { 2146 mAllowReturnTransitionOverlap = allow; 2147 } 2148 2149 /** 2150 * Returns whether the the return transition and reenter transition overlap or not. 2151 * When true, the reenter transition will start as soon as possible. When false, the 2152 * reenter transition will wait until the return transition completes before starting. 2153 * 2154 * @return true to start the reenter transition when possible or false to wait until the 2155 * return transition completes. 2156 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap 2157 */ 2158 public boolean getAllowReturnTransitionOverlap() { 2159 return (mAllowReturnTransitionOverlap == null) ? true : mAllowReturnTransitionOverlap; 2160 } 2161 2162 /** 2163 * Print the Fragments's state into the given stream. 2164 * 2165 * @param prefix Text to print at the front of each line. 2166 * @param fd The raw file descriptor that the dump is being sent to. 2167 * @param writer The PrintWriter to which you should dump your state. This will be 2168 * closed for you after you return. 2169 * @param args additional arguments to the dump request. 2170 */ 2171 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 2172 writer.print(prefix); writer.print("mFragmentId=#"); 2173 writer.print(Integer.toHexString(mFragmentId)); 2174 writer.print(" mContainerId=#"); 2175 writer.print(Integer.toHexString(mContainerId)); 2176 writer.print(" mTag="); writer.println(mTag); 2177 writer.print(prefix); writer.print("mState="); writer.print(mState); 2178 writer.print(" mIndex="); writer.print(mIndex); 2179 writer.print(" mWho="); writer.print(mWho); 2180 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting); 2181 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded); 2182 writer.print(" mRemoving="); writer.print(mRemoving); 2183 writer.print(" mFromLayout="); writer.print(mFromLayout); 2184 writer.print(" mInLayout="); writer.println(mInLayout); 2185 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden); 2186 writer.print(" mDetached="); writer.print(mDetached); 2187 writer.print(" mMenuVisible="); writer.print(mMenuVisible); 2188 writer.print(" mHasMenu="); writer.println(mHasMenu); 2189 writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance); 2190 writer.print(" mRetaining="); writer.print(mRetaining); 2191 writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint); 2192 if (mFragmentManager != null) { 2193 writer.print(prefix); writer.print("mFragmentManager="); 2194 writer.println(mFragmentManager); 2195 } 2196 if (mHost != null) { 2197 writer.print(prefix); writer.print("mHost="); 2198 writer.println(mHost); 2199 } 2200 if (mParentFragment != null) { 2201 writer.print(prefix); writer.print("mParentFragment="); 2202 writer.println(mParentFragment); 2203 } 2204 if (mArguments != null) { 2205 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments); 2206 } 2207 if (mSavedFragmentState != null) { 2208 writer.print(prefix); writer.print("mSavedFragmentState="); 2209 writer.println(mSavedFragmentState); 2210 } 2211 if (mSavedViewState != null) { 2212 writer.print(prefix); writer.print("mSavedViewState="); 2213 writer.println(mSavedViewState); 2214 } 2215 if (mTarget != null) { 2216 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget); 2217 writer.print(" mTargetRequestCode="); 2218 writer.println(mTargetRequestCode); 2219 } 2220 if (mNextAnim != 0) { 2221 writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim); 2222 } 2223 if (mContainer != null) { 2224 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer); 2225 } 2226 if (mView != null) { 2227 writer.print(prefix); writer.print("mView="); writer.println(mView); 2228 } 2229 if (mAnimatingAway != null) { 2230 writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway); 2231 writer.print(prefix); writer.print("mStateAfterAnimating="); 2232 writer.println(mStateAfterAnimating); 2233 } 2234 if (mLoaderManager != null) { 2235 writer.print(prefix); writer.println("Loader Manager:"); 2236 mLoaderManager.dump(prefix + " ", fd, writer, args); 2237 } 2238 if (mChildFragmentManager != null) { 2239 writer.print(prefix); writer.println("Child " + mChildFragmentManager + ":"); 2240 mChildFragmentManager.dump(prefix + " ", fd, writer, args); 2241 } 2242 } 2243 2244 Fragment findFragmentByWho(String who) { 2245 if (who.equals(mWho)) { 2246 return this; 2247 } 2248 if (mChildFragmentManager != null) { 2249 return mChildFragmentManager.findFragmentByWho(who); 2250 } 2251 return null; 2252 } 2253 2254 void instantiateChildFragmentManager() { 2255 mChildFragmentManager = new FragmentManagerImpl(); 2256 mChildFragmentManager.attachController(mHost, new FragmentContainer() { 2257 @Override 2258 @Nullable 2259 public View onFindViewById(int id) { 2260 if (mView == null) { 2261 throw new IllegalStateException("Fragment does not have a view"); 2262 } 2263 return mView.findViewById(id); 2264 } 2265 2266 @Override 2267 public boolean onHasView() { 2268 return (mView != null); 2269 } 2270 }, this); 2271 } 2272 2273 void performCreate(Bundle savedInstanceState) { 2274 if (mChildFragmentManager != null) { 2275 mChildFragmentManager.noteStateNotSaved(); 2276 } 2277 mState = CREATED; 2278 mCalled = false; 2279 onCreate(savedInstanceState); 2280 if (!mCalled) { 2281 throw new SuperNotCalledException("Fragment " + this 2282 + " did not call through to super.onCreate()"); 2283 } 2284 final Context context = getContext(); 2285 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0; 2286 if (version < Build.VERSION_CODES.N) { 2287 restoreChildFragmentState(savedInstanceState, false); 2288 } 2289 } 2290 2291 View performCreateView(LayoutInflater inflater, ViewGroup container, 2292 Bundle savedInstanceState) { 2293 if (mChildFragmentManager != null) { 2294 mChildFragmentManager.noteStateNotSaved(); 2295 } 2296 return onCreateView(inflater, container, savedInstanceState); 2297 } 2298 2299 void performActivityCreated(Bundle savedInstanceState) { 2300 if (mChildFragmentManager != null) { 2301 mChildFragmentManager.noteStateNotSaved(); 2302 } 2303 mState = ACTIVITY_CREATED; 2304 mCalled = false; 2305 onActivityCreated(savedInstanceState); 2306 if (!mCalled) { 2307 throw new SuperNotCalledException("Fragment " + this 2308 + " did not call through to super.onActivityCreated()"); 2309 } 2310 if (mChildFragmentManager != null) { 2311 mChildFragmentManager.dispatchActivityCreated(); 2312 } 2313 } 2314 2315 void performStart() { 2316 if (mChildFragmentManager != null) { 2317 mChildFragmentManager.noteStateNotSaved(); 2318 mChildFragmentManager.execPendingActions(); 2319 } 2320 mState = STARTED; 2321 mCalled = false; 2322 onStart(); 2323 if (!mCalled) { 2324 throw new SuperNotCalledException("Fragment " + this 2325 + " did not call through to super.onStart()"); 2326 } 2327 if (mChildFragmentManager != null) { 2328 mChildFragmentManager.dispatchStart(); 2329 } 2330 if (mLoaderManager != null) { 2331 mLoaderManager.doReportStart(); 2332 } 2333 } 2334 2335 void performResume() { 2336 if (mChildFragmentManager != null) { 2337 mChildFragmentManager.noteStateNotSaved(); 2338 mChildFragmentManager.execPendingActions(); 2339 } 2340 mState = RESUMED; 2341 mCalled = false; 2342 onResume(); 2343 if (!mCalled) { 2344 throw new SuperNotCalledException("Fragment " + this 2345 + " did not call through to super.onResume()"); 2346 } 2347 if (mChildFragmentManager != null) { 2348 mChildFragmentManager.dispatchResume(); 2349 mChildFragmentManager.execPendingActions(); 2350 } 2351 } 2352 2353 void performMultiWindowModeChanged(boolean isInMultiWindowMode) { 2354 onMultiWindowModeChanged(isInMultiWindowMode); 2355 if (mChildFragmentManager != null) { 2356 mChildFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode); 2357 } 2358 } 2359 2360 void performPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 2361 onPictureInPictureModeChanged(isInPictureInPictureMode); 2362 if (mChildFragmentManager != null) { 2363 mChildFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode); 2364 } 2365 } 2366 2367 void performConfigurationChanged(Configuration newConfig) { 2368 onConfigurationChanged(newConfig); 2369 if (mChildFragmentManager != null) { 2370 mChildFragmentManager.dispatchConfigurationChanged(newConfig); 2371 } 2372 } 2373 2374 void performLowMemory() { 2375 onLowMemory(); 2376 if (mChildFragmentManager != null) { 2377 mChildFragmentManager.dispatchLowMemory(); 2378 } 2379 } 2380 2381 void performTrimMemory(int level) { 2382 onTrimMemory(level); 2383 if (mChildFragmentManager != null) { 2384 mChildFragmentManager.dispatchTrimMemory(level); 2385 } 2386 } 2387 2388 boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) { 2389 boolean show = false; 2390 if (!mHidden) { 2391 if (mHasMenu && mMenuVisible) { 2392 show = true; 2393 onCreateOptionsMenu(menu, inflater); 2394 } 2395 if (mChildFragmentManager != null) { 2396 show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater); 2397 } 2398 } 2399 return show; 2400 } 2401 2402 boolean performPrepareOptionsMenu(Menu menu) { 2403 boolean show = false; 2404 if (!mHidden) { 2405 if (mHasMenu && mMenuVisible) { 2406 show = true; 2407 onPrepareOptionsMenu(menu); 2408 } 2409 if (mChildFragmentManager != null) { 2410 show |= mChildFragmentManager.dispatchPrepareOptionsMenu(menu); 2411 } 2412 } 2413 return show; 2414 } 2415 2416 boolean performOptionsItemSelected(MenuItem item) { 2417 if (!mHidden) { 2418 if (mHasMenu && mMenuVisible) { 2419 if (onOptionsItemSelected(item)) { 2420 return true; 2421 } 2422 } 2423 if (mChildFragmentManager != null) { 2424 if (mChildFragmentManager.dispatchOptionsItemSelected(item)) { 2425 return true; 2426 } 2427 } 2428 } 2429 return false; 2430 } 2431 2432 boolean performContextItemSelected(MenuItem item) { 2433 if (!mHidden) { 2434 if (onContextItemSelected(item)) { 2435 return true; 2436 } 2437 if (mChildFragmentManager != null) { 2438 if (mChildFragmentManager.dispatchContextItemSelected(item)) { 2439 return true; 2440 } 2441 } 2442 } 2443 return false; 2444 } 2445 2446 void performOptionsMenuClosed(Menu menu) { 2447 if (!mHidden) { 2448 if (mHasMenu && mMenuVisible) { 2449 onOptionsMenuClosed(menu); 2450 } 2451 if (mChildFragmentManager != null) { 2452 mChildFragmentManager.dispatchOptionsMenuClosed(menu); 2453 } 2454 } 2455 } 2456 2457 void performSaveInstanceState(Bundle outState) { 2458 onSaveInstanceState(outState); 2459 if (mChildFragmentManager != null) { 2460 Parcelable p = mChildFragmentManager.saveAllState(); 2461 if (p != null) { 2462 outState.putParcelable(Activity.FRAGMENTS_TAG, p); 2463 } 2464 } 2465 } 2466 2467 void performPause() { 2468 if (mChildFragmentManager != null) { 2469 mChildFragmentManager.dispatchPause(); 2470 } 2471 mState = STARTED; 2472 mCalled = false; 2473 onPause(); 2474 if (!mCalled) { 2475 throw new SuperNotCalledException("Fragment " + this 2476 + " did not call through to super.onPause()"); 2477 } 2478 } 2479 2480 void performStop() { 2481 if (mChildFragmentManager != null) { 2482 mChildFragmentManager.dispatchStop(); 2483 } 2484 mState = STOPPED; 2485 mCalled = false; 2486 onStop(); 2487 if (!mCalled) { 2488 throw new SuperNotCalledException("Fragment " + this 2489 + " did not call through to super.onStop()"); 2490 } 2491 2492 if (mLoadersStarted) { 2493 mLoadersStarted = false; 2494 if (!mCheckedForLoaderManager) { 2495 mCheckedForLoaderManager = true; 2496 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false); 2497 } 2498 if (mLoaderManager != null) { 2499 if (mHost.getRetainLoaders()) { 2500 mLoaderManager.doRetain(); 2501 } else { 2502 mLoaderManager.doStop(); 2503 } 2504 } 2505 } 2506 } 2507 2508 void performDestroyView() { 2509 if (mChildFragmentManager != null) { 2510 mChildFragmentManager.dispatchDestroyView(); 2511 } 2512 mState = CREATED; 2513 mCalled = false; 2514 onDestroyView(); 2515 if (!mCalled) { 2516 throw new SuperNotCalledException("Fragment " + this 2517 + " did not call through to super.onDestroyView()"); 2518 } 2519 if (mLoaderManager != null) { 2520 mLoaderManager.doReportNextStart(); 2521 } 2522 } 2523 2524 void performDestroy() { 2525 if (mChildFragmentManager != null) { 2526 mChildFragmentManager.dispatchDestroy(); 2527 } 2528 mState = INITIALIZING; 2529 mCalled = false; 2530 onDestroy(); 2531 if (!mCalled) { 2532 throw new SuperNotCalledException("Fragment " + this 2533 + " did not call through to super.onDestroy()"); 2534 } 2535 mChildFragmentManager = null; 2536 } 2537 2538 private static Transition loadTransition(Context context, TypedArray typedArray, 2539 Transition currentValue, Transition defaultValue, int id) { 2540 if (currentValue != defaultValue) { 2541 return currentValue; 2542 } 2543 int transitionId = typedArray.getResourceId(id, 0); 2544 Transition transition = defaultValue; 2545 if (transitionId != 0 && transitionId != com.android.internal.R.transition.no_transition) { 2546 TransitionInflater inflater = TransitionInflater.from(context); 2547 transition = inflater.inflateTransition(transitionId); 2548 if (transition instanceof TransitionSet && 2549 ((TransitionSet)transition).getTransitionCount() == 0) { 2550 transition = null; 2551 } 2552 } 2553 return transition; 2554 } 2555 2556} 2557