Fragment.java revision b46ed7636be9341b6ce0b158b3d86f34a437e6da
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>&lt;fragment&gt;</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 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 selected item here 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 title title 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 &lt;fragment&gt;
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 &lt;fragment&gt; 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 &lt;fragment&gt; to provide
302 * a specific tag name for the fragment.
303 * <li><code>android:id</code> can be used in &lt;fragment&gt; 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 void clearIndex() {
591        mIndex = -1;
592        mWho = null;
593    }
594
595    final boolean isInBackStack() {
596        return mBackStackNesting > 0;
597    }
598
599    /**
600     * Subclasses can not override equals().
601     */
602    @Override final public boolean equals(Object o) {
603        return super.equals(o);
604    }
605
606    /**
607     * Subclasses can not override hashCode().
608     */
609    @Override final public int hashCode() {
610        return super.hashCode();
611    }
612
613    @Override
614    public String toString() {
615        StringBuilder sb = new StringBuilder(128);
616        DebugUtils.buildShortClassTag(this, sb);
617        if (mIndex >= 0) {
618            sb.append(" #");
619            sb.append(mIndex);
620        }
621        if (mFragmentId != 0) {
622            sb.append(" id=0x");
623            sb.append(Integer.toHexString(mFragmentId));
624        }
625        if (mTag != null) {
626            sb.append(" ");
627            sb.append(mTag);
628        }
629        sb.append('}');
630        return sb.toString();
631    }
632
633    /**
634     * Return the identifier this fragment is known by.  This is either
635     * the android:id value supplied in a layout or the container view ID
636     * supplied when adding the fragment.
637     */
638    final public int getId() {
639        return mFragmentId;
640    }
641
642    /**
643     * Get the tag name of the fragment, if specified.
644     */
645    final public String getTag() {
646        return mTag;
647    }
648
649    /**
650     * Supply the construction arguments for this fragment.  This can only
651     * be called before the fragment has been attached to its activity; that
652     * is, you should call it immediately after constructing the fragment.  The
653     * arguments supplied here will be retained across fragment destroy and
654     * creation.
655     */
656    public void setArguments(Bundle args) {
657        if (mIndex >= 0) {
658            throw new IllegalStateException("Fragment already active");
659        }
660        mArguments = args;
661    }
662
663    /**
664     * Return the arguments supplied when the fragment was instantiated,
665     * if any.
666     */
667    final public Bundle getArguments() {
668        return mArguments;
669    }
670
671    /**
672     * Set the initial saved state that this Fragment should restore itself
673     * from when first being constructed, as returned by
674     * {@link FragmentManager#saveFragmentInstanceState(Fragment)
675     * FragmentManager.saveFragmentInstanceState}.
676     *
677     * @param state The state the fragment should be restored from.
678     */
679    public void setInitialSavedState(SavedState state) {
680        if (mIndex >= 0) {
681            throw new IllegalStateException("Fragment already active");
682        }
683        mSavedFragmentState = state != null && state.mState != null
684                ? state.mState : null;
685    }
686
687    /**
688     * Optional target for this fragment.  This may be used, for example,
689     * if this fragment is being started by another, and when done wants to
690     * give a result back to the first.  The target set here is retained
691     * across instances via {@link FragmentManager#putFragment
692     * FragmentManager.putFragment()}.
693     *
694     * @param fragment The fragment that is the target of this one.
695     * @param requestCode Optional request code, for convenience if you
696     * are going to call back with {@link #onActivityResult(int, int, Intent)}.
697     */
698    public void setTargetFragment(Fragment fragment, int requestCode) {
699        mTarget = fragment;
700        mTargetRequestCode = requestCode;
701    }
702
703    /**
704     * Return the target fragment set by {@link #setTargetFragment}.
705     */
706    final public Fragment getTargetFragment() {
707        return mTarget;
708    }
709
710    /**
711     * Return the target request code set by {@link #setTargetFragment}.
712     */
713    final public int getTargetRequestCode() {
714        return mTargetRequestCode;
715    }
716
717    /**
718     * Return the Activity this fragment is currently associated with.
719     */
720    final public Activity getActivity() {
721        return mActivity;
722    }
723
724    /**
725     * Return <code>getActivity().getResources()</code>.
726     */
727    final public Resources getResources() {
728        if (mActivity == null) {
729            throw new IllegalStateException("Fragment " + this + " not attached to Activity");
730        }
731        return mActivity.getResources();
732    }
733
734    /**
735     * Return a localized, styled CharSequence from the application's package's
736     * default string table.
737     *
738     * @param resId Resource id for the CharSequence text
739     */
740    public final CharSequence getText(int resId) {
741        return getResources().getText(resId);
742    }
743
744    /**
745     * Return a localized string from the application's package's
746     * default string table.
747     *
748     * @param resId Resource id for the string
749     */
750    public final String getString(int resId) {
751        return getResources().getString(resId);
752    }
753
754    /**
755     * Return a localized formatted string from the application's package's
756     * default string table, substituting the format arguments as defined in
757     * {@link java.util.Formatter} and {@link java.lang.String#format}.
758     *
759     * @param resId Resource id for the format string
760     * @param formatArgs The format arguments that will be used for substitution.
761     */
762
763    public final String getString(int resId, Object... formatArgs) {
764        return getResources().getString(resId, formatArgs);
765    }
766
767    /**
768     * Return the FragmentManager for interacting with fragments associated
769     * with this fragment's activity.  Note that this will be non-null slightly
770     * before {@link #getActivity()}, during the time from when the fragment is
771     * placed in a {@link FragmentTransaction} until it is committed and
772     * attached to its activity.
773     */
774    final public FragmentManager getFragmentManager() {
775        return mFragmentManager;
776    }
777
778    /**
779     * Return true if the fragment is currently added to its activity.
780     */
781    final public boolean isAdded() {
782        return mActivity != null && mAdded;
783    }
784
785    /**
786     * Return true if this fragment is currently being removed from its
787     * activity.  This is  <em>not</em> whether its activity is finishing, but
788     * rather whether it is in the process of being removed from its activity.
789     */
790    final public boolean isRemoving() {
791        return mRemoving;
792    }
793
794    /**
795     * Return true if the layout is included as part of an activity view
796     * hierarchy via the &lt;fragment&gt; tag.  This will always be true when
797     * fragments are created through the &lt;fragment&gt; tag, <em>except</em>
798     * in the case where an old fragment is restored from a previous state and
799     * it does not appear in the layout of the current state.
800     */
801    final public boolean isInLayout() {
802        return mInLayout;
803    }
804
805    /**
806     * Return true if the fragment is in the resumed state.  This is true
807     * for the duration of {@link #onResume()} and {@link #onPause()} as well.
808     */
809    final public boolean isResumed() {
810        return mResumed;
811    }
812
813    /**
814     * Return true if the fragment is currently visible to the user.  This means
815     * it: (1) has been added, (2) has its view attached to the window, and
816     * (3) is not hidden.
817     */
818    final public boolean isVisible() {
819        return isAdded() && !isHidden() && mView != null
820                && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
821    }
822
823    /**
824     * Return true if the fragment has been hidden.  By default fragments
825     * are shown.  You can find out about changes to this state with
826     * {@link #onHiddenChanged}.  Note that the hidden state is orthogonal
827     * to other states -- that is, to be visible to the user, a fragment
828     * must be both started and not hidden.
829     */
830    final public boolean isHidden() {
831        return mHidden;
832    }
833
834    /**
835     * Called when the hidden state (as returned by {@link #isHidden()} of
836     * the fragment has changed.  Fragments start out not hidden; this will
837     * be called whenever the fragment changes state from that.
838     * @param hidden True if the fragment is now hidden, false if it is not
839     * visible.
840     */
841    public void onHiddenChanged(boolean hidden) {
842    }
843
844    /**
845     * Control whether a fragment instance is retained across Activity
846     * re-creation (such as from a configuration change).  This can only
847     * be used with fragments not in the back stack.  If set, the fragment
848     * lifecycle will be slightly different when an activity is recreated:
849     * <ul>
850     * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still
851     * will be, because the fragment is being detached from its current activity).
852     * <li> {@link #onCreate(Bundle)} will not be called since the fragment
853     * is not being re-created.
854     * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b>
855     * still be called.
856     * </ul>
857     */
858    public void setRetainInstance(boolean retain) {
859        mRetainInstance = retain;
860    }
861
862    final public boolean getRetainInstance() {
863        return mRetainInstance;
864    }
865
866    /**
867     * Report that this fragment would like to participate in populating
868     * the options menu by receiving a call to {@link #onCreateOptionsMenu}
869     * and related methods.
870     *
871     * @param hasMenu If true, the fragment has menu items to contribute.
872     */
873    public void setHasOptionsMenu(boolean hasMenu) {
874        if (mHasMenu != hasMenu) {
875            mHasMenu = hasMenu;
876            if (isAdded() && !isHidden()) {
877                mActivity.invalidateOptionsMenu();
878            }
879        }
880    }
881
882    /**
883     * Return the LoaderManager for this fragment, creating it if needed.
884     */
885    public LoaderManager getLoaderManager() {
886        if (mLoaderManager != null) {
887            return mLoaderManager;
888        }
889        if (mActivity == null) {
890            throw new IllegalStateException("Fragment " + this + " not attached to Activity");
891        }
892        mCheckedForLoaderManager = true;
893        mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, true);
894        return mLoaderManager;
895    }
896
897    /**
898     * Call {@link Activity#startActivity(Intent)} on the fragment's
899     * containing Activity.
900     */
901    public void startActivity(Intent intent) {
902        if (mActivity == null) {
903            throw new IllegalStateException("Fragment " + this + " not attached to Activity");
904        }
905        mActivity.startActivityFromFragment(this, intent, -1);
906    }
907
908    /**
909     * Call {@link Activity#startActivityForResult(Intent, int)} on the fragment's
910     * containing Activity.
911     */
912    public void startActivityForResult(Intent intent, int requestCode) {
913        if (mActivity == null) {
914            throw new IllegalStateException("Fragment " + this + " not attached to Activity");
915        }
916        mActivity.startActivityFromFragment(this, intent, requestCode);
917    }
918
919    /**
920     * Receive the result from a previous call to
921     * {@link #startActivityForResult(Intent, int)}.  This follows the
922     * related Activity API as described there in
923     * {@link Activity#onActivityResult(int, int, Intent)}.
924     *
925     * @param requestCode The integer request code originally supplied to
926     *                    startActivityForResult(), allowing you to identify who this
927     *                    result came from.
928     * @param resultCode The integer result code returned by the child activity
929     *                   through its setResult().
930     * @param data An Intent, which can return result data to the caller
931     *               (various data can be attached to Intent "extras").
932     */
933    public void onActivityResult(int requestCode, int resultCode, Intent data) {
934    }
935
936    /**
937     * @hide Hack so that DialogFragment can make its Dialog before creating
938     * its views, and the view construction can use the dialog's context for
939     * inflation.  Maybe this should become a public API. Note sure.
940     */
941    public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
942        return mActivity.getLayoutInflater();
943    }
944
945    /**
946     * @deprecated Use {@link #onInflate(Activity, AttributeSet, Bundle)} instead.
947     */
948    @Deprecated
949    public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
950        mCalled = true;
951    }
952
953    /**
954     * Called when a fragment is being created as part of a view layout
955     * inflation, typically from setting the content view of an activity.  This
956     * may be called immediately after the fragment is created from a <fragment>
957     * tag in a layout file.  Note this is <em>before</em> the fragment's
958     * {@link #onAttach(Activity)} has been called; all you should do here is
959     * parse the attributes and save them away.
960     *
961     * <p>This is called every time the fragment is inflated, even if it is
962     * being inflated into a new instance with saved state.  It typically makes
963     * sense to re-parse the parameters each time, to allow them to change with
964     * different configurations.</p>
965     *
966     * <p>Here is a typical implementation of a fragment that can take parameters
967     * both through attributes supplied here as well from {@link #getArguments()}:</p>
968     *
969     * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
970     *      fragment}
971     *
972     * <p>Note that parsing the XML attributes uses a "styleable" resource.  The
973     * declaration for the styleable used here is:</p>
974     *
975     * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments}
976     *
977     * <p>The fragment can then be declared within its activity's content layout
978     * through a tag like this:</p>
979     *
980     * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes}
981     *
982     * <p>This fragment can also be created dynamically from arguments given
983     * at runtime in the arguments Bundle; here is an example of doing so at
984     * creation of the containing activity:</p>
985     *
986     * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
987     *      create}
988     *
989     * @param activity The Activity that is inflating this fragment.
990     * @param attrs The attributes at the tag where the fragment is
991     * being created.
992     * @param savedInstanceState If the fragment is being re-created from
993     * a previous saved state, this is the state.
994     */
995    public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
996        onInflate(attrs, savedInstanceState);
997        mCalled = true;
998    }
999
1000    /**
1001     * Called when a fragment is first attached to its activity.
1002     * {@link #onCreate(Bundle)} will be called after this.
1003     */
1004    public void onAttach(Activity activity) {
1005        mCalled = true;
1006    }
1007
1008    /**
1009     * Called when a fragment loads an animation.
1010     */
1011    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
1012        return null;
1013    }
1014
1015    /**
1016     * Called to do initial creation of a fragment.  This is called after
1017     * {@link #onAttach(Activity)} and before
1018     * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1019     *
1020     * <p>Note that this can be called while the fragment's activity is
1021     * still in the process of being created.  As such, you can not rely
1022     * on things like the activity's content view hierarchy being initialized
1023     * at this point.  If you want to do work once the activity itself is
1024     * created, see {@link #onActivityCreated(Bundle)}.
1025     *
1026     * @param savedInstanceState If the fragment is being re-created from
1027     * a previous saved state, this is the state.
1028     */
1029    public void onCreate(Bundle savedInstanceState) {
1030        mCalled = true;
1031    }
1032
1033    /**
1034     * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
1035     * has returned, but before any saved state has been restored in to the view.
1036     * This gives subclasses a chance to initialize themselves once
1037     * they know their view hierarchy has been completely created.  The fragment's
1038     * view hierarchy is not however attached to its parent at this point.
1039     * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1040     * @param savedInstanceState If non-null, this fragment is being re-constructed
1041     * from a previous saved state as given here.
1042     */
1043    public void onViewCreated(View view, Bundle savedInstanceState) {
1044    }
1045
1046    /**
1047     * Called to have the fragment instantiate its user interface view.
1048     * This is optional, and non-graphical fragments can return null (which
1049     * is the default implementation).  This will be called between
1050     * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
1051     *
1052     * <p>If you return a View from here, you will later be called in
1053     * {@link #onDestroyView} when the view is being released.
1054     *
1055     * @param inflater The LayoutInflater object that can be used to inflate
1056     * any views in the fragment,
1057     * @param container If non-null, this is the parent view that the fragment's
1058     * UI should be attached to.  The fragment should not add the view itself,
1059     * but this can be used to generate the LayoutParams of the view.
1060     * @param savedInstanceState If non-null, this fragment is being re-constructed
1061     * from a previous saved state as given here.
1062     *
1063     * @return Return the View for the fragment's UI, or null.
1064     */
1065    public View onCreateView(LayoutInflater inflater, ViewGroup container,
1066            Bundle savedInstanceState) {
1067        return null;
1068    }
1069
1070    /**
1071     * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
1072     * if provided.
1073     *
1074     * @return The fragment's root view, or null if it has no layout.
1075     */
1076    public View getView() {
1077        return mView;
1078    }
1079
1080    /**
1081     * Called when the fragment's activity has been created and this
1082     * fragment's view hierarchy instantiated.  It can be used to do final
1083     * initialization once these pieces are in place, such as retrieving
1084     * views or restoring state.  It is also useful for fragments that use
1085     * {@link #setRetainInstance(boolean)} to retain their instance,
1086     * as this callback tells the fragment when it is fully associated with
1087     * the new activity instance.  This is called after {@link #onCreateView}
1088     * and before {@link #onStart()}.
1089     *
1090     * @param savedInstanceState If the fragment is being re-created from
1091     * a previous saved state, this is the state.
1092     */
1093    public void onActivityCreated(Bundle savedInstanceState) {
1094        mCalled = true;
1095    }
1096
1097    /**
1098     * Called when the Fragment is visible to the user.  This is generally
1099     * tied to {@link Activity#onStart() Activity.onStart} of the containing
1100     * Activity's lifecycle.
1101     */
1102    public void onStart() {
1103        mCalled = true;
1104
1105        if (!mLoadersStarted) {
1106            mLoadersStarted = true;
1107            if (!mCheckedForLoaderManager) {
1108                mCheckedForLoaderManager = true;
1109                mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
1110            }
1111            if (mLoaderManager != null) {
1112                mLoaderManager.doStart();
1113            }
1114        }
1115    }
1116
1117    /**
1118     * Called when the fragment is visible to the user and actively running.
1119     * This is generally
1120     * tied to {@link Activity#onResume() Activity.onResume} of the containing
1121     * Activity's lifecycle.
1122     */
1123    public void onResume() {
1124        mCalled = true;
1125    }
1126
1127    /**
1128     * Called to ask the fragment to save its current dynamic state, so it
1129     * can later be reconstructed in a new instance of its process is
1130     * restarted.  If a new instance of the fragment later needs to be
1131     * created, the data you place in the Bundle here will be available
1132     * in the Bundle given to {@link #onCreate(Bundle)},
1133     * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
1134     * {@link #onActivityCreated(Bundle)}.
1135     *
1136     * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
1137     * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
1138     * applies here as well.  Note however: <em>this method may be called
1139     * at any time before {@link #onDestroy()}</em>.  There are many situations
1140     * where a fragment may be mostly torn down (such as when placed on the
1141     * back stack with no UI showing), but its state will not be saved until
1142     * its owning activity actually needs to save its state.
1143     *
1144     * @param outState Bundle in which to place your saved state.
1145     */
1146    public void onSaveInstanceState(Bundle outState) {
1147    }
1148
1149    public void onConfigurationChanged(Configuration newConfig) {
1150        mCalled = true;
1151    }
1152
1153    /**
1154     * Called when the Fragment is no longer resumed.  This is generally
1155     * tied to {@link Activity#onPause() Activity.onPause} of the containing
1156     * Activity's lifecycle.
1157     */
1158    public void onPause() {
1159        mCalled = true;
1160    }
1161
1162    /**
1163     * Called when the Fragment is no longer started.  This is generally
1164     * tied to {@link Activity#onStop() Activity.onStop} of the containing
1165     * Activity's lifecycle.
1166     */
1167    public void onStop() {
1168        mCalled = true;
1169    }
1170
1171    public void onLowMemory() {
1172        mCalled = true;
1173    }
1174
1175    /**
1176     * Called when the view previously created by {@link #onCreateView} has
1177     * been detached from the fragment.  The next time the fragment needs
1178     * to be displayed, a new view will be created.  This is called
1179     * after {@link #onStop()} and before {@link #onDestroy()}.  It is called
1180     * <em>regardless</em> of whether {@link #onCreateView} returned a
1181     * non-null view.  Internally it is called after the view's state has
1182     * been saved but before it has been removed from its parent.
1183     */
1184    public void onDestroyView() {
1185        mCalled = true;
1186    }
1187
1188    /**
1189     * Called when the fragment is no longer in use.  This is called
1190     * after {@link #onStop()} and before {@link #onDetach()}.
1191     */
1192    public void onDestroy() {
1193        mCalled = true;
1194        //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager
1195        //        + " mLoaderManager=" + mLoaderManager);
1196        if (!mCheckedForLoaderManager) {
1197            mCheckedForLoaderManager = true;
1198            mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
1199        }
1200        if (mLoaderManager != null) {
1201            mLoaderManager.doDestroy();
1202        }
1203    }
1204
1205    /**
1206     * Called when the fragment is no longer attached to its activity.  This
1207     * is called after {@link #onDestroy()}.
1208     */
1209    public void onDetach() {
1210        mCalled = true;
1211    }
1212
1213    /**
1214     * Initialize the contents of the Activity's standard options menu.  You
1215     * should place your menu items in to <var>menu</var>.  For this method
1216     * to be called, you must have first called {@link #setHasOptionsMenu}.  See
1217     * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
1218     * for more information.
1219     *
1220     * @param menu The options menu in which you place your items.
1221     *
1222     * @see #setHasOptionsMenu
1223     * @see #onPrepareOptionsMenu
1224     * @see #onOptionsItemSelected
1225     */
1226    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1227    }
1228
1229    /**
1230     * Prepare the Screen's standard options menu to be displayed.  This is
1231     * called right before the menu is shown, every time it is shown.  You can
1232     * use this method to efficiently enable/disable items or otherwise
1233     * dynamically modify the contents.  See
1234     * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu}
1235     * for more information.
1236     *
1237     * @param menu The options menu as last shown or first initialized by
1238     *             onCreateOptionsMenu().
1239     *
1240     * @see #setHasOptionsMenu
1241     * @see #onCreateOptionsMenu
1242     */
1243    public void onPrepareOptionsMenu(Menu menu) {
1244    }
1245
1246    /**
1247     * Called when this fragment's option menu items are no longer being
1248     * included in the overall options menu.  Receiving this call means that
1249     * the menu needed to be rebuilt, but this fragment's items were not
1250     * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
1251     * was not called).
1252     */
1253    public void onDestroyOptionsMenu() {
1254    }
1255
1256    /**
1257     * This hook is called whenever an item in your options menu is selected.
1258     * The default implementation simply returns false to have the normal
1259     * processing happen (calling the item's Runnable or sending a message to
1260     * its Handler as appropriate).  You can use this method for any items
1261     * for which you would like to do processing without those other
1262     * facilities.
1263     *
1264     * <p>Derived classes should call through to the base class for it to
1265     * perform the default menu handling.
1266     *
1267     * @param item The menu item that was selected.
1268     *
1269     * @return boolean Return false to allow normal menu processing to
1270     *         proceed, true to consume it here.
1271     *
1272     * @see #onCreateOptionsMenu
1273     */
1274    public boolean onOptionsItemSelected(MenuItem item) {
1275        return false;
1276    }
1277
1278    /**
1279     * This hook is called whenever the options menu is being closed (either by the user canceling
1280     * the menu with the back/menu button, or when an item is selected).
1281     *
1282     * @param menu The options menu as last shown or first initialized by
1283     *             onCreateOptionsMenu().
1284     */
1285    public void onOptionsMenuClosed(Menu menu) {
1286    }
1287
1288    /**
1289     * Called when a context menu for the {@code view} is about to be shown.
1290     * Unlike {@link #onCreateOptionsMenu}, this will be called every
1291     * time the context menu is about to be shown and should be populated for
1292     * the view (or item inside the view for {@link AdapterView} subclasses,
1293     * this can be found in the {@code menuInfo})).
1294     * <p>
1295     * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
1296     * item has been selected.
1297     * <p>
1298     * The default implementation calls up to
1299     * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
1300     * you can not call this implementation if you don't want that behavior.
1301     * <p>
1302     * It is not safe to hold onto the context menu after this method returns.
1303     * {@inheritDoc}
1304     */
1305    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1306        getActivity().onCreateContextMenu(menu, v, menuInfo);
1307    }
1308
1309    /**
1310     * Registers a context menu to be shown for the given view (multiple views
1311     * can show the context menu). This method will set the
1312     * {@link OnCreateContextMenuListener} on the view to this fragment, so
1313     * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
1314     * called when it is time to show the context menu.
1315     *
1316     * @see #unregisterForContextMenu(View)
1317     * @param view The view that should show a context menu.
1318     */
1319    public void registerForContextMenu(View view) {
1320        view.setOnCreateContextMenuListener(this);
1321    }
1322
1323    /**
1324     * Prevents a context menu to be shown for the given view. This method will
1325     * remove the {@link OnCreateContextMenuListener} on the view.
1326     *
1327     * @see #registerForContextMenu(View)
1328     * @param view The view that should stop showing a context menu.
1329     */
1330    public void unregisterForContextMenu(View view) {
1331        view.setOnCreateContextMenuListener(null);
1332    }
1333
1334    /**
1335     * This hook is called whenever an item in a context menu is selected. The
1336     * default implementation simply returns false to have the normal processing
1337     * happen (calling the item's Runnable or sending a message to its Handler
1338     * as appropriate). You can use this method for any items for which you
1339     * would like to do processing without those other facilities.
1340     * <p>
1341     * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
1342     * View that added this menu item.
1343     * <p>
1344     * Derived classes should call through to the base class for it to perform
1345     * the default menu handling.
1346     *
1347     * @param item The context menu item that was selected.
1348     * @return boolean Return false to allow normal context menu processing to
1349     *         proceed, true to consume it here.
1350     */
1351    public boolean onContextItemSelected(MenuItem item) {
1352        return false;
1353    }
1354
1355    /**
1356     * Print the Fragments's state into the given stream.
1357     *
1358     * @param prefix Text to print at the front of each line.
1359     * @param fd The raw file descriptor that the dump is being sent to.
1360     * @param writer The PrintWriter to which you should dump your state.  This will be
1361     * closed for you after you return.
1362     * @param args additional arguments to the dump request.
1363     */
1364    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
1365        writer.print(prefix); writer.print("mFragmentId=#");
1366                writer.print(Integer.toHexString(mFragmentId));
1367                writer.print(" mContainerId#=");
1368                writer.print(Integer.toHexString(mContainerId));
1369                writer.print(" mTag="); writer.println(mTag);
1370        writer.print(prefix); writer.print("mState="); writer.print(mState);
1371                writer.print(" mIndex="); writer.print(mIndex);
1372                writer.print(" mWho="); writer.print(mWho);
1373                writer.print(" mBackStackNesting="); writer.println(mBackStackNesting);
1374        writer.print(prefix); writer.print("mAdded="); writer.print(mAdded);
1375                writer.print(" mRemoving="); writer.print(mRemoving);
1376                writer.print(" mResumed="); writer.print(mResumed);
1377                writer.print(" mFromLayout="); writer.print(mFromLayout);
1378                writer.print(" mInLayout="); writer.println(mInLayout);
1379        writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
1380                writer.print(" mDetached="); writer.print(mDetached);
1381                writer.print(" mRetainInstance="); writer.print(mRetainInstance);
1382                writer.print(" mRetaining="); writer.print(mRetaining);
1383                writer.print(" mHasMenu="); writer.println(mHasMenu);
1384        if (mFragmentManager != null) {
1385            writer.print(prefix); writer.print("mFragmentManager=");
1386                    writer.println(mFragmentManager);
1387        }
1388        if (mImmediateActivity != null) {
1389            writer.print(prefix); writer.print("mImmediateActivity=");
1390                    writer.println(mImmediateActivity);
1391        }
1392        if (mActivity != null) {
1393            writer.print(prefix); writer.print("mActivity=");
1394                    writer.println(mActivity);
1395        }
1396        if (mArguments != null) {
1397            writer.print(prefix); writer.print("mArguments="); writer.println(mArguments);
1398        }
1399        if (mSavedFragmentState != null) {
1400            writer.print(prefix); writer.print("mSavedFragmentState=");
1401                    writer.println(mSavedFragmentState);
1402        }
1403        if (mSavedViewState != null) {
1404            writer.print(prefix); writer.print("mSavedViewState=");
1405                    writer.println(mSavedViewState);
1406        }
1407        if (mTarget != null) {
1408            writer.print(prefix); writer.print("mTarget="); writer.print(mTarget);
1409                    writer.print(" mTargetRequestCode=");
1410                    writer.println(mTargetRequestCode);
1411        }
1412        if (mNextAnim != 0) {
1413            writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim);
1414        }
1415        if (mContainer != null) {
1416            writer.print(prefix); writer.print("mContainer="); writer.println(mContainer);
1417        }
1418        if (mView != null) {
1419            writer.print(prefix); writer.print("mView="); writer.println(mView);
1420        }
1421        if (mAnimatingAway != null) {
1422            writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway);
1423            writer.print(prefix); writer.print("mStateAfterAnimating=");
1424                    writer.println(mStateAfterAnimating);
1425        }
1426        if (mLoaderManager != null) {
1427            writer.print(prefix); writer.println("Loader Manager:");
1428            mLoaderManager.dump(prefix + "  ", fd, writer, args);
1429        }
1430    }
1431
1432    void performStop() {
1433        onStop();
1434
1435        if (mLoadersStarted) {
1436            mLoadersStarted = false;
1437            if (!mCheckedForLoaderManager) {
1438                mCheckedForLoaderManager = true;
1439                mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
1440            }
1441            if (mLoaderManager != null) {
1442                if (mActivity == null || !mActivity.mChangingConfigurations) {
1443                    mLoaderManager.doStop();
1444                } else {
1445                    mLoaderManager.doRetain();
1446                }
1447            }
1448        }
1449    }
1450}
1451