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