blob: f7a4557f87601f286578d4410ee4b6b97769d9e9 [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;
Svetoslavc6d1c342015-02-26 14:44:43 -080020import android.annotation.NonNull;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070023import android.content.ComponentCallbacks2;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070024import android.content.Context;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070025import android.content.Intent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070026import android.content.res.Configuration;
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -080027import android.content.res.Resources;
George Mountc03da0e2014-08-22 17:04:02 -070028import android.content.res.TypedArray;
Adam Powell592df7d2016-01-14 12:58:23 -080029import android.os.Build;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070030import android.os.Bundle;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070031import android.os.Parcel;
32import android.os.Parcelable;
George Mountc03da0e2014-08-22 17:04:02 -070033import android.transition.Transition;
34import android.transition.TransitionInflater;
35import android.transition.TransitionSet;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070036import android.util.AndroidRuntimeException;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070037import android.util.ArrayMap;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070038import android.util.AttributeSet;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080039import android.util.DebugUtils;
Dianne Hackborn03fcc332012-05-15 12:49:40 -070040import android.util.Log;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070041import android.util.SparseArray;
Adam Powell14874662013-07-18 19:42:41 -070042import android.util.SuperNotCalledException;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070043import android.view.ContextMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -070044import android.view.ContextMenu.ContextMenuInfo;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070045import android.view.LayoutInflater;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070046import android.view.Menu;
47import android.view.MenuInflater;
48import android.view.MenuItem;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070049import android.view.View;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070050import android.view.View.OnCreateContextMenuListener;
Adam Powellf0f5fff2011-08-01 13:42:50 -070051import android.view.ViewGroup;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070052import android.widget.AdapterView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070053
Dianne Hackborn625ac272010-09-17 18:29:22 -070054import java.io.FileDescriptor;
55import java.io.PrintWriter;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070056
57final class FragmentState implements Parcelable {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070058 final String mClassName;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070059 final int mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070060 final boolean mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070061 final int mFragmentId;
62 final int mContainerId;
63 final String mTag;
64 final boolean mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070065 final boolean mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070066 final Bundle mArguments;
Todd Kennedyd6d18582015-07-23 10:19:46 -070067 final boolean mHidden;
George Mountc03da0e2014-08-22 17:04:02 -070068
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070069 Bundle mSavedFragmentState;
George Mountc03da0e2014-08-22 17:04:02 -070070
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070071 Fragment mInstance;
George Mountc03da0e2014-08-22 17:04:02 -070072
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070073 public FragmentState(Fragment frag) {
74 mClassName = frag.getClass().getName();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070075 mIndex = frag.mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070076 mFromLayout = frag.mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070077 mFragmentId = frag.mFragmentId;
78 mContainerId = frag.mContainerId;
79 mTag = frag.mTag;
80 mRetainInstance = frag.mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070081 mDetached = frag.mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070082 mArguments = frag.mArguments;
Todd Kennedyd6d18582015-07-23 10:19:46 -070083 mHidden = frag.mHidden;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070084 }
George Mountc03da0e2014-08-22 17:04:02 -070085
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070086 public FragmentState(Parcel in) {
87 mClassName = in.readString();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070088 mIndex = in.readInt();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070089 mFromLayout = in.readInt() != 0;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070090 mFragmentId = in.readInt();
91 mContainerId = in.readInt();
92 mTag = in.readString();
93 mRetainInstance = in.readInt() != 0;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070094 mDetached = in.readInt() != 0;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070095 mArguments = in.readBundle();
Todd Kennedyd6d18582015-07-23 10:19:46 -070096 mHidden = in.readInt() != 0;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070097 mSavedFragmentState = in.readBundle();
98 }
George Mountc03da0e2014-08-22 17:04:02 -070099
Adam Powell44ba79e2016-02-04 16:20:37 -0800100 public Fragment instantiate(FragmentHostCallback host, Fragment parent,
101 FragmentManagerNonConfig childNonConfig) {
102 if (mInstance == null) {
103 final Context context = host.getContext();
104 if (mArguments != null) {
105 mArguments.setClassLoader(context.getClassLoader());
106 }
107
108 mInstance = Fragment.instantiate(context, mClassName, mArguments);
109
110 if (mSavedFragmentState != null) {
111 mSavedFragmentState.setClassLoader(context.getClassLoader());
112 mInstance.mSavedFragmentState = mSavedFragmentState;
113 }
114 mInstance.setIndex(mIndex, parent);
115 mInstance.mFromLayout = mFromLayout;
116 mInstance.mRestored = true;
117 mInstance.mFragmentId = mFragmentId;
118 mInstance.mContainerId = mContainerId;
119 mInstance.mTag = mTag;
120 mInstance.mRetainInstance = mRetainInstance;
121 mInstance.mDetached = mDetached;
122 mInstance.mHidden = mHidden;
123 mInstance.mFragmentManager = host.mFragmentManager;
124 if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG,
125 "Instantiated fragment " + mInstance);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700126 }
Adam Powell44ba79e2016-02-04 16:20:37 -0800127 mInstance.mChildNonConfig = childNonConfig;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700128 return mInstance;
129 }
George Mountc03da0e2014-08-22 17:04:02 -0700130
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700131 public int describeContents() {
132 return 0;
133 }
134
135 public void writeToParcel(Parcel dest, int flags) {
136 dest.writeString(mClassName);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700137 dest.writeInt(mIndex);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700138 dest.writeInt(mFromLayout ? 1 : 0);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700139 dest.writeInt(mFragmentId);
140 dest.writeInt(mContainerId);
141 dest.writeString(mTag);
142 dest.writeInt(mRetainInstance ? 1 : 0);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700143 dest.writeInt(mDetached ? 1 : 0);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700144 dest.writeBundle(mArguments);
Todd Kennedyd6d18582015-07-23 10:19:46 -0700145 dest.writeInt(mHidden ? 1 : 0);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700146 dest.writeBundle(mSavedFragmentState);
147 }
George Mountc03da0e2014-08-22 17:04:02 -0700148
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700149 public static final Parcelable.Creator<FragmentState> CREATOR
150 = new Parcelable.Creator<FragmentState>() {
151 public FragmentState createFromParcel(Parcel in) {
152 return new FragmentState(in);
153 }
George Mountc03da0e2014-08-22 17:04:02 -0700154
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700155 public FragmentState[] newArray(int size) {
156 return new FragmentState[size];
157 }
158 };
159}
160
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700161/**
162 * A Fragment is a piece of an application's user interface or behavior
Dianne Hackborn291905e2010-08-17 15:17:15 -0700163 * that can be placed in an {@link Activity}. Interaction with fragments
164 * is done through {@link FragmentManager}, which can be obtained via
165 * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and
166 * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}.
167 *
168 * <p>The Fragment class can be used many ways to achieve a wide variety of
felipeal1b4e4b62012-02-27 12:09:13 -0800169 * results. In its core, it represents a particular operation or interface
Dianne Hackborn291905e2010-08-17 15:17:15 -0700170 * that is running within a larger {@link Activity}. A Fragment is closely
171 * tied to the Activity it is in, and can not be used apart from one. Though
172 * Fragment defines its own lifecycle, that lifecycle is dependent on its
173 * activity: if the activity is stopped, no fragments inside of it can be
174 * started; when the activity is destroyed, all fragments will be destroyed.
175 *
Jeff Davidson2a129782014-09-09 18:10:57 -0700176 * <p>All subclasses of Fragment must include a public no-argument constructor.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700177 * The framework will often re-instantiate a fragment class when needed,
178 * in particular during state restore, and needs to be able to find this
Jeff Davidson2a129782014-09-09 18:10:57 -0700179 * constructor to instantiate it. If the no-argument constructor is not
180 * available, a runtime exception will occur in some cases during state
181 * restore.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700182 *
183 * <p>Topics covered here:
184 * <ol>
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700185 * <li><a href="#OlderPlatforms">Older Platforms</a>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700186 * <li><a href="#Lifecycle">Lifecycle</a>
187 * <li><a href="#Layout">Layout</a>
188 * <li><a href="#BackStack">Back Stack</a>
189 * </ol>
190 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700191 * <div class="special reference">
192 * <h3>Developer Guides</h3>
193 * <p>For more information about using fragments, read the
194 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
195 * </div>
196 *
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700197 * <a name="OlderPlatforms"></a>
198 * <h3>Older Platforms</h3>
199 *
200 * While the Fragment API was introduced in
201 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
Dianne Hackborn7871bad2011-12-12 15:19:26 -0800202 * at is also available for use on older platforms through
203 * {@link android.support.v4.app.FragmentActivity}. See the blog post
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700204 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
205 * Fragments For All</a> for more details.
206 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700207 * <a name="Lifecycle"></a>
208 * <h3>Lifecycle</h3>
209 *
210 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has
211 * its own wrinkle on the standard activity lifecycle. It includes basic
212 * activity lifecycle methods such as {@link #onResume}, but also important
213 * are methods related to interactions with the activity and UI generation.
214 *
215 * <p>The core series of lifecycle methods that are called to bring a fragment
216 * up to resumed state (interacting with the user) are:
217 *
218 * <ol>
219 * <li> {@link #onAttach} called once the fragment is associated with its activity.
220 * <li> {@link #onCreate} called to do initial creation of the fragment.
221 * <li> {@link #onCreateView} creates and returns the view hierarchy associated
222 * with the fragment.
223 * <li> {@link #onActivityCreated} tells the fragment that its activity has
Quddus Chong37900012012-04-11 11:57:50 -0700224 * completed its own {@link Activity#onCreate Activity.onCreate()}.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700225 * <li> {@link #onViewStateRestored} tells the fragment that all of the saved
226 * state of its view hierarchy has been restored.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700227 * <li> {@link #onStart} makes the fragment visible to the user (based on its
228 * containing activity being started).
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700229 * <li> {@link #onResume} makes the fragment begin interacting with the user
230 * (based on its containing activity being resumed).
Dianne Hackborn291905e2010-08-17 15:17:15 -0700231 * </ol>
232 *
233 * <p>As a fragment is no longer being used, it goes through a reverse
234 * series of callbacks:
235 *
236 * <ol>
237 * <li> {@link #onPause} fragment is no longer interacting with the user either
238 * because its activity is being paused or a fragment operation is modifying it
239 * in the activity.
240 * <li> {@link #onStop} fragment is no longer visible to the user either
241 * because its activity is being stopped or a fragment operation is modifying it
242 * in the activity.
243 * <li> {@link #onDestroyView} allows the fragment to clean up resources
244 * associated with its View.
245 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state.
246 * <li> {@link #onDetach} called immediately prior to the fragment no longer
247 * being associated with its activity.
248 * </ol>
249 *
250 * <a name="Layout"></a>
251 * <h3>Layout</h3>
252 *
253 * <p>Fragments can be used as part of your application's layout, allowing
254 * you to better modularize your code and more easily adjust your user
255 * interface to the screen it is running on. As an example, we can look
256 * at a simple program consisting of a list of items, and display of the
257 * details of each item.</p>
258 *
259 * <p>An activity's layout XML can include <code>&lt;fragment&gt;</code> tags
260 * to embed fragment instances inside of the layout. For example, here is
Dianne Hackborn625ac272010-09-17 18:29:22 -0700261 * a simple layout that embeds one fragment:</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700262 *
263 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout}
264 *
265 * <p>The layout is installed in the activity in the normal way:</p>
266 *
267 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
268 * main}
269 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800270 * <p>The titles fragment, showing a list of titles, is fairly simple, relying
Dianne Hackborn291905e2010-08-17 15:17:15 -0700271 * on {@link ListFragment} for most of its work. Note the implementation of
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800272 * clicking an item: depending on the current activity's layout, it can either
273 * create and display a new fragment to show the details in-place (more about
Ben Dodson542f2402011-06-14 16:40:23 -0700274 * this later), or start a new activity to show the details.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700275 *
276 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
277 * titles}
278 *
Ben Dodson542f2402011-06-14 16:40:23 -0700279 * <p>The details fragment showing the contents of a selected item just
Dianne Hackborn291905e2010-08-17 15:17:15 -0700280 * displays a string of text based on an index of a string array built in to
281 * the app:</p>
282 *
283 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
284 * details}
285 *
286 * <p>In this case when the user clicks on a title, there is no details
Ben Dodson542f2402011-06-14 16:40:23 -0700287 * container in the current activity, so the titles fragment's click code will
Dianne Hackborn291905e2010-08-17 15:17:15 -0700288 * launch a new activity to display the details fragment:</p>
289 *
290 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
291 * details_activity}
292 *
293 * <p>However the screen may be large enough to show both the list of titles
294 * and details about the currently selected title. To use such a layout on
295 * a landscape screen, this alternative layout can be placed under layout-land:</p>
296 *
297 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout}
298 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800299 * <p>Note how the prior code will adjust to this alternative UI flow: the titles
300 * fragment will now embed the details fragment inside of this activity, and the
301 * details activity will finish itself if it is running in a configuration
302 * where the details can be shown in-place.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700303 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700304 * <p>When a configuration change causes the activity hosting these fragments
305 * to restart, its new instance may use a different layout that doesn't
306 * include the same fragments as the previous layout. In this case all of
307 * the previous fragments will still be instantiated and running in the new
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800308 * instance. However, any that are no longer associated with a &lt;fragment&gt;
309 * tag in the view hierarchy will not have their content view created
310 * and will return false from {@link #isInLayout}. (The code here also shows
311 * how you can determine if a fragment placed in a container is no longer
312 * running in a layout with that container and avoid creating its view hierarchy
313 * in that case.)
George Mountc03da0e2014-08-22 17:04:02 -0700314 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700315 * <p>The attributes of the &lt;fragment&gt; tag are used to control the
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800316 * LayoutParams provided when attaching the fragment's view to the parent
317 * container. They can also be parsed by the fragment in {@link #onInflate}
Dianne Hackborn625ac272010-09-17 18:29:22 -0700318 * as parameters.
George Mountc03da0e2014-08-22 17:04:02 -0700319 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700320 * <p>The fragment being instantiated must have some kind of unique identifier
321 * so that it can be re-associated with a previous instance if the parent
322 * activity needs to be destroyed and recreated. This can be provided these
323 * ways:
George Mountc03da0e2014-08-22 17:04:02 -0700324 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700325 * <ul>
326 * <li>If nothing is explicitly supplied, the view ID of the container will
327 * be used.
328 * <li><code>android:tag</code> can be used in &lt;fragment&gt; to provide
329 * a specific tag name for the fragment.
330 * <li><code>android:id</code> can be used in &lt;fragment&gt; to provide
331 * a specific identifier for the fragment.
332 * </ul>
George Mountc03da0e2014-08-22 17:04:02 -0700333 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700334 * <a name="BackStack"></a>
335 * <h3>Back Stack</h3>
336 *
337 * <p>The transaction in which fragments are modified can be placed on an
338 * internal back-stack of the owning activity. When the user presses back
339 * in the activity, any transactions on the back stack are popped off before
340 * the activity itself is finished.
341 *
342 * <p>For example, consider this simple fragment that is instantiated with
343 * an integer argument and displays that in a TextView in its UI:</p>
344 *
345 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
346 * fragment}
347 *
348 * <p>A function that creates a new instance of the fragment, replacing
349 * whatever current fragment instance is being shown and pushing that change
350 * on to the back stack could be written as:
351 *
352 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
353 * add_stack}
354 *
355 * <p>After each call to this function, a new entry is on the stack, and
356 * pressing back will pop it to return the user to whatever previous state
357 * the activity UI was in.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700358 */
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700359public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700360 private static final ArrayMap<String, Class<?>> sClassMap =
361 new ArrayMap<String, Class<?>>();
George Mountc03da0e2014-08-22 17:04:02 -0700362
Adam Powell635c60a2011-10-26 10:22:16 -0700363 static final int INVALID_STATE = -1; // Invalid state used as a null value.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700364 static final int INITIALIZING = 0; // Not yet created.
365 static final int CREATED = 1; // Created.
366 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700367 static final int STOPPED = 3; // Fully created, not started.
368 static final int STARTED = 4; // Created and started, not resumed.
369 static final int RESUMED = 5; // Created started and resumed.
George Mountc03da0e2014-08-22 17:04:02 -0700370
371 private static final Transition USE_DEFAULT_TRANSITION = new TransitionSet();
372
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700373 int mState = INITIALIZING;
George Mountc03da0e2014-08-22 17:04:02 -0700374
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800375 // Non-null if the fragment's view hierarchy is currently animating away,
376 // meaning we need to wait a bit on completely destroying it. This is the
377 // animation that is running.
378 Animator mAnimatingAway;
379
380 // If mAnimatingAway != null, this is the state we should move to once the
381 // animation is done.
382 int mStateAfterAnimating;
383
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700384 // When instantiated from saved state, this is the saved state.
385 Bundle mSavedFragmentState;
386 SparseArray<Parcelable> mSavedViewState;
George Mountc03da0e2014-08-22 17:04:02 -0700387
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700388 // Index into active fragment array.
389 int mIndex = -1;
George Mountc03da0e2014-08-22 17:04:02 -0700390
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700391 // Internal unique name for this fragment;
392 String mWho;
George Mountc03da0e2014-08-22 17:04:02 -0700393
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700394 // Construction arguments;
395 Bundle mArguments;
396
Dianne Hackborndef15372010-08-15 12:43:52 -0700397 // Target fragment.
398 Fragment mTarget;
399
Dianne Hackbornf9302322011-06-14 18:36:14 -0700400 // For use when retaining a fragment: this is the index of the last mTarget.
401 int mTargetIndex = -1;
402
Dianne Hackborndef15372010-08-15 12:43:52 -0700403 // Target request code.
404 int mTargetRequestCode;
405
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700406 // True if the fragment is in the list of added fragments.
407 boolean mAdded;
George Mountc03da0e2014-08-22 17:04:02 -0700408
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800409 // If set this fragment is being removed from its activity.
410 boolean mRemoving;
411
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700412 // Set to true if this fragment was instantiated from a layout file.
413 boolean mFromLayout;
George Mountc03da0e2014-08-22 17:04:02 -0700414
Dianne Hackborn625ac272010-09-17 18:29:22 -0700415 // Set to true when the view has actually been inflated in its layout.
416 boolean mInLayout;
417
Dianne Hackborn352cc982011-01-04 11:34:18 -0800418 // True if this fragment has been restored from previously saved state.
419 boolean mRestored;
George Mountc03da0e2014-08-22 17:04:02 -0700420
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700421 // Number of active back stack entries this fragment is in.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700422 int mBackStackNesting;
George Mountc03da0e2014-08-22 17:04:02 -0700423
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700424 // The fragment manager we are associated with. Set as soon as the
425 // fragment is used in a transaction; cleared after it has been removed
426 // from all transactions.
Adam Powell635c60a2011-10-26 10:22:16 -0700427 FragmentManagerImpl mFragmentManager;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700428
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700429 // Activity this fragment is attached to.
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700430 FragmentHostCallback mHost;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700431
432 // Private fragment manager for child fragments inside of this one.
433 FragmentManagerImpl mChildFragmentManager;
434
Adam Powell44ba79e2016-02-04 16:20:37 -0800435 // For use when restoring fragment state and descendant fragments are retained.
436 // This state is set by FragmentState.instantiate and cleared in onCreate.
437 FragmentManagerNonConfig mChildNonConfig;
438
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700439 // If this Fragment is contained in another Fragment, this is that container.
440 Fragment mParentFragment;
441
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700442 // The optional identifier for this fragment -- either the container ID if it
443 // was dynamically added to the view hierarchy, or the ID supplied in
444 // layout.
445 int mFragmentId;
George Mountc03da0e2014-08-22 17:04:02 -0700446
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700447 // When a fragment is being dynamically added to the view hierarchy, this
448 // is the identifier of the parent container it is being added to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700449 int mContainerId;
George Mountc03da0e2014-08-22 17:04:02 -0700450
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700451 // The optional named tag for this fragment -- usually used to find
452 // fragments that are not part of the layout.
453 String mTag;
George Mountc03da0e2014-08-22 17:04:02 -0700454
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700455 // Set to true when the app has requested that this fragment be hidden
456 // from the user.
457 boolean mHidden;
George Mountc03da0e2014-08-22 17:04:02 -0700458
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700459 // Set to true when the app has requested that this fragment be detached.
460 boolean mDetached;
461
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700462 // If set this fragment would like its instance retained across
463 // configuration changes.
464 boolean mRetainInstance;
George Mountc03da0e2014-08-22 17:04:02 -0700465
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700466 // If set this fragment is being retained across the current config change.
467 boolean mRetaining;
George Mountc03da0e2014-08-22 17:04:02 -0700468
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700469 // If set this fragment has menu items to contribute.
470 boolean mHasMenu;
Dianne Hackborn6c285972011-08-29 16:53:49 -0700471
472 // Set to true to allow the fragment's menu to be shown.
473 boolean mMenuVisible = true;
474
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700475 // Used to verify that subclasses call through to super class.
476 boolean mCalled;
George Mountc03da0e2014-08-22 17:04:02 -0700477
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700478 // If app has requested a specific animation, this is the one to use.
479 int mNextAnim;
George Mountc03da0e2014-08-22 17:04:02 -0700480
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700481 // The parent container of the fragment after dynamically added to UI.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700482 ViewGroup mContainer;
George Mountc03da0e2014-08-22 17:04:02 -0700483
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700484 // The View generated for this fragment.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700485 View mView;
George Mountc03da0e2014-08-22 17:04:02 -0700486
Adam Powell635c60a2011-10-26 10:22:16 -0700487 // Whether this fragment should defer starting until after other fragments
488 // have been started and their loaders are finished.
489 boolean mDeferStart;
490
Adam Powell78fed9b2011-11-07 10:45:34 -0800491 // Hint provided by the app that this fragment is currently visible to the user.
492 boolean mUserVisibleHint = true;
493
Dianne Hackborn4911b782010-07-15 12:54:39 -0700494 LoaderManagerImpl mLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700495 boolean mLoadersStarted;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700496 boolean mCheckedForLoaderManager;
George Mountc03da0e2014-08-22 17:04:02 -0700497
498 private Transition mEnterTransition = null;
499 private Transition mReturnTransition = USE_DEFAULT_TRANSITION;
500 private Transition mExitTransition = null;
501 private Transition mReenterTransition = USE_DEFAULT_TRANSITION;
502 private Transition mSharedElementEnterTransition = null;
503 private Transition mSharedElementReturnTransition = USE_DEFAULT_TRANSITION;
504 private Boolean mAllowReturnTransitionOverlap;
505 private Boolean mAllowEnterTransitionOverlap;
506
George Mount65580562014-08-29 08:15:48 -0700507 SharedElementCallback mEnterTransitionCallback = SharedElementCallback.NULL_CALLBACK;
508 SharedElementCallback mExitTransitionCallback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -0700509
Dianne Hackbornc8017682010-07-06 13:34:38 -0700510 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700511 * State information that has been retrieved from a fragment instance
512 * through {@link FragmentManager#saveFragmentInstanceState(Fragment)
513 * FragmentManager.saveFragmentInstanceState}.
514 */
515 public static class SavedState implements Parcelable {
516 final Bundle mState;
517
518 SavedState(Bundle state) {
519 mState = state;
520 }
521
522 SavedState(Parcel in, ClassLoader loader) {
523 mState = in.readBundle();
524 if (loader != null && mState != null) {
525 mState.setClassLoader(loader);
526 }
527 }
528
529 @Override
530 public int describeContents() {
531 return 0;
532 }
533
534 @Override
535 public void writeToParcel(Parcel dest, int flags) {
536 dest.writeBundle(mState);
537 }
538
539 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR
540 = new Parcelable.ClassLoaderCreator<SavedState>() {
541 public SavedState createFromParcel(Parcel in) {
542 return new SavedState(in, null);
543 }
544
545 public SavedState createFromParcel(Parcel in, ClassLoader loader) {
546 return new SavedState(in, loader);
547 }
548
549 public SavedState[] newArray(int size) {
550 return new SavedState[size];
551 }
552 };
553 }
554
555 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700556 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when
557 * there is an instantiation failure.
558 */
559 static public class InstantiationException extends AndroidRuntimeException {
560 public InstantiationException(String msg, Exception cause) {
561 super(msg, cause);
562 }
563 }
564
565 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700566 * Default constructor. <strong>Every</strong> fragment must have an
Dianne Hackbornc8017682010-07-06 13:34:38 -0700567 * empty constructor, so it can be instantiated when restoring its
568 * activity's state. It is strongly recommended that subclasses do not
569 * have other constructors with parameters, since these constructors
570 * will not be called when the fragment is re-instantiated; instead,
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700571 * arguments can be supplied by the caller with {@link #setArguments}
572 * and later retrieved by the Fragment with {@link #getArguments}.
George Mountc03da0e2014-08-22 17:04:02 -0700573 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700574 * <p>Applications should generally not implement a constructor. The
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700575 * first place application code can run where the fragment is ready to
Dianne Hackborn291905e2010-08-17 15:17:15 -0700576 * be used is in {@link #onAttach(Activity)}, the point where the fragment
577 * is actually associated with its activity. Some applications may also
578 * want to implement {@link #onInflate} to retrieve attributes from a
579 * layout resource, though should take care here because this happens for
580 * the fragment is attached to its activity.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700581 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700582 public Fragment() {
583 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700584
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700585 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700586 * Like {@link #instantiate(Context, String, Bundle)} but with a null
587 * argument Bundle.
588 */
589 public static Fragment instantiate(Context context, String fname) {
590 return instantiate(context, fname, null);
591 }
592
593 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700594 * Create a new instance of a Fragment with the given class name. This is
595 * the same as calling its empty constructor.
596 *
597 * @param context The calling context being used to instantiate the fragment.
598 * This is currently just used to get its ClassLoader.
599 * @param fname The class name of the fragment to instantiate.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700600 * @param args Bundle of arguments to supply to the fragment, which it
601 * can retrieve with {@link #getArguments()}. May be null.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700602 * @return Returns a new fragment instance.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700603 * @throws InstantiationException If there is a failure in instantiating
604 * the given fragment class. This is a runtime exception; it is not
605 * normally expected to happen.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700606 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700607 public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700608 try {
609 Class<?> clazz = sClassMap.get(fname);
610 if (clazz == null) {
611 // Class not found in the cache, see if it's real, and try to add it
612 clazz = context.getClassLoader().loadClass(fname);
Amith Yamasani364ed4d2013-07-26 13:37:56 -0700613 if (!Fragment.class.isAssignableFrom(clazz)) {
614 throw new InstantiationException("Trying to instantiate a class " + fname
615 + " that is not a Fragment", new ClassCastException());
616 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700617 sClassMap.put(fname, clazz);
618 }
619 Fragment f = (Fragment)clazz.newInstance();
620 if (args != null) {
621 args.setClassLoader(f.getClass().getClassLoader());
622 f.mArguments = args;
623 }
624 return f;
625 } catch (ClassNotFoundException e) {
626 throw new InstantiationException("Unable to instantiate fragment " + fname
627 + ": make sure class name exists, is public, and has an"
628 + " empty constructor that is public", e);
629 } catch (java.lang.InstantiationException e) {
630 throw new InstantiationException("Unable to instantiate fragment " + fname
631 + ": make sure class name exists, is public, and has an"
632 + " empty constructor that is public", e);
633 } catch (IllegalAccessException e) {
634 throw new InstantiationException("Unable to instantiate fragment " + fname
635 + ": make sure class name exists, is public, and has an"
636 + " empty constructor that is public", e);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700637 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700638 }
George Mountc03da0e2014-08-22 17:04:02 -0700639
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700640 final void restoreViewState(Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700641 if (mSavedViewState != null) {
642 mView.restoreHierarchyState(mSavedViewState);
643 mSavedViewState = null;
644 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700645 mCalled = false;
646 onViewStateRestored(savedInstanceState);
647 if (!mCalled) {
648 throw new SuperNotCalledException("Fragment " + this
649 + " did not call through to super.onViewStateRestored()");
650 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700651 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700652
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700653 final void setIndex(int index, Fragment parent) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700654 mIndex = index;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700655 if (parent != null) {
656 mWho = parent.mWho + ":" + mIndex;
657 } else {
658 mWho = "android:fragment:" + mIndex;
659 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700660 }
661
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700662 final boolean isInBackStack() {
663 return mBackStackNesting > 0;
664 }
665
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700666 /**
667 * Subclasses can not override equals().
668 */
669 @Override final public boolean equals(Object o) {
670 return super.equals(o);
671 }
672
673 /**
674 * Subclasses can not override hashCode().
675 */
676 @Override final public int hashCode() {
677 return super.hashCode();
678 }
George Mountc03da0e2014-08-22 17:04:02 -0700679
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700680 @Override
681 public String toString() {
682 StringBuilder sb = new StringBuilder(128);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800683 DebugUtils.buildShortClassTag(this, sb);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700684 if (mIndex >= 0) {
685 sb.append(" #");
686 sb.append(mIndex);
687 }
688 if (mFragmentId != 0) {
689 sb.append(" id=0x");
690 sb.append(Integer.toHexString(mFragmentId));
691 }
692 if (mTag != null) {
693 sb.append(" ");
694 sb.append(mTag);
695 }
696 sb.append('}');
697 return sb.toString();
698 }
George Mountc03da0e2014-08-22 17:04:02 -0700699
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700700 /**
701 * Return the identifier this fragment is known by. This is either
702 * the android:id value supplied in a layout or the container view ID
703 * supplied when adding the fragment.
704 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700705 final public int getId() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700706 return mFragmentId;
707 }
George Mountc03da0e2014-08-22 17:04:02 -0700708
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700709 /**
710 * Get the tag name of the fragment, if specified.
711 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700712 final public String getTag() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700713 return mTag;
714 }
George Mountc03da0e2014-08-22 17:04:02 -0700715
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700716 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700717 * Supply the construction arguments for this fragment. This can only
718 * be called before the fragment has been attached to its activity; that
719 * is, you should call it immediately after constructing the fragment. The
720 * arguments supplied here will be retained across fragment destroy and
721 * creation.
722 */
Dianne Hackborndef15372010-08-15 12:43:52 -0700723 public void setArguments(Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700724 if (mIndex >= 0) {
725 throw new IllegalStateException("Fragment already active");
726 }
727 mArguments = args;
728 }
729
730 /**
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700731 * Return the arguments supplied to {@link #setArguments}, if any.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700732 */
733 final public Bundle getArguments() {
734 return mArguments;
735 }
736
737 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700738 * Set the initial saved state that this Fragment should restore itself
739 * from when first being constructed, as returned by
740 * {@link FragmentManager#saveFragmentInstanceState(Fragment)
741 * FragmentManager.saveFragmentInstanceState}.
742 *
743 * @param state The state the fragment should be restored from.
744 */
745 public void setInitialSavedState(SavedState state) {
746 if (mIndex >= 0) {
747 throw new IllegalStateException("Fragment already active");
748 }
749 mSavedFragmentState = state != null && state.mState != null
750 ? state.mState : null;
751 }
752
753 /**
Dianne Hackborndef15372010-08-15 12:43:52 -0700754 * Optional target for this fragment. This may be used, for example,
755 * if this fragment is being started by another, and when done wants to
756 * give a result back to the first. The target set here is retained
757 * across instances via {@link FragmentManager#putFragment
758 * FragmentManager.putFragment()}.
759 *
760 * @param fragment The fragment that is the target of this one.
761 * @param requestCode Optional request code, for convenience if you
762 * are going to call back with {@link #onActivityResult(int, int, Intent)}.
763 */
764 public void setTargetFragment(Fragment fragment, int requestCode) {
765 mTarget = fragment;
766 mTargetRequestCode = requestCode;
767 }
768
769 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700770 * Return the target fragment set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700771 */
772 final public Fragment getTargetFragment() {
773 return mTarget;
774 }
775
776 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700777 * Return the target request code set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700778 */
779 final public int getTargetRequestCode() {
780 return mTargetRequestCode;
781 }
782
783 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700784 * Return the {@link Context} this fragment is currently associated with.
785 */
786 public Context getContext() {
787 return mHost == null ? null : mHost.getContext();
788 }
789
790 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700791 * Return the Activity this fragment is currently associated with.
792 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700793 final public Activity getActivity() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700794 return mHost == null ? null : mHost.getActivity();
795 }
796
797 /**
798 * Return the host object of this fragment. May return {@code null} if the fragment
799 * isn't currently being hosted.
800 */
801 @Nullable
802 final public Object getHost() {
803 return mHost == null ? null : mHost.onGetHost();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700804 }
George Mountc03da0e2014-08-22 17:04:02 -0700805
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700806 /**
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800807 * Return <code>getActivity().getResources()</code>.
808 */
809 final public Resources getResources() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700810 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800811 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
812 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700813 return mHost.getContext().getResources();
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800814 }
George Mountc03da0e2014-08-22 17:04:02 -0700815
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800816 /**
817 * Return a localized, styled CharSequence from the application's package's
818 * default string table.
819 *
820 * @param resId Resource id for the CharSequence text
821 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700822 public final CharSequence getText(@StringRes int resId) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800823 return getResources().getText(resId);
824 }
825
826 /**
827 * Return a localized string from the application's package's
828 * default string table.
829 *
830 * @param resId Resource id for the string
831 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700832 public final String getString(@StringRes int resId) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800833 return getResources().getString(resId);
834 }
835
836 /**
837 * Return a localized formatted string from the application's package's
838 * default string table, substituting the format arguments as defined in
839 * {@link java.util.Formatter} and {@link java.lang.String#format}.
840 *
841 * @param resId Resource id for the format string
842 * @param formatArgs The format arguments that will be used for substitution.
843 */
844
Tor Norbye7b9c9122013-05-30 16:48:33 -0700845 public final String getString(@StringRes int resId, Object... formatArgs) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800846 return getResources().getString(resId, formatArgs);
847 }
848
849 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700850 * Return the FragmentManager for interacting with fragments associated
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700851 * with this fragment's activity. Note that this will be non-null slightly
Dianne Hackborn625ac272010-09-17 18:29:22 -0700852 * before {@link #getActivity()}, during the time from when the fragment is
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700853 * placed in a {@link FragmentTransaction} until it is committed and
854 * attached to its activity.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700855 *
856 * <p>If this Fragment is a child of another Fragment, the FragmentManager
857 * returned here will be the parent's {@link #getChildFragmentManager()}.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700858 */
859 final public FragmentManager getFragmentManager() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700860 return mFragmentManager;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700861 }
862
863 /**
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700864 * Return a private FragmentManager for placing and managing Fragments
865 * inside of this Fragment.
866 */
867 final public FragmentManager getChildFragmentManager() {
868 if (mChildFragmentManager == null) {
869 instantiateChildFragmentManager();
870 if (mState >= RESUMED) {
871 mChildFragmentManager.dispatchResume();
872 } else if (mState >= STARTED) {
873 mChildFragmentManager.dispatchStart();
874 } else if (mState >= ACTIVITY_CREATED) {
875 mChildFragmentManager.dispatchActivityCreated();
876 } else if (mState >= CREATED) {
877 mChildFragmentManager.dispatchCreate();
878 }
879 }
880 return mChildFragmentManager;
881 }
882
883 /**
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700884 * Returns the parent Fragment containing this Fragment. If this Fragment
885 * is attached directly to an Activity, returns null.
886 */
887 final public Fragment getParentFragment() {
888 return mParentFragment;
889 }
890
891 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700892 * Return true if the fragment is currently added to its activity.
893 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700894 final public boolean isAdded() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700895 return mHost != null && mAdded;
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800896 }
897
898 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700899 * Return true if the fragment has been explicitly detached from the UI.
900 * That is, {@link FragmentTransaction#detach(Fragment)
901 * FragmentTransaction.detach(Fragment)} has been used on it.
902 */
903 final public boolean isDetached() {
904 return mDetached;
905 }
906
907 /**
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800908 * Return true if this fragment is currently being removed from its
909 * activity. This is <em>not</em> whether its activity is finishing, but
910 * rather whether it is in the process of being removed from its activity.
911 */
912 final public boolean isRemoving() {
913 return mRemoving;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700914 }
George Mountc03da0e2014-08-22 17:04:02 -0700915
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700916 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700917 * Return true if the layout is included as part of an activity view
918 * hierarchy via the &lt;fragment&gt; tag. This will always be true when
919 * fragments are created through the &lt;fragment&gt; tag, <em>except</em>
920 * in the case where an old fragment is restored from a previous state and
921 * it does not appear in the layout of the current state.
922 */
923 final public boolean isInLayout() {
924 return mInLayout;
925 }
926
927 /**
Dianne Hackborn2707d602010-07-09 18:01:20 -0700928 * Return true if the fragment is in the resumed state. This is true
929 * for the duration of {@link #onResume()} and {@link #onPause()} as well.
930 */
931 final public boolean isResumed() {
Adam Powelld1d4d9c2016-01-12 10:11:42 -0800932 return mState >= RESUMED;
Dianne Hackborn2707d602010-07-09 18:01:20 -0700933 }
George Mountc03da0e2014-08-22 17:04:02 -0700934
Dianne Hackborn2707d602010-07-09 18:01:20 -0700935 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700936 * Return true if the fragment is currently visible to the user. This means
937 * it: (1) has been added, (2) has its view attached to the window, and
938 * (3) is not hidden.
939 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700940 final public boolean isVisible() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700941 return isAdded() && !isHidden() && mView != null
942 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
943 }
George Mountc03da0e2014-08-22 17:04:02 -0700944
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700945 /**
946 * Return true if the fragment has been hidden. By default fragments
947 * are shown. You can find out about changes to this state with
Dianne Hackborncddfa6d2010-05-19 22:56:37 -0700948 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700949 * to other states -- that is, to be visible to the user, a fragment
950 * must be both started and not hidden.
951 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700952 final public boolean isHidden() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700953 return mHidden;
954 }
George Mountc03da0e2014-08-22 17:04:02 -0700955
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700956 /**
957 * Called when the hidden state (as returned by {@link #isHidden()} of
958 * the fragment has changed. Fragments start out not hidden; this will
959 * be called whenever the fragment changes state from that.
960 * @param hidden True if the fragment is now hidden, false if it is not
961 * visible.
962 */
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 Powell78fed9b2011-11-07 10:45:34 -08001032 * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
1033 * false if it is not.
Adam Powell635c60a2011-10-26 10:22:16 -07001034 */
Adam Powell78fed9b2011-11-07 10:45:34 -08001035 public void setUserVisibleHint(boolean isVisibleToUser) {
Adam Powell255b0082016-02-18 15:17:29 -08001036 if (!mUserVisibleHint && isVisibleToUser && mState < STARTED && mFragmentManager != null) {
Adam Powell635c60a2011-10-26 10:22:16 -07001037 mFragmentManager.performPendingDeferredStart(this);
1038 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001039 mUserVisibleHint = isVisibleToUser;
1040 mDeferStart = !isVisibleToUser;
Adam Powell635c60a2011-10-26 10:22:16 -07001041 }
1042
1043 /**
Adam Powell78fed9b2011-11-07 10:45:34 -08001044 * @return The current value of the user-visible hint on this fragment.
1045 * @see #setUserVisibleHint(boolean)
Adam Powell635c60a2011-10-26 10:22:16 -07001046 */
Adam Powell78fed9b2011-11-07 10:45:34 -08001047 public boolean getUserVisibleHint() {
1048 return mUserVisibleHint;
Adam Powell635c60a2011-10-26 10:22:16 -07001049 }
1050
1051 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001052 * Return the LoaderManager for this fragment, creating it if needed.
1053 */
1054 public LoaderManager getLoaderManager() {
1055 if (mLoaderManager != null) {
1056 return mLoaderManager;
1057 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001058 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001059 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1060 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001061 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001062 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -07001063 return mLoaderManager;
1064 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001065
Dianne Hackbornc8017682010-07-06 13:34:38 -07001066 /**
Scott Main87bff972013-02-27 15:46:34 -08001067 * Call {@link Activity#startActivity(Intent)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001068 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001069 *
1070 * @param intent The intent to start.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001071 */
1072 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001073 startActivity(intent, null);
1074 }
George Mountc03da0e2014-08-22 17:04:02 -07001075
Dianne Hackborna4972e92012-03-14 10:38:05 -07001076 /**
Scott Main87bff972013-02-27 15:46:34 -08001077 * Call {@link Activity#startActivity(Intent, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001078 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001079 *
1080 * @param intent The intent to start.
1081 * @param options Additional options for how the Activity should be started.
1082 * See {@link android.content.Context#startActivity(Intent, Bundle)
1083 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07001084 */
1085 public void startActivity(Intent intent, Bundle options) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001086 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001087 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1088 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001089 if (options != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001090 mHost.onStartActivityFromFragment(this, intent, -1, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07001091 } else {
1092 // Note we want to go through this call for compatibility with
1093 // applications that may have overridden the method.
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001094 mHost.onStartActivityFromFragment(this, intent, -1, null /*options*/);
Dianne Hackborna4972e92012-03-14 10:38:05 -07001095 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001096 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001097
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001098 /**
Scott Main87bff972013-02-27 15:46:34 -08001099 * Call {@link Activity#startActivityForResult(Intent, int)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001100 * containing Activity.
1101 */
1102 public void startActivityForResult(Intent intent, int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001103 startActivityForResult(intent, requestCode, null);
1104 }
1105
1106 /**
Scott Main87bff972013-02-27 15:46:34 -08001107 * Call {@link Activity#startActivityForResult(Intent, int, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001108 * containing Activity.
1109 */
1110 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001111 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001112 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1113 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001114 mHost.onStartActivityFromFragment(this, intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001115 }
George Mountc03da0e2014-08-22 17:04:02 -07001116
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001117 /**
1118 * Receive the result from a previous call to
1119 * {@link #startActivityForResult(Intent, int)}. This follows the
1120 * related Activity API as described there in
1121 * {@link Activity#onActivityResult(int, int, Intent)}.
George Mountc03da0e2014-08-22 17:04:02 -07001122 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001123 * @param requestCode The integer request code originally supplied to
1124 * startActivityForResult(), allowing you to identify who this
1125 * result came from.
1126 * @param resultCode The integer result code returned by the child activity
1127 * through its setResult().
1128 * @param data An Intent, which can return result data to the caller
1129 * (various data can be attached to Intent "extras").
1130 */
1131 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1132 }
George Mountc03da0e2014-08-22 17:04:02 -07001133
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001134 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08001135 * Requests permissions to be granted to this application. These permissions
1136 * must be requested in your manifest, they should not be granted to your app,
1137 * and they should have protection level {@link android.content.pm.PermissionInfo
1138 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
1139 * the platform or a third-party app.
1140 * <p>
1141 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
1142 * are granted at install time if requested in the manifest. Signature permissions
1143 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
1144 * install time if requested in the manifest and the signature of your app matches
1145 * the signature of the app declaring the permissions.
1146 * </p>
1147 * <p>
1148 * If your app does not have the requested permissions the user will be presented
1149 * with UI for accepting them. After the user has accepted or rejected the
1150 * requested permissions you will receive a callback on {@link
1151 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
1152 * permissions were granted or not.
1153 * </p>
1154 * <p>
1155 * Note that requesting a permission does not guarantee it will be granted and
1156 * your app should be able to run without having this permission.
1157 * </p>
1158 * <p>
1159 * This method may start an activity allowing the user to choose which permissions
1160 * to grant and which to reject. Hence, you should be prepared that your activity
1161 * may be paused and resumed. Further, granting some permissions may require
1162 * a restart of you application. In such a case, the system will recreate the
1163 * activity stack before delivering the result to {@link
1164 * #onRequestPermissionsResult(int, String[], int[])}.
1165 * </p>
1166 * <p>
1167 * When checking whether you have a permission you should use {@link
1168 * android.content.Context#checkSelfPermission(String)}.
1169 * </p>
1170 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07001171 * Calling this API for permissions already granted to your app would show UI
1172 * to the user to decide whether the app can still hold these permissions. This
1173 * can be useful if the way your app uses data guarded by the permissions
1174 * changes significantly.
1175 * </p>
1176 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07001177 * You cannot request a permission if your activity sets {@link
1178 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
1179 * <code>true</code> because in this case the activity would not receive
1180 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
1181 * </p>
1182 * <p>
Svetoslavc6d1c342015-02-26 14:44:43 -08001183 * A sample permissions request looks like this:
1184 * </p>
1185 * <code><pre><p>
1186 * private void showContacts() {
1187 * if (getActivity().checkSelfPermission(Manifest.permission.READ_CONTACTS)
1188 * != PackageManager.PERMISSION_GRANTED) {
1189 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
1190 * PERMISSIONS_REQUEST_READ_CONTACTS);
1191 * } else {
1192 * doShowContacts();
1193 * }
1194 * }
1195 *
1196 * {@literal @}Override
1197 * public void onRequestPermissionsResult(int requestCode, String[] permissions,
1198 * int[] grantResults) {
1199 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS
1200 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
1201 * doShowContacts();
1202 * }
1203 * }
1204 * </code></pre></p>
1205 *
Svet Ganovf66381c2016-02-18 20:02:36 -08001206 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08001207 * @param requestCode Application specific request code to match with a result
1208 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07001209 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08001210 *
1211 * @see #onRequestPermissionsResult(int, String[], int[])
1212 * @see android.content.Context#checkSelfPermission(String)
1213 */
1214 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001215 if (mHost == null) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001216 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1217 }
Svetoslav970b59c2015-06-09 16:05:21 -07001218 mHost.onRequestPermissionsFromFragment(this, permissions,requestCode);
Svetoslavc6d1c342015-02-26 14:44:43 -08001219 }
1220
1221 /**
1222 * Callback for the result from requesting permissions. This method
1223 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07001224 * <p>
1225 * <strong>Note:</strong> It is possible that the permissions request interaction
1226 * with the user is interrupted. In this case you will receive empty permissions
1227 * and results arrays which should be treated as a cancellation.
1228 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08001229 *
1230 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
1231 * @param permissions The requested permissions. Never null.
1232 * @param grantResults The grant results for the corresponding permissions
1233 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
1234 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
1235 *
1236 * @see #requestPermissions(String[], int)
1237 */
1238 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
1239 @NonNull int[] grantResults) {
1240 /* callback - do nothing */
1241 }
1242
1243 /**
Svetoslava2011d22015-06-03 15:43:59 -07001244 * Gets whether you should show UI with rationale for requesting a permission.
1245 * You should do this only if you do not have the permission and the context in
1246 * which the permission is requested does not clearly communicate to the user
1247 * what would be the benefit from granting this permission.
1248 * <p>
1249 * For example, if you write a camera app, requesting the camera permission
1250 * would be expected by the user and no rationale for why it is requested is
1251 * needed. If however, the app needs location for tagging photos then a non-tech
1252 * savvy user may wonder how location is related to taking photos. In this case
1253 * you may choose to show UI with rationale of requesting this permission.
1254 * </p>
1255 *
1256 * @param permission A permission your app wants to request.
1257 * @return Whether you can show permission rationale UI.
1258 *
1259 * @see Context#checkSelfPermission(String)
1260 * @see #requestPermissions(String[], int)
1261 * @see #onRequestPermissionsResult(int, String[], int[])
1262 */
1263 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
1264 if (mHost != null) {
Svet Ganov9e136b82015-07-16 09:58:38 -07001265 return mHost.getContext().getPackageManager()
1266 .shouldShowRequestPermissionRationale(permission);
Svetoslava2011d22015-06-03 15:43:59 -07001267 }
1268 return false;
1269 }
1270
1271 /**
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001272 * @hide Hack so that DialogFragment can make its Dialog before creating
1273 * its views, and the view construction can use the dialog's context for
1274 * inflation. Maybe this should become a public API. Note sure.
1275 */
1276 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001277 final LayoutInflater result = mHost.onGetLayoutInflater();
1278 if (mHost.onUseFragmentManagerInflaterFactory()) {
Adam Powell371a8092014-06-20 12:51:12 -07001279 getChildFragmentManager(); // Init if needed; use raw implementation below.
1280 result.setPrivateFactory(mChildFragmentManager.getLayoutInflaterFactory());
Adam Powell371a8092014-06-20 12:51:12 -07001281 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001282 return result;
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001283 }
George Mountc03da0e2014-08-22 17:04:02 -07001284
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001285 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001286 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead.
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001287 */
1288 @Deprecated
1289 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
1290 mCalled = true;
1291 }
1292
1293 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001294 * Called when a fragment is being created as part of a view layout
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001295 * inflation, typically from setting the content view of an activity. This
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001296 * may be called immediately after the fragment is created from a <fragment>
Dianne Hackborndef15372010-08-15 12:43:52 -07001297 * tag in a layout file. Note this is <em>before</em> the fragment's
1298 * {@link #onAttach(Activity)} has been called; all you should do here is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001299 * parse the attributes and save them away.
George Mountc03da0e2014-08-22 17:04:02 -07001300 *
Dianne Hackborndef15372010-08-15 12:43:52 -07001301 * <p>This is called every time the fragment is inflated, even if it is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001302 * being inflated into a new instance with saved state. It typically makes
1303 * sense to re-parse the parameters each time, to allow them to change with
1304 * different configurations.</p>
1305 *
1306 * <p>Here is a typical implementation of a fragment that can take parameters
1307 * both through attributes supplied here as well from {@link #getArguments()}:</p>
1308 *
1309 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1310 * fragment}
1311 *
1312 * <p>Note that parsing the XML attributes uses a "styleable" resource. The
1313 * declaration for the styleable used here is:</p>
1314 *
1315 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments}
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001316 *
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001317 * <p>The fragment can then be declared within its activity's content layout
1318 * through a tag like this:</p>
1319 *
1320 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes}
1321 *
1322 * <p>This fragment can also be created dynamically from arguments given
1323 * at runtime in the arguments Bundle; here is an example of doing so at
1324 * creation of the containing activity:</p>
1325 *
1326 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1327 * create}
1328 *
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001329 * @param context The Context that is inflating this fragment.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001330 * @param attrs The attributes at the tag where the fragment is
1331 * being created.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001332 * @param savedInstanceState If the fragment is being re-created from
1333 * a previous saved state, this is the state.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001334 */
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001335 public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001336 onInflate(attrs, savedInstanceState);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001337 mCalled = true;
George Mountc03da0e2014-08-22 17:04:02 -07001338
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001339 TypedArray a = context.obtainStyledAttributes(attrs,
George Mountc03da0e2014-08-22 17:04:02 -07001340 com.android.internal.R.styleable.Fragment);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001341 mEnterTransition = loadTransition(context, a, mEnterTransition, null,
George Mountc03da0e2014-08-22 17:04:02 -07001342 com.android.internal.R.styleable.Fragment_fragmentEnterTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001343 mReturnTransition = loadTransition(context, a, mReturnTransition, USE_DEFAULT_TRANSITION,
George Mountc03da0e2014-08-22 17:04:02 -07001344 com.android.internal.R.styleable.Fragment_fragmentReturnTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001345 mExitTransition = loadTransition(context, a, mExitTransition, null,
George Mountc03da0e2014-08-22 17:04:02 -07001346 com.android.internal.R.styleable.Fragment_fragmentExitTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001347 mReenterTransition = loadTransition(context, a, mReenterTransition, USE_DEFAULT_TRANSITION,
George Mountc03da0e2014-08-22 17:04:02 -07001348 com.android.internal.R.styleable.Fragment_fragmentReenterTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001349 mSharedElementEnterTransition = loadTransition(context, a, mSharedElementEnterTransition,
George Mountc03da0e2014-08-22 17:04:02 -07001350 null, com.android.internal.R.styleable.Fragment_fragmentSharedElementEnterTransition);
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001351 mSharedElementReturnTransition = loadTransition(context, a, mSharedElementReturnTransition,
George Mountc03da0e2014-08-22 17:04:02 -07001352 USE_DEFAULT_TRANSITION,
1353 com.android.internal.R.styleable.Fragment_fragmentSharedElementReturnTransition);
1354 if (mAllowEnterTransitionOverlap == null) {
1355 mAllowEnterTransitionOverlap = a.getBoolean(
1356 com.android.internal.R.styleable.Fragment_fragmentAllowEnterTransitionOverlap, true);
1357 }
1358 if (mAllowReturnTransitionOverlap == null) {
1359 mAllowReturnTransitionOverlap = a.getBoolean(
1360 com.android.internal.R.styleable.Fragment_fragmentAllowReturnTransitionOverlap, true);
1361 }
1362 a.recycle();
Todd Kennedy434bd652015-05-04 12:29:50 -07001363
1364 final Activity hostActivity = mHost == null ? null : mHost.getActivity();
1365 if (hostActivity != null) {
1366 mCalled = false;
1367 onInflate(hostActivity, attrs, savedInstanceState);
1368 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001369 }
George Mountc03da0e2014-08-22 17:04:02 -07001370
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001371 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001372 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead.
1373 */
1374 @Deprecated
1375 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
1376 mCalled = true;
1377 }
1378
1379 /**
1380 * Called when a fragment is first attached to its context.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001381 * {@link #onCreate(Bundle)} will be called after this.
1382 */
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001383 public void onAttach(Context context) {
1384 mCalled = true;
1385 final Activity hostActivity = mHost == null ? null : mHost.getActivity();
1386 if (hostActivity != null) {
1387 mCalled = false;
1388 onAttach(hostActivity);
1389 }
1390 }
1391
1392 /**
1393 * @deprecated Use {@link #onAttach(Context)} instead.
1394 */
1395 @Deprecated
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001396 public void onAttach(Activity activity) {
1397 mCalled = true;
1398 }
George Mountc03da0e2014-08-22 17:04:02 -07001399
Chet Haase811ed1062010-08-06 10:38:15 -07001400 /**
1401 * Called when a fragment loads an animation.
1402 */
Chet Haasea18a86b2010-09-07 13:20:00 -07001403 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001404 return null;
1405 }
George Mountc03da0e2014-08-22 17:04:02 -07001406
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001407 /**
1408 * Called to do initial creation of a fragment. This is called after
Dianne Hackbornc8017682010-07-06 13:34:38 -07001409 * {@link #onAttach(Activity)} and before
Felipe Lemeb1444572015-11-03 17:39:07 -08001410 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, but is not called if the fragment
1411 * instance is retained across Activity re-creation (see {@link #setRetainInstance(boolean)}).
George Mountc03da0e2014-08-22 17:04:02 -07001412 *
Dianne Hackbornc8017682010-07-06 13:34:38 -07001413 * <p>Note that this can be called while the fragment's activity is
1414 * still in the process of being created. As such, you can not rely
1415 * on things like the activity's content view hierarchy being initialized
1416 * at this point. If you want to do work once the activity itself is
1417 * created, see {@link #onActivityCreated(Bundle)}.
George Mountc03da0e2014-08-22 17:04:02 -07001418 *
Adam Powell592df7d2016-01-14 12:58:23 -08001419 * <p>If your app's <code>targetSdkVersion</code> is 23 or lower, child fragments
1420 * being restored from the savedInstanceState are restored after <code>onCreate</code>
1421 * returns. When targeting N or above and running on an N or newer platform version
1422 * they are restored by <code>Fragment.onCreate</code>.</p>
1423 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001424 * @param savedInstanceState If the fragment is being re-created from
1425 * a previous saved state, this is the state.
1426 */
Scott Kennedy3c32b622015-02-22 16:58:58 -08001427 public void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001428 mCalled = true;
Adam Powell592df7d2016-01-14 12:58:23 -08001429 final Context context = getContext();
1430 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
1431 if (version >= Build.VERSION_CODES.N) {
Adam Powelleacacb52016-03-23 13:07:27 -07001432 restoreChildFragmentState(savedInstanceState, true);
1433 }
1434 }
1435
1436 void restoreChildFragmentState(@Nullable Bundle savedInstanceState, boolean provideNonConfig) {
1437 if (savedInstanceState != null) {
1438 Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG);
1439 if (p != null) {
1440 if (mChildFragmentManager == null) {
1441 instantiateChildFragmentManager();
Adam Powell592df7d2016-01-14 12:58:23 -08001442 }
Adam Powelleacacb52016-03-23 13:07:27 -07001443 mChildFragmentManager.restoreAllState(p, provideNonConfig ? mChildNonConfig : null);
1444 mChildNonConfig = null;
1445 mChildFragmentManager.dispatchCreate();
Adam Powell592df7d2016-01-14 12:58:23 -08001446 }
1447 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001448 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001449
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001450 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001451 * Called to have the fragment instantiate its user interface view.
1452 * This is optional, and non-graphical fragments can return null (which
1453 * is the default implementation). This will be called between
Dianne Hackbornc8017682010-07-06 13:34:38 -07001454 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
George Mountc03da0e2014-08-22 17:04:02 -07001455 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001456 * <p>If you return a View from here, you will later be called in
1457 * {@link #onDestroyView} when the view is being released.
George Mountc03da0e2014-08-22 17:04:02 -07001458 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001459 * @param inflater The LayoutInflater object that can be used to inflate
1460 * any views in the fragment,
1461 * @param container If non-null, this is the parent view that the fragment's
1462 * UI should be attached to. The fragment should not add the view itself,
1463 * but this can be used to generate the LayoutParams of the view.
1464 * @param savedInstanceState If non-null, this fragment is being re-constructed
1465 * from a previous saved state as given here.
George Mountc03da0e2014-08-22 17:04:02 -07001466 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001467 * @return Return the View for the fragment's UI, or null.
1468 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001469 @Nullable
1470 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001471 Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001472 return null;
1473 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001474
1475 /**
1476 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
1477 * has returned, but before any saved state has been restored in to the view.
1478 * This gives subclasses a chance to initialize themselves once
1479 * they know their view hierarchy has been completely created. The fragment's
1480 * view hierarchy is not however attached to its parent at this point.
1481 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1482 * @param savedInstanceState If non-null, this fragment is being re-constructed
1483 * from a previous saved state as given here.
1484 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001485 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001486 }
George Mountc03da0e2014-08-22 17:04:02 -07001487
Scott Main662cc7a42011-01-17 15:02:07 -08001488 /**
1489 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
1490 * if provided.
George Mountc03da0e2014-08-22 17:04:02 -07001491 *
Scott Main662cc7a42011-01-17 15:02:07 -08001492 * @return The fragment's root view, or null if it has no layout.
1493 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001494 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001495 public View getView() {
1496 return mView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001497 }
George Mountc03da0e2014-08-22 17:04:02 -07001498
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001499 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001500 * Called when the fragment's activity has been created and this
1501 * fragment's view hierarchy instantiated. It can be used to do final
1502 * initialization once these pieces are in place, such as retrieving
1503 * views or restoring state. It is also useful for fragments that use
1504 * {@link #setRetainInstance(boolean)} to retain their instance,
1505 * as this callback tells the fragment when it is fully associated with
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001506 * the new activity instance. This is called after {@link #onCreateView}
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001507 * and before {@link #onViewStateRestored(Bundle)}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001508 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001509 * @param savedInstanceState If the fragment is being re-created from
1510 * a previous saved state, this is the state.
1511 */
Scott Kennedy3c32b622015-02-22 16:58:58 -08001512 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001513 mCalled = true;
1514 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001515
1516 /**
1517 * Called when all saved state has been restored into the view hierarchy
1518 * of the fragment. This can be used to do initialization based on saved
1519 * state that you are letting the view hierarchy track itself, such as
1520 * whether check box widgets are currently checked. This is called
1521 * after {@link #onActivityCreated(Bundle)} and before
1522 * {@link #onStart()}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001523 *
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001524 * @param savedInstanceState If the fragment is being re-created from
1525 * a previous saved state, this is the state.
1526 */
1527 public void onViewStateRestored(Bundle savedInstanceState) {
1528 mCalled = true;
1529 }
1530
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001531 /**
1532 * Called when the Fragment is visible to the user. This is generally
1533 * tied to {@link Activity#onStart() Activity.onStart} of the containing
1534 * Activity's lifecycle.
1535 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001536 public void onStart() {
1537 mCalled = true;
George Mountc03da0e2014-08-22 17:04:02 -07001538
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001539 if (!mLoadersStarted) {
1540 mLoadersStarted = true;
1541 if (!mCheckedForLoaderManager) {
1542 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001543 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001544 }
1545 if (mLoaderManager != null) {
1546 mLoaderManager.doStart();
1547 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001548 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001549 }
George Mountc03da0e2014-08-22 17:04:02 -07001550
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001551 /**
1552 * Called when the fragment is visible to the user and actively running.
1553 * This is generally
1554 * tied to {@link Activity#onResume() Activity.onResume} of the containing
1555 * Activity's lifecycle.
1556 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001557 public void onResume() {
1558 mCalled = true;
1559 }
George Mountc03da0e2014-08-22 17:04:02 -07001560
Dianne Hackborn72778202010-08-20 18:26:01 -07001561 /**
1562 * Called to ask the fragment to save its current dynamic state, so it
1563 * can later be reconstructed in a new instance of its process is
1564 * restarted. If a new instance of the fragment later needs to be
1565 * created, the data you place in the Bundle here will be available
1566 * in the Bundle given to {@link #onCreate(Bundle)},
1567 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
1568 * {@link #onActivityCreated(Bundle)}.
1569 *
1570 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
Daisuke Miyakawa2f761762010-09-12 16:53:17 -07001571 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
Dianne Hackborn72778202010-08-20 18:26:01 -07001572 * applies here as well. Note however: <em>this method may be called
1573 * at any time before {@link #onDestroy()}</em>. There are many situations
1574 * where a fragment may be mostly torn down (such as when placed on the
1575 * back stack with no UI showing), but its state will not be saved until
1576 * its owning activity actually needs to save its state.
1577 *
1578 * @param outState Bundle in which to place your saved state.
1579 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001580 public void onSaveInstanceState(Bundle outState) {
1581 }
George Mountc03da0e2014-08-22 17:04:02 -07001582
Wale Ogunwale7c796812016-01-29 21:13:50 -08001583 /**
1584 * Called when the Fragment's activity changes from fullscreen mode to multi-window mode and
1585 * visa-versa. This is generally tied to {@link Activity#onMultiWindowChanged} of the containing
1586 * Activity.
1587 *
1588 * @param inMultiWindow True if the activity is in multi-window mode.
1589 */
1590 public void onMultiWindowChanged(boolean inMultiWindow) {
1591 }
1592
1593 /**
1594 * Called by the system when the activity changes to and from picture-in-picture mode. This is
1595 * generally tied to {@link Activity#onPictureInPictureChanged} of the containing Activity.
1596 *
1597 * @param inPictureInPicture True if the activity is in picture-in-picture mode.
1598 */
1599 public void onPictureInPictureChanged(boolean inPictureInPicture) {
1600 }
1601
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001602 public void onConfigurationChanged(Configuration newConfig) {
1603 mCalled = true;
1604 }
George Mountc03da0e2014-08-22 17:04:02 -07001605
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001606 /**
1607 * Called when the Fragment is no longer resumed. This is generally
1608 * tied to {@link Activity#onPause() Activity.onPause} of the containing
1609 * Activity's lifecycle.
1610 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001611 public void onPause() {
1612 mCalled = true;
1613 }
George Mountc03da0e2014-08-22 17:04:02 -07001614
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001615 /**
1616 * Called when the Fragment is no longer started. This is generally
1617 * tied to {@link Activity#onStop() Activity.onStop} of the containing
1618 * Activity's lifecycle.
1619 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001620 public void onStop() {
1621 mCalled = true;
1622 }
George Mountc03da0e2014-08-22 17:04:02 -07001623
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001624 public void onLowMemory() {
1625 mCalled = true;
1626 }
George Mountc03da0e2014-08-22 17:04:02 -07001627
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001628 public void onTrimMemory(int level) {
1629 mCalled = true;
1630 }
1631
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001632 /**
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001633 * Called when the view previously created by {@link #onCreateView} has
1634 * been detached from the fragment. The next time the fragment needs
1635 * to be displayed, a new view will be created. This is called
Dianne Hackborndef15372010-08-15 12:43:52 -07001636 * after {@link #onStop()} and before {@link #onDestroy()}. It is called
1637 * <em>regardless</em> of whether {@link #onCreateView} returned a
1638 * non-null view. Internally it is called after the view's state has
1639 * been saved but before it has been removed from its parent.
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001640 */
1641 public void onDestroyView() {
1642 mCalled = true;
1643 }
George Mountc03da0e2014-08-22 17:04:02 -07001644
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001645 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001646 * Called when the fragment is no longer in use. This is called
1647 * after {@link #onStop()} and before {@link #onDetach()}.
1648 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001649 public void onDestroy() {
1650 mCalled = true;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001651 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager
1652 // + " mLoaderManager=" + mLoaderManager);
1653 if (!mCheckedForLoaderManager) {
1654 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001655 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001656 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001657 if (mLoaderManager != null) {
1658 mLoaderManager.doDestroy();
1659 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001660 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001661
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001662 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001663 * Called by the fragment manager once this fragment has been removed,
1664 * so that we don't have any left-over state if the application decides
1665 * to re-use the instance. This only clears state that the framework
1666 * internally manages, not things the application sets.
1667 */
1668 void initState() {
1669 mIndex = -1;
1670 mWho = null;
1671 mAdded = false;
1672 mRemoving = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001673 mFromLayout = false;
1674 mInLayout = false;
1675 mRestored = false;
1676 mBackStackNesting = 0;
1677 mFragmentManager = null;
Tim Kilbourn70717862014-02-28 01:00:06 -08001678 mChildFragmentManager = null;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001679 mHost = null;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001680 mFragmentId = 0;
1681 mContainerId = 0;
1682 mTag = null;
1683 mHidden = false;
1684 mDetached = false;
1685 mRetaining = false;
1686 mLoaderManager = null;
1687 mLoadersStarted = false;
1688 mCheckedForLoaderManager = false;
1689 }
1690
1691 /**
Felipe Lemeb1444572015-11-03 17:39:07 -08001692 * Called when the fragment is no longer attached to its activity. This is called after
1693 * {@link #onDestroy()}, except in the cases where the fragment instance is retained across
1694 * Activity re-creation (see {@link #setRetainInstance(boolean)}), in which case it is called
1695 * after {@link #onStop()}.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001696 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001697 public void onDetach() {
1698 mCalled = true;
Adam Powelleacacb52016-03-23 13:07:27 -07001699
1700 // Destroy the child FragmentManager if we still have it here.
1701 // We won't unless we're retaining our instance and if we do,
1702 // our child FragmentManager instance state will have already been saved.
1703 if (mChildFragmentManager != null) {
1704 if (!mRetaining) {
1705 throw new IllegalStateException("Child FragmentManager of " + this + " was not "
1706 + " destroyed and this fragment is not retaining instance");
1707 }
1708 mChildFragmentManager.dispatchDestroy();
1709 mChildFragmentManager = null;
1710 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001711 }
George Mountc03da0e2014-08-22 17:04:02 -07001712
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001713 /**
1714 * Initialize the contents of the Activity's standard options menu. You
1715 * should place your menu items in to <var>menu</var>. For this method
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001716 * to be called, you must have first called {@link #setHasOptionsMenu}. See
1717 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
1718 * for more information.
George Mountc03da0e2014-08-22 17:04:02 -07001719 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001720 * @param menu The options menu in which you place your items.
George Mountc03da0e2014-08-22 17:04:02 -07001721 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001722 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001723 * @see #onPrepareOptionsMenu
1724 * @see #onOptionsItemSelected
1725 */
1726 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1727 }
1728
1729 /**
1730 * Prepare the Screen's standard options menu to be displayed. This is
1731 * called right before the menu is shown, every time it is shown. You can
1732 * use this method to efficiently enable/disable items or otherwise
1733 * dynamically modify the contents. See
1734 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu}
1735 * for more information.
George Mountc03da0e2014-08-22 17:04:02 -07001736 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001737 * @param menu The options menu as last shown or first initialized by
1738 * onCreateOptionsMenu().
George Mountc03da0e2014-08-22 17:04:02 -07001739 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001740 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001741 * @see #onCreateOptionsMenu
1742 */
1743 public void onPrepareOptionsMenu(Menu menu) {
1744 }
1745
1746 /**
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001747 * Called when this fragment's option menu items are no longer being
1748 * included in the overall options menu. Receiving this call means that
1749 * the menu needed to be rebuilt, but this fragment's items were not
1750 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
1751 * was not called).
1752 */
1753 public void onDestroyOptionsMenu() {
1754 }
George Mountc03da0e2014-08-22 17:04:02 -07001755
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001756 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001757 * This hook is called whenever an item in your options menu is selected.
1758 * The default implementation simply returns false to have the normal
1759 * processing happen (calling the item's Runnable or sending a message to
1760 * its Handler as appropriate). You can use this method for any items
1761 * for which you would like to do processing without those other
1762 * facilities.
George Mountc03da0e2014-08-22 17:04:02 -07001763 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001764 * <p>Derived classes should call through to the base class for it to
1765 * perform the default menu handling.
George Mountc03da0e2014-08-22 17:04:02 -07001766 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001767 * @param item The menu item that was selected.
George Mountc03da0e2014-08-22 17:04:02 -07001768 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001769 * @return boolean Return false to allow normal menu processing to
1770 * proceed, true to consume it here.
George Mountc03da0e2014-08-22 17:04:02 -07001771 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001772 * @see #onCreateOptionsMenu
1773 */
1774 public boolean onOptionsItemSelected(MenuItem item) {
1775 return false;
1776 }
1777
1778 /**
1779 * This hook is called whenever the options menu is being closed (either by the user canceling
1780 * the menu with the back/menu button, or when an item is selected).
George Mountc03da0e2014-08-22 17:04:02 -07001781 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001782 * @param menu The options menu as last shown or first initialized by
1783 * onCreateOptionsMenu().
1784 */
1785 public void onOptionsMenuClosed(Menu menu) {
1786 }
George Mountc03da0e2014-08-22 17:04:02 -07001787
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001788 /**
1789 * Called when a context menu for the {@code view} is about to be shown.
1790 * Unlike {@link #onCreateOptionsMenu}, this will be called every
1791 * time the context menu is about to be shown and should be populated for
1792 * the view (or item inside the view for {@link AdapterView} subclasses,
1793 * this can be found in the {@code menuInfo})).
1794 * <p>
1795 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
1796 * item has been selected.
1797 * <p>
1798 * The default implementation calls up to
1799 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
1800 * you can not call this implementation if you don't want that behavior.
1801 * <p>
1802 * It is not safe to hold onto the context menu after this method returns.
1803 * {@inheritDoc}
1804 */
1805 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1806 getActivity().onCreateContextMenu(menu, v, menuInfo);
1807 }
1808
1809 /**
1810 * Registers a context menu to be shown for the given view (multiple views
1811 * can show the context menu). This method will set the
1812 * {@link OnCreateContextMenuListener} on the view to this fragment, so
1813 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
1814 * called when it is time to show the context menu.
George Mountc03da0e2014-08-22 17:04:02 -07001815 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001816 * @see #unregisterForContextMenu(View)
1817 * @param view The view that should show a context menu.
1818 */
1819 public void registerForContextMenu(View view) {
1820 view.setOnCreateContextMenuListener(this);
1821 }
George Mountc03da0e2014-08-22 17:04:02 -07001822
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001823 /**
1824 * Prevents a context menu to be shown for the given view. This method will
1825 * remove the {@link OnCreateContextMenuListener} on the view.
George Mountc03da0e2014-08-22 17:04:02 -07001826 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001827 * @see #registerForContextMenu(View)
1828 * @param view The view that should stop showing a context menu.
1829 */
1830 public void unregisterForContextMenu(View view) {
1831 view.setOnCreateContextMenuListener(null);
1832 }
George Mountc03da0e2014-08-22 17:04:02 -07001833
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001834 /**
1835 * This hook is called whenever an item in a context menu is selected. The
1836 * default implementation simply returns false to have the normal processing
1837 * happen (calling the item's Runnable or sending a message to its Handler
1838 * as appropriate). You can use this method for any items for which you
1839 * would like to do processing without those other facilities.
1840 * <p>
1841 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
1842 * View that added this menu item.
1843 * <p>
1844 * Derived classes should call through to the base class for it to perform
1845 * the default menu handling.
George Mountc03da0e2014-08-22 17:04:02 -07001846 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001847 * @param item The context menu item that was selected.
1848 * @return boolean Return false to allow normal context menu processing to
1849 * proceed, true to consume it here.
1850 */
1851 public boolean onContextItemSelected(MenuItem item) {
1852 return false;
1853 }
George Mountc03da0e2014-08-22 17:04:02 -07001854
1855 /**
George Mount65580562014-08-29 08:15:48 -07001856 * When custom transitions are used with Fragments, the enter transition callback
George Mountc03da0e2014-08-22 17:04:02 -07001857 * is called when this Fragment is attached or detached when not popping the back stack.
1858 *
George Mount65580562014-08-29 08:15:48 -07001859 * @param callback Used to manipulate the shared element transitions on this Fragment
George Mountc03da0e2014-08-22 17:04:02 -07001860 * when added not as a pop from the back stack.
1861 */
George Mount85ea4982014-09-12 14:16:18 -07001862 public void setEnterSharedElementCallback(SharedElementCallback callback) {
George Mount65580562014-08-29 08:15:48 -07001863 if (callback == null) {
1864 callback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -07001865 }
George Mount65580562014-08-29 08:15:48 -07001866 mEnterTransitionCallback = callback;
George Mountc03da0e2014-08-22 17:04:02 -07001867 }
1868
1869 /**
George Mount85ea4982014-09-12 14:16:18 -07001870 * @hide
1871 */
1872 public void setEnterSharedElementTransitionCallback(SharedElementCallback callback) {
1873 setEnterSharedElementCallback(callback);
1874 }
1875
1876 /**
George Mount65580562014-08-29 08:15:48 -07001877 * When custom transitions are used with Fragments, the exit transition callback
George Mountc03da0e2014-08-22 17:04:02 -07001878 * is called when this Fragment is attached or detached when popping the back stack.
1879 *
George Mount65580562014-08-29 08:15:48 -07001880 * @param callback Used to manipulate the shared element transitions on this Fragment
George Mountc03da0e2014-08-22 17:04:02 -07001881 * when added as a pop from the back stack.
1882 */
George Mount85ea4982014-09-12 14:16:18 -07001883 public void setExitSharedElementCallback(SharedElementCallback callback) {
George Mount65580562014-08-29 08:15:48 -07001884 if (callback == null) {
1885 callback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -07001886 }
George Mount65580562014-08-29 08:15:48 -07001887 mExitTransitionCallback = callback;
George Mountc03da0e2014-08-22 17:04:02 -07001888 }
1889
1890 /**
George Mount85ea4982014-09-12 14:16:18 -07001891 * @hide
1892 */
1893 public void setExitSharedElementTransitionCallback(SharedElementCallback callback) {
1894 setExitSharedElementCallback(callback);
1895 }
1896
1897 /**
George Mountc03da0e2014-08-22 17:04:02 -07001898 * Sets the Transition that will be used to move Views into the initial scene. The entering
1899 * Views will be those that are regular Views or ViewGroups that have
1900 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1901 * {@link android.transition.Visibility} as entering is governed by changing visibility from
1902 * {@link View#INVISIBLE} to {@link View#VISIBLE}. If <code>transition</code> is null,
1903 * entering Views will remain unaffected.
1904 *
1905 * @param transition The Transition to use to move Views into the initial Scene.
1906 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition
1907 */
1908 public void setEnterTransition(Transition transition) {
1909 mEnterTransition = transition;
1910 }
1911
1912 /**
1913 * Returns the Transition that will be used to move Views into the initial scene. The entering
1914 * Views will be those that are regular Views or ViewGroups that have
1915 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1916 * {@link android.transition.Visibility} as entering is governed by changing visibility from
1917 * {@link View#INVISIBLE} to {@link View#VISIBLE}.
1918 *
1919 * @return the Transition to use to move Views into the initial Scene.
1920 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition
1921 */
1922 public Transition getEnterTransition() {
1923 return mEnterTransition;
1924 }
1925
1926 /**
1927 * Sets the Transition that will be used to move Views out of the scene when the Fragment is
1928 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting
1929 * Views will be those that are regular Views or ViewGroups that have
1930 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1931 * {@link android.transition.Visibility} as entering is governed by changing visibility from
1932 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null,
1933 * entering Views will remain unaffected. If nothing is set, the default will be to
1934 * use the same value as set in {@link #setEnterTransition(android.transition.Transition)}.
1935 *
1936 * @param transition The Transition to use to move Views out of the Scene when the Fragment
1937 * is preparing to close.
1938 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
1939 */
1940 public void setReturnTransition(Transition transition) {
1941 mReturnTransition = transition;
1942 }
1943
1944 /**
1945 * Returns the Transition that will be used to move Views out of the scene when the Fragment is
1946 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting
1947 * Views will be those that are regular Views or ViewGroups that have
1948 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1949 * {@link android.transition.Visibility} as entering is governed by changing visibility from
1950 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null,
1951 * entering Views will remain unaffected.
1952 *
1953 * @return the Transition to use to move Views out of the Scene when the Fragment
1954 * is preparing to close.
1955 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
1956 */
1957 public Transition getReturnTransition() {
1958 return mReturnTransition == USE_DEFAULT_TRANSITION ? getEnterTransition()
1959 : mReturnTransition;
1960 }
1961
1962 /**
1963 * Sets the Transition that will be used to move Views out of the scene when the
1964 * fragment is removed, hidden, or detached when not popping the back stack.
1965 * The exiting Views will be those that are regular Views or ViewGroups that
1966 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1967 * {@link android.transition.Visibility} as exiting is governed by changing visibility
1968 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will
1969 * remain unaffected.
1970 *
1971 * @param transition The Transition to use to move Views out of the Scene when the Fragment
1972 * is being closed not due to popping the back stack.
1973 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
1974 */
1975 public void setExitTransition(Transition transition) {
1976 mExitTransition = transition;
1977 }
1978
1979 /**
1980 * Returns the Transition that will be used to move Views out of the scene when the
1981 * fragment is removed, hidden, or detached when not popping the back stack.
1982 * The exiting Views will be those that are regular Views or ViewGroups that
1983 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
1984 * {@link android.transition.Visibility} as exiting is governed by changing visibility
1985 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will
1986 * remain unaffected.
1987 *
1988 * @return the Transition to use to move Views out of the Scene when the Fragment
1989 * is being closed not due to popping the back stack.
1990 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
1991 */
1992 public Transition getExitTransition() {
1993 return mExitTransition;
1994 }
1995
1996 /**
1997 * Sets the Transition that will be used to move Views in to the scene when returning due
1998 * to popping a back stack. The entering Views will be those that are regular Views
1999 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions
2000 * will extend {@link android.transition.Visibility} as exiting is governed by changing
2001 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null,
2002 * the views will remain unaffected. If nothing is set, the default will be to use the same
2003 * transition as {@link #setExitTransition(android.transition.Transition)}.
2004 *
2005 * @param transition The Transition to use to move Views into the scene when reentering from a
2006 * previously-started Activity.
2007 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition
2008 */
2009 public void setReenterTransition(Transition transition) {
2010 mReenterTransition = transition;
2011 }
2012
2013 /**
2014 * Returns the Transition that will be used to move Views in to the scene when returning due
2015 * to popping a back stack. The entering Views will be those that are regular Views
2016 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions
2017 * will extend {@link android.transition.Visibility} as exiting is governed by changing
2018 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null,
2019 * the views will remain unaffected. If nothing is set, the default will be to use the same
2020 * transition as {@link #setExitTransition(android.transition.Transition)}.
2021 *
2022 * @return the Transition to use to move Views into the scene when reentering from a
2023 * previously-started Activity.
2024 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition
2025 */
2026 public Transition getReenterTransition() {
2027 return mReenterTransition == USE_DEFAULT_TRANSITION ? getExitTransition()
2028 : mReenterTransition;
2029 }
2030
2031 /**
2032 * Sets the Transition that will be used for shared elements transferred into the content
2033 * Scene. Typical Transitions will affect size and location, such as
2034 * {@link android.transition.ChangeBounds}. A null
2035 * value will cause transferred shared elements to blink to the final position.
2036 *
2037 * @param transition The Transition to use for shared elements transferred into the content
2038 * Scene.
2039 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition
2040 */
2041 public void setSharedElementEnterTransition(Transition transition) {
2042 mSharedElementEnterTransition = transition;
2043 }
2044
2045 /**
2046 * Returns the Transition that will be used for shared elements transferred into the content
2047 * Scene. Typical Transitions will affect size and location, such as
2048 * {@link android.transition.ChangeBounds}. A null
2049 * value will cause transferred shared elements to blink to the final position.
2050 *
2051 * @return The Transition to use for shared elements transferred into the content
2052 * Scene.
2053 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition
2054 */
2055 public Transition getSharedElementEnterTransition() {
2056 return mSharedElementEnterTransition;
2057 }
2058
2059 /**
2060 * Sets the Transition that will be used for shared elements transferred back during a
2061 * pop of the back stack. This Transition acts in the leaving Fragment.
2062 * Typical Transitions will affect size and location, such as
2063 * {@link android.transition.ChangeBounds}. A null
2064 * value will cause transferred shared elements to blink to the final position.
2065 * If no value is set, the default will be to use the same value as
2066 * {@link #setSharedElementEnterTransition(android.transition.Transition)}.
2067 *
2068 * @param transition The Transition to use for shared elements transferred out of the content
2069 * Scene.
2070 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition
2071 */
2072 public void setSharedElementReturnTransition(Transition transition) {
2073 mSharedElementReturnTransition = transition;
2074 }
2075
2076 /**
2077 * Return the Transition that will be used for shared elements transferred back during a
2078 * pop of the back stack. This Transition acts in the leaving Fragment.
2079 * Typical Transitions will affect size and location, such as
2080 * {@link android.transition.ChangeBounds}. A null
2081 * value will cause transferred shared elements to blink to the final position.
2082 * If no value is set, the default will be to use the same value as
2083 * {@link #setSharedElementEnterTransition(android.transition.Transition)}.
2084 *
2085 * @return The Transition to use for shared elements transferred out of the content
2086 * Scene.
2087 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition
2088 */
2089 public Transition getSharedElementReturnTransition() {
2090 return mSharedElementReturnTransition == USE_DEFAULT_TRANSITION ?
2091 getSharedElementEnterTransition() : mSharedElementReturnTransition;
2092 }
2093
2094 /**
2095 * Sets whether the the exit transition and enter transition overlap or not.
2096 * When true, the enter transition will start as soon as possible. When false, the
2097 * enter transition will wait until the exit transition completes before starting.
2098 *
2099 * @param allow true to start the enter transition when possible or false to
2100 * wait until the exiting transition completes.
2101 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap
2102 */
2103 public void setAllowEnterTransitionOverlap(boolean allow) {
2104 mAllowEnterTransitionOverlap = allow;
2105 }
2106
2107 /**
2108 * Returns whether the the exit transition and enter transition overlap or not.
2109 * When true, the enter transition will start as soon as possible. When false, the
2110 * enter transition will wait until the exit transition completes before starting.
2111 *
2112 * @return true when the enter transition should start as soon as possible or false to
2113 * when it should wait until the exiting transition completes.
2114 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap
2115 */
2116 public boolean getAllowEnterTransitionOverlap() {
2117 return (mAllowEnterTransitionOverlap == null) ? true : mAllowEnterTransitionOverlap;
2118 }
2119
2120 /**
2121 * Sets whether the the return transition and reenter transition overlap or not.
2122 * When true, the reenter transition will start as soon as possible. When false, the
2123 * reenter transition will wait until the return transition completes before starting.
2124 *
2125 * @param allow true to start the reenter transition when possible or false to wait until the
2126 * return transition completes.
2127 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap
2128 */
2129 public void setAllowReturnTransitionOverlap(boolean allow) {
2130 mAllowReturnTransitionOverlap = allow;
2131 }
2132
2133 /**
2134 * Returns whether the the return transition and reenter transition overlap or not.
2135 * When true, the reenter transition will start as soon as possible. When false, the
2136 * reenter transition will wait until the return transition completes before starting.
2137 *
2138 * @return true to start the reenter transition when possible or false to wait until the
2139 * return transition completes.
2140 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap
2141 */
2142 public boolean getAllowReturnTransitionOverlap() {
2143 return (mAllowReturnTransitionOverlap == null) ? true : mAllowReturnTransitionOverlap;
2144 }
2145
Dianne Hackborn625ac272010-09-17 18:29:22 -07002146 /**
2147 * Print the Fragments's state into the given stream.
2148 *
2149 * @param prefix Text to print at the front of each line.
2150 * @param fd The raw file descriptor that the dump is being sent to.
2151 * @param writer The PrintWriter to which you should dump your state. This will be
2152 * closed for you after you return.
2153 * @param args additional arguments to the dump request.
2154 */
2155 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08002156 writer.print(prefix); writer.print("mFragmentId=#");
George Mountc03da0e2014-08-22 17:04:02 -07002157 writer.print(Integer.toHexString(mFragmentId));
2158 writer.print(" mContainerId=#");
2159 writer.print(Integer.toHexString(mContainerId));
2160 writer.print(" mTag="); writer.println(mTag);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002161 writer.print(prefix); writer.print("mState="); writer.print(mState);
George Mountc03da0e2014-08-22 17:04:02 -07002162 writer.print(" mIndex="); writer.print(mIndex);
2163 writer.print(" mWho="); writer.print(mWho);
2164 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002165 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded);
George Mountc03da0e2014-08-22 17:04:02 -07002166 writer.print(" mRemoving="); writer.print(mRemoving);
George Mountc03da0e2014-08-22 17:04:02 -07002167 writer.print(" mFromLayout="); writer.print(mFromLayout);
2168 writer.print(" mInLayout="); writer.println(mInLayout);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002169 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
George Mountc03da0e2014-08-22 17:04:02 -07002170 writer.print(" mDetached="); writer.print(mDetached);
2171 writer.print(" mMenuVisible="); writer.print(mMenuVisible);
2172 writer.print(" mHasMenu="); writer.println(mHasMenu);
Dianne Hackborn6c285972011-08-29 16:53:49 -07002173 writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance);
George Mountc03da0e2014-08-22 17:04:02 -07002174 writer.print(" mRetaining="); writer.print(mRetaining);
2175 writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002176 if (mFragmentManager != null) {
2177 writer.print(prefix); writer.print("mFragmentManager=");
George Mountc03da0e2014-08-22 17:04:02 -07002178 writer.println(mFragmentManager);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002179 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002180 if (mHost != null) {
2181 writer.print(prefix); writer.print("mHost=");
2182 writer.println(mHost);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002183 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002184 if (mParentFragment != null) {
2185 writer.print(prefix); writer.print("mParentFragment=");
George Mountc03da0e2014-08-22 17:04:02 -07002186 writer.println(mParentFragment);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002187 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002188 if (mArguments != null) {
2189 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments);
2190 }
2191 if (mSavedFragmentState != null) {
2192 writer.print(prefix); writer.print("mSavedFragmentState=");
George Mountc03da0e2014-08-22 17:04:02 -07002193 writer.println(mSavedFragmentState);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002194 }
2195 if (mSavedViewState != null) {
2196 writer.print(prefix); writer.print("mSavedViewState=");
George Mountc03da0e2014-08-22 17:04:02 -07002197 writer.println(mSavedViewState);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002198 }
2199 if (mTarget != null) {
2200 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget);
George Mountc03da0e2014-08-22 17:04:02 -07002201 writer.print(" mTargetRequestCode=");
2202 writer.println(mTargetRequestCode);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002203 }
2204 if (mNextAnim != 0) {
2205 writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim);
2206 }
2207 if (mContainer != null) {
2208 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer);
2209 }
2210 if (mView != null) {
2211 writer.print(prefix); writer.print("mView="); writer.println(mView);
2212 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08002213 if (mAnimatingAway != null) {
2214 writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway);
2215 writer.print(prefix); writer.print("mStateAfterAnimating=");
George Mountc03da0e2014-08-22 17:04:02 -07002216 writer.println(mStateAfterAnimating);
Dianne Hackbornd173fa32010-12-23 13:58:22 -08002217 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002218 if (mLoaderManager != null) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08002219 writer.print(prefix); writer.println("Loader Manager:");
2220 mLoaderManager.dump(prefix + " ", fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002221 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002222 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002223 writer.print(prefix); writer.println("Child " + mChildFragmentManager + ":");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002224 mChildFragmentManager.dump(prefix + " ", fd, writer, args);
2225 }
2226 }
2227
2228 Fragment findFragmentByWho(String who) {
2229 if (who.equals(mWho)) {
2230 return this;
2231 }
2232 if (mChildFragmentManager != null) {
2233 return mChildFragmentManager.findFragmentByWho(who);
2234 }
2235 return null;
2236 }
2237
2238 void instantiateChildFragmentManager() {
2239 mChildFragmentManager = new FragmentManagerImpl();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002240 mChildFragmentManager.attachController(mHost, new FragmentContainer() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002241 @Override
Scott Kennedyc0519552015-02-11 15:33:10 -08002242 @Nullable
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002243 public View onFindViewById(int id) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002244 if (mView == null) {
2245 throw new IllegalStateException("Fragment does not have a view");
2246 }
2247 return mView.findViewById(id);
2248 }
George Mount0b26e4d2014-09-17 16:36:42 -07002249
2250 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002251 public boolean onHasView() {
George Mount0b26e4d2014-09-17 16:36:42 -07002252 return (mView != null);
2253 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002254 }, this);
2255 }
2256
2257 void performCreate(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002258 if (mChildFragmentManager != null) {
2259 mChildFragmentManager.noteStateNotSaved();
2260 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002261 mState = CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002262 mCalled = false;
2263 onCreate(savedInstanceState);
2264 if (!mCalled) {
2265 throw new SuperNotCalledException("Fragment " + this
2266 + " did not call through to super.onCreate()");
2267 }
Adam Powell592df7d2016-01-14 12:58:23 -08002268 final Context context = getContext();
2269 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
2270 if (version < Build.VERSION_CODES.N) {
Adam Powelleacacb52016-03-23 13:07:27 -07002271 restoreChildFragmentState(savedInstanceState, false);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002272 }
2273 }
2274
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002275 View performCreateView(LayoutInflater inflater, ViewGroup container,
2276 Bundle savedInstanceState) {
2277 if (mChildFragmentManager != null) {
2278 mChildFragmentManager.noteStateNotSaved();
2279 }
2280 return onCreateView(inflater, container, savedInstanceState);
2281 }
2282
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002283 void performActivityCreated(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002284 if (mChildFragmentManager != null) {
2285 mChildFragmentManager.noteStateNotSaved();
2286 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002287 mState = ACTIVITY_CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002288 mCalled = false;
2289 onActivityCreated(savedInstanceState);
2290 if (!mCalled) {
2291 throw new SuperNotCalledException("Fragment " + this
2292 + " did not call through to super.onActivityCreated()");
2293 }
2294 if (mChildFragmentManager != null) {
2295 mChildFragmentManager.dispatchActivityCreated();
2296 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002297 }
2298
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002299 void performStart() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002300 if (mChildFragmentManager != null) {
2301 mChildFragmentManager.noteStateNotSaved();
2302 mChildFragmentManager.execPendingActions();
2303 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002304 mState = STARTED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002305 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002306 onStart();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002307 if (!mCalled) {
2308 throw new SuperNotCalledException("Fragment " + this
2309 + " did not call through to super.onStart()");
2310 }
2311 if (mChildFragmentManager != null) {
2312 mChildFragmentManager.dispatchStart();
2313 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002314 if (mLoaderManager != null) {
2315 mLoaderManager.doReportStart();
2316 }
2317 }
2318
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002319 void performResume() {
2320 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002321 mChildFragmentManager.noteStateNotSaved();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002322 mChildFragmentManager.execPendingActions();
2323 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002324 mState = RESUMED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002325 mCalled = false;
2326 onResume();
2327 if (!mCalled) {
2328 throw new SuperNotCalledException("Fragment " + this
2329 + " did not call through to super.onResume()");
2330 }
2331 if (mChildFragmentManager != null) {
2332 mChildFragmentManager.dispatchResume();
2333 mChildFragmentManager.execPendingActions();
2334 }
2335 }
2336
Wale Ogunwale7c796812016-01-29 21:13:50 -08002337 void performMultiWindowChanged(boolean inMultiWindow) {
2338 onMultiWindowChanged(inMultiWindow);
2339 if (mChildFragmentManager != null) {
2340 mChildFragmentManager.dispatchMultiWindowChanged(inMultiWindow);
2341 }
2342 }
2343
2344 void performPictureInPictureChanged(boolean inPictureInPicture) {
2345 onPictureInPictureChanged(inPictureInPicture);
2346 if (mChildFragmentManager != null) {
2347 mChildFragmentManager.dispatchPictureInPictureChanged(inPictureInPicture);
2348 }
2349 }
2350
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002351 void performConfigurationChanged(Configuration newConfig) {
2352 onConfigurationChanged(newConfig);
2353 if (mChildFragmentManager != null) {
2354 mChildFragmentManager.dispatchConfigurationChanged(newConfig);
2355 }
2356 }
2357
2358 void performLowMemory() {
2359 onLowMemory();
2360 if (mChildFragmentManager != null) {
2361 mChildFragmentManager.dispatchLowMemory();
2362 }
2363 }
2364
2365 void performTrimMemory(int level) {
2366 onTrimMemory(level);
2367 if (mChildFragmentManager != null) {
2368 mChildFragmentManager.dispatchTrimMemory(level);
2369 }
2370 }
2371
2372 boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2373 boolean show = false;
2374 if (!mHidden) {
2375 if (mHasMenu && mMenuVisible) {
2376 show = true;
2377 onCreateOptionsMenu(menu, inflater);
2378 }
2379 if (mChildFragmentManager != null) {
2380 show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater);
2381 }
2382 }
2383 return show;
2384 }
2385
2386 boolean performPrepareOptionsMenu(Menu menu) {
2387 boolean show = false;
2388 if (!mHidden) {
2389 if (mHasMenu && mMenuVisible) {
2390 show = true;
2391 onPrepareOptionsMenu(menu);
2392 }
2393 if (mChildFragmentManager != null) {
2394 show |= mChildFragmentManager.dispatchPrepareOptionsMenu(menu);
2395 }
2396 }
2397 return show;
2398 }
2399
2400 boolean performOptionsItemSelected(MenuItem item) {
2401 if (!mHidden) {
2402 if (mHasMenu && mMenuVisible) {
2403 if (onOptionsItemSelected(item)) {
2404 return true;
2405 }
2406 }
2407 if (mChildFragmentManager != null) {
2408 if (mChildFragmentManager.dispatchOptionsItemSelected(item)) {
2409 return true;
2410 }
2411 }
2412 }
2413 return false;
2414 }
2415
2416 boolean performContextItemSelected(MenuItem item) {
2417 if (!mHidden) {
2418 if (onContextItemSelected(item)) {
2419 return true;
2420 }
2421 if (mChildFragmentManager != null) {
2422 if (mChildFragmentManager.dispatchContextItemSelected(item)) {
2423 return true;
2424 }
2425 }
2426 }
2427 return false;
2428 }
2429
2430 void performOptionsMenuClosed(Menu menu) {
2431 if (!mHidden) {
2432 if (mHasMenu && mMenuVisible) {
2433 onOptionsMenuClosed(menu);
2434 }
2435 if (mChildFragmentManager != null) {
2436 mChildFragmentManager.dispatchOptionsMenuClosed(menu);
2437 }
2438 }
2439 }
2440
2441 void performSaveInstanceState(Bundle outState) {
2442 onSaveInstanceState(outState);
2443 if (mChildFragmentManager != null) {
2444 Parcelable p = mChildFragmentManager.saveAllState();
2445 if (p != null) {
2446 outState.putParcelable(Activity.FRAGMENTS_TAG, p);
2447 }
2448 }
2449 }
2450
2451 void performPause() {
2452 if (mChildFragmentManager != null) {
2453 mChildFragmentManager.dispatchPause();
2454 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002455 mState = STARTED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002456 mCalled = false;
2457 onPause();
2458 if (!mCalled) {
2459 throw new SuperNotCalledException("Fragment " + this
2460 + " did not call through to super.onPause()");
2461 }
2462 }
2463
Dianne Hackborn2707d602010-07-09 18:01:20 -07002464 void performStop() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002465 if (mChildFragmentManager != null) {
2466 mChildFragmentManager.dispatchStop();
2467 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002468 mState = STOPPED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002469 mCalled = false;
Dianne Hackborn2707d602010-07-09 18:01:20 -07002470 onStop();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002471 if (!mCalled) {
2472 throw new SuperNotCalledException("Fragment " + this
2473 + " did not call through to super.onStop()");
2474 }
George Mountc03da0e2014-08-22 17:04:02 -07002475
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002476 if (mLoadersStarted) {
2477 mLoadersStarted = false;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07002478 if (!mCheckedForLoaderManager) {
2479 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002480 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07002481 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07002482 if (mLoaderManager != null) {
Todd Kennedy2ee19c7f2015-10-19 15:31:51 -07002483 if (mHost.getRetainLoaders()) {
Dianne Hackborn2707d602010-07-09 18:01:20 -07002484 mLoaderManager.doRetain();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002485 } else {
2486 mLoaderManager.doStop();
Dianne Hackborn2707d602010-07-09 18:01:20 -07002487 }
2488 }
2489 }
2490 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002491
2492 void performDestroyView() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002493 if (mChildFragmentManager != null) {
2494 mChildFragmentManager.dispatchDestroyView();
2495 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002496 mState = CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002497 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002498 onDestroyView();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002499 if (!mCalled) {
2500 throw new SuperNotCalledException("Fragment " + this
2501 + " did not call through to super.onDestroyView()");
2502 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002503 if (mLoaderManager != null) {
2504 mLoaderManager.doReportNextStart();
2505 }
2506 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002507
2508 void performDestroy() {
2509 if (mChildFragmentManager != null) {
2510 mChildFragmentManager.dispatchDestroy();
2511 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002512 mState = INITIALIZING;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002513 mCalled = false;
2514 onDestroy();
2515 if (!mCalled) {
2516 throw new SuperNotCalledException("Fragment " + this
2517 + " did not call through to super.onDestroy()");
2518 }
George Mount4c656b32016-02-17 12:59:49 -08002519 mChildFragmentManager = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002520 }
George Mountc03da0e2014-08-22 17:04:02 -07002521
2522 private static Transition loadTransition(Context context, TypedArray typedArray,
2523 Transition currentValue, Transition defaultValue, int id) {
2524 if (currentValue != defaultValue) {
2525 return currentValue;
2526 }
2527 int transitionId = typedArray.getResourceId(id, 0);
2528 Transition transition = defaultValue;
2529 if (transitionId != 0 && transitionId != com.android.internal.R.transition.no_transition) {
2530 TransitionInflater inflater = TransitionInflater.from(context);
2531 transition = inflater.inflateTransition(transitionId);
2532 if (transition instanceof TransitionSet &&
2533 ((TransitionSet)transition).getTransitionCount() == 0) {
2534 transition = null;
2535 }
2536 }
2537 return transition;
2538 }
2539
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002540}