Fragment.java revision 542f2409d9be9211dcb8cdfe2f863c22ff6cf26f
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.content.ComponentCallbacks; 21import android.content.Context; 22import android.content.Intent; 23import android.content.res.Configuration; 24import android.content.res.Resources; 25import android.os.Bundle; 26import android.os.Parcel; 27import android.os.Parcelable; 28import android.util.AndroidRuntimeException; 29import android.util.AttributeSet; 30import android.util.DebugUtils; 31import android.util.SparseArray; 32import android.view.ContextMenu; 33import android.view.LayoutInflater; 34import android.view.Menu; 35import android.view.MenuInflater; 36import android.view.MenuItem; 37import android.view.View; 38import android.view.ViewGroup; 39import android.view.ContextMenu.ContextMenuInfo; 40import android.view.View.OnCreateContextMenuListener; 41import android.widget.AdapterView; 42 43import java.io.FileDescriptor; 44import java.io.PrintWriter; 45import java.util.HashMap; 46 47final class FragmentState implements Parcelable { 48 final String mClassName; 49 final int mIndex; 50 final boolean mFromLayout; 51 final int mFragmentId; 52 final int mContainerId; 53 final String mTag; 54 final boolean mRetainInstance; 55 final boolean mDetached; 56 final Bundle mArguments; 57 58 Bundle mSavedFragmentState; 59 60 Fragment mInstance; 61 62 public FragmentState(Fragment frag) { 63 mClassName = frag.getClass().getName(); 64 mIndex = frag.mIndex; 65 mFromLayout = frag.mFromLayout; 66 mFragmentId = frag.mFragmentId; 67 mContainerId = frag.mContainerId; 68 mTag = frag.mTag; 69 mRetainInstance = frag.mRetainInstance; 70 mDetached = frag.mDetached; 71 mArguments = frag.mArguments; 72 } 73 74 public FragmentState(Parcel in) { 75 mClassName = in.readString(); 76 mIndex = in.readInt(); 77 mFromLayout = in.readInt() != 0; 78 mFragmentId = in.readInt(); 79 mContainerId = in.readInt(); 80 mTag = in.readString(); 81 mRetainInstance = in.readInt() != 0; 82 mDetached = in.readInt() != 0; 83 mArguments = in.readBundle(); 84 mSavedFragmentState = in.readBundle(); 85 } 86 87 public Fragment instantiate(Activity activity) { 88 if (mInstance != null) { 89 return mInstance; 90 } 91 92 if (mArguments != null) { 93 mArguments.setClassLoader(activity.getClassLoader()); 94 } 95 96 mInstance = Fragment.instantiate(activity, mClassName, mArguments); 97 98 if (mSavedFragmentState != null) { 99 mSavedFragmentState.setClassLoader(activity.getClassLoader()); 100 mInstance.mSavedFragmentState = mSavedFragmentState; 101 } 102 mInstance.setIndex(mIndex); 103 mInstance.mFromLayout = mFromLayout; 104 mInstance.mRestored = true; 105 mInstance.mFragmentId = mFragmentId; 106 mInstance.mContainerId = mContainerId; 107 mInstance.mTag = mTag; 108 mInstance.mRetainInstance = mRetainInstance; 109 mInstance.mDetached = mDetached; 110 mInstance.mFragmentManager = activity.mFragments; 111 112 return mInstance; 113 } 114 115 public int describeContents() { 116 return 0; 117 } 118 119 public void writeToParcel(Parcel dest, int flags) { 120 dest.writeString(mClassName); 121 dest.writeInt(mIndex); 122 dest.writeInt(mFromLayout ? 1 : 0); 123 dest.writeInt(mFragmentId); 124 dest.writeInt(mContainerId); 125 dest.writeString(mTag); 126 dest.writeInt(mRetainInstance ? 1 : 0); 127 dest.writeInt(mDetached ? 1 : 0); 128 dest.writeBundle(mArguments); 129 dest.writeBundle(mSavedFragmentState); 130 } 131 132 public static final Parcelable.Creator<FragmentState> CREATOR 133 = new Parcelable.Creator<FragmentState>() { 134 public FragmentState createFromParcel(Parcel in) { 135 return new FragmentState(in); 136 } 137 138 public FragmentState[] newArray(int size) { 139 return new FragmentState[size]; 140 } 141 }; 142} 143 144/** 145 * A Fragment is a piece of an application's user interface or behavior 146 * that can be placed in an {@link Activity}. Interaction with fragments 147 * is done through {@link FragmentManager}, which can be obtained via 148 * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and 149 * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}. 150 * 151 * <p>The Fragment class can be used many ways to achieve a wide variety of 152 * results. It is core, it represents a particular operation or interface 153 * that is running within a larger {@link Activity}. A Fragment is closely 154 * tied to the Activity it is in, and can not be used apart from one. Though 155 * Fragment defines its own lifecycle, that lifecycle is dependent on its 156 * activity: if the activity is stopped, no fragments inside of it can be 157 * started; when the activity is destroyed, all fragments will be destroyed. 158 * 159 * <p>All subclasses of Fragment must include a public empty constructor. 160 * The framework will often re-instantiate a fragment class when needed, 161 * in particular during state restore, and needs to be able to find this 162 * constructor to instantiate it. If the empty constructor is not available, 163 * a runtime exception will occur in some cases during state restore. 164 * 165 * <p>Topics covered here: 166 * <ol> 167 * <li><a href="#OlderPlatforms">Older Platforms</a> 168 * <li><a href="#Lifecycle">Lifecycle</a> 169 * <li><a href="#Layout">Layout</a> 170 * <li><a href="#BackStack">Back Stack</a> 171 * </ol> 172 * 173 * <a name="OlderPlatforms"></a> 174 * <h3>Older Platforms</h3> 175 * 176 * While the Fragment API was introduced in 177 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API 178 * is also available for use on older platforms. See the blog post 179 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html"> 180 * Fragments For All</a> for more details. 181 * 182 * <a name="Lifecycle"></a> 183 * <h3>Lifecycle</h3> 184 * 185 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has 186 * its own wrinkle on the standard activity lifecycle. It includes basic 187 * activity lifecycle methods such as {@link #onResume}, but also important 188 * are methods related to interactions with the activity and UI generation. 189 * 190 * <p>The core series of lifecycle methods that are called to bring a fragment 191 * up to resumed state (interacting with the user) are: 192 * 193 * <ol> 194 * <li> {@link #onAttach} called once the fragment is associated with its activity. 195 * <li> {@link #onCreate} called to do initial creation of the fragment. 196 * <li> {@link #onCreateView} creates and returns the view hierarchy associated 197 * with the fragment. 198 * <li> {@link #onActivityCreated} tells the fragment that its activity has 199 * completed its own {@link Activity#onCreate Activity.onCreaate}. 200 * <li> {@link #onStart} makes the fragment visible to the user (based on its 201 * containing activity being started). 202 * <li> {@link #onResume} makes the fragment interacting with the user (based on its 203 * containing activity being resumed). 204 * </ol> 205 * 206 * <p>As a fragment is no longer being used, it goes through a reverse 207 * series of callbacks: 208 * 209 * <ol> 210 * <li> {@link #onPause} fragment is no longer interacting with the user either 211 * because its activity is being paused or a fragment operation is modifying it 212 * in the activity. 213 * <li> {@link #onStop} fragment is no longer visible to the user either 214 * because its activity is being stopped or a fragment operation is modifying it 215 * in the activity. 216 * <li> {@link #onDestroyView} allows the fragment to clean up resources 217 * associated with its View. 218 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state. 219 * <li> {@link #onDetach} called immediately prior to the fragment no longer 220 * being associated with its activity. 221 * </ol> 222 * 223 * <a name="Layout"></a> 224 * <h3>Layout</h3> 225 * 226 * <p>Fragments can be used as part of your application's layout, allowing 227 * you to better modularize your code and more easily adjust your user 228 * interface to the screen it is running on. As an example, we can look 229 * at a simple program consisting of a list of items, and display of the 230 * details of each item.</p> 231 * 232 * <p>An activity's layout XML can include <code><fragment></code> tags 233 * to embed fragment instances inside of the layout. For example, here is 234 * a simple layout that embeds one fragment:</p> 235 * 236 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout} 237 * 238 * <p>The layout is installed in the activity in the normal way:</p> 239 * 240 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 241 * main} 242 * 243 * <p>The titles fragment, showing a list of titles, is fairly simple, relying 244 * on {@link ListFragment} for most of its work. Note the implementation of 245 * clicking an item: depending on the current activity's layout, it can either 246 * create and display a new fragment to show the details in-place (more about 247 * this later), or start a new activity to show the details.</p> 248 * 249 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 250 * titles} 251 * 252 * <p>The details fragment showing the contents of a selected item just 253 * displays a string of text based on an index of a string array built in to 254 * the app:</p> 255 * 256 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 257 * details} 258 * 259 * <p>In this case when the user clicks on a title, there is no details 260 * container in the current activity, so the titles fragment's click code will 261 * launch a new activity to display the details fragment:</p> 262 * 263 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java 264 * details_activity} 265 * 266 * <p>However the screen may be large enough to show both the list of titles 267 * and details about the currently selected title. To use such a layout on 268 * a landscape screen, this alternative layout can be placed under layout-land:</p> 269 * 270 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout} 271 * 272 * <p>Note how the prior code will adjust to this alternative UI flow: the titles 273 * fragment will now embed the details fragment inside of this activity, and the 274 * details activity will finish itself if it is running in a configuration 275 * where the details can be shown in-place. 276 * 277 * <p>When a configuration change causes the activity hosting these fragments 278 * to restart, its new instance may use a different layout that doesn't 279 * include the same fragments as the previous layout. In this case all of 280 * the previous fragments will still be instantiated and running in the new 281 * instance. However, any that are no longer associated with a <fragment> 282 * tag in the view hierarchy will not have their content view created 283 * and will return false from {@link #isInLayout}. (The code here also shows 284 * how you can determine if a fragment placed in a container is no longer 285 * running in a layout with that container and avoid creating its view hierarchy 286 * in that case.) 287 * 288 * <p>The attributes of the <fragment> tag are used to control the 289 * LayoutParams provided when attaching the fragment's view to the parent 290 * container. They can also be parsed by the fragment in {@link #onInflate} 291 * as parameters. 292 * 293 * <p>The fragment being instantiated must have some kind of unique identifier 294 * so that it can be re-associated with a previous instance if the parent 295 * activity needs to be destroyed and recreated. This can be provided these 296 * ways: 297 * 298 * <ul> 299 * <li>If nothing is explicitly supplied, the view ID of the container will 300 * be used. 301 * <li><code>android:tag</code> can be used in <fragment> to provide 302 * a specific tag name for the fragment. 303 * <li><code>android:id</code> can be used in <fragment> to provide 304 * a specific identifier for the fragment. 305 * </ul> 306 * 307 * <a name="BackStack"></a> 308 * <h3>Back Stack</h3> 309 * 310 * <p>The transaction in which fragments are modified can be placed on an 311 * internal back-stack of the owning activity. When the user presses back 312 * in the activity, any transactions on the back stack are popped off before 313 * the activity itself is finished. 314 * 315 * <p>For example, consider this simple fragment that is instantiated with 316 * an integer argument and displays that in a TextView in its UI:</p> 317 * 318 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java 319 * fragment} 320 * 321 * <p>A function that creates a new instance of the fragment, replacing 322 * whatever current fragment instance is being shown and pushing that change 323 * on to the back stack could be written as: 324 * 325 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java 326 * add_stack} 327 * 328 * <p>After each call to this function, a new entry is on the stack, and 329 * pressing back will pop it to return the user to whatever previous state 330 * the activity UI was in. 331 */ 332public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener { 333 private static final HashMap<String, Class<?>> sClassMap = 334 new HashMap<String, Class<?>>(); 335 336 static final int INITIALIZING = 0; // Not yet created. 337 static final int CREATED = 1; // Created. 338 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation. 339 static final int STOPPED = 3; // Fully created, not started. 340 static final int STARTED = 4; // Created and started, not resumed. 341 static final int RESUMED = 5; // Created started and resumed. 342 343 int mState = INITIALIZING; 344 345 // Non-null if the fragment's view hierarchy is currently animating away, 346 // meaning we need to wait a bit on completely destroying it. This is the 347 // animation that is running. 348 Animator mAnimatingAway; 349 350 // If mAnimatingAway != null, this is the state we should move to once the 351 // animation is done. 352 int mStateAfterAnimating; 353 354 // When instantiated from saved state, this is the saved state. 355 Bundle mSavedFragmentState; 356 SparseArray<Parcelable> mSavedViewState; 357 358 // Index into active fragment array. 359 int mIndex = -1; 360 361 // Internal unique name for this fragment; 362 String mWho; 363 364 // Construction arguments; 365 Bundle mArguments; 366 367 // Target fragment. 368 Fragment mTarget; 369 370 // Target request code. 371 int mTargetRequestCode; 372 373 // True if the fragment is in the list of added fragments. 374 boolean mAdded; 375 376 // If set this fragment is being removed from its activity. 377 boolean mRemoving; 378 379 // True if the fragment is in the resumed state. 380 boolean mResumed; 381 382 // Set to true if this fragment was instantiated from a layout file. 383 boolean mFromLayout; 384 385 // Set to true when the view has actually been inflated in its layout. 386 boolean mInLayout; 387 388 // True if this fragment has been restored from previously saved state. 389 boolean mRestored; 390 391 // Number of active back stack entries this fragment is in. 392 int mBackStackNesting; 393 394 // The fragment manager we are associated with. Set as soon as the 395 // fragment is used in a transaction; cleared after it has been removed 396 // from all transactions. 397 FragmentManager mFragmentManager; 398 399 // Set as soon as a fragment is added to a transaction (or removed), 400 // to be able to do validation. 401 Activity mImmediateActivity; 402 403 // Activity this fragment is attached to. 404 Activity mActivity; 405 406 // The optional identifier for this fragment -- either the container ID if it 407 // was dynamically added to the view hierarchy, or the ID supplied in 408 // layout. 409 int mFragmentId; 410 411 // When a fragment is being dynamically added to the view hierarchy, this 412 // is the identifier of the parent container it is being added to. 413 int mContainerId; 414 415 // The optional named tag for this fragment -- usually used to find 416 // fragments that are not part of the layout. 417 String mTag; 418 419 // Set to true when the app has requested that this fragment be hidden 420 // from the user. 421 boolean mHidden; 422 423 // Set to true when the app has requested that this fragment be detached. 424 boolean mDetached; 425 426 // If set this fragment would like its instance retained across 427 // configuration changes. 428 boolean mRetainInstance; 429 430 // If set this fragment is being retained across the current config change. 431 boolean mRetaining; 432 433 // If set this fragment has menu items to contribute. 434 boolean mHasMenu; 435 436 // Used to verify that subclasses call through to super class. 437 boolean mCalled; 438 439 // If app has requested a specific animation, this is the one to use. 440 int mNextAnim; 441 442 // The parent container of the fragment after dynamically added to UI. 443 ViewGroup mContainer; 444 445 // The View generated for this fragment. 446 View mView; 447 448 LoaderManagerImpl mLoaderManager; 449 boolean mLoadersStarted; 450 boolean mCheckedForLoaderManager; 451 452 /** 453 * State information that has been retrieved from a fragment instance 454 * through {@link FragmentManager#saveFragmentInstanceState(Fragment) 455 * FragmentManager.saveFragmentInstanceState}. 456 */ 457 public static class SavedState implements Parcelable { 458 final Bundle mState; 459 460 SavedState(Bundle state) { 461 mState = state; 462 } 463 464 SavedState(Parcel in, ClassLoader loader) { 465 mState = in.readBundle(); 466 if (loader != null && mState != null) { 467 mState.setClassLoader(loader); 468 } 469 } 470 471 @Override 472 public int describeContents() { 473 return 0; 474 } 475 476 @Override 477 public void writeToParcel(Parcel dest, int flags) { 478 dest.writeBundle(mState); 479 } 480 481 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR 482 = new Parcelable.ClassLoaderCreator<SavedState>() { 483 public SavedState createFromParcel(Parcel in) { 484 return new SavedState(in, null); 485 } 486 487 public SavedState createFromParcel(Parcel in, ClassLoader loader) { 488 return new SavedState(in, loader); 489 } 490 491 public SavedState[] newArray(int size) { 492 return new SavedState[size]; 493 } 494 }; 495 } 496 497 /** 498 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when 499 * there is an instantiation failure. 500 */ 501 static public class InstantiationException extends AndroidRuntimeException { 502 public InstantiationException(String msg, Exception cause) { 503 super(msg, cause); 504 } 505 } 506 507 /** 508 * Default constructor. <strong>Every</strong> fragment must have an 509 * empty constructor, so it can be instantiated when restoring its 510 * activity's state. It is strongly recommended that subclasses do not 511 * have other constructors with parameters, since these constructors 512 * will not be called when the fragment is re-instantiated; instead, 513 * arguments can be supplied by the caller with {@link #setArguments} 514 * and later retrieved by the Fragment with {@link #getArguments}. 515 * 516 * <p>Applications should generally not implement a constructor. The 517 * first place application code an run where the fragment is ready to 518 * be used is in {@link #onAttach(Activity)}, the point where the fragment 519 * is actually associated with its activity. Some applications may also 520 * want to implement {@link #onInflate} to retrieve attributes from a 521 * layout resource, though should take care here because this happens for 522 * the fragment is attached to its activity. 523 */ 524 public Fragment() { 525 } 526 527 /** 528 * Like {@link #instantiate(Context, String, Bundle)} but with a null 529 * argument Bundle. 530 */ 531 public static Fragment instantiate(Context context, String fname) { 532 return instantiate(context, fname, null); 533 } 534 535 /** 536 * Create a new instance of a Fragment with the given class name. This is 537 * the same as calling its empty constructor. 538 * 539 * @param context The calling context being used to instantiate the fragment. 540 * This is currently just used to get its ClassLoader. 541 * @param fname The class name of the fragment to instantiate. 542 * @param args Bundle of arguments to supply to the fragment, which it 543 * can retrieve with {@link #getArguments()}. May be null. 544 * @return Returns a new fragment instance. 545 * @throws InstantiationException If there is a failure in instantiating 546 * the given fragment class. This is a runtime exception; it is not 547 * normally expected to happen. 548 */ 549 public static Fragment instantiate(Context context, String fname, Bundle args) { 550 try { 551 Class<?> clazz = sClassMap.get(fname); 552 if (clazz == null) { 553 // Class not found in the cache, see if it's real, and try to add it 554 clazz = context.getClassLoader().loadClass(fname); 555 sClassMap.put(fname, clazz); 556 } 557 Fragment f = (Fragment)clazz.newInstance(); 558 if (args != null) { 559 args.setClassLoader(f.getClass().getClassLoader()); 560 f.mArguments = args; 561 } 562 return f; 563 } catch (ClassNotFoundException e) { 564 throw new InstantiationException("Unable to instantiate fragment " + fname 565 + ": make sure class name exists, is public, and has an" 566 + " empty constructor that is public", e); 567 } catch (java.lang.InstantiationException e) { 568 throw new InstantiationException("Unable to instantiate fragment " + fname 569 + ": make sure class name exists, is public, and has an" 570 + " empty constructor that is public", e); 571 } catch (IllegalAccessException e) { 572 throw new InstantiationException("Unable to instantiate fragment " + fname 573 + ": make sure class name exists, is public, and has an" 574 + " empty constructor that is public", e); 575 } 576 } 577 578 final void restoreViewState() { 579 if (mSavedViewState != null) { 580 mView.restoreHierarchyState(mSavedViewState); 581 mSavedViewState = null; 582 } 583 } 584 585 final void setIndex(int index) { 586 mIndex = index; 587 mWho = "android:fragment:" + mIndex; 588 } 589 590 final boolean isInBackStack() { 591 return mBackStackNesting > 0; 592 } 593 594 /** 595 * Subclasses can not override equals(). 596 */ 597 @Override final public boolean equals(Object o) { 598 return super.equals(o); 599 } 600 601 /** 602 * Subclasses can not override hashCode(). 603 */ 604 @Override final public int hashCode() { 605 return super.hashCode(); 606 } 607 608 @Override 609 public String toString() { 610 StringBuilder sb = new StringBuilder(128); 611 DebugUtils.buildShortClassTag(this, sb); 612 if (mIndex >= 0) { 613 sb.append(" #"); 614 sb.append(mIndex); 615 } 616 if (mFragmentId != 0) { 617 sb.append(" id=0x"); 618 sb.append(Integer.toHexString(mFragmentId)); 619 } 620 if (mTag != null) { 621 sb.append(" "); 622 sb.append(mTag); 623 } 624 sb.append('}'); 625 return sb.toString(); 626 } 627 628 /** 629 * Return the identifier this fragment is known by. This is either 630 * the android:id value supplied in a layout or the container view ID 631 * supplied when adding the fragment. 632 */ 633 final public int getId() { 634 return mFragmentId; 635 } 636 637 /** 638 * Get the tag name of the fragment, if specified. 639 */ 640 final public String getTag() { 641 return mTag; 642 } 643 644 /** 645 * Supply the construction arguments for this fragment. This can only 646 * be called before the fragment has been attached to its activity; that 647 * is, you should call it immediately after constructing the fragment. The 648 * arguments supplied here will be retained across fragment destroy and 649 * creation. 650 */ 651 public void setArguments(Bundle args) { 652 if (mIndex >= 0) { 653 throw new IllegalStateException("Fragment already active"); 654 } 655 mArguments = args; 656 } 657 658 /** 659 * Return the arguments supplied when the fragment was instantiated, 660 * if any. 661 */ 662 final public Bundle getArguments() { 663 return mArguments; 664 } 665 666 /** 667 * Set the initial saved state that this Fragment should restore itself 668 * from when first being constructed, as returned by 669 * {@link FragmentManager#saveFragmentInstanceState(Fragment) 670 * FragmentManager.saveFragmentInstanceState}. 671 * 672 * @param state The state the fragment should be restored from. 673 */ 674 public void setInitialSavedState(SavedState state) { 675 if (mIndex >= 0) { 676 throw new IllegalStateException("Fragment already active"); 677 } 678 mSavedFragmentState = state != null && state.mState != null 679 ? state.mState : null; 680 } 681 682 /** 683 * Optional target for this fragment. This may be used, for example, 684 * if this fragment is being started by another, and when done wants to 685 * give a result back to the first. The target set here is retained 686 * across instances via {@link FragmentManager#putFragment 687 * FragmentManager.putFragment()}. 688 * 689 * @param fragment The fragment that is the target of this one. 690 * @param requestCode Optional request code, for convenience if you 691 * are going to call back with {@link #onActivityResult(int, int, Intent)}. 692 */ 693 public void setTargetFragment(Fragment fragment, int requestCode) { 694 mTarget = fragment; 695 mTargetRequestCode = requestCode; 696 } 697 698 /** 699 * Return the target fragment set by {@link #setTargetFragment}. 700 */ 701 final public Fragment getTargetFragment() { 702 return mTarget; 703 } 704 705 /** 706 * Return the target request code set by {@link #setTargetFragment}. 707 */ 708 final public int getTargetRequestCode() { 709 return mTargetRequestCode; 710 } 711 712 /** 713 * Return the Activity this fragment is currently associated with. 714 */ 715 final public Activity getActivity() { 716 return mActivity; 717 } 718 719 /** 720 * Return <code>getActivity().getResources()</code>. 721 */ 722 final public Resources getResources() { 723 if (mActivity == null) { 724 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 725 } 726 return mActivity.getResources(); 727 } 728 729 /** 730 * Return a localized, styled CharSequence from the application's package's 731 * default string table. 732 * 733 * @param resId Resource id for the CharSequence text 734 */ 735 public final CharSequence getText(int resId) { 736 return getResources().getText(resId); 737 } 738 739 /** 740 * Return a localized string from the application's package's 741 * default string table. 742 * 743 * @param resId Resource id for the string 744 */ 745 public final String getString(int resId) { 746 return getResources().getString(resId); 747 } 748 749 /** 750 * Return a localized formatted string from the application's package's 751 * default string table, substituting the format arguments as defined in 752 * {@link java.util.Formatter} and {@link java.lang.String#format}. 753 * 754 * @param resId Resource id for the format string 755 * @param formatArgs The format arguments that will be used for substitution. 756 */ 757 758 public final String getString(int resId, Object... formatArgs) { 759 return getResources().getString(resId, formatArgs); 760 } 761 762 /** 763 * Return the FragmentManager for interacting with fragments associated 764 * with this fragment's activity. Note that this will be non-null slightly 765 * before {@link #getActivity()}, during the time from when the fragment is 766 * placed in a {@link FragmentTransaction} until it is committed and 767 * attached to its activity. 768 */ 769 final public FragmentManager getFragmentManager() { 770 return mFragmentManager; 771 } 772 773 /** 774 * Return true if the fragment is currently added to its activity. 775 */ 776 final public boolean isAdded() { 777 return mActivity != null && mAdded; 778 } 779 780 /** 781 * Return true if the fragment has been explicitly detached from the UI. 782 * That is, {@link FragmentTransaction#detach(Fragment) 783 * FragmentTransaction.detach(Fragment)} has been used on it. 784 */ 785 final public boolean isDetached() { 786 return mDetached; 787 } 788 789 /** 790 * Return true if this fragment is currently being removed from its 791 * activity. This is <em>not</em> whether its activity is finishing, but 792 * rather whether it is in the process of being removed from its activity. 793 */ 794 final public boolean isRemoving() { 795 return mRemoving; 796 } 797 798 /** 799 * Return true if the layout is included as part of an activity view 800 * hierarchy via the <fragment> tag. This will always be true when 801 * fragments are created through the <fragment> tag, <em>except</em> 802 * in the case where an old fragment is restored from a previous state and 803 * it does not appear in the layout of the current state. 804 */ 805 final public boolean isInLayout() { 806 return mInLayout; 807 } 808 809 /** 810 * Return true if the fragment is in the resumed state. This is true 811 * for the duration of {@link #onResume()} and {@link #onPause()} as well. 812 */ 813 final public boolean isResumed() { 814 return mResumed; 815 } 816 817 /** 818 * Return true if the fragment is currently visible to the user. This means 819 * it: (1) has been added, (2) has its view attached to the window, and 820 * (3) is not hidden. 821 */ 822 final public boolean isVisible() { 823 return isAdded() && !isHidden() && mView != null 824 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE; 825 } 826 827 /** 828 * Return true if the fragment has been hidden. By default fragments 829 * are shown. You can find out about changes to this state with 830 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal 831 * to other states -- that is, to be visible to the user, a fragment 832 * must be both started and not hidden. 833 */ 834 final public boolean isHidden() { 835 return mHidden; 836 } 837 838 /** 839 * Called when the hidden state (as returned by {@link #isHidden()} of 840 * the fragment has changed. Fragments start out not hidden; this will 841 * be called whenever the fragment changes state from that. 842 * @param hidden True if the fragment is now hidden, false if it is not 843 * visible. 844 */ 845 public void onHiddenChanged(boolean hidden) { 846 } 847 848 /** 849 * Control whether a fragment instance is retained across Activity 850 * re-creation (such as from a configuration change). This can only 851 * be used with fragments not in the back stack. If set, the fragment 852 * lifecycle will be slightly different when an activity is recreated: 853 * <ul> 854 * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still 855 * will be, because the fragment is being detached from its current activity). 856 * <li> {@link #onCreate(Bundle)} will not be called since the fragment 857 * is not being re-created. 858 * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b> 859 * still be called. 860 * </ul> 861 */ 862 public void setRetainInstance(boolean retain) { 863 mRetainInstance = retain; 864 } 865 866 final public boolean getRetainInstance() { 867 return mRetainInstance; 868 } 869 870 /** 871 * Report that this fragment would like to participate in populating 872 * the options menu by receiving a call to {@link #onCreateOptionsMenu} 873 * and related methods. 874 * 875 * @param hasMenu If true, the fragment has menu items to contribute. 876 */ 877 public void setHasOptionsMenu(boolean hasMenu) { 878 if (mHasMenu != hasMenu) { 879 mHasMenu = hasMenu; 880 if (isAdded() && !isHidden()) { 881 mActivity.invalidateOptionsMenu(); 882 } 883 } 884 } 885 886 /** 887 * Return the LoaderManager for this fragment, creating it if needed. 888 */ 889 public LoaderManager getLoaderManager() { 890 if (mLoaderManager != null) { 891 return mLoaderManager; 892 } 893 if (mActivity == null) { 894 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 895 } 896 mCheckedForLoaderManager = true; 897 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, true); 898 return mLoaderManager; 899 } 900 901 /** 902 * Call {@link Activity#startActivity(Intent)} on the fragment's 903 * containing Activity. 904 */ 905 public void startActivity(Intent intent) { 906 if (mActivity == null) { 907 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 908 } 909 mActivity.startActivityFromFragment(this, intent, -1); 910 } 911 912 /** 913 * Call {@link Activity#startActivityForResult(Intent, int)} on the fragment's 914 * containing Activity. 915 */ 916 public void startActivityForResult(Intent intent, int requestCode) { 917 if (mActivity == null) { 918 throw new IllegalStateException("Fragment " + this + " not attached to Activity"); 919 } 920 mActivity.startActivityFromFragment(this, intent, requestCode); 921 } 922 923 /** 924 * Receive the result from a previous call to 925 * {@link #startActivityForResult(Intent, int)}. This follows the 926 * related Activity API as described there in 927 * {@link Activity#onActivityResult(int, int, Intent)}. 928 * 929 * @param requestCode The integer request code originally supplied to 930 * startActivityForResult(), allowing you to identify who this 931 * result came from. 932 * @param resultCode The integer result code returned by the child activity 933 * through its setResult(). 934 * @param data An Intent, which can return result data to the caller 935 * (various data can be attached to Intent "extras"). 936 */ 937 public void onActivityResult(int requestCode, int resultCode, Intent data) { 938 } 939 940 /** 941 * @hide Hack so that DialogFragment can make its Dialog before creating 942 * its views, and the view construction can use the dialog's context for 943 * inflation. Maybe this should become a public API. Note sure. 944 */ 945 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) { 946 return mActivity.getLayoutInflater(); 947 } 948 949 /** 950 * @deprecated Use {@link #onInflate(Activity, AttributeSet, Bundle)} instead. 951 */ 952 @Deprecated 953 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) { 954 mCalled = true; 955 } 956 957 /** 958 * Called when a fragment is being created as part of a view layout 959 * inflation, typically from setting the content view of an activity. This 960 * may be called immediately after the fragment is created from a <fragment> 961 * tag in a layout file. Note this is <em>before</em> the fragment's 962 * {@link #onAttach(Activity)} has been called; all you should do here is 963 * parse the attributes and save them away. 964 * 965 * <p>This is called every time the fragment is inflated, even if it is 966 * being inflated into a new instance with saved state. It typically makes 967 * sense to re-parse the parameters each time, to allow them to change with 968 * different configurations.</p> 969 * 970 * <p>Here is a typical implementation of a fragment that can take parameters 971 * both through attributes supplied here as well from {@link #getArguments()}:</p> 972 * 973 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java 974 * fragment} 975 * 976 * <p>Note that parsing the XML attributes uses a "styleable" resource. The 977 * declaration for the styleable used here is:</p> 978 * 979 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments} 980 * 981 * <p>The fragment can then be declared within its activity's content layout 982 * through a tag like this:</p> 983 * 984 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes} 985 * 986 * <p>This fragment can also be created dynamically from arguments given 987 * at runtime in the arguments Bundle; here is an example of doing so at 988 * creation of the containing activity:</p> 989 * 990 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java 991 * create} 992 * 993 * @param activity The Activity that is inflating this fragment. 994 * @param attrs The attributes at the tag where the fragment is 995 * being created. 996 * @param savedInstanceState If the fragment is being re-created from 997 * a previous saved state, this is the state. 998 */ 999 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { 1000 onInflate(attrs, savedInstanceState); 1001 mCalled = true; 1002 } 1003 1004 /** 1005 * Called when a fragment is first attached to its activity. 1006 * {@link #onCreate(Bundle)} will be called after this. 1007 */ 1008 public void onAttach(Activity activity) { 1009 mCalled = true; 1010 } 1011 1012 /** 1013 * Called when a fragment loads an animation. 1014 */ 1015 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { 1016 return null; 1017 } 1018 1019 /** 1020 * Called to do initial creation of a fragment. This is called after 1021 * {@link #onAttach(Activity)} and before 1022 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. 1023 * 1024 * <p>Note that this can be called while the fragment's activity is 1025 * still in the process of being created. As such, you can not rely 1026 * on things like the activity's content view hierarchy being initialized 1027 * at this point. If you want to do work once the activity itself is 1028 * created, see {@link #onActivityCreated(Bundle)}. 1029 * 1030 * @param savedInstanceState If the fragment is being re-created from 1031 * a previous saved state, this is the state. 1032 */ 1033 public void onCreate(Bundle savedInstanceState) { 1034 mCalled = true; 1035 } 1036 1037 /** 1038 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} 1039 * has returned, but before any saved state has been restored in to the view. 1040 * This gives subclasses a chance to initialize themselves once 1041 * they know their view hierarchy has been completely created. The fragment's 1042 * view hierarchy is not however attached to its parent at this point. 1043 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. 1044 * @param savedInstanceState If non-null, this fragment is being re-constructed 1045 * from a previous saved state as given here. 1046 */ 1047 public void onViewCreated(View view, Bundle savedInstanceState) { 1048 } 1049 1050 /** 1051 * Called to have the fragment instantiate its user interface view. 1052 * This is optional, and non-graphical fragments can return null (which 1053 * is the default implementation). This will be called between 1054 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}. 1055 * 1056 * <p>If you return a View from here, you will later be called in 1057 * {@link #onDestroyView} when the view is being released. 1058 * 1059 * @param inflater The LayoutInflater object that can be used to inflate 1060 * any views in the fragment, 1061 * @param container If non-null, this is the parent view that the fragment's 1062 * UI should be attached to. The fragment should not add the view itself, 1063 * but this can be used to generate the LayoutParams of the view. 1064 * @param savedInstanceState If non-null, this fragment is being re-constructed 1065 * from a previous saved state as given here. 1066 * 1067 * @return Return the View for the fragment's UI, or null. 1068 */ 1069 public View onCreateView(LayoutInflater inflater, ViewGroup container, 1070 Bundle savedInstanceState) { 1071 return null; 1072 } 1073 1074 /** 1075 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}), 1076 * if provided. 1077 * 1078 * @return The fragment's root view, or null if it has no layout. 1079 */ 1080 public View getView() { 1081 return mView; 1082 } 1083 1084 /** 1085 * Called when the fragment's activity has been created and this 1086 * fragment's view hierarchy instantiated. It can be used to do final 1087 * initialization once these pieces are in place, such as retrieving 1088 * views or restoring state. It is also useful for fragments that use 1089 * {@link #setRetainInstance(boolean)} to retain their instance, 1090 * as this callback tells the fragment when it is fully associated with 1091 * the new activity instance. This is called after {@link #onCreateView} 1092 * and before {@link #onStart()}. 1093 * 1094 * @param savedInstanceState If the fragment is being re-created from 1095 * a previous saved state, this is the state. 1096 */ 1097 public void onActivityCreated(Bundle savedInstanceState) { 1098 mCalled = true; 1099 } 1100 1101 /** 1102 * Called when the Fragment is visible to the user. This is generally 1103 * tied to {@link Activity#onStart() Activity.onStart} of the containing 1104 * Activity's lifecycle. 1105 */ 1106 public void onStart() { 1107 mCalled = true; 1108 1109 if (!mLoadersStarted) { 1110 mLoadersStarted = true; 1111 if (!mCheckedForLoaderManager) { 1112 mCheckedForLoaderManager = true; 1113 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false); 1114 } 1115 if (mLoaderManager != null) { 1116 mLoaderManager.doStart(); 1117 } 1118 } 1119 } 1120 1121 /** 1122 * Called when the fragment is visible to the user and actively running. 1123 * This is generally 1124 * tied to {@link Activity#onResume() Activity.onResume} of the containing 1125 * Activity's lifecycle. 1126 */ 1127 public void onResume() { 1128 mCalled = true; 1129 } 1130 1131 /** 1132 * Called to ask the fragment to save its current dynamic state, so it 1133 * can later be reconstructed in a new instance of its process is 1134 * restarted. If a new instance of the fragment later needs to be 1135 * created, the data you place in the Bundle here will be available 1136 * in the Bundle given to {@link #onCreate(Bundle)}, 1137 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and 1138 * {@link #onActivityCreated(Bundle)}. 1139 * 1140 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle) 1141 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there 1142 * applies here as well. Note however: <em>this method may be called 1143 * at any time before {@link #onDestroy()}</em>. There are many situations 1144 * where a fragment may be mostly torn down (such as when placed on the 1145 * back stack with no UI showing), but its state will not be saved until 1146 * its owning activity actually needs to save its state. 1147 * 1148 * @param outState Bundle in which to place your saved state. 1149 */ 1150 public void onSaveInstanceState(Bundle outState) { 1151 } 1152 1153 public void onConfigurationChanged(Configuration newConfig) { 1154 mCalled = true; 1155 } 1156 1157 /** 1158 * Called when the Fragment is no longer resumed. This is generally 1159 * tied to {@link Activity#onPause() Activity.onPause} of the containing 1160 * Activity's lifecycle. 1161 */ 1162 public void onPause() { 1163 mCalled = true; 1164 } 1165 1166 /** 1167 * Called when the Fragment is no longer started. This is generally 1168 * tied to {@link Activity#onStop() Activity.onStop} of the containing 1169 * Activity's lifecycle. 1170 */ 1171 public void onStop() { 1172 mCalled = true; 1173 } 1174 1175 public void onLowMemory() { 1176 mCalled = true; 1177 } 1178 1179 /** 1180 * Called when the view previously created by {@link #onCreateView} has 1181 * been detached from the fragment. The next time the fragment needs 1182 * to be displayed, a new view will be created. This is called 1183 * after {@link #onStop()} and before {@link #onDestroy()}. It is called 1184 * <em>regardless</em> of whether {@link #onCreateView} returned a 1185 * non-null view. Internally it is called after the view's state has 1186 * been saved but before it has been removed from its parent. 1187 */ 1188 public void onDestroyView() { 1189 mCalled = true; 1190 } 1191 1192 /** 1193 * Called when the fragment is no longer in use. This is called 1194 * after {@link #onStop()} and before {@link #onDetach()}. 1195 */ 1196 public void onDestroy() { 1197 mCalled = true; 1198 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager 1199 // + " mLoaderManager=" + mLoaderManager); 1200 if (!mCheckedForLoaderManager) { 1201 mCheckedForLoaderManager = true; 1202 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false); 1203 } 1204 if (mLoaderManager != null) { 1205 mLoaderManager.doDestroy(); 1206 } 1207 } 1208 1209 /** 1210 * Called by the fragment manager once this fragment has been removed, 1211 * so that we don't have any left-over state if the application decides 1212 * to re-use the instance. This only clears state that the framework 1213 * internally manages, not things the application sets. 1214 */ 1215 void initState() { 1216 mIndex = -1; 1217 mWho = null; 1218 mAdded = false; 1219 mRemoving = false; 1220 mResumed = false; 1221 mFromLayout = false; 1222 mInLayout = false; 1223 mRestored = false; 1224 mBackStackNesting = 0; 1225 mFragmentManager = null; 1226 mActivity = mImmediateActivity = null; 1227 mFragmentId = 0; 1228 mContainerId = 0; 1229 mTag = null; 1230 mHidden = false; 1231 mDetached = false; 1232 mRetaining = false; 1233 mLoaderManager = null; 1234 mLoadersStarted = false; 1235 mCheckedForLoaderManager = false; 1236 } 1237 1238 /** 1239 * Called when the fragment is no longer attached to its activity. This 1240 * is called after {@link #onDestroy()}. 1241 */ 1242 public void onDetach() { 1243 mCalled = true; 1244 } 1245 1246 /** 1247 * Initialize the contents of the Activity's standard options menu. You 1248 * should place your menu items in to <var>menu</var>. For this method 1249 * to be called, you must have first called {@link #setHasOptionsMenu}. See 1250 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu} 1251 * for more information. 1252 * 1253 * @param menu The options menu in which you place your items. 1254 * 1255 * @see #setHasOptionsMenu 1256 * @see #onPrepareOptionsMenu 1257 * @see #onOptionsItemSelected 1258 */ 1259 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 1260 } 1261 1262 /** 1263 * Prepare the Screen's standard options menu to be displayed. This is 1264 * called right before the menu is shown, every time it is shown. You can 1265 * use this method to efficiently enable/disable items or otherwise 1266 * dynamically modify the contents. See 1267 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu} 1268 * for more information. 1269 * 1270 * @param menu The options menu as last shown or first initialized by 1271 * onCreateOptionsMenu(). 1272 * 1273 * @see #setHasOptionsMenu 1274 * @see #onCreateOptionsMenu 1275 */ 1276 public void onPrepareOptionsMenu(Menu menu) { 1277 } 1278 1279 /** 1280 * Called when this fragment's option menu items are no longer being 1281 * included in the overall options menu. Receiving this call means that 1282 * the menu needed to be rebuilt, but this fragment's items were not 1283 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)} 1284 * was not called). 1285 */ 1286 public void onDestroyOptionsMenu() { 1287 } 1288 1289 /** 1290 * This hook is called whenever an item in your options menu is selected. 1291 * The default implementation simply returns false to have the normal 1292 * processing happen (calling the item's Runnable or sending a message to 1293 * its Handler as appropriate). You can use this method for any items 1294 * for which you would like to do processing without those other 1295 * facilities. 1296 * 1297 * <p>Derived classes should call through to the base class for it to 1298 * perform the default menu handling. 1299 * 1300 * @param item The menu item that was selected. 1301 * 1302 * @return boolean Return false to allow normal menu processing to 1303 * proceed, true to consume it here. 1304 * 1305 * @see #onCreateOptionsMenu 1306 */ 1307 public boolean onOptionsItemSelected(MenuItem item) { 1308 return false; 1309 } 1310 1311 /** 1312 * This hook is called whenever the options menu is being closed (either by the user canceling 1313 * the menu with the back/menu button, or when an item is selected). 1314 * 1315 * @param menu The options menu as last shown or first initialized by 1316 * onCreateOptionsMenu(). 1317 */ 1318 public void onOptionsMenuClosed(Menu menu) { 1319 } 1320 1321 /** 1322 * Called when a context menu for the {@code view} is about to be shown. 1323 * Unlike {@link #onCreateOptionsMenu}, this will be called every 1324 * time the context menu is about to be shown and should be populated for 1325 * the view (or item inside the view for {@link AdapterView} subclasses, 1326 * this can be found in the {@code menuInfo})). 1327 * <p> 1328 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an 1329 * item has been selected. 1330 * <p> 1331 * The default implementation calls up to 1332 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though 1333 * you can not call this implementation if you don't want that behavior. 1334 * <p> 1335 * It is not safe to hold onto the context menu after this method returns. 1336 * {@inheritDoc} 1337 */ 1338 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 1339 getActivity().onCreateContextMenu(menu, v, menuInfo); 1340 } 1341 1342 /** 1343 * Registers a context menu to be shown for the given view (multiple views 1344 * can show the context menu). This method will set the 1345 * {@link OnCreateContextMenuListener} on the view to this fragment, so 1346 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be 1347 * called when it is time to show the context menu. 1348 * 1349 * @see #unregisterForContextMenu(View) 1350 * @param view The view that should show a context menu. 1351 */ 1352 public void registerForContextMenu(View view) { 1353 view.setOnCreateContextMenuListener(this); 1354 } 1355 1356 /** 1357 * Prevents a context menu to be shown for the given view. This method will 1358 * remove the {@link OnCreateContextMenuListener} on the view. 1359 * 1360 * @see #registerForContextMenu(View) 1361 * @param view The view that should stop showing a context menu. 1362 */ 1363 public void unregisterForContextMenu(View view) { 1364 view.setOnCreateContextMenuListener(null); 1365 } 1366 1367 /** 1368 * This hook is called whenever an item in a context menu is selected. The 1369 * default implementation simply returns false to have the normal processing 1370 * happen (calling the item's Runnable or sending a message to its Handler 1371 * as appropriate). You can use this method for any items for which you 1372 * would like to do processing without those other facilities. 1373 * <p> 1374 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the 1375 * View that added this menu item. 1376 * <p> 1377 * Derived classes should call through to the base class for it to perform 1378 * the default menu handling. 1379 * 1380 * @param item The context menu item that was selected. 1381 * @return boolean Return false to allow normal context menu processing to 1382 * proceed, true to consume it here. 1383 */ 1384 public boolean onContextItemSelected(MenuItem item) { 1385 return false; 1386 } 1387 1388 /** 1389 * Print the Fragments's state into the given stream. 1390 * 1391 * @param prefix Text to print at the front of each line. 1392 * @param fd The raw file descriptor that the dump is being sent to. 1393 * @param writer The PrintWriter to which you should dump your state. This will be 1394 * closed for you after you return. 1395 * @param args additional arguments to the dump request. 1396 */ 1397 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 1398 writer.print(prefix); writer.print("mFragmentId=#"); 1399 writer.print(Integer.toHexString(mFragmentId)); 1400 writer.print(" mContainerId#="); 1401 writer.print(Integer.toHexString(mContainerId)); 1402 writer.print(" mTag="); writer.println(mTag); 1403 writer.print(prefix); writer.print("mState="); writer.print(mState); 1404 writer.print(" mIndex="); writer.print(mIndex); 1405 writer.print(" mWho="); writer.print(mWho); 1406 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting); 1407 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded); 1408 writer.print(" mRemoving="); writer.print(mRemoving); 1409 writer.print(" mResumed="); writer.print(mResumed); 1410 writer.print(" mFromLayout="); writer.print(mFromLayout); 1411 writer.print(" mInLayout="); writer.println(mInLayout); 1412 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden); 1413 writer.print(" mDetached="); writer.print(mDetached); 1414 writer.print(" mRetainInstance="); writer.print(mRetainInstance); 1415 writer.print(" mRetaining="); writer.print(mRetaining); 1416 writer.print(" mHasMenu="); writer.println(mHasMenu); 1417 if (mFragmentManager != null) { 1418 writer.print(prefix); writer.print("mFragmentManager="); 1419 writer.println(mFragmentManager); 1420 } 1421 if (mImmediateActivity != null) { 1422 writer.print(prefix); writer.print("mImmediateActivity="); 1423 writer.println(mImmediateActivity); 1424 } 1425 if (mActivity != null) { 1426 writer.print(prefix); writer.print("mActivity="); 1427 writer.println(mActivity); 1428 } 1429 if (mArguments != null) { 1430 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments); 1431 } 1432 if (mSavedFragmentState != null) { 1433 writer.print(prefix); writer.print("mSavedFragmentState="); 1434 writer.println(mSavedFragmentState); 1435 } 1436 if (mSavedViewState != null) { 1437 writer.print(prefix); writer.print("mSavedViewState="); 1438 writer.println(mSavedViewState); 1439 } 1440 if (mTarget != null) { 1441 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget); 1442 writer.print(" mTargetRequestCode="); 1443 writer.println(mTargetRequestCode); 1444 } 1445 if (mNextAnim != 0) { 1446 writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim); 1447 } 1448 if (mContainer != null) { 1449 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer); 1450 } 1451 if (mView != null) { 1452 writer.print(prefix); writer.print("mView="); writer.println(mView); 1453 } 1454 if (mAnimatingAway != null) { 1455 writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway); 1456 writer.print(prefix); writer.print("mStateAfterAnimating="); 1457 writer.println(mStateAfterAnimating); 1458 } 1459 if (mLoaderManager != null) { 1460 writer.print(prefix); writer.println("Loader Manager:"); 1461 mLoaderManager.dump(prefix + " ", fd, writer, args); 1462 } 1463 } 1464 1465 void performStart() { 1466 onStart(); 1467 if (mLoaderManager != null) { 1468 mLoaderManager.doReportStart(); 1469 } 1470 } 1471 1472 void performStop() { 1473 onStop(); 1474 1475 if (mLoadersStarted) { 1476 mLoadersStarted = false; 1477 if (!mCheckedForLoaderManager) { 1478 mCheckedForLoaderManager = true; 1479 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false); 1480 } 1481 if (mLoaderManager != null) { 1482 if (mActivity == null || !mActivity.mChangingConfigurations) { 1483 mLoaderManager.doStop(); 1484 } else { 1485 mLoaderManager.doRetain(); 1486 } 1487 } 1488 } 1489 } 1490 1491 void performDestroyView() { 1492 onDestroyView(); 1493 if (mLoaderManager != null) { 1494 mLoaderManager.doReportNextStart(); 1495 } 1496 } 1497} 1498