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