blob: 6ea170ea1a8e0901da9d150b51d8a87b7ce927ae [file] [log] [blame]
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Chet Haasea18a86b2010-09-07 13:20:00 -070019import android.animation.Animator;
Adam Powella9bab982016-04-21 11:04:41 -070020import android.annotation.CallSuper;
Svetoslavc6d1c342015-02-26 14:44:43 -080021import android.annotation.NonNull;
Tor Norbyed9273d62013-05-30 15:59:53 -070022import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070023import android.annotation.StringRes;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070024import android.content.ComponentCallbacks2;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070025import android.content.Context;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070026import android.content.Intent;
Clara Bayarria0c2dc32016-04-12 12:00:15 +010027import android.content.IntentSender;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070028import android.content.res.Configuration;
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -080029import android.content.res.Resources;
George Mountc03da0e2014-08-22 17:04:02 -070030import android.content.res.TypedArray;
Adam Powell592df7d2016-01-14 12:58:23 -080031import android.os.Build;
Adam Powellef4d99f2016-04-18 11:01:55 -070032import android.os.Build.VERSION_CODES;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070033import android.os.Bundle;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070034import android.os.Parcel;
35import android.os.Parcelable;
George Mountc03da0e2014-08-22 17:04:02 -070036import android.transition.Transition;
37import android.transition.TransitionInflater;
38import android.transition.TransitionSet;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070039import android.util.AndroidRuntimeException;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070040import android.util.ArrayMap;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070041import android.util.AttributeSet;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080042import android.util.DebugUtils;
Dianne Hackborn03fcc332012-05-15 12:49:40 -070043import android.util.Log;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070044import android.util.SparseArray;
Adam Powell14874662013-07-18 19:42:41 -070045import android.util.SuperNotCalledException;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070046import android.view.ContextMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -070047import android.view.ContextMenu.ContextMenuInfo;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070048import android.view.LayoutInflater;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070049import android.view.Menu;
50import android.view.MenuInflater;
51import android.view.MenuItem;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070052import android.view.View;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070053import android.view.View.OnCreateContextMenuListener;
Adam Powellf0f5fff2011-08-01 13:42:50 -070054import android.view.ViewGroup;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070055import android.widget.AdapterView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070056
Dianne Hackborn625ac272010-09-17 18:29:22 -070057import java.io.FileDescriptor;
58import java.io.PrintWriter;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070059
60final class FragmentState implements Parcelable {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070061 final String mClassName;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070062 final int mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070063 final boolean mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070064 final int mFragmentId;
65 final int mContainerId;
66 final String mTag;
67 final boolean mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070068 final boolean mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070069 final Bundle mArguments;
Todd Kennedyd6d18582015-07-23 10:19:46 -070070 final boolean mHidden;
George Mountc03da0e2014-08-22 17:04:02 -070071
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070072 Bundle mSavedFragmentState;
George Mountc03da0e2014-08-22 17:04:02 -070073
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070074 Fragment mInstance;
George Mountc03da0e2014-08-22 17:04:02 -070075
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070076 public FragmentState(Fragment frag) {
77 mClassName = frag.getClass().getName();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070078 mIndex = frag.mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070079 mFromLayout = frag.mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070080 mFragmentId = frag.mFragmentId;
81 mContainerId = frag.mContainerId;
82 mTag = frag.mTag;
83 mRetainInstance = frag.mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070084 mDetached = frag.mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070085 mArguments = frag.mArguments;
Todd Kennedyd6d18582015-07-23 10:19:46 -070086 mHidden = frag.mHidden;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070087 }
George Mountc03da0e2014-08-22 17:04:02 -070088
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070089 public FragmentState(Parcel in) {
90 mClassName = in.readString();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070091 mIndex = in.readInt();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070092 mFromLayout = in.readInt() != 0;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070093 mFragmentId = in.readInt();
94 mContainerId = in.readInt();
95 mTag = in.readString();
96 mRetainInstance = in.readInt() != 0;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070097 mDetached = in.readInt() != 0;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070098 mArguments = in.readBundle();
Todd Kennedyd6d18582015-07-23 10:19:46 -070099 mHidden = in.readInt() != 0;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700100 mSavedFragmentState = in.readBundle();
101 }
George Mountc03da0e2014-08-22 17:04:02 -0700102
Adam Powell44ba79e2016-02-04 16:20:37 -0800103 public Fragment instantiate(FragmentHostCallback host, Fragment parent,
104 FragmentManagerNonConfig childNonConfig) {
105 if (mInstance == null) {
106 final Context context = host.getContext();
107 if (mArguments != null) {
108 mArguments.setClassLoader(context.getClassLoader());
109 }
110
111 mInstance = Fragment.instantiate(context, mClassName, mArguments);
112
113 if (mSavedFragmentState != null) {
114 mSavedFragmentState.setClassLoader(context.getClassLoader());
115 mInstance.mSavedFragmentState = mSavedFragmentState;
116 }
117 mInstance.setIndex(mIndex, parent);
118 mInstance.mFromLayout = mFromLayout;
119 mInstance.mRestored = true;
120 mInstance.mFragmentId = mFragmentId;
121 mInstance.mContainerId = mContainerId;
122 mInstance.mTag = mTag;
123 mInstance.mRetainInstance = mRetainInstance;
124 mInstance.mDetached = mDetached;
125 mInstance.mHidden = mHidden;
126 mInstance.mFragmentManager = host.mFragmentManager;
127 if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG,
128 "Instantiated fragment " + mInstance);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700129 }
Adam Powell44ba79e2016-02-04 16:20:37 -0800130 mInstance.mChildNonConfig = childNonConfig;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700131 return mInstance;
132 }
George Mountc03da0e2014-08-22 17:04:02 -0700133
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700134 public int describeContents() {
135 return 0;
136 }
137
138 public void writeToParcel(Parcel dest, int flags) {
139 dest.writeString(mClassName);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700140 dest.writeInt(mIndex);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700141 dest.writeInt(mFromLayout ? 1 : 0);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700142 dest.writeInt(mFragmentId);
143 dest.writeInt(mContainerId);
144 dest.writeString(mTag);
145 dest.writeInt(mRetainInstance ? 1 : 0);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700146 dest.writeInt(mDetached ? 1 : 0);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700147 dest.writeBundle(mArguments);
Todd Kennedyd6d18582015-07-23 10:19:46 -0700148 dest.writeInt(mHidden ? 1 : 0);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700149 dest.writeBundle(mSavedFragmentState);
150 }
George Mountc03da0e2014-08-22 17:04:02 -0700151
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700152 public static final Parcelable.Creator<FragmentState> CREATOR
153 = new Parcelable.Creator<FragmentState>() {
154 public FragmentState createFromParcel(Parcel in) {
155 return new FragmentState(in);
156 }
George Mountc03da0e2014-08-22 17:04:02 -0700157
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700158 public FragmentState[] newArray(int size) {
159 return new FragmentState[size];
160 }
161 };
162}
163
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700164/**
165 * A Fragment is a piece of an application's user interface or behavior
Dianne Hackborn291905e2010-08-17 15:17:15 -0700166 * that can be placed in an {@link Activity}. Interaction with fragments
167 * is done through {@link FragmentManager}, which can be obtained via
168 * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and
169 * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}.
170 *
171 * <p>The Fragment class can be used many ways to achieve a wide variety of
felipeal1b4e4b62012-02-27 12:09:13 -0800172 * results. In its core, it represents a particular operation or interface
Dianne Hackborn291905e2010-08-17 15:17:15 -0700173 * that is running within a larger {@link Activity}. A Fragment is closely
174 * tied to the Activity it is in, and can not be used apart from one. Though
175 * Fragment defines its own lifecycle, that lifecycle is dependent on its
176 * activity: if the activity is stopped, no fragments inside of it can be
177 * started; when the activity is destroyed, all fragments will be destroyed.
178 *
Jeff Davidson2a129782014-09-09 18:10:57 -0700179 * <p>All subclasses of Fragment must include a public no-argument constructor.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700180 * The framework will often re-instantiate a fragment class when needed,
181 * in particular during state restore, and needs to be able to find this
Jeff Davidson2a129782014-09-09 18:10:57 -0700182 * constructor to instantiate it. If the no-argument constructor is not
183 * available, a runtime exception will occur in some cases during state
184 * restore.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700185 *
186 * <p>Topics covered here:
187 * <ol>
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700188 * <li><a href="#OlderPlatforms">Older Platforms</a>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700189 * <li><a href="#Lifecycle">Lifecycle</a>
190 * <li><a href="#Layout">Layout</a>
191 * <li><a href="#BackStack">Back Stack</a>
192 * </ol>
193 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700194 * <div class="special reference">
195 * <h3>Developer Guides</h3>
196 * <p>For more information about using fragments, read the
Andrew Solovayf5396802016-08-26 15:46:38 -0700197 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700198 * </div>
199 *
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700200 * <a name="OlderPlatforms"></a>
201 * <h3>Older Platforms</h3>
202 *
203 * While the Fragment API was introduced in
204 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
Dianne Hackborn7871bad2011-12-12 15:19:26 -0800205 * at is also available for use on older platforms through
206 * {@link android.support.v4.app.FragmentActivity}. See the blog post
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700207 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
208 * Fragments For All</a> for more details.
209 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700210 * <a name="Lifecycle"></a>
211 * <h3>Lifecycle</h3>
212 *
213 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has
214 * its own wrinkle on the standard activity lifecycle. It includes basic
215 * activity lifecycle methods such as {@link #onResume}, but also important
216 * are methods related to interactions with the activity and UI generation.
217 *
218 * <p>The core series of lifecycle methods that are called to bring a fragment
219 * up to resumed state (interacting with the user) are:
220 *
221 * <ol>
222 * <li> {@link #onAttach} called once the fragment is associated with its activity.
223 * <li> {@link #onCreate} called to do initial creation of the fragment.
224 * <li> {@link #onCreateView} creates and returns the view hierarchy associated
225 * with the fragment.
226 * <li> {@link #onActivityCreated} tells the fragment that its activity has
Quddus Chong37900012012-04-11 11:57:50 -0700227 * completed its own {@link Activity#onCreate Activity.onCreate()}.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700228 * <li> {@link #onViewStateRestored} tells the fragment that all of the saved
229 * state of its view hierarchy has been restored.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700230 * <li> {@link #onStart} makes the fragment visible to the user (based on its
231 * containing activity being started).
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700232 * <li> {@link #onResume} makes the fragment begin interacting with the user
233 * (based on its containing activity being resumed).
Dianne Hackborn291905e2010-08-17 15:17:15 -0700234 * </ol>
235 *
236 * <p>As a fragment is no longer being used, it goes through a reverse
237 * series of callbacks:
238 *
239 * <ol>
240 * <li> {@link #onPause} fragment is no longer interacting with the user either
241 * because its activity is being paused or a fragment operation is modifying it
242 * in the activity.
243 * <li> {@link #onStop} fragment is no longer visible to the user either
244 * because its activity is being stopped or a fragment operation is modifying it
245 * in the activity.
246 * <li> {@link #onDestroyView} allows the fragment to clean up resources
247 * associated with its View.
248 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state.
249 * <li> {@link #onDetach} called immediately prior to the fragment no longer
250 * being associated with its activity.
251 * </ol>
252 *
253 * <a name="Layout"></a>
254 * <h3>Layout</h3>
255 *
256 * <p>Fragments can be used as part of your application's layout, allowing
257 * you to better modularize your code and more easily adjust your user
258 * interface to the screen it is running on. As an example, we can look
259 * at a simple program consisting of a list of items, and display of the
260 * details of each item.</p>
261 *
262 * <p>An activity's layout XML can include <code>&lt;fragment&gt;</code> tags
263 * to embed fragment instances inside of the layout. For example, here is
Dianne Hackborn625ac272010-09-17 18:29:22 -0700264 * a simple layout that embeds one fragment:</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700265 *
266 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout}
267 *
268 * <p>The layout is installed in the activity in the normal way:</p>
269 *
270 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
271 * main}
272 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800273 * <p>The titles fragment, showing a list of titles, is fairly simple, relying
Dianne Hackborn291905e2010-08-17 15:17:15 -0700274 * on {@link ListFragment} for most of its work. Note the implementation of
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800275 * clicking an item: depending on the current activity's layout, it can either
276 * create and display a new fragment to show the details in-place (more about
Ben Dodson542f2402011-06-14 16:40:23 -0700277 * this later), or start a new activity to show the details.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700278 *
279 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
280 * titles}
281 *
Ben Dodson542f2402011-06-14 16:40:23 -0700282 * <p>The details fragment showing the contents of a selected item just
Dianne Hackborn291905e2010-08-17 15:17:15 -0700283 * displays a string of text based on an index of a string array built in to
284 * the app:</p>
285 *
286 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
287 * details}
288 *
289 * <p>In this case when the user clicks on a title, there is no details
Ben Dodson542f2402011-06-14 16:40:23 -0700290 * container in the current activity, so the titles fragment's click code will
Dianne Hackborn291905e2010-08-17 15:17:15 -0700291 * launch a new activity to display the details fragment:</p>
292 *
293 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
294 * details_activity}
295 *
296 * <p>However the screen may be large enough to show both the list of titles
297 * and details about the currently selected title. To use such a layout on
298 * a landscape screen, this alternative layout can be placed under layout-land:</p>
299 *
300 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout}
301 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800302 * <p>Note how the prior code will adjust to this alternative UI flow: the titles
303 * fragment will now embed the details fragment inside of this activity, and the
304 * details activity will finish itself if it is running in a configuration
305 * where the details can be shown in-place.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700306 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700307 * <p>When a configuration change causes the activity hosting these fragments
308 * to restart, its new instance may use a different layout that doesn't
309 * include the same fragments as the previous layout. In this case all of
310 * the previous fragments will still be instantiated and running in the new
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800311 * instance. However, any that are no longer associated with a &lt;fragment&gt;
312 * tag in the view hierarchy will not have their content view created
313 * and will return false from {@link #isInLayout}. (The code here also shows
314 * how you can determine if a fragment placed in a container is no longer
315 * running in a layout with that container and avoid creating its view hierarchy
316 * in that case.)
George Mountc03da0e2014-08-22 17:04:02 -0700317 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700318 * <p>The attributes of the &lt;fragment&gt; tag are used to control the
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800319 * LayoutParams provided when attaching the fragment's view to the parent
320 * container. They can also be parsed by the fragment in {@link #onInflate}
Dianne Hackborn625ac272010-09-17 18:29:22 -0700321 * as parameters.
George Mountc03da0e2014-08-22 17:04:02 -0700322 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700323 * <p>The fragment being instantiated must have some kind of unique identifier
324 * so that it can be re-associated with a previous instance if the parent
325 * activity needs to be destroyed and recreated. This can be provided these
326 * ways:
George Mountc03da0e2014-08-22 17:04:02 -0700327 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700328 * <ul>
329 * <li>If nothing is explicitly supplied, the view ID of the container will
330 * be used.
331 * <li><code>android:tag</code> can be used in &lt;fragment&gt; to provide
332 * a specific tag name for the fragment.
333 * <li><code>android:id</code> can be used in &lt;fragment&gt; to provide
334 * a specific identifier for the fragment.
335 * </ul>
George Mountc03da0e2014-08-22 17:04:02 -0700336 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700337 * <a name="BackStack"></a>
338 * <h3>Back Stack</h3>
339 *
340 * <p>The transaction in which fragments are modified can be placed on an
341 * internal back-stack of the owning activity. When the user presses back
342 * in the activity, any transactions on the back stack are popped off before
343 * the activity itself is finished.
344 *
345 * <p>For example, consider this simple fragment that is instantiated with
346 * an integer argument and displays that in a TextView in its UI:</p>
347 *
348 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
349 * fragment}
350 *
351 * <p>A function that creates a new instance of the fragment, replacing
352 * whatever current fragment instance is being shown and pushing that change
353 * on to the back stack could be written as:
354 *
355 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
356 * add_stack}
357 *
358 * <p>After each call to this function, a new entry is on the stack, and
359 * pressing back will pop it to return the user to whatever previous state
360 * the activity UI was in.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700361 */
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700362public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700363 private static final ArrayMap<String, Class<?>> sClassMap =
364 new ArrayMap<String, Class<?>>();
George Mountc03da0e2014-08-22 17:04:02 -0700365
Adam Powell635c60a2011-10-26 10:22:16 -0700366 static final int INVALID_STATE = -1; // Invalid state used as a null value.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700367 static final int INITIALIZING = 0; // Not yet created.
368 static final int CREATED = 1; // Created.
369 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700370 static final int STOPPED = 3; // Fully created, not started.
371 static final int STARTED = 4; // Created and started, not resumed.
372 static final int RESUMED = 5; // Created started and resumed.
George Mountc03da0e2014-08-22 17:04:02 -0700373
374 private static final Transition USE_DEFAULT_TRANSITION = new TransitionSet();
375
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700376 int mState = INITIALIZING;
George Mountc03da0e2014-08-22 17:04:02 -0700377
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800378 // Non-null if the fragment's view hierarchy is currently animating away,
379 // meaning we need to wait a bit on completely destroying it. This is the
380 // animation that is running.
381 Animator mAnimatingAway;
382
383 // If mAnimatingAway != null, this is the state we should move to once the
384 // animation is done.
385 int mStateAfterAnimating;
386
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700387 // When instantiated from saved state, this is the saved state.
388 Bundle mSavedFragmentState;
389 SparseArray<Parcelable> mSavedViewState;
George Mountc03da0e2014-08-22 17:04:02 -0700390
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700391 // Index into active fragment array.
392 int mIndex = -1;
George Mountc03da0e2014-08-22 17:04:02 -0700393
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700394 // Internal unique name for this fragment;
395 String mWho;
George Mountc03da0e2014-08-22 17:04:02 -0700396
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700397 // Construction arguments;
398 Bundle mArguments;
399
Dianne Hackborndef15372010-08-15 12:43:52 -0700400 // Target fragment.
401 Fragment mTarget;
402
Dianne Hackbornf9302322011-06-14 18:36:14 -0700403 // For use when retaining a fragment: this is the index of the last mTarget.
404 int mTargetIndex = -1;
405
Dianne Hackborndef15372010-08-15 12:43:52 -0700406 // Target request code.
407 int mTargetRequestCode;
408
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700409 // True if the fragment is in the list of added fragments.
410 boolean mAdded;
George Mountc03da0e2014-08-22 17:04:02 -0700411
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800412 // If set this fragment is being removed from its activity.
413 boolean mRemoving;
414
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700415 // Set to true if this fragment was instantiated from a layout file.
416 boolean mFromLayout;
George Mountc03da0e2014-08-22 17:04:02 -0700417
Dianne Hackborn625ac272010-09-17 18:29:22 -0700418 // Set to true when the view has actually been inflated in its layout.
419 boolean mInLayout;
420
Dianne Hackborn352cc982011-01-04 11:34:18 -0800421 // True if this fragment has been restored from previously saved state.
422 boolean mRestored;
George Mountc03da0e2014-08-22 17:04:02 -0700423
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700424 // Number of active back stack entries this fragment is in.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700425 int mBackStackNesting;
George Mountc03da0e2014-08-22 17:04:02 -0700426
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700427 // The fragment manager we are associated with. Set as soon as the
428 // fragment is used in a transaction; cleared after it has been removed
429 // from all transactions.
Adam Powell635c60a2011-10-26 10:22:16 -0700430 FragmentManagerImpl mFragmentManager;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700431
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700432 // Activity this fragment is attached to.
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700433 FragmentHostCallback mHost;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700434
435 // Private fragment manager for child fragments inside of this one.
436 FragmentManagerImpl mChildFragmentManager;
437
Adam Powell44ba79e2016-02-04 16:20:37 -0800438 // For use when restoring fragment state and descendant fragments are retained.
439 // This state is set by FragmentState.instantiate and cleared in onCreate.
440 FragmentManagerNonConfig mChildNonConfig;
441
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700442 // If this Fragment is contained in another Fragment, this is that container.
443 Fragment mParentFragment;
444
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700445 // The optional identifier for this fragment -- either the container ID if it
446 // was dynamically added to the view hierarchy, or the ID supplied in
447 // layout.
448 int mFragmentId;
George Mountc03da0e2014-08-22 17:04:02 -0700449
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700450 // When a fragment is being dynamically added to the view hierarchy, this
451 // is the identifier of the parent container it is being added to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700452 int mContainerId;
George Mountc03da0e2014-08-22 17:04:02 -0700453
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700454 // The optional named tag for this fragment -- usually used to find
455 // fragments that are not part of the layout.
456 String mTag;
George Mountc03da0e2014-08-22 17:04:02 -0700457
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700458 // Set to true when the app has requested that this fragment be hidden
459 // from the user.
460 boolean mHidden;
George Mountc03da0e2014-08-22 17:04:02 -0700461
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700462 // Set to true when the app has requested that this fragment be detached.
463 boolean mDetached;
464
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700465 // If set this fragment would like its instance retained across
466 // configuration changes.
467 boolean mRetainInstance;
George Mountc03da0e2014-08-22 17:04:02 -0700468
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700469 // If set this fragment is being retained across the current config change.
470 boolean mRetaining;
George Mountc03da0e2014-08-22 17:04:02 -0700471
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700472 // If set this fragment has menu items to contribute.
473 boolean mHasMenu;
Dianne Hackborn6c285972011-08-29 16:53:49 -0700474
475 // Set to true to allow the fragment's menu to be shown.
476 boolean mMenuVisible = true;
477
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700478 // Used to verify that subclasses call through to super class.
479 boolean mCalled;
George Mountc03da0e2014-08-22 17:04:02 -0700480
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700481 // If app has requested a specific animation, this is the one to use.
482 int mNextAnim;
George Mountc03da0e2014-08-22 17:04:02 -0700483
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700484 // The parent container of the fragment after dynamically added to UI.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700485 ViewGroup mContainer;
George Mountc03da0e2014-08-22 17:04:02 -0700486
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700487 // The View generated for this fragment.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700488 View mView;
George Mountc03da0e2014-08-22 17:04:02 -0700489
Adam Powell635c60a2011-10-26 10:22:16 -0700490 // Whether this fragment should defer starting until after other fragments
491 // have been started and their loaders are finished.
492 boolean mDeferStart;
493
Adam Powell78fed9b2011-11-07 10:45:34 -0800494 // Hint provided by the app that this fragment is currently visible to the user.
495 boolean mUserVisibleHint = true;
496
Dianne Hackborn4911b782010-07-15 12:54:39 -0700497 LoaderManagerImpl mLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700498 boolean mLoadersStarted;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700499 boolean mCheckedForLoaderManager;
George Mountc03da0e2014-08-22 17:04:02 -0700500
501 private Transition mEnterTransition = null;
502 private Transition mReturnTransition = USE_DEFAULT_TRANSITION;
503 private Transition mExitTransition = null;
504 private Transition mReenterTransition = USE_DEFAULT_TRANSITION;
505 private Transition mSharedElementEnterTransition = null;
506 private Transition mSharedElementReturnTransition = USE_DEFAULT_TRANSITION;
507 private Boolean mAllowReturnTransitionOverlap;
508 private Boolean mAllowEnterTransitionOverlap;
509
George Mount65580562014-08-29 08:15:48 -0700510 SharedElementCallback mEnterTransitionCallback = SharedElementCallback.NULL_CALLBACK;
511 SharedElementCallback mExitTransitionCallback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -0700512
Dianne Hackbornc8017682010-07-06 13:34:38 -0700513 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700514 * State information that has been retrieved from a fragment instance
515 * through {@link FragmentManager#saveFragmentInstanceState(Fragment)
516 * FragmentManager.saveFragmentInstanceState}.
517 */
518 public static class SavedState implements Parcelable {
519 final Bundle mState;
520
521 SavedState(Bundle state) {
522 mState = state;
523 }
524
525 SavedState(Parcel in, ClassLoader loader) {
526 mState = in.readBundle();
527 if (loader != null && mState != null) {
528 mState.setClassLoader(loader);
529 }
530 }
531
532 @Override
533 public int describeContents() {
534 return 0;
535 }
536
537 @Override
538 public void writeToParcel(Parcel dest, int flags) {
539 dest.writeBundle(mState);
540 }
541
542 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR
543 = new Parcelable.ClassLoaderCreator<SavedState>() {
544 public SavedState createFromParcel(Parcel in) {
545 return new SavedState(in, null);
546 }
547
548 public SavedState createFromParcel(Parcel in, ClassLoader loader) {
549 return new SavedState(in, loader);
550 }
551
552 public SavedState[] newArray(int size) {
553 return new SavedState[size];
554 }
555 };
556 }
557
558 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700559 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when
560 * there is an instantiation failure.
561 */
562 static public class InstantiationException extends AndroidRuntimeException {
563 public InstantiationException(String msg, Exception cause) {
564 super(msg, cause);
565 }
566 }
567
568 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700569 * Default constructor. <strong>Every</strong> fragment must have an
Dianne Hackbornc8017682010-07-06 13:34:38 -0700570 * empty constructor, so it can be instantiated when restoring its
571 * activity's state. It is strongly recommended that subclasses do not
572 * have other constructors with parameters, since these constructors
573 * will not be called when the fragment is re-instantiated; instead,
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700574 * arguments can be supplied by the caller with {@link #setArguments}
575 * and later retrieved by the Fragment with {@link #getArguments}.
George Mountc03da0e2014-08-22 17:04:02 -0700576 *
Aurimas Liutikasc59422a2016-06-14 16:50:07 -0700577 * <p>Applications should generally not implement a constructor. Prefer
578 * {@link #onAttach(Context)} instead. It is the first place application code can run where
579 * the fragment is ready to be used - the point where the fragment is actually associated with
580 * its context. Some applications may also want to implement {@link #onInflate} to retrieve
581 * attributes from a layout resource, although note this happens when the fragment is attached.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700582 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700583 public Fragment() {
584 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700585
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700586 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700587 * Like {@link #instantiate(Context, String, Bundle)} but with a null
588 * argument Bundle.
589 */
590 public static Fragment instantiate(Context context, String fname) {
591 return instantiate(context, fname, null);
592 }
593
594 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700595 * Create a new instance of a Fragment with the given class name. This is
596 * the same as calling its empty constructor.
597 *
598 * @param context The calling context being used to instantiate the fragment.
599 * This is currently just used to get its ClassLoader.
600 * @param fname The class name of the fragment to instantiate.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700601 * @param args Bundle of arguments to supply to the fragment, which it
602 * can retrieve with {@link #getArguments()}. May be null.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700603 * @return Returns a new fragment instance.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700604 * @throws InstantiationException If there is a failure in instantiating
605 * the given fragment class. This is a runtime exception; it is not
606 * normally expected to happen.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700607 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700608 public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700609 try {
610 Class<?> clazz = sClassMap.get(fname);
611 if (clazz == null) {
612 // Class not found in the cache, see if it's real, and try to add it
613 clazz = context.getClassLoader().loadClass(fname);
Amith Yamasani364ed4d2013-07-26 13:37:56 -0700614 if (!Fragment.class.isAssignableFrom(clazz)) {
615 throw new InstantiationException("Trying to instantiate a class " + fname
616 + " that is not a Fragment", new ClassCastException());
617 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700618 sClassMap.put(fname, clazz);
619 }
620 Fragment f = (Fragment)clazz.newInstance();
621 if (args != null) {
622 args.setClassLoader(f.getClass().getClassLoader());
623 f.mArguments = args;
624 }
625 return f;
626 } catch (ClassNotFoundException e) {
627 throw new InstantiationException("Unable to instantiate fragment " + fname
628 + ": make sure class name exists, is public, and has an"
629 + " empty constructor that is public", e);
630 } catch (java.lang.InstantiationException e) {
631 throw new InstantiationException("Unable to instantiate fragment " + fname
632 + ": make sure class name exists, is public, and has an"
633 + " empty constructor that is public", e);
634 } catch (IllegalAccessException e) {
635 throw new InstantiationException("Unable to instantiate fragment " + fname
636 + ": make sure class name exists, is public, and has an"
637 + " empty constructor that is public", e);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700638 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700639 }
George Mountc03da0e2014-08-22 17:04:02 -0700640
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700641 final void restoreViewState(Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700642 if (mSavedViewState != null) {
643 mView.restoreHierarchyState(mSavedViewState);
644 mSavedViewState = null;
645 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700646 mCalled = false;
647 onViewStateRestored(savedInstanceState);
648 if (!mCalled) {
649 throw new SuperNotCalledException("Fragment " + this
650 + " did not call through to super.onViewStateRestored()");
651 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700652 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700653
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700654 final void setIndex(int index, Fragment parent) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700655 mIndex = index;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700656 if (parent != null) {
657 mWho = parent.mWho + ":" + mIndex;
658 } else {
659 mWho = "android:fragment:" + mIndex;
660 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700661 }
662
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700663 final boolean isInBackStack() {
664 return mBackStackNesting > 0;
665 }
666
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700667 /**
668 * Subclasses can not override equals().
669 */
670 @Override final public boolean equals(Object o) {
671 return super.equals(o);
672 }
673
674 /**
675 * Subclasses can not override hashCode().
676 */
677 @Override final public int hashCode() {
678 return super.hashCode();
679 }
George Mountc03da0e2014-08-22 17:04:02 -0700680
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700681 @Override
682 public String toString() {
683 StringBuilder sb = new StringBuilder(128);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800684 DebugUtils.buildShortClassTag(this, sb);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700685 if (mIndex >= 0) {
686 sb.append(" #");
687 sb.append(mIndex);
688 }
689 if (mFragmentId != 0) {
690 sb.append(" id=0x");
691 sb.append(Integer.toHexString(mFragmentId));
692 }
693 if (mTag != null) {
694 sb.append(" ");
695 sb.append(mTag);
696 }
697 sb.append('}');
698 return sb.toString();
699 }
George Mountc03da0e2014-08-22 17:04:02 -0700700
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700701 /**
702 * Return the identifier this fragment is known by. This is either
703 * the android:id value supplied in a layout or the container view ID
704 * supplied when adding the fragment.
705 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700706 final public int getId() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700707 return mFragmentId;
708 }
George Mountc03da0e2014-08-22 17:04:02 -0700709
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700710 /**
711 * Get the tag name of the fragment, if specified.
712 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700713 final public String getTag() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700714 return mTag;
715 }
George Mountc03da0e2014-08-22 17:04:02 -0700716
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700717 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700718 * Supply the construction arguments for this fragment. This can only
719 * be called before the fragment has been attached to its activity; that
720 * is, you should call it immediately after constructing the fragment. The
721 * arguments supplied here will be retained across fragment destroy and
722 * creation.
723 */
Dianne Hackborndef15372010-08-15 12:43:52 -0700724 public void setArguments(Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700725 if (mIndex >= 0) {
726 throw new IllegalStateException("Fragment already active");
727 }
728 mArguments = args;
729 }
730
731 /**
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700732 * Return the arguments supplied to {@link #setArguments}, if any.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700733 */
734 final public Bundle getArguments() {
735 return mArguments;
736 }
737
738 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700739 * Set the initial saved state that this Fragment should restore itself
740 * from when first being constructed, as returned by
741 * {@link FragmentManager#saveFragmentInstanceState(Fragment)
742 * FragmentManager.saveFragmentInstanceState}.
743 *
744 * @param state The state the fragment should be restored from.
745 */
746 public void setInitialSavedState(SavedState state) {
747 if (mIndex >= 0) {
748 throw new IllegalStateException("Fragment already active");
749 }
750 mSavedFragmentState = state != null && state.mState != null
751 ? state.mState : null;
752 }
753
754 /**
Dianne Hackborndef15372010-08-15 12:43:52 -0700755 * Optional target for this fragment. This may be used, for example,
756 * if this fragment is being started by another, and when done wants to
757 * give a result back to the first. The target set here is retained
758 * across instances via {@link FragmentManager#putFragment
759 * FragmentManager.putFragment()}.
760 *
761 * @param fragment The fragment that is the target of this one.
762 * @param requestCode Optional request code, for convenience if you
763 * are going to call back with {@link #onActivityResult(int, int, Intent)}.
764 */
765 public void setTargetFragment(Fragment fragment, int requestCode) {
766 mTarget = fragment;
767 mTargetRequestCode = requestCode;
768 }
769
770 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700771 * Return the target fragment set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700772 */
773 final public Fragment getTargetFragment() {
774 return mTarget;
775 }
776
777 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700778 * Return the target request code set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700779 */
780 final public int getTargetRequestCode() {
781 return mTargetRequestCode;
782 }
783
784 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700785 * Return the {@link Context} this fragment is currently associated with.
786 */
787 public Context getContext() {
788 return mHost == null ? null : mHost.getContext();
789 }
790
791 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700792 * Return the Activity this fragment is currently associated with.
793 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700794 final public Activity getActivity() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700795 return mHost == null ? null : mHost.getActivity();
796 }
797
798 /**
799 * Return the host object of this fragment. May return {@code null} if the fragment
800 * isn't currently being hosted.
801 */
802 @Nullable
803 final public Object getHost() {
804 return mHost == null ? null : mHost.onGetHost();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700805 }
George Mountc03da0e2014-08-22 17:04:02 -0700806
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700807 /**
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800808 * Return <code>getActivity().getResources()</code>.
809 */
810 final public Resources getResources() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700811 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800812 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
813 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700814 return mHost.getContext().getResources();
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800815 }
George Mountc03da0e2014-08-22 17:04:02 -0700816
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800817 /**
818 * Return a localized, styled CharSequence from the application's package's
819 * default string table.
820 *
821 * @param resId Resource id for the CharSequence text
822 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700823 public final CharSequence getText(@StringRes int resId) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800824 return getResources().getText(resId);
825 }
826
827 /**
828 * Return a localized string from the application's package's
829 * default string table.
830 *
831 * @param resId Resource id for the string
832 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700833 public final String getString(@StringRes int resId) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800834 return getResources().getString(resId);
835 }
836
837 /**
838 * Return a localized formatted string from the application's package's
839 * default string table, substituting the format arguments as defined in
840 * {@link java.util.Formatter} and {@link java.lang.String#format}.
841 *
842 * @param resId Resource id for the format string
843 * @param formatArgs The format arguments that will be used for substitution.
844 */
845
Tor Norbye7b9c9122013-05-30 16:48:33 -0700846 public final String getString(@StringRes int resId, Object... formatArgs) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800847 return getResources().getString(resId, formatArgs);
848 }
849
850 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700851 * Return the FragmentManager for interacting with fragments associated
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700852 * with this fragment's activity. Note that this will be non-null slightly
Dianne Hackborn625ac272010-09-17 18:29:22 -0700853 * before {@link #getActivity()}, during the time from when the fragment is
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700854 * placed in a {@link FragmentTransaction} until it is committed and
855 * attached to its activity.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700856 *
857 * <p>If this Fragment is a child of another Fragment, the FragmentManager
858 * returned here will be the parent's {@link #getChildFragmentManager()}.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700859 */
860 final public FragmentManager getFragmentManager() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700861 return mFragmentManager;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700862 }
863
864 /**
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700865 * Return a private FragmentManager for placing and managing Fragments
866 * inside of this Fragment.
867 */
868 final public FragmentManager getChildFragmentManager() {
869 if (mChildFragmentManager == null) {
870 instantiateChildFragmentManager();
871 if (mState >= RESUMED) {
872 mChildFragmentManager.dispatchResume();
873 } else if (mState >= STARTED) {
874 mChildFragmentManager.dispatchStart();
875 } else if (mState >= ACTIVITY_CREATED) {
876 mChildFragmentManager.dispatchActivityCreated();
877 } else if (mState >= CREATED) {
878 mChildFragmentManager.dispatchCreate();
879 }
880 }
881 return mChildFragmentManager;
882 }
883
884 /**
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700885 * Returns the parent Fragment containing this Fragment. If this Fragment
886 * is attached directly to an Activity, returns null.
887 */
888 final public Fragment getParentFragment() {
889 return mParentFragment;
890 }
891
892 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700893 * Return true if the fragment is currently added to its activity.
894 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700895 final public boolean isAdded() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700896 return mHost != null && mAdded;
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800897 }
898
899 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700900 * Return true if the fragment has been explicitly detached from the UI.
901 * That is, {@link FragmentTransaction#detach(Fragment)
902 * FragmentTransaction.detach(Fragment)} has been used on it.
903 */
904 final public boolean isDetached() {
905 return mDetached;
906 }
907
908 /**
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800909 * Return true if this fragment is currently being removed from its
910 * activity. This is <em>not</em> whether its activity is finishing, but
911 * rather whether it is in the process of being removed from its activity.
912 */
913 final public boolean isRemoving() {
914 return mRemoving;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700915 }
George Mountc03da0e2014-08-22 17:04:02 -0700916
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700917 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700918 * Return true if the layout is included as part of an activity view
919 * hierarchy via the &lt;fragment&gt; tag. This will always be true when
920 * fragments are created through the &lt;fragment&gt; tag, <em>except</em>
921 * in the case where an old fragment is restored from a previous state and
922 * it does not appear in the layout of the current state.
923 */
924 final public boolean isInLayout() {
925 return mInLayout;
926 }
927
928 /**
Dianne Hackborn2707d602010-07-09 18:01:20 -0700929 * Return true if the fragment is in the resumed state. This is true
930 * for the duration of {@link #onResume()} and {@link #onPause()} as well.
931 */
932 final public boolean isResumed() {
Adam Powelld1d4d9c2016-01-12 10:11:42 -0800933 return mState >= RESUMED;
Dianne Hackborn2707d602010-07-09 18:01:20 -0700934 }
George Mountc03da0e2014-08-22 17:04:02 -0700935
Dianne Hackborn2707d602010-07-09 18:01:20 -0700936 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700937 * Return true if the fragment is currently visible to the user. This means
938 * it: (1) has been added, (2) has its view attached to the window, and
939 * (3) is not hidden.
940 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700941 final public boolean isVisible() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700942 return isAdded() && !isHidden() && mView != null
943 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
944 }
George Mountc03da0e2014-08-22 17:04:02 -0700945
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700946 /**
947 * Return true if the fragment has been hidden. By default fragments
948 * are shown. You can find out about changes to this state with
Dianne Hackborncddfa6d2010-05-19 22:56:37 -0700949 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700950 * to other states -- that is, to be visible to the user, a fragment
951 * must be both started and not hidden.
952 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700953 final public boolean isHidden() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700954 return mHidden;
955 }
George Mountc03da0e2014-08-22 17:04:02 -0700956
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700957 /**
958 * Called when the hidden state (as returned by {@link #isHidden()} of
959 * the fragment has changed. Fragments start out not hidden; this will
960 * be called whenever the fragment changes state from that.
Aurimas Liutikasc3cfdaf2016-06-07 14:56:24 -0700961 * @param hidden True if the fragment is now hidden, false otherwise.
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700962 */
963 public void onHiddenChanged(boolean hidden) {
964 }
George Mountc03da0e2014-08-22 17:04:02 -0700965
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700966 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700967 * Control whether a fragment instance is retained across Activity
968 * re-creation (such as from a configuration change). This can only
969 * be used with fragments not in the back stack. If set, the fragment
970 * lifecycle will be slightly different when an activity is recreated:
971 * <ul>
972 * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still
973 * will be, because the fragment is being detached from its current activity).
974 * <li> {@link #onCreate(Bundle)} will not be called since the fragment
975 * is not being re-created.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700976 * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b>
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700977 * still be called.
978 * </ul>
979 */
980 public void setRetainInstance(boolean retain) {
981 mRetainInstance = retain;
982 }
George Mountc03da0e2014-08-22 17:04:02 -0700983
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700984 final public boolean getRetainInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700985 return mRetainInstance;
986 }
George Mountc03da0e2014-08-22 17:04:02 -0700987
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700988 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700989 * Report that this fragment would like to participate in populating
Wink Saville4dc643e2010-06-12 22:16:41 -0700990 * the options menu by receiving a call to {@link #onCreateOptionsMenu}
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700991 * and related methods.
George Mountc03da0e2014-08-22 17:04:02 -0700992 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700993 * @param hasMenu If true, the fragment has menu items to contribute.
994 */
995 public void setHasOptionsMenu(boolean hasMenu) {
996 if (mHasMenu != hasMenu) {
997 mHasMenu = hasMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -0700998 if (isAdded() && !isHidden()) {
999 mFragmentManager.invalidateOptionsMenu();
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001000 }
1001 }
1002 }
Dianne Hackborn6c285972011-08-29 16:53:49 -07001003
1004 /**
1005 * Set a hint for whether this fragment's menu should be visible. This
1006 * is useful if you know that a fragment has been placed in your view
1007 * hierarchy so that the user can not currently seen it, so any menu items
1008 * it has should also not be shown.
1009 *
1010 * @param menuVisible The default is true, meaning the fragment's menu will
1011 * be shown as usual. If false, the user will not see the menu.
1012 */
1013 public void setMenuVisibility(boolean menuVisible) {
1014 if (mMenuVisible != menuVisible) {
1015 mMenuVisible = menuVisible;
1016 if (mHasMenu && isAdded() && !isHidden()) {
1017 mFragmentManager.invalidateOptionsMenu();
1018 }
1019 }
1020 }
1021
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001022 /**
Adam Powell78fed9b2011-11-07 10:45:34 -08001023 * Set a hint to the system about whether this fragment's UI is currently visible
1024 * to the user. This hint defaults to true and is persistent across fragment instance
1025 * state save and restore.
Adam Powell635c60a2011-10-26 10:22:16 -07001026 *
Adam Powell78fed9b2011-11-07 10:45:34 -08001027 * <p>An app may set this to false to indicate that the fragment's UI is
1028 * scrolled out of visibility or is otherwise not directly visible to the user.
1029 * This may be used by the system to prioritize operations such as fragment lifecycle updates
1030 * or loader ordering behavior.</p>
Adam Powell635c60a2011-10-26 10:22:16 -07001031 *
Adam Powell7f46c1b2016-06-22 10:23:02 -07001032 * <p><strong>Note:</strong> This method may be called outside of the fragment lifecycle
1033 * and thus has no ordering guarantees with regard to fragment lifecycle method calls.</p>
1034 *
Adam Powellef4d99f2016-04-18 11:01:55 -07001035 * <p><strong>Note:</strong> Prior to Android N there was a platform bug that could cause
1036 * <code>setUserVisibleHint</code> to bring a fragment up to the started state before its
1037 * <code>FragmentTransaction</code> had been committed. As some apps relied on this behavior,
1038 * it is preserved for apps that declare a <code>targetSdkVersion</code> of 23 or lower.</p>
1039 *
Adam Powell78fed9b2011-11-07 10:45:34 -08001040 * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
1041 * false if it is not.
Adam Powell635c60a2011-10-26 10:22:16 -07001042 */
Adam Powell78fed9b2011-11-07 10:45:34 -08001043 public void setUserVisibleHint(boolean isVisibleToUser) {
Adam Powellef4d99f2016-04-18 11:01:55 -07001044 // Prior to Android N we were simply checking if this fragment had a FragmentManager
1045 // set before we would trigger a deferred start. Unfortunately this also gets set before
1046 // a fragment transaction is committed, so if setUserVisibleHint was called before a
1047 // transaction commit, we would start the fragment way too early. FragmentPagerAdapter
1048 // triggers this situation.
1049 // Unfortunately some apps relied on this timing in overrides of setUserVisibleHint
1050 // on their own fragments, and expected, however erroneously, that after a call to
1051 // super.setUserVisibleHint their onStart methods had been run.
1052 // We preserve this behavior for apps targeting old platform versions below.
1053 boolean useBrokenAddedCheck = false;
1054 Context context = getContext();
1055 if (mFragmentManager != null && mFragmentManager.mHost != null) {
1056 context = mFragmentManager.mHost.getContext();
1057 }
1058 if (context != null) {
1059 useBrokenAddedCheck = context.getApplicationInfo().targetSdkVersion <= VERSION_CODES.M;
1060 }
1061
1062 final boolean performDeferredStart;
1063 if (useBrokenAddedCheck) {
1064 performDeferredStart = !mUserVisibleHint && isVisibleToUser && mState < STARTED
1065 && mFragmentManager != null;
1066 } else {
1067 performDeferredStart = !mUserVisibleHint && isVisibleToUser && mState < STARTED
1068 && mFragmentManager != null && isAdded();
1069 }
1070
1071 if (performDeferredStart) {
Adam Powell635c60a2011-10-26 10:22:16 -07001072 mFragmentManager.performPendingDeferredStart(this);
1073 }
Adam Powellef4d99f2016-04-18 11:01:55 -07001074
Adam Powell78fed9b2011-11-07 10:45:34 -08001075 mUserVisibleHint = isVisibleToUser;
Adam Powellb963c992016-04-01 15:29:04 -07001076 mDeferStart = mState < STARTED && !isVisibleToUser;
Adam Powell635c60a2011-10-26 10:22:16 -07001077 }
1078
1079 /**
Adam Powell78fed9b2011-11-07 10:45:34 -08001080 * @return The current value of the user-visible hint on this fragment.
1081 * @see #setUserVisibleHint(boolean)
Adam Powell635c60a2011-10-26 10:22:16 -07001082 */
Adam Powell78fed9b2011-11-07 10:45:34 -08001083 public boolean getUserVisibleHint() {
1084 return mUserVisibleHint;
Adam Powell635c60a2011-10-26 10:22:16 -07001085 }
1086
1087 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001088 * Return the LoaderManager for this fragment, creating it if needed.
1089 */
1090 public LoaderManager getLoaderManager() {
1091 if (mLoaderManager != null) {
1092 return mLoaderManager;
1093 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001094 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001095 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1096 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001097 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001098 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -07001099 return mLoaderManager;
1100 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001101
Dianne Hackbornc8017682010-07-06 13:34:38 -07001102 /**
Scott Main87bff972013-02-27 15:46:34 -08001103 * Call {@link Activity#startActivity(Intent)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001104 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001105 *
1106 * @param intent The intent to start.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001107 */
1108 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001109 startActivity(intent, null);
1110 }
George Mountc03da0e2014-08-22 17:04:02 -07001111
Dianne Hackborna4972e92012-03-14 10:38:05 -07001112 /**
Scott Main87bff972013-02-27 15:46:34 -08001113 * Call {@link Activity#startActivity(Intent, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001114 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001115 *
1116 * @param intent The intent to start.
1117 * @param options Additional options for how the Activity should be started.
1118 * See {@link android.content.Context#startActivity(Intent, Bundle)
1119 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07001120 */
1121 public void startActivity(Intent intent, Bundle options) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001122 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001123 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1124 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001125 if (options != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001126 mHost.onStartActivityFromFragment(this, intent, -1, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07001127 } else {
1128 // Note we want to go through this call for compatibility with
1129 // applications that may have overridden the method.
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001130 mHost.onStartActivityFromFragment(this, intent, -1, null /*options*/);
Dianne Hackborna4972e92012-03-14 10:38:05 -07001131 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001132 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001133
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001134 /**
Scott Main87bff972013-02-27 15:46:34 -08001135 * Call {@link Activity#startActivityForResult(Intent, int)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001136 * containing Activity.
1137 */
1138 public void startActivityForResult(Intent intent, int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001139 startActivityForResult(intent, requestCode, null);
1140 }
1141
1142 /**
Scott Main87bff972013-02-27 15:46:34 -08001143 * Call {@link Activity#startActivityForResult(Intent, int, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001144 * containing Activity.
1145 */
1146 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001147 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001148 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1149 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001150 mHost.onStartActivityFromFragment(this, intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001151 }
George Mountc03da0e2014-08-22 17:04:02 -07001152
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001153 /**
Clara Bayarria0c2dc32016-04-12 12:00:15 +01001154 * Call {@link Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int,
1155 * Bundle)} from the fragment's containing Activity.
1156 */
1157 public void startIntentSenderForResult(IntentSender intent, int requestCode,
1158 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
1159 Bundle options) throws IntentSender.SendIntentException {
1160 if (mHost == null) {
1161 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1162 }
1163 mHost.onStartIntentSenderFromFragment(this, intent, requestCode, fillInIntent, flagsMask,
1164 flagsValues, extraFlags, options);
1165 }
1166
1167 /**
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001168 * Receive the result from a previous call to
1169 * {@link #startActivityForResult(Intent, int)}. This follows the
1170 * related Activity API as described there in
1171 * {@link Activity#onActivityResult(int, int, Intent)}.
George Mountc03da0e2014-08-22 17:04:02 -07001172 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001173 * @param requestCode The integer request code originally supplied to
1174 * startActivityForResult(), allowing you to identify who this
1175 * result came from.
1176 * @param resultCode The integer result code returned by the child activity
1177 * through its setResult().
1178 * @param data An Intent, which can return result data to the caller
1179 * (various data can be attached to Intent "extras").
1180 */
1181 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1182 }
George Mountc03da0e2014-08-22 17:04:02 -07001183
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001184 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08001185 * Requests permissions to be granted to this application. These permissions
1186 * must be requested in your manifest, they should not be granted to your app,
1187 * and they should have protection level {@link android.content.pm.PermissionInfo
1188 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
1189 * the platform or a third-party app.
1190 * <p>
1191 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
1192 * are granted at install time if requested in the manifest. Signature permissions
1193 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
1194 * install time if requested in the manifest and the signature of your app matches
1195 * the signature of the app declaring the permissions.
1196 * </p>
1197 * <p>
1198 * If your app does not have the requested permissions the user will be presented
1199 * with UI for accepting them. After the user has accepted or rejected the
1200 * requested permissions you will receive a callback on {@link
1201 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
1202 * permissions were granted or not.
1203 * </p>
1204 * <p>
1205 * Note that requesting a permission does not guarantee it will be granted and
1206 * your app should be able to run without having this permission.
1207 * </p>
1208 * <p>
1209 * This method may start an activity allowing the user to choose which permissions
1210 * to grant and which to reject. Hence, you should be prepared that your activity
1211 * may be paused and resumed. Further, granting some permissions may require
1212 * a restart of you application. In such a case, the system will recreate the
1213 * activity stack before delivering the result to {@link
1214 * #onRequestPermissionsResult(int, String[], int[])}.
1215 * </p>
1216 * <p>
1217 * When checking whether you have a permission you should use {@link
1218 * android.content.Context#checkSelfPermission(String)}.
1219 * </p>
1220 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07001221 * Calling this API for permissions already granted to your app would show UI
1222 * to the user to decide whether the app can still hold these permissions. This
1223 * can be useful if the way your app uses data guarded by the permissions
1224 * changes significantly.
1225 * </p>
1226 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07001227 * You cannot request a permission if your activity sets {@link
1228 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
1229 * <code>true</code> because in this case the activity would not receive
1230 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
1231 * </p>
1232 * <p>
Svetoslavc6d1c342015-02-26 14:44:43 -08001233 * A sample permissions request looks like this:
1234 * </p>
1235 * <code><pre><p>
1236 * private void showContacts() {
1237 * if (getActivity().checkSelfPermission(Manifest.permission.READ_CONTACTS)
1238 * != PackageManager.PERMISSION_GRANTED) {
1239 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
1240 * PERMISSIONS_REQUEST_READ_CONTACTS);
1241 * } else {
1242 * doShowContacts();
1243 * }
1244 * }
1245 *
1246 * {@literal @}Override
1247 * public void onRequestPermissionsResult(int requestCode, String[] permissions,
1248 * int[] grantResults) {
1249 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS
1250 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
1251 * doShowContacts();
1252 * }
1253 * }
1254 * </code></pre></p>
1255 *
Svet Ganovf66381c2016-02-18 20:02:36 -08001256 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08001257 * @param requestCode Application specific request code to match with a result
1258 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07001259 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08001260 *
1261 * @see #onRequestPermissionsResult(int, String[], int[])
1262 * @see android.content.Context#checkSelfPermission(String)
1263 */
1264 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001265 if (mHost == null) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001266 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1267 }
Svetoslav970b59c2015-06-09 16:05:21 -07001268 mHost.onRequestPermissionsFromFragment(this, permissions,requestCode);
Svetoslavc6d1c342015-02-26 14:44:43 -08001269 }
1270
1271 /**
1272 * Callback for the result from requesting permissions. This method
1273 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07001274 * <p>
1275 * <strong>Note:</strong> It is possible that the permissions request interaction
1276 * with the user is interrupted. In this case you will receive empty permissions
1277 * and results arrays which should be treated as a cancellation.
1278 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08001279 *
1280 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
1281 * @param permissions The requested permissions. Never null.
1282 * @param grantResults The grant results for the corresponding permissions
1283 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
1284 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
1285 *
1286 * @see #requestPermissions(String[], int)
1287 */
1288 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
1289 @NonNull int[] grantResults) {
1290 /* callback - do nothing */
1291 }
1292
1293 /**
Svetoslava2011d22015-06-03 15:43:59 -07001294 * Gets whether you should show UI with rationale for requesting a permission.
1295 * You should do this only if you do not have the permission and the context in
1296 * which the permission is requested does not clearly communicate to the user
1297 * what would be the benefit from granting this permission.
1298 * <p>
1299 * For example, if you write a camera app, requesting the camera permission
1300 * would be expected by the user and no rationale for why it is requested is
1301 * needed. If however, the app needs location for tagging photos then a non-tech
1302 * savvy user may wonder how location is related to taking photos. In this case
1303 * you may choose to show UI with rationale of requesting this permission.
1304 * </p>
1305 *
1306 * @param permission A permission your app wants to request.
1307 * @return Whether you can show permission rationale UI.
1308 *
1309 * @see Context#checkSelfPermission(String)
1310 * @see #requestPermissions(String[], int)
1311 * @see #onRequestPermissionsResult(int, String[], int[])
1312 */
1313 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
1314 if (mHost != null) {
Svet Ganov9e136b82015-07-16 09:58:38 -07001315 return mHost.getContext().getPackageManager()
1316 .shouldShowRequestPermissionRationale(permission);
Svetoslava2011d22015-06-03 15:43:59 -07001317 }
1318 return false;
1319 }
1320
1321 /**
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001322 * @hide Hack so that DialogFragment can make its Dialog before creating
1323 * its views, and the view construction can use the dialog's context for
1324 * inflation. Maybe this should become a public API. Note sure.
1325 */
1326 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001327 final LayoutInflater result = mHost.onGetLayoutInflater();
1328 if (mHost.onUseFragmentManagerInflaterFactory()) {
Adam Powell371a8092014-06-20 12:51:12 -07001329 getChildFragmentManager(); // Init if needed; use raw implementation below.
1330 result.setPrivateFactory(mChildFragmentManager.getLayoutInflaterFactory());
Adam Powell371a8092014-06-20 12:51:12 -07001331 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001332 return result;
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001333 }
George Mountc03da0e2014-08-22 17:04:02 -07001334
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001335 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001336 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead.
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001337 */
1338 @Deprecated
Adam Powella9bab982016-04-21 11:04:41 -07001339 @CallSuper
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001340 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
1341 mCalled = true;
1342 }
1343
1344 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001345 * Called when a fragment is being created as part of a view layout
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001346 * inflation, typically from setting the content view of an activity. This
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001347 * may be called immediately after the fragment is created from a <fragment>
Dianne Hackborndef15372010-08-15 12:43:52 -07001348 * tag in a layout file. Note this is <em>before</em> the fragment's
1349 * {@link #onAttach(Activity)} has been called; all you should do here is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001350 * parse the attributes and save them away.
George Mountc03da0e2014-08-22 17:04:02 -07001351 *
Dianne Hackborndef15372010-08-15 12:43:52 -07001352 * <p>This is called every time the fragment is inflated, even if it is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001353 * being inflated into a new instance with saved state. It typically makes
1354 * sense to re-parse the parameters each time, to allow them to change with
1355 * different configurations.</p>
1356 *
1357 * <p>Here is a typical implementation of a fragment that can take parameters
1358 * both through attributes supplied here as well from {@link #getArguments()}:</p>
1359 *
1360 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1361 * fragment}
1362 *
1363 * <p>Note that parsing the XML attributes uses a "styleable" resource. The
1364 * declaration for the styleable used here is:</p>
1365 *
1366 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments}
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001367 *
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001368 * <p>The fragment can then be declared within its activity's content layout
1369 * through a tag like this:</p>
1370 *
1371 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes}
1372 *
1373 * <p>This fragment can also be created dynamically from arguments given
1374 * at runtime in the arguments Bundle; here is an example of doing so at
1375 * creation of the containing activity:</p>
1376 *
1377 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1378 * create}
1379 *
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001380 * @param context The Context that is inflating this fragment.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001381 * @param attrs The attributes at the tag where the fragment is
1382 * being created.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001383 * @param savedInstanceState If the fragment is being re-created from
1384 * a previous saved state, this is the state.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001385 */
Adam Powella9bab982016-04-21 11:04:41 -07001386 @CallSuper
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001387 public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001388 onInflate(attrs, savedInstanceState);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001389 mCalled = true;
George Mountc03da0e2014-08-22 17:04:02 -07001390
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001391 TypedArray a = context.obtainStyledAttributes(attrs,
George Mountc03da0e2014-08-22 17:04:02 -07001392 com.android.internal.R.styleable.Fragment);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001393 mEnterTransition = loadTransition(context, a, mEnterTransition, null,
George Mountc03da0e2014-08-22 17:04:02 -07001394 com.android.internal.R.styleable.Fragment_fragmentEnterTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001395 mReturnTransition = loadTransition(context, a, mReturnTransition, USE_DEFAULT_TRANSITION,
George Mountc03da0e2014-08-22 17:04:02 -07001396 com.android.internal.R.styleable.Fragment_fragmentReturnTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001397 mExitTransition = loadTransition(context, a, mExitTransition, null,
George Mountc03da0e2014-08-22 17:04:02 -07001398 com.android.internal.R.styleable.Fragment_fragmentExitTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001399 mReenterTransition = loadTransition(context, a, mReenterTransition, USE_DEFAULT_TRANSITION,
George Mountc03da0e2014-08-22 17:04:02 -07001400 com.android.internal.R.styleable.Fragment_fragmentReenterTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001401 mSharedElementEnterTransition = loadTransition(context, a, mSharedElementEnterTransition,
George Mountc03da0e2014-08-22 17:04:02 -07001402 null, com.android.internal.R.styleable.Fragment_fragmentSharedElementEnterTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001403 mSharedElementReturnTransition = loadTransition(context, a, mSharedElementReturnTransition,
George Mountc03da0e2014-08-22 17:04:02 -07001404 USE_DEFAULT_TRANSITION,
1405 com.android.internal.R.styleable.Fragment_fragmentSharedElementReturnTransition);
1406 if (mAllowEnterTransitionOverlap == null) {
1407 mAllowEnterTransitionOverlap = a.getBoolean(
1408 com.android.internal.R.styleable.Fragment_fragmentAllowEnterTransitionOverlap, true);
1409 }
1410 if (mAllowReturnTransitionOverlap == null) {
1411 mAllowReturnTransitionOverlap = a.getBoolean(
1412 com.android.internal.R.styleable.Fragment_fragmentAllowReturnTransitionOverlap, true);
1413 }
1414 a.recycle();
Todd Kennedy434bd652015-05-04 12:29:50 -07001415
1416 final Activity hostActivity = mHost == null ? null : mHost.getActivity();
1417 if (hostActivity != null) {
1418 mCalled = false;
1419 onInflate(hostActivity, attrs, savedInstanceState);
1420 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001421 }
George Mountc03da0e2014-08-22 17:04:02 -07001422
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001423 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001424 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead.
1425 */
1426 @Deprecated
Adam Powella9bab982016-04-21 11:04:41 -07001427 @CallSuper
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001428 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
1429 mCalled = true;
1430 }
1431
1432 /**
Adam Powelle30299f2016-05-13 11:04:39 -07001433 * Called when a fragment is attached as a child of this fragment.
1434 *
1435 * <p>This is called after the attached fragment's <code>onAttach</code> and before
1436 * the attached fragment's <code>onCreate</code> if the fragment has not yet had a previous
1437 * call to <code>onCreate</code>.</p>
1438 *
1439 * @param childFragment child fragment being attached
1440 */
1441 public void onAttachFragment(Fragment childFragment) {
1442 }
1443
1444 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001445 * Called when a fragment is first attached to its context.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001446 * {@link #onCreate(Bundle)} will be called after this.
1447 */
Adam Powella9bab982016-04-21 11:04:41 -07001448 @CallSuper
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001449 public void onAttach(Context context) {
1450 mCalled = true;
1451 final Activity hostActivity = mHost == null ? null : mHost.getActivity();
1452 if (hostActivity != null) {
1453 mCalled = false;
1454 onAttach(hostActivity);
1455 }
1456 }
1457
1458 /**
1459 * @deprecated Use {@link #onAttach(Context)} instead.
1460 */
1461 @Deprecated
Adam Powella9bab982016-04-21 11:04:41 -07001462 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001463 public void onAttach(Activity activity) {
1464 mCalled = true;
1465 }
George Mountc03da0e2014-08-22 17:04:02 -07001466
Chet Haase811ed1062010-08-06 10:38:15 -07001467 /**
1468 * Called when a fragment loads an animation.
1469 */
Chet Haasea18a86b2010-09-07 13:20:00 -07001470 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001471 return null;
1472 }
George Mountc03da0e2014-08-22 17:04:02 -07001473
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001474 /**
1475 * Called to do initial creation of a fragment. This is called after
Dianne Hackbornc8017682010-07-06 13:34:38 -07001476 * {@link #onAttach(Activity)} and before
Felipe Lemeb1444572015-11-03 17:39:07 -08001477 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, but is not called if the fragment
1478 * instance is retained across Activity re-creation (see {@link #setRetainInstance(boolean)}).
George Mountc03da0e2014-08-22 17:04:02 -07001479 *
Dianne Hackbornc8017682010-07-06 13:34:38 -07001480 * <p>Note that this can be called while the fragment's activity is
1481 * still in the process of being created. As such, you can not rely
1482 * on things like the activity's content view hierarchy being initialized
1483 * at this point. If you want to do work once the activity itself is
1484 * created, see {@link #onActivityCreated(Bundle)}.
George Mountc03da0e2014-08-22 17:04:02 -07001485 *
Dianne Hackbornb34cbed2016-08-09 13:18:21 -07001486 * <p>If your app's <code>targetSdkVersion</code> is {@link android.os.Build.VERSION_CODES#M}
1487 * or lower, child fragments being restored from the savedInstanceState are restored after
Dianne Hackborn5f1649e2016-08-09 18:00:31 -07001488 * <code>onCreate</code> returns. When targeting {@link android.os.Build.VERSION_CODES#N} or
Dianne Hackbornb34cbed2016-08-09 13:18:21 -07001489 * above and running on an N or newer platform version
Adam Powell592df7d2016-01-14 12:58:23 -08001490 * they are restored by <code>Fragment.onCreate</code>.</p>
1491 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001492 * @param savedInstanceState If the fragment is being re-created from
1493 * a previous saved state, this is the state.
1494 */
Adam Powella9bab982016-04-21 11:04:41 -07001495 @CallSuper
Scott Kennedy3c32b622015-02-22 16:58:58 -08001496 public void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001497 mCalled = true;
Adam Powell592df7d2016-01-14 12:58:23 -08001498 final Context context = getContext();
1499 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
1500 if (version >= Build.VERSION_CODES.N) {
Adam Powelleacacb52016-03-23 13:07:27 -07001501 restoreChildFragmentState(savedInstanceState, true);
Adam Powell467cc6f2016-05-11 13:45:33 -07001502 if (mChildFragmentManager != null
1503 && !mChildFragmentManager.isStateAtLeast(Fragment.CREATED)) {
1504 mChildFragmentManager.dispatchCreate();
1505 }
Adam Powelleacacb52016-03-23 13:07:27 -07001506 }
1507 }
1508
1509 void restoreChildFragmentState(@Nullable Bundle savedInstanceState, boolean provideNonConfig) {
1510 if (savedInstanceState != null) {
1511 Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG);
1512 if (p != null) {
1513 if (mChildFragmentManager == null) {
1514 instantiateChildFragmentManager();
Adam Powell592df7d2016-01-14 12:58:23 -08001515 }
Adam Powelleacacb52016-03-23 13:07:27 -07001516 mChildFragmentManager.restoreAllState(p, provideNonConfig ? mChildNonConfig : null);
1517 mChildNonConfig = null;
1518 mChildFragmentManager.dispatchCreate();
Adam Powell592df7d2016-01-14 12:58:23 -08001519 }
1520 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001521 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001522
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001523 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001524 * Called to have the fragment instantiate its user interface view.
1525 * This is optional, and non-graphical fragments can return null (which
1526 * is the default implementation). This will be called between
Dianne Hackbornc8017682010-07-06 13:34:38 -07001527 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
George Mountc03da0e2014-08-22 17:04:02 -07001528 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001529 * <p>If you return a View from here, you will later be called in
1530 * {@link #onDestroyView} when the view is being released.
George Mountc03da0e2014-08-22 17:04:02 -07001531 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001532 * @param inflater The LayoutInflater object that can be used to inflate
1533 * any views in the fragment,
1534 * @param container If non-null, this is the parent view that the fragment's
1535 * UI should be attached to. The fragment should not add the view itself,
1536 * but this can be used to generate the LayoutParams of the view.
1537 * @param savedInstanceState If non-null, this fragment is being re-constructed
1538 * from a previous saved state as given here.
George Mountc03da0e2014-08-22 17:04:02 -07001539 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001540 * @return Return the View for the fragment's UI, or null.
1541 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001542 @Nullable
1543 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001544 Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001545 return null;
1546 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001547
1548 /**
1549 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
1550 * has returned, but before any saved state has been restored in to the view.
1551 * This gives subclasses a chance to initialize themselves once
1552 * they know their view hierarchy has been completely created. The fragment's
1553 * view hierarchy is not however attached to its parent at this point.
1554 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1555 * @param savedInstanceState If non-null, this fragment is being re-constructed
1556 * from a previous saved state as given here.
1557 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001558 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001559 }
George Mountc03da0e2014-08-22 17:04:02 -07001560
Scott Main662cc7a42011-01-17 15:02:07 -08001561 /**
1562 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
1563 * if provided.
George Mountc03da0e2014-08-22 17:04:02 -07001564 *
Scott Main662cc7a42011-01-17 15:02:07 -08001565 * @return The fragment's root view, or null if it has no layout.
1566 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001567 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001568 public View getView() {
1569 return mView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001570 }
George Mountc03da0e2014-08-22 17:04:02 -07001571
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001572 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001573 * Called when the fragment's activity has been created and this
1574 * fragment's view hierarchy instantiated. It can be used to do final
1575 * initialization once these pieces are in place, such as retrieving
1576 * views or restoring state. It is also useful for fragments that use
1577 * {@link #setRetainInstance(boolean)} to retain their instance,
1578 * as this callback tells the fragment when it is fully associated with
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001579 * the new activity instance. This is called after {@link #onCreateView}
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001580 * and before {@link #onViewStateRestored(Bundle)}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001581 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001582 * @param savedInstanceState If the fragment is being re-created from
1583 * a previous saved state, this is the state.
1584 */
Adam Powella9bab982016-04-21 11:04:41 -07001585 @CallSuper
Scott Kennedy3c32b622015-02-22 16:58:58 -08001586 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001587 mCalled = true;
1588 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001589
1590 /**
1591 * Called when all saved state has been restored into the view hierarchy
1592 * of the fragment. This can be used to do initialization based on saved
1593 * state that you are letting the view hierarchy track itself, such as
1594 * whether check box widgets are currently checked. This is called
1595 * after {@link #onActivityCreated(Bundle)} and before
1596 * {@link #onStart()}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001597 *
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001598 * @param savedInstanceState If the fragment is being re-created from
1599 * a previous saved state, this is the state.
1600 */
Adam Powella9bab982016-04-21 11:04:41 -07001601 @CallSuper
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001602 public void onViewStateRestored(Bundle savedInstanceState) {
1603 mCalled = true;
1604 }
1605
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001606 /**
1607 * Called when the Fragment is visible to the user. This is generally
1608 * tied to {@link Activity#onStart() Activity.onStart} of the containing
1609 * Activity's lifecycle.
1610 */
Adam Powella9bab982016-04-21 11:04:41 -07001611 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001612 public void onStart() {
1613 mCalled = true;
George Mountc03da0e2014-08-22 17:04:02 -07001614
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001615 if (!mLoadersStarted) {
1616 mLoadersStarted = true;
1617 if (!mCheckedForLoaderManager) {
1618 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001619 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001620 }
1621 if (mLoaderManager != null) {
1622 mLoaderManager.doStart();
1623 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001624 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001625 }
George Mountc03da0e2014-08-22 17:04:02 -07001626
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001627 /**
1628 * Called when the fragment is visible to the user and actively running.
1629 * This is generally
1630 * tied to {@link Activity#onResume() Activity.onResume} of the containing
1631 * Activity's lifecycle.
1632 */
Adam Powella9bab982016-04-21 11:04:41 -07001633 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001634 public void onResume() {
1635 mCalled = true;
1636 }
George Mountc03da0e2014-08-22 17:04:02 -07001637
Dianne Hackborn72778202010-08-20 18:26:01 -07001638 /**
1639 * Called to ask the fragment to save its current dynamic state, so it
1640 * can later be reconstructed in a new instance of its process is
1641 * restarted. If a new instance of the fragment later needs to be
1642 * created, the data you place in the Bundle here will be available
1643 * in the Bundle given to {@link #onCreate(Bundle)},
1644 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
1645 * {@link #onActivityCreated(Bundle)}.
1646 *
1647 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
Daisuke Miyakawa2f761762010-09-12 16:53:17 -07001648 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
Dianne Hackborn72778202010-08-20 18:26:01 -07001649 * applies here as well. Note however: <em>this method may be called
1650 * at any time before {@link #onDestroy()}</em>. There are many situations
1651 * where a fragment may be mostly torn down (such as when placed on the
1652 * back stack with no UI showing), but its state will not be saved until
1653 * its owning activity actually needs to save its state.
1654 *
1655 * @param outState Bundle in which to place your saved state.
1656 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001657 public void onSaveInstanceState(Bundle outState) {
1658 }
George Mountc03da0e2014-08-22 17:04:02 -07001659
Wale Ogunwale7c796812016-01-29 21:13:50 -08001660 /**
1661 * Called when the Fragment's activity changes from fullscreen mode to multi-window mode and
Andrii Kulian933076d2016-03-29 17:04:42 -07001662 * visa-versa. This is generally tied to {@link Activity#onMultiWindowModeChanged} of the
1663 * containing Activity.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001664 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001665 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001666 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001667 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Wale Ogunwale7c796812016-01-29 21:13:50 -08001668 }
1669
1670 /**
1671 * Called by the system when the activity changes to and from picture-in-picture mode. This is
Andrii Kulian933076d2016-03-29 17:04:42 -07001672 * generally tied to {@link Activity#onPictureInPictureModeChanged} of the containing Activity.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001673 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001674 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001675 */
Andrii Kulian933076d2016-03-29 17:04:42 -07001676 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Wale Ogunwale7c796812016-01-29 21:13:50 -08001677 }
1678
Adam Powella9bab982016-04-21 11:04:41 -07001679 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001680 public void onConfigurationChanged(Configuration newConfig) {
1681 mCalled = true;
1682 }
George Mountc03da0e2014-08-22 17:04:02 -07001683
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001684 /**
1685 * Called when the Fragment is no longer resumed. This is generally
1686 * tied to {@link Activity#onPause() Activity.onPause} of the containing
1687 * Activity's lifecycle.
1688 */
Adam Powella9bab982016-04-21 11:04:41 -07001689 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001690 public void onPause() {
1691 mCalled = true;
1692 }
George Mountc03da0e2014-08-22 17:04:02 -07001693
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001694 /**
1695 * Called when the Fragment is no longer started. This is generally
1696 * tied to {@link Activity#onStop() Activity.onStop} of the containing
1697 * Activity's lifecycle.
1698 */
Adam Powella9bab982016-04-21 11:04:41 -07001699 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001700 public void onStop() {
1701 mCalled = true;
1702 }
George Mountc03da0e2014-08-22 17:04:02 -07001703
Adam Powella9bab982016-04-21 11:04:41 -07001704 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001705 public void onLowMemory() {
1706 mCalled = true;
1707 }
George Mountc03da0e2014-08-22 17:04:02 -07001708
Adam Powella9bab982016-04-21 11:04:41 -07001709 @CallSuper
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001710 public void onTrimMemory(int level) {
1711 mCalled = true;
1712 }
1713
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001714 /**
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001715 * Called when the view previously created by {@link #onCreateView} has
1716 * been detached from the fragment. The next time the fragment needs
1717 * to be displayed, a new view will be created. This is called
Dianne Hackborndef15372010-08-15 12:43:52 -07001718 * after {@link #onStop()} and before {@link #onDestroy()}. It is called
1719 * <em>regardless</em> of whether {@link #onCreateView} returned a
1720 * non-null view. Internally it is called after the view's state has
1721 * been saved but before it has been removed from its parent.
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001722 */
Adam Powella9bab982016-04-21 11:04:41 -07001723 @CallSuper
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001724 public void onDestroyView() {
1725 mCalled = true;
1726 }
George Mountc03da0e2014-08-22 17:04:02 -07001727
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001728 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001729 * Called when the fragment is no longer in use. This is called
1730 * after {@link #onStop()} and before {@link #onDetach()}.
1731 */
Adam Powella9bab982016-04-21 11:04:41 -07001732 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001733 public void onDestroy() {
1734 mCalled = true;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001735 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager
1736 // + " mLoaderManager=" + mLoaderManager);
1737 if (!mCheckedForLoaderManager) {
1738 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001739 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001740 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001741 if (mLoaderManager != null) {
1742 mLoaderManager.doDestroy();
1743 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001744 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001745
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001746 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001747 * Called by the fragment manager once this fragment has been removed,
1748 * so that we don't have any left-over state if the application decides
1749 * to re-use the instance. This only clears state that the framework
1750 * internally manages, not things the application sets.
1751 */
1752 void initState() {
1753 mIndex = -1;
1754 mWho = null;
1755 mAdded = false;
1756 mRemoving = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001757 mFromLayout = false;
1758 mInLayout = false;
1759 mRestored = false;
1760 mBackStackNesting = 0;
1761 mFragmentManager = null;
Tim Kilbourn70717862014-02-28 01:00:06 -08001762 mChildFragmentManager = null;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001763 mHost = null;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001764 mFragmentId = 0;
1765 mContainerId = 0;
1766 mTag = null;
1767 mHidden = false;
1768 mDetached = false;
1769 mRetaining = false;
1770 mLoaderManager = null;
1771 mLoadersStarted = false;
1772 mCheckedForLoaderManager = false;
1773 }
1774
1775 /**
Felipe Lemeb1444572015-11-03 17:39:07 -08001776 * Called when the fragment is no longer attached to its activity. This is called after
1777 * {@link #onDestroy()}, except in the cases where the fragment instance is retained across
1778 * Activity re-creation (see {@link #setRetainInstance(boolean)}), in which case it is called
1779 * after {@link #onStop()}.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001780 */
Adam Powella9bab982016-04-21 11:04:41 -07001781 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001782 public void onDetach() {
1783 mCalled = true;
1784 }
George Mountc03da0e2014-08-22 17:04:02 -07001785
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001786 /**
1787 * Initialize the contents of the Activity's standard options menu. You
1788 * should place your menu items in to <var>menu</var>. For this method
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001789 * to be called, you must have first called {@link #setHasOptionsMenu}. See
1790 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
1791 * for more information.
George Mountc03da0e2014-08-22 17:04:02 -07001792 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001793 * @param menu The options menu in which you place your items.
George Mountc03da0e2014-08-22 17:04:02 -07001794 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001795 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001796 * @see #onPrepareOptionsMenu
1797 * @see #onOptionsItemSelected
1798 */
1799 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1800 }
1801
1802 /**
1803 * Prepare the Screen's standard options menu to be displayed. This is
1804 * called right before the menu is shown, every time it is shown. You can
1805 * use this method to efficiently enable/disable items or otherwise
1806 * dynamically modify the contents. See
1807 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu}
1808 * for more information.
George Mountc03da0e2014-08-22 17:04:02 -07001809 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001810 * @param menu The options menu as last shown or first initialized by
1811 * onCreateOptionsMenu().
George Mountc03da0e2014-08-22 17:04:02 -07001812 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001813 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001814 * @see #onCreateOptionsMenu
1815 */
1816 public void onPrepareOptionsMenu(Menu menu) {
1817 }
1818
1819 /**
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001820 * Called when this fragment's option menu items are no longer being
1821 * included in the overall options menu. Receiving this call means that
1822 * the menu needed to be rebuilt, but this fragment's items were not
1823 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
1824 * was not called).
1825 */
1826 public void onDestroyOptionsMenu() {
1827 }
George Mountc03da0e2014-08-22 17:04:02 -07001828
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001829 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001830 * This hook is called whenever an item in your options menu is selected.
1831 * The default implementation simply returns false to have the normal
1832 * processing happen (calling the item's Runnable or sending a message to
1833 * its Handler as appropriate). You can use this method for any items
1834 * for which you would like to do processing without those other
1835 * facilities.
George Mountc03da0e2014-08-22 17:04:02 -07001836 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001837 * <p>Derived classes should call through to the base class for it to
1838 * perform the default menu handling.
George Mountc03da0e2014-08-22 17:04:02 -07001839 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001840 * @param item The menu item that was selected.
George Mountc03da0e2014-08-22 17:04:02 -07001841 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001842 * @return boolean Return false to allow normal menu processing to
1843 * proceed, true to consume it here.
George Mountc03da0e2014-08-22 17:04:02 -07001844 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001845 * @see #onCreateOptionsMenu
1846 */
1847 public boolean onOptionsItemSelected(MenuItem item) {
1848 return false;
1849 }
1850
1851 /**
1852 * This hook is called whenever the options menu is being closed (either by the user canceling
1853 * the menu with the back/menu button, or when an item is selected).
George Mountc03da0e2014-08-22 17:04:02 -07001854 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001855 * @param menu The options menu as last shown or first initialized by
1856 * onCreateOptionsMenu().
1857 */
1858 public void onOptionsMenuClosed(Menu menu) {
1859 }
George Mountc03da0e2014-08-22 17:04:02 -07001860
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001861 /**
1862 * Called when a context menu for the {@code view} is about to be shown.
1863 * Unlike {@link #onCreateOptionsMenu}, this will be called every
1864 * time the context menu is about to be shown and should be populated for
1865 * the view (or item inside the view for {@link AdapterView} subclasses,
1866 * this can be found in the {@code menuInfo})).
1867 * <p>
1868 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
1869 * item has been selected.
1870 * <p>
1871 * The default implementation calls up to
1872 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
1873 * you can not call this implementation if you don't want that behavior.
1874 * <p>
1875 * It is not safe to hold onto the context menu after this method returns.
1876 * {@inheritDoc}
1877 */
1878 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1879 getActivity().onCreateContextMenu(menu, v, menuInfo);
1880 }
1881
1882 /**
1883 * Registers a context menu to be shown for the given view (multiple views
1884 * can show the context menu). This method will set the
1885 * {@link OnCreateContextMenuListener} on the view to this fragment, so
1886 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
1887 * called when it is time to show the context menu.
George Mountc03da0e2014-08-22 17:04:02 -07001888 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001889 * @see #unregisterForContextMenu(View)
1890 * @param view The view that should show a context menu.
1891 */
1892 public void registerForContextMenu(View view) {
1893 view.setOnCreateContextMenuListener(this);
1894 }
George Mountc03da0e2014-08-22 17:04:02 -07001895
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001896 /**
1897 * Prevents a context menu to be shown for the given view. This method will
1898 * remove the {@link OnCreateContextMenuListener} on the view.
George Mountc03da0e2014-08-22 17:04:02 -07001899 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001900 * @see #registerForContextMenu(View)
1901 * @param view The view that should stop showing a context menu.
1902 */
1903 public void unregisterForContextMenu(View view) {
1904 view.setOnCreateContextMenuListener(null);
1905 }
George Mountc03da0e2014-08-22 17:04:02 -07001906
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001907 /**
1908 * This hook is called whenever an item in a context menu is selected. The
1909 * default implementation simply returns false to have the normal processing
1910 * happen (calling the item's Runnable or sending a message to its Handler
1911 * as appropriate). You can use this method for any items for which you
1912 * would like to do processing without those other facilities.
1913 * <p>
1914 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
1915 * View that added this menu item.
1916 * <p>
1917 * Derived classes should call through to the base class for it to perform
1918 * the default menu handling.
George Mountc03da0e2014-08-22 17:04:02 -07001919 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001920 * @param item The context menu item that was selected.
1921 * @return boolean Return false to allow normal context menu processing to
1922 * proceed, true to consume it here.
1923 */
1924 public boolean onContextItemSelected(MenuItem item) {
1925 return false;
1926 }
George Mountc03da0e2014-08-22 17:04:02 -07001927
1928 /**
George Mount65580562014-08-29 08:15:48 -07001929 * When custom transitions are used with Fragments, the enter transition callback
George Mountc03da0e2014-08-22 17:04:02 -07001930 * is called when this Fragment is attached or detached when not popping the back stack.
1931 *
George Mount65580562014-08-29 08:15:48 -07001932 * @param callback Used to manipulate the shared element transitions on this Fragment
George Mountc03da0e2014-08-22 17:04:02 -07001933 * when added not as a pop from the back stack.
1934 */
George Mount85ea4982014-09-12 14:16:18 -07001935 public void setEnterSharedElementCallback(SharedElementCallback callback) {
George Mount65580562014-08-29 08:15:48 -07001936 if (callback == null) {
1937 callback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -07001938 }
George Mount65580562014-08-29 08:15:48 -07001939 mEnterTransitionCallback = callback;
George Mountc03da0e2014-08-22 17:04:02 -07001940 }
1941
1942 /**
George Mount85ea4982014-09-12 14:16:18 -07001943 * @hide
1944 */
1945 public void setEnterSharedElementTransitionCallback(SharedElementCallback callback) {
1946 setEnterSharedElementCallback(callback);
1947 }
1948
1949 /**
George Mount65580562014-08-29 08:15:48 -07001950 * When custom transitions are used with Fragments, the exit transition callback
George Mountc03da0e2014-08-22 17:04:02 -07001951 * is called when this Fragment is attached or detached when popping the back stack.
1952 *
George Mount65580562014-08-29 08:15:48 -07001953 * @param callback Used to manipulate the shared element transitions on this Fragment
George Mountc03da0e2014-08-22 17:04:02 -07001954 * when added as a pop from the back stack.
1955 */
George Mount85ea4982014-09-12 14:16:18 -07001956 public void setExitSharedElementCallback(SharedElementCallback callback) {
George Mount65580562014-08-29 08:15:48 -07001957 if (callback == null) {
1958 callback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -07001959 }
George Mount65580562014-08-29 08:15:48 -07001960 mExitTransitionCallback = callback;
George Mountc03da0e2014-08-22 17:04:02 -07001961 }
1962
1963 /**
George Mount85ea4982014-09-12 14:16:18 -07001964 * @hide
1965 */
1966 public void setExitSharedElementTransitionCallback(SharedElementCallback callback) {
1967 setExitSharedElementCallback(callback);
1968 }
1969
1970 /**
George Mountc03da0e2014-08-22 17:04:02 -07001971 * Sets the Transition that will be used to move Views into the initial scene. The entering
1972 * Views will be those that are regular Views or ViewGroups that have
1973 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1974 * {@link android.transition.Visibility} as entering is governed by changing visibility from
1975 * {@link View#INVISIBLE} to {@link View#VISIBLE}. If <code>transition</code> is null,
1976 * entering Views will remain unaffected.
1977 *
1978 * @param transition The Transition to use to move Views into the initial Scene.
1979 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition
1980 */
1981 public void setEnterTransition(Transition transition) {
1982 mEnterTransition = transition;
1983 }
1984
1985 /**
1986 * Returns the Transition that will be used to move Views into the initial scene. The entering
1987 * Views will be those that are regular Views or ViewGroups that have
1988 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1989 * {@link android.transition.Visibility} as entering is governed by changing visibility from
1990 * {@link View#INVISIBLE} to {@link View#VISIBLE}.
1991 *
1992 * @return the Transition to use to move Views into the initial Scene.
1993 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition
1994 */
1995 public Transition getEnterTransition() {
1996 return mEnterTransition;
1997 }
1998
1999 /**
2000 * Sets the Transition that will be used to move Views out of the scene when the Fragment is
2001 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting
2002 * Views will be those that are regular Views or ViewGroups that have
2003 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2004 * {@link android.transition.Visibility} as entering is governed by changing visibility from
2005 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null,
2006 * entering Views will remain unaffected. If nothing is set, the default will be to
2007 * use the same value as set in {@link #setEnterTransition(android.transition.Transition)}.
2008 *
2009 * @param transition The Transition to use to move Views out of the Scene when the Fragment
2010 * is preparing to close.
2011 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2012 */
2013 public void setReturnTransition(Transition transition) {
2014 mReturnTransition = transition;
2015 }
2016
2017 /**
2018 * Returns the Transition that will be used to move Views out of the scene when the Fragment is
2019 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting
2020 * Views will be those that are regular Views or ViewGroups that have
2021 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2022 * {@link android.transition.Visibility} as entering is governed by changing visibility from
2023 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null,
2024 * entering Views will remain unaffected.
2025 *
2026 * @return the Transition to use to move Views out of the Scene when the Fragment
2027 * is preparing to close.
2028 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2029 */
2030 public Transition getReturnTransition() {
2031 return mReturnTransition == USE_DEFAULT_TRANSITION ? getEnterTransition()
2032 : mReturnTransition;
2033 }
2034
2035 /**
2036 * Sets the Transition that will be used to move Views out of the scene when the
2037 * fragment is removed, hidden, or detached when not popping the back stack.
2038 * The exiting Views will be those that are regular Views or ViewGroups that
2039 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2040 * {@link android.transition.Visibility} as exiting is governed by changing visibility
2041 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will
2042 * remain unaffected.
2043 *
2044 * @param transition The Transition to use to move Views out of the Scene when the Fragment
2045 * is being closed not due to popping the back stack.
2046 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2047 */
2048 public void setExitTransition(Transition transition) {
2049 mExitTransition = transition;
2050 }
2051
2052 /**
2053 * Returns the Transition that will be used to move Views out of the scene when the
2054 * fragment is removed, hidden, or detached when not popping the back stack.
2055 * The exiting Views will be those that are regular Views or ViewGroups that
2056 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2057 * {@link android.transition.Visibility} as exiting is governed by changing visibility
2058 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will
2059 * remain unaffected.
2060 *
2061 * @return the Transition to use to move Views out of the Scene when the Fragment
2062 * is being closed not due to popping the back stack.
2063 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2064 */
2065 public Transition getExitTransition() {
2066 return mExitTransition;
2067 }
2068
2069 /**
2070 * Sets the Transition that will be used to move Views in to the scene when returning due
2071 * to popping a back stack. The entering Views will be those that are regular Views
2072 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions
2073 * will extend {@link android.transition.Visibility} as exiting is governed by changing
2074 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null,
2075 * the views will remain unaffected. If nothing is set, the default will be to use the same
2076 * transition as {@link #setExitTransition(android.transition.Transition)}.
2077 *
2078 * @param transition The Transition to use to move Views into the scene when reentering from a
2079 * previously-started Activity.
2080 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition
2081 */
2082 public void setReenterTransition(Transition transition) {
2083 mReenterTransition = transition;
2084 }
2085
2086 /**
2087 * Returns the Transition that will be used to move Views in to the scene when returning due
2088 * to popping a back stack. The entering Views will be those that are regular Views
2089 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions
2090 * will extend {@link android.transition.Visibility} as exiting is governed by changing
2091 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null,
2092 * the views will remain unaffected. If nothing is set, the default will be to use the same
2093 * transition as {@link #setExitTransition(android.transition.Transition)}.
2094 *
2095 * @return the Transition to use to move Views into the scene when reentering from a
2096 * previously-started Activity.
2097 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition
2098 */
2099 public Transition getReenterTransition() {
2100 return mReenterTransition == USE_DEFAULT_TRANSITION ? getExitTransition()
2101 : mReenterTransition;
2102 }
2103
2104 /**
2105 * Sets the Transition that will be used for shared elements transferred into the content
2106 * Scene. Typical Transitions will affect size and location, such as
2107 * {@link android.transition.ChangeBounds}. A null
2108 * value will cause transferred shared elements to blink to the final position.
2109 *
2110 * @param transition The Transition to use for shared elements transferred into the content
2111 * Scene.
2112 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition
2113 */
2114 public void setSharedElementEnterTransition(Transition transition) {
2115 mSharedElementEnterTransition = transition;
2116 }
2117
2118 /**
2119 * Returns the Transition that will be used for shared elements transferred into the content
2120 * Scene. Typical Transitions will affect size and location, such as
2121 * {@link android.transition.ChangeBounds}. A null
2122 * value will cause transferred shared elements to blink to the final position.
2123 *
2124 * @return The Transition to use for shared elements transferred into the content
2125 * Scene.
2126 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition
2127 */
2128 public Transition getSharedElementEnterTransition() {
2129 return mSharedElementEnterTransition;
2130 }
2131
2132 /**
2133 * Sets the Transition that will be used for shared elements transferred back during a
2134 * pop of the back stack. This Transition acts in the leaving Fragment.
2135 * Typical Transitions will affect size and location, such as
2136 * {@link android.transition.ChangeBounds}. A null
2137 * value will cause transferred shared elements to blink to the final position.
2138 * If no value is set, the default will be to use the same value as
2139 * {@link #setSharedElementEnterTransition(android.transition.Transition)}.
2140 *
2141 * @param transition The Transition to use for shared elements transferred out of the content
2142 * Scene.
2143 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition
2144 */
2145 public void setSharedElementReturnTransition(Transition transition) {
2146 mSharedElementReturnTransition = transition;
2147 }
2148
2149 /**
2150 * Return the Transition that will be used for shared elements transferred back during a
2151 * pop of the back stack. This Transition acts in the leaving Fragment.
2152 * Typical Transitions will affect size and location, such as
2153 * {@link android.transition.ChangeBounds}. A null
2154 * value will cause transferred shared elements to blink to the final position.
2155 * If no value is set, the default will be to use the same value as
2156 * {@link #setSharedElementEnterTransition(android.transition.Transition)}.
2157 *
2158 * @return The Transition to use for shared elements transferred out of the content
2159 * Scene.
2160 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition
2161 */
2162 public Transition getSharedElementReturnTransition() {
2163 return mSharedElementReturnTransition == USE_DEFAULT_TRANSITION ?
2164 getSharedElementEnterTransition() : mSharedElementReturnTransition;
2165 }
2166
2167 /**
2168 * Sets whether the the exit transition and enter transition overlap or not.
2169 * When true, the enter transition will start as soon as possible. When false, the
2170 * enter transition will wait until the exit transition completes before starting.
2171 *
2172 * @param allow true to start the enter transition when possible or false to
2173 * wait until the exiting transition completes.
2174 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap
2175 */
2176 public void setAllowEnterTransitionOverlap(boolean allow) {
2177 mAllowEnterTransitionOverlap = allow;
2178 }
2179
2180 /**
2181 * Returns whether the the exit transition and enter transition overlap or not.
2182 * When true, the enter transition will start as soon as possible. When false, the
2183 * enter transition will wait until the exit transition completes before starting.
2184 *
2185 * @return true when the enter transition should start as soon as possible or false to
2186 * when it should wait until the exiting transition completes.
2187 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap
2188 */
2189 public boolean getAllowEnterTransitionOverlap() {
2190 return (mAllowEnterTransitionOverlap == null) ? true : mAllowEnterTransitionOverlap;
2191 }
2192
2193 /**
2194 * Sets whether the the return transition and reenter transition overlap or not.
2195 * When true, the reenter transition will start as soon as possible. When false, the
2196 * reenter transition will wait until the return transition completes before starting.
2197 *
2198 * @param allow true to start the reenter transition when possible or false to wait until the
2199 * return transition completes.
2200 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap
2201 */
2202 public void setAllowReturnTransitionOverlap(boolean allow) {
2203 mAllowReturnTransitionOverlap = allow;
2204 }
2205
2206 /**
2207 * Returns whether the the return transition and reenter transition overlap or not.
2208 * When true, the reenter transition will start as soon as possible. When false, the
2209 * reenter transition will wait until the return transition completes before starting.
2210 *
2211 * @return true to start the reenter transition when possible or false to wait until the
2212 * return transition completes.
2213 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap
2214 */
2215 public boolean getAllowReturnTransitionOverlap() {
2216 return (mAllowReturnTransitionOverlap == null) ? true : mAllowReturnTransitionOverlap;
2217 }
2218
Dianne Hackborn625ac272010-09-17 18:29:22 -07002219 /**
2220 * Print the Fragments's state into the given stream.
2221 *
2222 * @param prefix Text to print at the front of each line.
2223 * @param fd The raw file descriptor that the dump is being sent to.
2224 * @param writer The PrintWriter to which you should dump your state. This will be
2225 * closed for you after you return.
2226 * @param args additional arguments to the dump request.
2227 */
2228 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08002229 writer.print(prefix); writer.print("mFragmentId=#");
George Mountc03da0e2014-08-22 17:04:02 -07002230 writer.print(Integer.toHexString(mFragmentId));
2231 writer.print(" mContainerId=#");
2232 writer.print(Integer.toHexString(mContainerId));
2233 writer.print(" mTag="); writer.println(mTag);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002234 writer.print(prefix); writer.print("mState="); writer.print(mState);
George Mountc03da0e2014-08-22 17:04:02 -07002235 writer.print(" mIndex="); writer.print(mIndex);
2236 writer.print(" mWho="); writer.print(mWho);
2237 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002238 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded);
George Mountc03da0e2014-08-22 17:04:02 -07002239 writer.print(" mRemoving="); writer.print(mRemoving);
George Mountc03da0e2014-08-22 17:04:02 -07002240 writer.print(" mFromLayout="); writer.print(mFromLayout);
2241 writer.print(" mInLayout="); writer.println(mInLayout);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002242 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
George Mountc03da0e2014-08-22 17:04:02 -07002243 writer.print(" mDetached="); writer.print(mDetached);
2244 writer.print(" mMenuVisible="); writer.print(mMenuVisible);
2245 writer.print(" mHasMenu="); writer.println(mHasMenu);
Dianne Hackborn6c285972011-08-29 16:53:49 -07002246 writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance);
George Mountc03da0e2014-08-22 17:04:02 -07002247 writer.print(" mRetaining="); writer.print(mRetaining);
2248 writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002249 if (mFragmentManager != null) {
2250 writer.print(prefix); writer.print("mFragmentManager=");
George Mountc03da0e2014-08-22 17:04:02 -07002251 writer.println(mFragmentManager);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002252 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002253 if (mHost != null) {
2254 writer.print(prefix); writer.print("mHost=");
2255 writer.println(mHost);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002256 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002257 if (mParentFragment != null) {
2258 writer.print(prefix); writer.print("mParentFragment=");
George Mountc03da0e2014-08-22 17:04:02 -07002259 writer.println(mParentFragment);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002260 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002261 if (mArguments != null) {
2262 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments);
2263 }
2264 if (mSavedFragmentState != null) {
2265 writer.print(prefix); writer.print("mSavedFragmentState=");
George Mountc03da0e2014-08-22 17:04:02 -07002266 writer.println(mSavedFragmentState);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002267 }
2268 if (mSavedViewState != null) {
2269 writer.print(prefix); writer.print("mSavedViewState=");
George Mountc03da0e2014-08-22 17:04:02 -07002270 writer.println(mSavedViewState);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002271 }
2272 if (mTarget != null) {
2273 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget);
George Mountc03da0e2014-08-22 17:04:02 -07002274 writer.print(" mTargetRequestCode=");
2275 writer.println(mTargetRequestCode);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002276 }
2277 if (mNextAnim != 0) {
2278 writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim);
2279 }
2280 if (mContainer != null) {
2281 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer);
2282 }
2283 if (mView != null) {
2284 writer.print(prefix); writer.print("mView="); writer.println(mView);
2285 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08002286 if (mAnimatingAway != null) {
2287 writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway);
2288 writer.print(prefix); writer.print("mStateAfterAnimating=");
George Mountc03da0e2014-08-22 17:04:02 -07002289 writer.println(mStateAfterAnimating);
Dianne Hackbornd173fa32010-12-23 13:58:22 -08002290 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002291 if (mLoaderManager != null) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08002292 writer.print(prefix); writer.println("Loader Manager:");
2293 mLoaderManager.dump(prefix + " ", fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002294 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002295 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002296 writer.print(prefix); writer.println("Child " + mChildFragmentManager + ":");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002297 mChildFragmentManager.dump(prefix + " ", fd, writer, args);
2298 }
2299 }
2300
2301 Fragment findFragmentByWho(String who) {
2302 if (who.equals(mWho)) {
2303 return this;
2304 }
2305 if (mChildFragmentManager != null) {
2306 return mChildFragmentManager.findFragmentByWho(who);
2307 }
2308 return null;
2309 }
2310
2311 void instantiateChildFragmentManager() {
2312 mChildFragmentManager = new FragmentManagerImpl();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002313 mChildFragmentManager.attachController(mHost, new FragmentContainer() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002314 @Override
Scott Kennedyc0519552015-02-11 15:33:10 -08002315 @Nullable
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002316 public View onFindViewById(int id) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002317 if (mView == null) {
2318 throw new IllegalStateException("Fragment does not have a view");
2319 }
2320 return mView.findViewById(id);
2321 }
George Mount0b26e4d2014-09-17 16:36:42 -07002322
2323 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002324 public boolean onHasView() {
George Mount0b26e4d2014-09-17 16:36:42 -07002325 return (mView != null);
2326 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002327 }, this);
2328 }
2329
2330 void performCreate(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002331 if (mChildFragmentManager != null) {
2332 mChildFragmentManager.noteStateNotSaved();
2333 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002334 mState = CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002335 mCalled = false;
2336 onCreate(savedInstanceState);
2337 if (!mCalled) {
2338 throw new SuperNotCalledException("Fragment " + this
2339 + " did not call through to super.onCreate()");
2340 }
Adam Powell592df7d2016-01-14 12:58:23 -08002341 final Context context = getContext();
2342 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
2343 if (version < Build.VERSION_CODES.N) {
Adam Powelleacacb52016-03-23 13:07:27 -07002344 restoreChildFragmentState(savedInstanceState, false);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002345 }
2346 }
2347
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002348 View performCreateView(LayoutInflater inflater, ViewGroup container,
2349 Bundle savedInstanceState) {
2350 if (mChildFragmentManager != null) {
2351 mChildFragmentManager.noteStateNotSaved();
2352 }
2353 return onCreateView(inflater, container, savedInstanceState);
2354 }
2355
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002356 void performActivityCreated(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002357 if (mChildFragmentManager != null) {
2358 mChildFragmentManager.noteStateNotSaved();
2359 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002360 mState = ACTIVITY_CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002361 mCalled = false;
2362 onActivityCreated(savedInstanceState);
2363 if (!mCalled) {
2364 throw new SuperNotCalledException("Fragment " + this
2365 + " did not call through to super.onActivityCreated()");
2366 }
2367 if (mChildFragmentManager != null) {
2368 mChildFragmentManager.dispatchActivityCreated();
2369 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002370 }
2371
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002372 void performStart() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002373 if (mChildFragmentManager != null) {
2374 mChildFragmentManager.noteStateNotSaved();
2375 mChildFragmentManager.execPendingActions();
2376 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002377 mState = STARTED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002378 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002379 onStart();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002380 if (!mCalled) {
2381 throw new SuperNotCalledException("Fragment " + this
2382 + " did not call through to super.onStart()");
2383 }
2384 if (mChildFragmentManager != null) {
2385 mChildFragmentManager.dispatchStart();
2386 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002387 if (mLoaderManager != null) {
2388 mLoaderManager.doReportStart();
2389 }
2390 }
2391
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002392 void performResume() {
2393 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002394 mChildFragmentManager.noteStateNotSaved();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002395 mChildFragmentManager.execPendingActions();
2396 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002397 mState = RESUMED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002398 mCalled = false;
2399 onResume();
2400 if (!mCalled) {
2401 throw new SuperNotCalledException("Fragment " + this
2402 + " did not call through to super.onResume()");
2403 }
2404 if (mChildFragmentManager != null) {
2405 mChildFragmentManager.dispatchResume();
2406 mChildFragmentManager.execPendingActions();
2407 }
2408 }
2409
Andrii Kulian933076d2016-03-29 17:04:42 -07002410 void performMultiWindowModeChanged(boolean isInMultiWindowMode) {
2411 onMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002412 if (mChildFragmentManager != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002413 mChildFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002414 }
2415 }
2416
Andrii Kulian933076d2016-03-29 17:04:42 -07002417 void performPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
2418 onPictureInPictureModeChanged(isInPictureInPictureMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002419 if (mChildFragmentManager != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002420 mChildFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002421 }
2422 }
2423
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002424 void performConfigurationChanged(Configuration newConfig) {
2425 onConfigurationChanged(newConfig);
2426 if (mChildFragmentManager != null) {
2427 mChildFragmentManager.dispatchConfigurationChanged(newConfig);
2428 }
2429 }
2430
2431 void performLowMemory() {
2432 onLowMemory();
2433 if (mChildFragmentManager != null) {
2434 mChildFragmentManager.dispatchLowMemory();
2435 }
2436 }
2437
2438 void performTrimMemory(int level) {
2439 onTrimMemory(level);
2440 if (mChildFragmentManager != null) {
2441 mChildFragmentManager.dispatchTrimMemory(level);
2442 }
2443 }
2444
2445 boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2446 boolean show = false;
2447 if (!mHidden) {
2448 if (mHasMenu && mMenuVisible) {
2449 show = true;
2450 onCreateOptionsMenu(menu, inflater);
2451 }
2452 if (mChildFragmentManager != null) {
2453 show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater);
2454 }
2455 }
2456 return show;
2457 }
2458
2459 boolean performPrepareOptionsMenu(Menu menu) {
2460 boolean show = false;
2461 if (!mHidden) {
2462 if (mHasMenu && mMenuVisible) {
2463 show = true;
2464 onPrepareOptionsMenu(menu);
2465 }
2466 if (mChildFragmentManager != null) {
2467 show |= mChildFragmentManager.dispatchPrepareOptionsMenu(menu);
2468 }
2469 }
2470 return show;
2471 }
2472
2473 boolean performOptionsItemSelected(MenuItem item) {
2474 if (!mHidden) {
2475 if (mHasMenu && mMenuVisible) {
2476 if (onOptionsItemSelected(item)) {
2477 return true;
2478 }
2479 }
2480 if (mChildFragmentManager != null) {
2481 if (mChildFragmentManager.dispatchOptionsItemSelected(item)) {
2482 return true;
2483 }
2484 }
2485 }
2486 return false;
2487 }
2488
2489 boolean performContextItemSelected(MenuItem item) {
2490 if (!mHidden) {
2491 if (onContextItemSelected(item)) {
2492 return true;
2493 }
2494 if (mChildFragmentManager != null) {
2495 if (mChildFragmentManager.dispatchContextItemSelected(item)) {
2496 return true;
2497 }
2498 }
2499 }
2500 return false;
2501 }
2502
2503 void performOptionsMenuClosed(Menu menu) {
2504 if (!mHidden) {
2505 if (mHasMenu && mMenuVisible) {
2506 onOptionsMenuClosed(menu);
2507 }
2508 if (mChildFragmentManager != null) {
2509 mChildFragmentManager.dispatchOptionsMenuClosed(menu);
2510 }
2511 }
2512 }
2513
2514 void performSaveInstanceState(Bundle outState) {
2515 onSaveInstanceState(outState);
2516 if (mChildFragmentManager != null) {
2517 Parcelable p = mChildFragmentManager.saveAllState();
2518 if (p != null) {
2519 outState.putParcelable(Activity.FRAGMENTS_TAG, p);
2520 }
2521 }
2522 }
2523
2524 void performPause() {
2525 if (mChildFragmentManager != null) {
2526 mChildFragmentManager.dispatchPause();
2527 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002528 mState = STARTED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002529 mCalled = false;
2530 onPause();
2531 if (!mCalled) {
2532 throw new SuperNotCalledException("Fragment " + this
2533 + " did not call through to super.onPause()");
2534 }
2535 }
2536
Dianne Hackborn2707d602010-07-09 18:01:20 -07002537 void performStop() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002538 if (mChildFragmentManager != null) {
2539 mChildFragmentManager.dispatchStop();
2540 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002541 mState = STOPPED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002542 mCalled = false;
Dianne Hackborn2707d602010-07-09 18:01:20 -07002543 onStop();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002544 if (!mCalled) {
2545 throw new SuperNotCalledException("Fragment " + this
2546 + " did not call through to super.onStop()");
2547 }
George Mountc03da0e2014-08-22 17:04:02 -07002548
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002549 if (mLoadersStarted) {
2550 mLoadersStarted = false;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07002551 if (!mCheckedForLoaderManager) {
2552 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002553 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07002554 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07002555 if (mLoaderManager != null) {
Todd Kennedy2ee19c7f2015-10-19 15:31:51 -07002556 if (mHost.getRetainLoaders()) {
Dianne Hackborn2707d602010-07-09 18:01:20 -07002557 mLoaderManager.doRetain();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002558 } else {
2559 mLoaderManager.doStop();
Dianne Hackborn2707d602010-07-09 18:01:20 -07002560 }
2561 }
2562 }
2563 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002564
2565 void performDestroyView() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002566 if (mChildFragmentManager != null) {
2567 mChildFragmentManager.dispatchDestroyView();
2568 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002569 mState = CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002570 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002571 onDestroyView();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002572 if (!mCalled) {
2573 throw new SuperNotCalledException("Fragment " + this
2574 + " did not call through to super.onDestroyView()");
2575 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002576 if (mLoaderManager != null) {
2577 mLoaderManager.doReportNextStart();
2578 }
2579 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002580
2581 void performDestroy() {
2582 if (mChildFragmentManager != null) {
2583 mChildFragmentManager.dispatchDestroy();
2584 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002585 mState = INITIALIZING;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002586 mCalled = false;
2587 onDestroy();
2588 if (!mCalled) {
2589 throw new SuperNotCalledException("Fragment " + this
2590 + " did not call through to super.onDestroy()");
2591 }
George Mount4c656b32016-02-17 12:59:49 -08002592 mChildFragmentManager = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002593 }
George Mountc03da0e2014-08-22 17:04:02 -07002594
Adam Powella9bab982016-04-21 11:04:41 -07002595 void performDetach() {
2596 mCalled = false;
2597 onDetach();
2598 if (!mCalled) {
2599 throw new SuperNotCalledException("Fragment " + this
2600 + " did not call through to super.onDetach()");
2601 }
2602
2603 // Destroy the child FragmentManager if we still have it here.
2604 // We won't unless we're retaining our instance and if we do,
2605 // our child FragmentManager instance state will have already been saved.
2606 if (mChildFragmentManager != null) {
2607 if (!mRetaining) {
2608 throw new IllegalStateException("Child FragmentManager of " + this + " was not "
2609 + " destroyed and this fragment is not retaining instance");
2610 }
2611 mChildFragmentManager.dispatchDestroy();
2612 mChildFragmentManager = null;
2613 }
2614 }
2615
George Mountc03da0e2014-08-22 17:04:02 -07002616 private static Transition loadTransition(Context context, TypedArray typedArray,
2617 Transition currentValue, Transition defaultValue, int id) {
2618 if (currentValue != defaultValue) {
2619 return currentValue;
2620 }
2621 int transitionId = typedArray.getResourceId(id, 0);
2622 Transition transition = defaultValue;
2623 if (transitionId != 0 && transitionId != com.android.internal.R.transition.no_transition) {
2624 TransitionInflater inflater = TransitionInflater.from(context);
2625 transition = inflater.inflateTransition(transitionId);
2626 if (transition instanceof TransitionSet &&
2627 ((TransitionSet)transition).getTransitionCount() == 0) {
2628 transition = null;
2629 }
2630 }
2631 return transition;
2632 }
2633
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002634}