blob: 4f121aa35f7c91839c932e492b123eec2e4df334 [file] [log] [blame]
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Chet Haasea18a86b2010-09-07 13:20:00 -070019import android.animation.Animator;
Adam Powella9bab982016-04-21 11:04:41 -070020import android.annotation.CallSuper;
Svetoslavc6d1c342015-02-26 14:44:43 -080021import android.annotation.NonNull;
Tor Norbyed9273d62013-05-30 15:59:53 -070022import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070023import android.annotation.StringRes;
Mathew Inwood4fb17d12018-08-14 14:25:44 +010024import android.annotation.UnsupportedAppUsage;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070025import android.content.ComponentCallbacks2;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070026import android.content.Context;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070027import android.content.Intent;
Clara Bayarria0c2dc32016-04-12 12:00:15 +010028import android.content.IntentSender;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070029import android.content.res.Configuration;
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -080030import android.content.res.Resources;
George Mountc03da0e2014-08-22 17:04:02 -070031import android.content.res.TypedArray;
Adam Powell592df7d2016-01-14 12:58:23 -080032import android.os.Build;
Adam Powellef4d99f2016-04-18 11:01:55 -070033import android.os.Build.VERSION_CODES;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070034import android.os.Bundle;
George Mount86bfc662016-07-12 16:06:06 -070035import android.os.Looper;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070036import android.os.Parcel;
37import android.os.Parcelable;
Tony Mak96d26fe2017-04-11 20:05:39 +010038import android.os.UserHandle;
George Mountc03da0e2014-08-22 17:04:02 -070039import android.transition.Transition;
40import android.transition.TransitionInflater;
41import android.transition.TransitionSet;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070042import android.util.AndroidRuntimeException;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070043import android.util.ArrayMap;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070044import android.util.AttributeSet;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080045import android.util.DebugUtils;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070046import android.util.SparseArray;
Adam Powell14874662013-07-18 19:42:41 -070047import android.util.SuperNotCalledException;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070048import android.view.ContextMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -070049import android.view.ContextMenu.ContextMenuInfo;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070050import android.view.LayoutInflater;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070051import android.view.Menu;
52import android.view.MenuInflater;
53import android.view.MenuItem;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070054import android.view.View;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070055import android.view.View.OnCreateContextMenuListener;
Adam Powellf0f5fff2011-08-01 13:42:50 -070056import android.view.ViewGroup;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070057import android.widget.AdapterView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070058
Dianne Hackborn625ac272010-09-17 18:29:22 -070059import java.io.FileDescriptor;
60import java.io.PrintWriter;
Aurimas Liutikas221f15d42017-03-30 16:32:58 -070061import java.lang.reflect.InvocationTargetException;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070062
Dianne Hackborn2dedce62010-04-15 14:45:25 -070063/**
64 * A Fragment is a piece of an application's user interface or behavior
Dianne Hackborn291905e2010-08-17 15:17:15 -070065 * that can be placed in an {@link Activity}. Interaction with fragments
66 * is done through {@link FragmentManager}, which can be obtained via
67 * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and
68 * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}.
69 *
70 * <p>The Fragment class can be used many ways to achieve a wide variety of
felipeal1b4e4b62012-02-27 12:09:13 -080071 * results. In its core, it represents a particular operation or interface
Dianne Hackborn291905e2010-08-17 15:17:15 -070072 * that is running within a larger {@link Activity}. A Fragment is closely
73 * tied to the Activity it is in, and can not be used apart from one. Though
74 * Fragment defines its own lifecycle, that lifecycle is dependent on its
75 * activity: if the activity is stopped, no fragments inside of it can be
76 * started; when the activity is destroyed, all fragments will be destroyed.
77 *
Jeff Davidson2a129782014-09-09 18:10:57 -070078 * <p>All subclasses of Fragment must include a public no-argument constructor.
Dianne Hackborn291905e2010-08-17 15:17:15 -070079 * The framework will often re-instantiate a fragment class when needed,
80 * in particular during state restore, and needs to be able to find this
Jeff Davidson2a129782014-09-09 18:10:57 -070081 * constructor to instantiate it. If the no-argument constructor is not
82 * available, a runtime exception will occur in some cases during state
83 * restore.
Dianne Hackborn291905e2010-08-17 15:17:15 -070084 *
85 * <p>Topics covered here:
86 * <ol>
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -070087 * <li><a href="#OlderPlatforms">Older Platforms</a>
Dianne Hackborn291905e2010-08-17 15:17:15 -070088 * <li><a href="#Lifecycle">Lifecycle</a>
89 * <li><a href="#Layout">Layout</a>
90 * <li><a href="#BackStack">Back Stack</a>
91 * </ol>
92 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070093 * <div class="special reference">
94 * <h3>Developer Guides</h3>
95 * <p>For more information about using fragments, read the
Andrew Solovayf5396802016-08-26 15:46:38 -070096 * <a href="{@docRoot}guide/components/fragments.html">Fragments</a> developer guide.</p>
Joe Fernandezb54e7a32011-10-03 15:09:50 -070097 * </div>
98 *
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -070099 * <a name="OlderPlatforms"></a>
100 * <h3>Older Platforms</h3>
101 *
102 * While the Fragment API was introduced in
103 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
Dianne Hackborn7871bad2011-12-12 15:19:26 -0800104 * at is also available for use on older platforms through
105 * {@link android.support.v4.app.FragmentActivity}. See the blog post
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700106 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
107 * Fragments For All</a> for more details.
108 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700109 * <a name="Lifecycle"></a>
110 * <h3>Lifecycle</h3>
111 *
112 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has
113 * its own wrinkle on the standard activity lifecycle. It includes basic
114 * activity lifecycle methods such as {@link #onResume}, but also important
115 * are methods related to interactions with the activity and UI generation.
116 *
117 * <p>The core series of lifecycle methods that are called to bring a fragment
118 * up to resumed state (interacting with the user) are:
119 *
120 * <ol>
121 * <li> {@link #onAttach} called once the fragment is associated with its activity.
122 * <li> {@link #onCreate} called to do initial creation of the fragment.
123 * <li> {@link #onCreateView} creates and returns the view hierarchy associated
124 * with the fragment.
125 * <li> {@link #onActivityCreated} tells the fragment that its activity has
Quddus Chong37900012012-04-11 11:57:50 -0700126 * completed its own {@link Activity#onCreate Activity.onCreate()}.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700127 * <li> {@link #onViewStateRestored} tells the fragment that all of the saved
128 * state of its view hierarchy has been restored.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700129 * <li> {@link #onStart} makes the fragment visible to the user (based on its
130 * containing activity being started).
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700131 * <li> {@link #onResume} makes the fragment begin interacting with the user
132 * (based on its containing activity being resumed).
Dianne Hackborn291905e2010-08-17 15:17:15 -0700133 * </ol>
134 *
135 * <p>As a fragment is no longer being used, it goes through a reverse
136 * series of callbacks:
137 *
138 * <ol>
139 * <li> {@link #onPause} fragment is no longer interacting with the user either
140 * because its activity is being paused or a fragment operation is modifying it
141 * in the activity.
142 * <li> {@link #onStop} fragment is no longer visible to the user either
143 * because its activity is being stopped or a fragment operation is modifying it
144 * in the activity.
145 * <li> {@link #onDestroyView} allows the fragment to clean up resources
146 * associated with its View.
147 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state.
148 * <li> {@link #onDetach} called immediately prior to the fragment no longer
149 * being associated with its activity.
150 * </ol>
151 *
152 * <a name="Layout"></a>
153 * <h3>Layout</h3>
154 *
155 * <p>Fragments can be used as part of your application's layout, allowing
156 * you to better modularize your code and more easily adjust your user
157 * interface to the screen it is running on. As an example, we can look
158 * at a simple program consisting of a list of items, and display of the
159 * details of each item.</p>
160 *
161 * <p>An activity's layout XML can include <code>&lt;fragment&gt;</code> tags
162 * to embed fragment instances inside of the layout. For example, here is
Dianne Hackborn625ac272010-09-17 18:29:22 -0700163 * a simple layout that embeds one fragment:</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700164 *
165 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout}
166 *
167 * <p>The layout is installed in the activity in the normal way:</p>
168 *
169 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
170 * main}
171 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800172 * <p>The titles fragment, showing a list of titles, is fairly simple, relying
Dianne Hackborn291905e2010-08-17 15:17:15 -0700173 * on {@link ListFragment} for most of its work. Note the implementation of
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800174 * clicking an item: depending on the current activity's layout, it can either
175 * create and display a new fragment to show the details in-place (more about
Ben Dodson542f2402011-06-14 16:40:23 -0700176 * this later), or start a new activity to show the details.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700177 *
178 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
179 * titles}
180 *
Ben Dodson542f2402011-06-14 16:40:23 -0700181 * <p>The details fragment showing the contents of a selected item just
Dianne Hackborn291905e2010-08-17 15:17:15 -0700182 * displays a string of text based on an index of a string array built in to
183 * the app:</p>
184 *
185 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
186 * details}
187 *
188 * <p>In this case when the user clicks on a title, there is no details
Ben Dodson542f2402011-06-14 16:40:23 -0700189 * container in the current activity, so the titles fragment's click code will
Dianne Hackborn291905e2010-08-17 15:17:15 -0700190 * launch a new activity to display the details fragment:</p>
191 *
192 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
193 * details_activity}
194 *
195 * <p>However the screen may be large enough to show both the list of titles
196 * and details about the currently selected title. To use such a layout on
197 * a landscape screen, this alternative layout can be placed under layout-land:</p>
198 *
199 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout}
200 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800201 * <p>Note how the prior code will adjust to this alternative UI flow: the titles
202 * fragment will now embed the details fragment inside of this activity, and the
203 * details activity will finish itself if it is running in a configuration
204 * where the details can be shown in-place.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700205 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700206 * <p>When a configuration change causes the activity hosting these fragments
207 * to restart, its new instance may use a different layout that doesn't
208 * include the same fragments as the previous layout. In this case all of
209 * the previous fragments will still be instantiated and running in the new
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800210 * instance. However, any that are no longer associated with a &lt;fragment&gt;
211 * tag in the view hierarchy will not have their content view created
212 * and will return false from {@link #isInLayout}. (The code here also shows
213 * how you can determine if a fragment placed in a container is no longer
214 * running in a layout with that container and avoid creating its view hierarchy
215 * in that case.)
George Mountc03da0e2014-08-22 17:04:02 -0700216 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700217 * <p>The attributes of the &lt;fragment&gt; tag are used to control the
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800218 * LayoutParams provided when attaching the fragment's view to the parent
219 * container. They can also be parsed by the fragment in {@link #onInflate}
Dianne Hackborn625ac272010-09-17 18:29:22 -0700220 * as parameters.
George Mountc03da0e2014-08-22 17:04:02 -0700221 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700222 * <p>The fragment being instantiated must have some kind of unique identifier
223 * so that it can be re-associated with a previous instance if the parent
224 * activity needs to be destroyed and recreated. This can be provided these
225 * ways:
George Mountc03da0e2014-08-22 17:04:02 -0700226 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700227 * <ul>
228 * <li>If nothing is explicitly supplied, the view ID of the container will
229 * be used.
230 * <li><code>android:tag</code> can be used in &lt;fragment&gt; to provide
231 * a specific tag name for the fragment.
232 * <li><code>android:id</code> can be used in &lt;fragment&gt; to provide
233 * a specific identifier for the fragment.
234 * </ul>
George Mountc03da0e2014-08-22 17:04:02 -0700235 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700236 * <a name="BackStack"></a>
237 * <h3>Back Stack</h3>
238 *
239 * <p>The transaction in which fragments are modified can be placed on an
240 * internal back-stack of the owning activity. When the user presses back
241 * in the activity, any transactions on the back stack are popped off before
242 * the activity itself is finished.
243 *
244 * <p>For example, consider this simple fragment that is instantiated with
245 * an integer argument and displays that in a TextView in its UI:</p>
246 *
247 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
248 * fragment}
249 *
250 * <p>A function that creates a new instance of the fragment, replacing
251 * whatever current fragment instance is being shown and pushing that change
252 * on to the back stack could be written as:
253 *
254 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
255 * add_stack}
256 *
257 * <p>After each call to this function, a new entry is on the stack, and
258 * pressing back will pop it to return the user to whatever previous state
259 * the activity UI was in.
Ian Lake0a1feb82017-11-13 10:26:46 -0800260 *
Ian Lake1f4e67b2017-12-18 10:36:18 -0800261 * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">Support Library</a>
262 * {@link android.support.v4.app.Fragment} for consistent behavior across all devices
263 * and access to <a href="{@docRoot}topic/libraries/architecture/lifecycle.html">Lifecycle</a>.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700264 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800265@Deprecated
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700266public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener {
Mathew Inwood45d2c252018-09-14 12:35:36 +0100267 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700268 private static final ArrayMap<String, Class<?>> sClassMap =
269 new ArrayMap<String, Class<?>>();
George Mountc03da0e2014-08-22 17:04:02 -0700270
Adam Powell635c60a2011-10-26 10:22:16 -0700271 static final int INVALID_STATE = -1; // Invalid state used as a null value.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700272 static final int INITIALIZING = 0; // Not yet created.
273 static final int CREATED = 1; // Created.
274 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700275 static final int STOPPED = 3; // Fully created, not started.
276 static final int STARTED = 4; // Created and started, not resumed.
277 static final int RESUMED = 5; // Created started and resumed.
George Mountc03da0e2014-08-22 17:04:02 -0700278
279 private static final Transition USE_DEFAULT_TRANSITION = new TransitionSet();
280
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700281 int mState = INITIALIZING;
George Mountc03da0e2014-08-22 17:04:02 -0700282
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700283 // When instantiated from saved state, this is the saved state.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100284 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700285 Bundle mSavedFragmentState;
286 SparseArray<Parcelable> mSavedViewState;
George Mountc03da0e2014-08-22 17:04:02 -0700287
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700288 // Index into active fragment array.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100289 @UnsupportedAppUsage
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700290 int mIndex = -1;
George Mountc03da0e2014-08-22 17:04:02 -0700291
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700292 // Internal unique name for this fragment;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100293 @UnsupportedAppUsage
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700294 String mWho;
George Mountc03da0e2014-08-22 17:04:02 -0700295
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700296 // Construction arguments;
297 Bundle mArguments;
298
Dianne Hackborndef15372010-08-15 12:43:52 -0700299 // Target fragment.
300 Fragment mTarget;
301
Dianne Hackbornf9302322011-06-14 18:36:14 -0700302 // For use when retaining a fragment: this is the index of the last mTarget.
303 int mTargetIndex = -1;
304
Dianne Hackborndef15372010-08-15 12:43:52 -0700305 // Target request code.
306 int mTargetRequestCode;
307
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700308 // True if the fragment is in the list of added fragments.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100309 @UnsupportedAppUsage
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700310 boolean mAdded;
George Mountc03da0e2014-08-22 17:04:02 -0700311
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800312 // If set this fragment is being removed from its activity.
313 boolean mRemoving;
314
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700315 // Set to true if this fragment was instantiated from a layout file.
316 boolean mFromLayout;
George Mountc03da0e2014-08-22 17:04:02 -0700317
Dianne Hackborn625ac272010-09-17 18:29:22 -0700318 // Set to true when the view has actually been inflated in its layout.
319 boolean mInLayout;
320
Dianne Hackborn352cc982011-01-04 11:34:18 -0800321 // True if this fragment has been restored from previously saved state.
322 boolean mRestored;
George Mountc03da0e2014-08-22 17:04:02 -0700323
Adam Powellbb3aa342016-11-16 10:34:57 -0800324 // True if performCreateView has been called and a matching call to performDestroyView
325 // has not yet happened.
326 boolean mPerformedCreateView;
327
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700328 // Number of active back stack entries this fragment is in.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700329 int mBackStackNesting;
George Mountc03da0e2014-08-22 17:04:02 -0700330
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700331 // The fragment manager we are associated with. Set as soon as the
332 // fragment is used in a transaction; cleared after it has been removed
333 // from all transactions.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100334 @UnsupportedAppUsage
Adam Powell635c60a2011-10-26 10:22:16 -0700335 FragmentManagerImpl mFragmentManager;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700336
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700337 // Activity this fragment is attached to.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100338 @UnsupportedAppUsage
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700339 FragmentHostCallback mHost;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700340
341 // Private fragment manager for child fragments inside of this one.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100342 @UnsupportedAppUsage
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700343 FragmentManagerImpl mChildFragmentManager;
344
Adam Powell44ba79e2016-02-04 16:20:37 -0800345 // For use when restoring fragment state and descendant fragments are retained.
346 // This state is set by FragmentState.instantiate and cleared in onCreate.
347 FragmentManagerNonConfig mChildNonConfig;
348
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700349 // If this Fragment is contained in another Fragment, this is that container.
350 Fragment mParentFragment;
351
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700352 // The optional identifier for this fragment -- either the container ID if it
353 // was dynamically added to the view hierarchy, or the ID supplied in
354 // layout.
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100355 @UnsupportedAppUsage
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700356 int mFragmentId;
George Mountc03da0e2014-08-22 17:04:02 -0700357
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700358 // When a fragment is being dynamically added to the view hierarchy, this
359 // is the identifier of the parent container it is being added to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700360 int mContainerId;
George Mountc03da0e2014-08-22 17:04:02 -0700361
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700362 // The optional named tag for this fragment -- usually used to find
363 // fragments that are not part of the layout.
364 String mTag;
George Mountc03da0e2014-08-22 17:04:02 -0700365
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700366 // Set to true when the app has requested that this fragment be hidden
367 // from the user.
368 boolean mHidden;
George Mountc03da0e2014-08-22 17:04:02 -0700369
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700370 // Set to true when the app has requested that this fragment be detached.
371 boolean mDetached;
372
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700373 // If set this fragment would like its instance retained across
374 // configuration changes.
375 boolean mRetainInstance;
George Mountc03da0e2014-08-22 17:04:02 -0700376
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700377 // If set this fragment is being retained across the current config change.
378 boolean mRetaining;
George Mountc03da0e2014-08-22 17:04:02 -0700379
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700380 // If set this fragment has menu items to contribute.
381 boolean mHasMenu;
Dianne Hackborn6c285972011-08-29 16:53:49 -0700382
383 // Set to true to allow the fragment's menu to be shown.
384 boolean mMenuVisible = true;
385
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700386 // Used to verify that subclasses call through to super class.
387 boolean mCalled;
George Mountc03da0e2014-08-22 17:04:02 -0700388
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700389 // The parent container of the fragment after dynamically added to UI.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700390 ViewGroup mContainer;
George Mountc03da0e2014-08-22 17:04:02 -0700391
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700392 // The View generated for this fragment.
Mathew Inwood55418ea2018-12-20 15:30:45 +0000393 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700394 View mView;
George Mountc03da0e2014-08-22 17:04:02 -0700395
Adam Powell635c60a2011-10-26 10:22:16 -0700396 // Whether this fragment should defer starting until after other fragments
397 // have been started and their loaders are finished.
398 boolean mDeferStart;
399
Adam Powell78fed9b2011-11-07 10:45:34 -0800400 // Hint provided by the app that this fragment is currently visible to the user.
401 boolean mUserVisibleHint = true;
402
Dianne Hackborn4911b782010-07-15 12:54:39 -0700403 LoaderManagerImpl mLoaderManager;
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100404 @UnsupportedAppUsage
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700405 boolean mLoadersStarted;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700406 boolean mCheckedForLoaderManager;
George Mountc03da0e2014-08-22 17:04:02 -0700407
George Mount86bfc662016-07-12 16:06:06 -0700408 // The animation and transition information for the fragment. This will be null
409 // unless the elements are explicitly accessed and should remain null for Fragments
410 // without Views.
411 AnimationInfo mAnimationInfo;
George Mountc03da0e2014-08-22 17:04:02 -0700412
George Mount86bfc662016-07-12 16:06:06 -0700413 // True if the View was added, and its animation has yet to be run. This could
414 // also indicate that the fragment view hasn't been made visible, even if there is no
415 // animation for this fragment.
416 boolean mIsNewlyAdded;
George Mountc03da0e2014-08-22 17:04:02 -0700417
George Mounteca8e222016-07-07 13:13:05 -0700418 // True if mHidden has been changed and the animation should be scheduled.
419 boolean mHiddenChanged;
420
George Mount3fd81292017-04-17 16:50:26 -0700421 // The cached value from onGetLayoutInflater(Bundle) that will be returned from
422 // getLayoutInflater()
423 LayoutInflater mLayoutInflater;
424
George Mount15c21ff2017-07-19 10:47:48 -0700425 // Keep track of whether or not this Fragment has run performCreate(). Retained instance
426 // fragments can have mRetaining set to true without going through creation, so we must
427 // track it separately.
428 boolean mIsCreated;
429
Dianne Hackbornc8017682010-07-06 13:34:38 -0700430 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700431 * State information that has been retrieved from a fragment instance
432 * through {@link FragmentManager#saveFragmentInstanceState(Fragment)
433 * FragmentManager.saveFragmentInstanceState}.
Ian Lake0a1feb82017-11-13 10:26:46 -0800434 *
435 * @deprecated Use {@link android.support.v4.app.Fragment.SavedState}
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700436 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800437 @Deprecated
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700438 public static class SavedState implements Parcelable {
439 final Bundle mState;
440
441 SavedState(Bundle state) {
442 mState = state;
443 }
444
445 SavedState(Parcel in, ClassLoader loader) {
446 mState = in.readBundle();
447 if (loader != null && mState != null) {
448 mState.setClassLoader(loader);
449 }
450 }
451
452 @Override
453 public int describeContents() {
454 return 0;
455 }
456
457 @Override
458 public void writeToParcel(Parcel dest, int flags) {
459 dest.writeBundle(mState);
460 }
461
462 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR
463 = new Parcelable.ClassLoaderCreator<SavedState>() {
464 public SavedState createFromParcel(Parcel in) {
465 return new SavedState(in, null);
466 }
467
468 public SavedState createFromParcel(Parcel in, ClassLoader loader) {
469 return new SavedState(in, loader);
470 }
471
472 public SavedState[] newArray(int size) {
473 return new SavedState[size];
474 }
475 };
476 }
477
478 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700479 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when
480 * there is an instantiation failure.
Ian Lake0a1feb82017-11-13 10:26:46 -0800481 *
482 * @deprecated Use {@link android.support.v4.app.Fragment.InstantiationException}
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700483 */
Ian Lake0a1feb82017-11-13 10:26:46 -0800484 @Deprecated
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700485 static public class InstantiationException extends AndroidRuntimeException {
486 public InstantiationException(String msg, Exception cause) {
487 super(msg, cause);
488 }
489 }
490
491 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700492 * Default constructor. <strong>Every</strong> fragment must have an
Dianne Hackbornc8017682010-07-06 13:34:38 -0700493 * empty constructor, so it can be instantiated when restoring its
494 * activity's state. It is strongly recommended that subclasses do not
495 * have other constructors with parameters, since these constructors
496 * will not be called when the fragment is re-instantiated; instead,
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700497 * arguments can be supplied by the caller with {@link #setArguments}
498 * and later retrieved by the Fragment with {@link #getArguments}.
George Mountc03da0e2014-08-22 17:04:02 -0700499 *
Aurimas Liutikasc59422a2016-06-14 16:50:07 -0700500 * <p>Applications should generally not implement a constructor. Prefer
501 * {@link #onAttach(Context)} instead. It is the first place application code can run where
502 * the fragment is ready to be used - the point where the fragment is actually associated with
503 * its context. Some applications may also want to implement {@link #onInflate} to retrieve
504 * attributes from a layout resource, although note this happens when the fragment is attached.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700505 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700506 public Fragment() {
507 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700508
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700509 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700510 * Like {@link #instantiate(Context, String, Bundle)} but with a null
511 * argument Bundle.
512 */
513 public static Fragment instantiate(Context context, String fname) {
514 return instantiate(context, fname, null);
515 }
516
517 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700518 * Create a new instance of a Fragment with the given class name. This is
519 * the same as calling its empty constructor.
520 *
521 * @param context The calling context being used to instantiate the fragment.
522 * This is currently just used to get its ClassLoader.
523 * @param fname The class name of the fragment to instantiate.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700524 * @param args Bundle of arguments to supply to the fragment, which it
525 * can retrieve with {@link #getArguments()}. May be null.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700526 * @return Returns a new fragment instance.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700527 * @throws InstantiationException If there is a failure in instantiating
528 * the given fragment class. This is a runtime exception; it is not
529 * normally expected to happen.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700530 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700531 public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700532 try {
533 Class<?> clazz = sClassMap.get(fname);
534 if (clazz == null) {
535 // Class not found in the cache, see if it's real, and try to add it
536 clazz = context.getClassLoader().loadClass(fname);
Amith Yamasani364ed4d2013-07-26 13:37:56 -0700537 if (!Fragment.class.isAssignableFrom(clazz)) {
538 throw new InstantiationException("Trying to instantiate a class " + fname
539 + " that is not a Fragment", new ClassCastException());
540 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700541 sClassMap.put(fname, clazz);
542 }
Aurimas Liutikas221f15d42017-03-30 16:32:58 -0700543 Fragment f = (Fragment) clazz.getConstructor().newInstance();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700544 if (args != null) {
545 args.setClassLoader(f.getClass().getClassLoader());
Adam Powellbb3aa342016-11-16 10:34:57 -0800546 f.setArguments(args);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700547 }
548 return f;
549 } catch (ClassNotFoundException e) {
550 throw new InstantiationException("Unable to instantiate fragment " + fname
551 + ": make sure class name exists, is public, and has an"
552 + " empty constructor that is public", e);
553 } catch (java.lang.InstantiationException e) {
554 throw new InstantiationException("Unable to instantiate fragment " + fname
555 + ": make sure class name exists, is public, and has an"
556 + " empty constructor that is public", e);
557 } catch (IllegalAccessException e) {
558 throw new InstantiationException("Unable to instantiate fragment " + fname
559 + ": make sure class name exists, is public, and has an"
560 + " empty constructor that is public", e);
Aurimas Liutikas221f15d42017-03-30 16:32:58 -0700561 } catch (NoSuchMethodException e) {
562 throw new InstantiationException("Unable to instantiate fragment " + fname
563 + ": could not find Fragment constructor", e);
564 } catch (InvocationTargetException e) {
565 throw new InstantiationException("Unable to instantiate fragment " + fname
566 + ": calling Fragment constructor caused an exception", e);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700567 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700568 }
George Mountc03da0e2014-08-22 17:04:02 -0700569
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700570 final void restoreViewState(Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700571 if (mSavedViewState != null) {
572 mView.restoreHierarchyState(mSavedViewState);
573 mSavedViewState = null;
574 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700575 mCalled = false;
576 onViewStateRestored(savedInstanceState);
577 if (!mCalled) {
578 throw new SuperNotCalledException("Fragment " + this
579 + " did not call through to super.onViewStateRestored()");
580 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700581 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700582
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700583 final void setIndex(int index, Fragment parent) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700584 mIndex = index;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700585 if (parent != null) {
586 mWho = parent.mWho + ":" + mIndex;
587 } else {
588 mWho = "android:fragment:" + mIndex;
589 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700590 }
591
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700592 final boolean isInBackStack() {
593 return mBackStackNesting > 0;
594 }
595
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700596 /**
597 * Subclasses can not override equals().
598 */
599 @Override final public boolean equals(Object o) {
600 return super.equals(o);
601 }
602
603 /**
604 * Subclasses can not override hashCode().
605 */
606 @Override final public int hashCode() {
607 return super.hashCode();
608 }
George Mountc03da0e2014-08-22 17:04:02 -0700609
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700610 @Override
611 public String toString() {
612 StringBuilder sb = new StringBuilder(128);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800613 DebugUtils.buildShortClassTag(this, sb);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700614 if (mIndex >= 0) {
615 sb.append(" #");
616 sb.append(mIndex);
617 }
618 if (mFragmentId != 0) {
619 sb.append(" id=0x");
620 sb.append(Integer.toHexString(mFragmentId));
621 }
622 if (mTag != null) {
623 sb.append(" ");
624 sb.append(mTag);
625 }
626 sb.append('}');
627 return sb.toString();
628 }
George Mountc03da0e2014-08-22 17:04:02 -0700629
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700630 /**
631 * Return the identifier this fragment is known by. This is either
632 * the android:id value supplied in a layout or the container view ID
633 * supplied when adding the fragment.
634 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700635 final public int getId() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700636 return mFragmentId;
637 }
George Mountc03da0e2014-08-22 17:04:02 -0700638
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700639 /**
640 * Get the tag name of the fragment, if specified.
641 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700642 final public String getTag() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700643 return mTag;
644 }
George Mountc03da0e2014-08-22 17:04:02 -0700645
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700646 /**
Adam Powelladfd62c2017-01-31 14:54:07 -0800647 * Supply the construction arguments for this fragment.
648 * The arguments supplied here will be retained across fragment destroy and
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700649 * creation.
Adam Powelladfd62c2017-01-31 14:54:07 -0800650 *
651 * <p>This method cannot be called if the fragment is added to a FragmentManager and
652 * if {@link #isStateSaved()} would return true. Prior to {@link Build.VERSION_CODES#O},
653 * this method may only be called if the fragment has not yet been added to a FragmentManager.
654 * </p>
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700655 */
Dianne Hackborndef15372010-08-15 12:43:52 -0700656 public void setArguments(Bundle args) {
Adam Powelladfd62c2017-01-31 14:54:07 -0800657 // The isStateSaved requirement below was only added in Android O and is compatible
658 // because it loosens previous requirements rather than making them more strict.
659 // See method javadoc.
660 if (mIndex >= 0 && isStateSaved()) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700661 throw new IllegalStateException("Fragment already active");
662 }
663 mArguments = args;
664 }
665
666 /**
Mark Dolinerd0646dc2014-08-27 16:04:02 -0700667 * Return the arguments supplied to {@link #setArguments}, if any.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700668 */
669 final public Bundle getArguments() {
670 return mArguments;
671 }
672
673 /**
Adam Powelladfd62c2017-01-31 14:54:07 -0800674 * Returns true if this fragment is added and its state has already been saved
675 * by its host. Any operations that would change saved state should not be performed
676 * if this method returns true, and some operations such as {@link #setArguments(Bundle)}
677 * will fail.
678 *
679 * @return true if this fragment's state has already been saved by its host
680 */
681 public final boolean isStateSaved() {
682 if (mFragmentManager == null) {
683 return false;
684 }
685 return mFragmentManager.isStateSaved();
686 }
687
688 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700689 * Set the initial saved state that this Fragment should restore itself
690 * from when first being constructed, as returned by
691 * {@link FragmentManager#saveFragmentInstanceState(Fragment)
692 * FragmentManager.saveFragmentInstanceState}.
693 *
694 * @param state The state the fragment should be restored from.
695 */
696 public void setInitialSavedState(SavedState state) {
697 if (mIndex >= 0) {
698 throw new IllegalStateException("Fragment already active");
699 }
700 mSavedFragmentState = state != null && state.mState != null
701 ? state.mState : null;
702 }
703
704 /**
Dianne Hackborndef15372010-08-15 12:43:52 -0700705 * Optional target for this fragment. This may be used, for example,
706 * if this fragment is being started by another, and when done wants to
707 * give a result back to the first. The target set here is retained
708 * across instances via {@link FragmentManager#putFragment
709 * FragmentManager.putFragment()}.
710 *
711 * @param fragment The fragment that is the target of this one.
712 * @param requestCode Optional request code, for convenience if you
713 * are going to call back with {@link #onActivityResult(int, int, Intent)}.
714 */
715 public void setTargetFragment(Fragment fragment, int requestCode) {
Adam Powellab209a62017-01-26 14:14:34 -0800716 // Don't allow a caller to set a target fragment in another FragmentManager,
717 // but there's a snag: people do set target fragments before fragments get added.
718 // We'll have the FragmentManager check that for validity when we move
719 // the fragments to a valid state.
720 final FragmentManager mine = getFragmentManager();
Adam Powell06217422017-02-01 10:45:12 -0800721 final FragmentManager theirs = fragment != null ? fragment.getFragmentManager() : null;
Adam Powellab209a62017-01-26 14:14:34 -0800722 if (mine != null && theirs != null && mine != theirs) {
723 throw new IllegalArgumentException("Fragment " + fragment
724 + " must share the same FragmentManager to be set as a target fragment");
725 }
726
727 // Don't let someone create a cycle.
728 for (Fragment check = fragment; check != null; check = check.getTargetFragment()) {
729 if (check == this) {
730 throw new IllegalArgumentException("Setting " + fragment + " as the target of "
731 + this + " would create a target cycle");
732 }
733 }
Dianne Hackborndef15372010-08-15 12:43:52 -0700734 mTarget = fragment;
735 mTargetRequestCode = requestCode;
736 }
737
738 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700739 * Return the target fragment set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700740 */
741 final public Fragment getTargetFragment() {
742 return mTarget;
743 }
744
745 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700746 * Return the target request code set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700747 */
748 final public int getTargetRequestCode() {
749 return mTargetRequestCode;
750 }
751
752 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700753 * Return the {@link Context} this fragment is currently associated with.
754 */
755 public Context getContext() {
756 return mHost == null ? null : mHost.getContext();
757 }
758
759 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700760 * Return the Activity this fragment is currently associated with.
761 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700762 final public Activity getActivity() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700763 return mHost == null ? null : mHost.getActivity();
764 }
765
766 /**
767 * Return the host object of this fragment. May return {@code null} if the fragment
768 * isn't currently being hosted.
769 */
770 @Nullable
771 final public Object getHost() {
772 return mHost == null ? null : mHost.onGetHost();
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700773 }
George Mountc03da0e2014-08-22 17:04:02 -0700774
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700775 /**
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800776 * Return <code>getActivity().getResources()</code>.
777 */
778 final public Resources getResources() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700779 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800780 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
781 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700782 return mHost.getContext().getResources();
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800783 }
George Mountc03da0e2014-08-22 17:04:02 -0700784
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800785 /**
786 * Return a localized, styled CharSequence from the application's package's
787 * default string table.
788 *
789 * @param resId Resource id for the CharSequence text
790 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700791 public final CharSequence getText(@StringRes int resId) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800792 return getResources().getText(resId);
793 }
794
795 /**
796 * Return a localized string from the application's package's
797 * default string table.
798 *
799 * @param resId Resource id for the string
800 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700801 public final String getString(@StringRes int resId) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800802 return getResources().getString(resId);
803 }
804
805 /**
806 * Return a localized formatted string from the application's package's
807 * default string table, substituting the format arguments as defined in
808 * {@link java.util.Formatter} and {@link java.lang.String#format}.
809 *
810 * @param resId Resource id for the format string
811 * @param formatArgs The format arguments that will be used for substitution.
812 */
813
Tor Norbye7b9c9122013-05-30 16:48:33 -0700814 public final String getString(@StringRes int resId, Object... formatArgs) {
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800815 return getResources().getString(resId, formatArgs);
816 }
817
818 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700819 * Return the FragmentManager for interacting with fragments associated
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700820 * with this fragment's activity. Note that this will be non-null slightly
Dianne Hackborn625ac272010-09-17 18:29:22 -0700821 * before {@link #getActivity()}, during the time from when the fragment is
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700822 * placed in a {@link FragmentTransaction} until it is committed and
823 * attached to its activity.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700824 *
825 * <p>If this Fragment is a child of another Fragment, the FragmentManager
826 * returned here will be the parent's {@link #getChildFragmentManager()}.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700827 */
828 final public FragmentManager getFragmentManager() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700829 return mFragmentManager;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700830 }
831
832 /**
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700833 * Return a private FragmentManager for placing and managing Fragments
834 * inside of this Fragment.
835 */
836 final public FragmentManager getChildFragmentManager() {
837 if (mChildFragmentManager == null) {
838 instantiateChildFragmentManager();
839 if (mState >= RESUMED) {
840 mChildFragmentManager.dispatchResume();
841 } else if (mState >= STARTED) {
842 mChildFragmentManager.dispatchStart();
843 } else if (mState >= ACTIVITY_CREATED) {
844 mChildFragmentManager.dispatchActivityCreated();
845 } else if (mState >= CREATED) {
846 mChildFragmentManager.dispatchCreate();
847 }
848 }
849 return mChildFragmentManager;
850 }
851
852 /**
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700853 * Returns the parent Fragment containing this Fragment. If this Fragment
854 * is attached directly to an Activity, returns null.
855 */
856 final public Fragment getParentFragment() {
857 return mParentFragment;
858 }
859
860 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700861 * Return true if the fragment is currently added to its activity.
862 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700863 final public boolean isAdded() {
Todd Kennedya5fc6f02015-04-14 18:22:54 -0700864 return mHost != null && mAdded;
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800865 }
866
867 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700868 * Return true if the fragment has been explicitly detached from the UI.
869 * That is, {@link FragmentTransaction#detach(Fragment)
870 * FragmentTransaction.detach(Fragment)} has been used on it.
871 */
872 final public boolean isDetached() {
873 return mDetached;
874 }
875
876 /**
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800877 * Return true if this fragment is currently being removed from its
878 * activity. This is <em>not</em> whether its activity is finishing, but
879 * rather whether it is in the process of being removed from its activity.
880 */
881 final public boolean isRemoving() {
882 return mRemoving;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700883 }
George Mountc03da0e2014-08-22 17:04:02 -0700884
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700885 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700886 * Return true if the layout is included as part of an activity view
887 * hierarchy via the &lt;fragment&gt; tag. This will always be true when
888 * fragments are created through the &lt;fragment&gt; tag, <em>except</em>
889 * in the case where an old fragment is restored from a previous state and
890 * it does not appear in the layout of the current state.
891 */
892 final public boolean isInLayout() {
893 return mInLayout;
894 }
895
896 /**
Dianne Hackborn2707d602010-07-09 18:01:20 -0700897 * Return true if the fragment is in the resumed state. This is true
898 * for the duration of {@link #onResume()} and {@link #onPause()} as well.
899 */
900 final public boolean isResumed() {
Adam Powelld1d4d9c2016-01-12 10:11:42 -0800901 return mState >= RESUMED;
Dianne Hackborn2707d602010-07-09 18:01:20 -0700902 }
George Mountc03da0e2014-08-22 17:04:02 -0700903
Dianne Hackborn2707d602010-07-09 18:01:20 -0700904 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700905 * Return true if the fragment is currently visible to the user. This means
Aurimas Liutikas6acbe072017-02-08 10:48:40 -0800906 * it: (1) has been added, (2) has its view attached to the window, and
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700907 * (3) is not hidden.
908 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700909 final public boolean isVisible() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700910 return isAdded() && !isHidden() && mView != null
911 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
912 }
George Mountc03da0e2014-08-22 17:04:02 -0700913
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700914 /**
915 * Return true if the fragment has been hidden. By default fragments
916 * are shown. You can find out about changes to this state with
Dianne Hackborncddfa6d2010-05-19 22:56:37 -0700917 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700918 * to other states -- that is, to be visible to the user, a fragment
919 * must be both started and not hidden.
920 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700921 final public boolean isHidden() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700922 return mHidden;
923 }
George Mountc03da0e2014-08-22 17:04:02 -0700924
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700925 /**
926 * Called when the hidden state (as returned by {@link #isHidden()} of
927 * the fragment has changed. Fragments start out not hidden; this will
928 * be called whenever the fragment changes state from that.
Aurimas Liutikasc3cfdaf2016-06-07 14:56:24 -0700929 * @param hidden True if the fragment is now hidden, false otherwise.
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700930 */
931 public void onHiddenChanged(boolean hidden) {
932 }
George Mountc03da0e2014-08-22 17:04:02 -0700933
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700934 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700935 * Control whether a fragment instance is retained across Activity
936 * re-creation (such as from a configuration change). This can only
937 * be used with fragments not in the back stack. If set, the fragment
938 * lifecycle will be slightly different when an activity is recreated:
939 * <ul>
940 * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still
941 * will be, because the fragment is being detached from its current activity).
942 * <li> {@link #onCreate(Bundle)} will not be called since the fragment
943 * is not being re-created.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700944 * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b>
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700945 * still be called.
946 * </ul>
947 */
948 public void setRetainInstance(boolean retain) {
949 mRetainInstance = retain;
950 }
George Mountc03da0e2014-08-22 17:04:02 -0700951
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700952 final public boolean getRetainInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700953 return mRetainInstance;
954 }
George Mountc03da0e2014-08-22 17:04:02 -0700955
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700956 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700957 * Report that this fragment would like to participate in populating
Wink Saville4dc643e2010-06-12 22:16:41 -0700958 * the options menu by receiving a call to {@link #onCreateOptionsMenu}
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700959 * and related methods.
George Mountc03da0e2014-08-22 17:04:02 -0700960 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700961 * @param hasMenu If true, the fragment has menu items to contribute.
962 */
963 public void setHasOptionsMenu(boolean hasMenu) {
964 if (mHasMenu != hasMenu) {
965 mHasMenu = hasMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -0700966 if (isAdded() && !isHidden()) {
967 mFragmentManager.invalidateOptionsMenu();
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700968 }
969 }
970 }
Dianne Hackborn6c285972011-08-29 16:53:49 -0700971
972 /**
973 * Set a hint for whether this fragment's menu should be visible. This
974 * is useful if you know that a fragment has been placed in your view
975 * hierarchy so that the user can not currently seen it, so any menu items
976 * it has should also not be shown.
977 *
978 * @param menuVisible The default is true, meaning the fragment's menu will
979 * be shown as usual. If false, the user will not see the menu.
980 */
981 public void setMenuVisibility(boolean menuVisible) {
982 if (mMenuVisible != menuVisible) {
983 mMenuVisible = menuVisible;
984 if (mHasMenu && isAdded() && !isHidden()) {
985 mFragmentManager.invalidateOptionsMenu();
986 }
987 }
988 }
989
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700990 /**
Adam Powell78fed9b2011-11-07 10:45:34 -0800991 * Set a hint to the system about whether this fragment's UI is currently visible
992 * to the user. This hint defaults to true and is persistent across fragment instance
993 * state save and restore.
Adam Powell635c60a2011-10-26 10:22:16 -0700994 *
Adam Powell78fed9b2011-11-07 10:45:34 -0800995 * <p>An app may set this to false to indicate that the fragment's UI is
996 * scrolled out of visibility or is otherwise not directly visible to the user.
997 * This may be used by the system to prioritize operations such as fragment lifecycle updates
998 * or loader ordering behavior.</p>
Adam Powell635c60a2011-10-26 10:22:16 -0700999 *
Adam Powell7f46c1b2016-06-22 10:23:02 -07001000 * <p><strong>Note:</strong> This method may be called outside of the fragment lifecycle
1001 * and thus has no ordering guarantees with regard to fragment lifecycle method calls.</p>
1002 *
Adam Powellef4d99f2016-04-18 11:01:55 -07001003 * <p><strong>Note:</strong> Prior to Android N there was a platform bug that could cause
1004 * <code>setUserVisibleHint</code> to bring a fragment up to the started state before its
1005 * <code>FragmentTransaction</code> had been committed. As some apps relied on this behavior,
1006 * it is preserved for apps that declare a <code>targetSdkVersion</code> of 23 or lower.</p>
1007 *
Adam Powell78fed9b2011-11-07 10:45:34 -08001008 * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
1009 * false if it is not.
Adam Powell635c60a2011-10-26 10:22:16 -07001010 */
Adam Powell78fed9b2011-11-07 10:45:34 -08001011 public void setUserVisibleHint(boolean isVisibleToUser) {
Adam Powellef4d99f2016-04-18 11:01:55 -07001012 // Prior to Android N we were simply checking if this fragment had a FragmentManager
1013 // set before we would trigger a deferred start. Unfortunately this also gets set before
1014 // a fragment transaction is committed, so if setUserVisibleHint was called before a
1015 // transaction commit, we would start the fragment way too early. FragmentPagerAdapter
1016 // triggers this situation.
1017 // Unfortunately some apps relied on this timing in overrides of setUserVisibleHint
1018 // on their own fragments, and expected, however erroneously, that after a call to
1019 // super.setUserVisibleHint their onStart methods had been run.
1020 // We preserve this behavior for apps targeting old platform versions below.
1021 boolean useBrokenAddedCheck = false;
1022 Context context = getContext();
1023 if (mFragmentManager != null && mFragmentManager.mHost != null) {
1024 context = mFragmentManager.mHost.getContext();
1025 }
1026 if (context != null) {
1027 useBrokenAddedCheck = context.getApplicationInfo().targetSdkVersion <= VERSION_CODES.M;
1028 }
1029
1030 final boolean performDeferredStart;
1031 if (useBrokenAddedCheck) {
1032 performDeferredStart = !mUserVisibleHint && isVisibleToUser && mState < STARTED
1033 && mFragmentManager != null;
1034 } else {
1035 performDeferredStart = !mUserVisibleHint && isVisibleToUser && mState < STARTED
1036 && mFragmentManager != null && isAdded();
1037 }
1038
1039 if (performDeferredStart) {
Adam Powell635c60a2011-10-26 10:22:16 -07001040 mFragmentManager.performPendingDeferredStart(this);
1041 }
Adam Powellef4d99f2016-04-18 11:01:55 -07001042
Adam Powell78fed9b2011-11-07 10:45:34 -08001043 mUserVisibleHint = isVisibleToUser;
Adam Powellb963c992016-04-01 15:29:04 -07001044 mDeferStart = mState < STARTED && !isVisibleToUser;
Adam Powell635c60a2011-10-26 10:22:16 -07001045 }
1046
1047 /**
Adam Powell78fed9b2011-11-07 10:45:34 -08001048 * @return The current value of the user-visible hint on this fragment.
1049 * @see #setUserVisibleHint(boolean)
Adam Powell635c60a2011-10-26 10:22:16 -07001050 */
Adam Powell78fed9b2011-11-07 10:45:34 -08001051 public boolean getUserVisibleHint() {
1052 return mUserVisibleHint;
Adam Powell635c60a2011-10-26 10:22:16 -07001053 }
1054
1055 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001056 * Return the LoaderManager for this fragment, creating it if needed.
Ian Lake0a1feb82017-11-13 10:26:46 -08001057 *
1058 * @deprecated Use {@link android.support.v4.app.Fragment#getLoaderManager()}
Dianne Hackbornc8017682010-07-06 13:34:38 -07001059 */
Ian Lake0a1feb82017-11-13 10:26:46 -08001060 @Deprecated
Dianne Hackbornc8017682010-07-06 13:34:38 -07001061 public LoaderManager getLoaderManager() {
1062 if (mLoaderManager != null) {
1063 return mLoaderManager;
1064 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001065 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001066 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1067 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001068 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001069 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -07001070 return mLoaderManager;
1071 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001072
Dianne Hackbornc8017682010-07-06 13:34:38 -07001073 /**
Scott Main87bff972013-02-27 15:46:34 -08001074 * Call {@link Activity#startActivity(Intent)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001075 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001076 *
1077 * @param intent The intent to start.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001078 */
1079 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001080 startActivity(intent, null);
1081 }
George Mountc03da0e2014-08-22 17:04:02 -07001082
Dianne Hackborna4972e92012-03-14 10:38:05 -07001083 /**
Scott Main87bff972013-02-27 15:46:34 -08001084 * Call {@link Activity#startActivity(Intent, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001085 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001086 *
1087 * @param intent The intent to start.
1088 * @param options Additional options for how the Activity should be started.
Elliot Waite54de7742017-01-11 15:30:35 -08001089 * See {@link android.content.Context#startActivity(Intent, Bundle)}
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001090 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07001091 */
1092 public void startActivity(Intent intent, Bundle options) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001093 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001094 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1095 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001096 if (options != null) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001097 mHost.onStartActivityFromFragment(this, intent, -1, options);
Dianne Hackborna4972e92012-03-14 10:38:05 -07001098 } else {
1099 // Note we want to go through this call for compatibility with
1100 // applications that may have overridden the method.
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001101 mHost.onStartActivityFromFragment(this, intent, -1, null /*options*/);
Dianne Hackborna4972e92012-03-14 10:38:05 -07001102 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001103 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001104
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001105 /**
Scott Main87bff972013-02-27 15:46:34 -08001106 * Call {@link Activity#startActivityForResult(Intent, int)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001107 * containing Activity.
1108 */
1109 public void startActivityForResult(Intent intent, int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001110 startActivityForResult(intent, requestCode, null);
1111 }
1112
1113 /**
Scott Main87bff972013-02-27 15:46:34 -08001114 * Call {@link Activity#startActivityForResult(Intent, int, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001115 * containing Activity.
1116 */
1117 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001118 if (mHost == null) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001119 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1120 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001121 mHost.onStartActivityFromFragment(this, intent, requestCode, options);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001122 }
George Mountc03da0e2014-08-22 17:04:02 -07001123
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001124 /**
Tony Mak96d26fe2017-04-11 20:05:39 +01001125 * @hide
1126 * Call {@link Activity#startActivityForResultAsUser(Intent, int, UserHandle)} from the
1127 * fragment's containing Activity.
1128 */
1129 public void startActivityForResultAsUser(
1130 Intent intent, int requestCode, Bundle options, UserHandle user) {
1131 if (mHost == null) {
1132 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1133 }
1134 mHost.onStartActivityAsUserFromFragment(this, intent, requestCode, options, user);
1135 }
1136
1137 /**
Clara Bayarria0c2dc32016-04-12 12:00:15 +01001138 * Call {@link Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int,
1139 * Bundle)} from the fragment's containing Activity.
1140 */
1141 public void startIntentSenderForResult(IntentSender intent, int requestCode,
1142 @Nullable Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags,
1143 Bundle options) throws IntentSender.SendIntentException {
1144 if (mHost == null) {
1145 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1146 }
1147 mHost.onStartIntentSenderFromFragment(this, intent, requestCode, fillInIntent, flagsMask,
1148 flagsValues, extraFlags, options);
1149 }
1150
1151 /**
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001152 * Receive the result from a previous call to
1153 * {@link #startActivityForResult(Intent, int)}. This follows the
1154 * related Activity API as described there in
1155 * {@link Activity#onActivityResult(int, int, Intent)}.
George Mountc03da0e2014-08-22 17:04:02 -07001156 *
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001157 * @param requestCode The integer request code originally supplied to
1158 * startActivityForResult(), allowing you to identify who this
1159 * result came from.
1160 * @param resultCode The integer result code returned by the child activity
1161 * through its setResult().
1162 * @param data An Intent, which can return result data to the caller
1163 * (various data can be attached to Intent "extras").
1164 */
1165 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1166 }
George Mountc03da0e2014-08-22 17:04:02 -07001167
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001168 /**
Svetoslavc6d1c342015-02-26 14:44:43 -08001169 * Requests permissions to be granted to this application. These permissions
1170 * must be requested in your manifest, they should not be granted to your app,
1171 * and they should have protection level {@link android.content.pm.PermissionInfo
1172 * #PROTECTION_DANGEROUS dangerous}, regardless whether they are declared by
1173 * the platform or a third-party app.
1174 * <p>
1175 * Normal permissions {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL}
1176 * are granted at install time if requested in the manifest. Signature permissions
1177 * {@link android.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at
1178 * install time if requested in the manifest and the signature of your app matches
1179 * the signature of the app declaring the permissions.
1180 * </p>
1181 * <p>
1182 * If your app does not have the requested permissions the user will be presented
1183 * with UI for accepting them. After the user has accepted or rejected the
1184 * requested permissions you will receive a callback on {@link
1185 * #onRequestPermissionsResult(int, String[], int[])} reporting whether the
1186 * permissions were granted or not.
1187 * </p>
1188 * <p>
1189 * Note that requesting a permission does not guarantee it will be granted and
1190 * your app should be able to run without having this permission.
1191 * </p>
1192 * <p>
1193 * This method may start an activity allowing the user to choose which permissions
1194 * to grant and which to reject. Hence, you should be prepared that your activity
1195 * may be paused and resumed. Further, granting some permissions may require
1196 * a restart of you application. In such a case, the system will recreate the
1197 * activity stack before delivering the result to {@link
1198 * #onRequestPermissionsResult(int, String[], int[])}.
1199 * </p>
1200 * <p>
1201 * When checking whether you have a permission you should use {@link
1202 * android.content.Context#checkSelfPermission(String)}.
1203 * </p>
1204 * <p>
Svet Ganovd7ad54e2015-08-21 06:18:09 -07001205 * Calling this API for permissions already granted to your app would show UI
1206 * to the user to decide whether the app can still hold these permissions. This
1207 * can be useful if the way your app uses data guarded by the permissions
1208 * changes significantly.
1209 * </p>
1210 * <p>
Svet Ganov255aece2015-07-03 16:03:53 -07001211 * You cannot request a permission if your activity sets {@link
1212 * android.R.styleable#AndroidManifestActivity_noHistory noHistory} to
1213 * <code>true</code> because in this case the activity would not receive
1214 * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}.
1215 * </p>
1216 * <p>
Svetoslavc6d1c342015-02-26 14:44:43 -08001217 * A sample permissions request looks like this:
1218 * </p>
1219 * <code><pre><p>
1220 * private void showContacts() {
1221 * if (getActivity().checkSelfPermission(Manifest.permission.READ_CONTACTS)
1222 * != PackageManager.PERMISSION_GRANTED) {
1223 * requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
1224 * PERMISSIONS_REQUEST_READ_CONTACTS);
1225 * } else {
1226 * doShowContacts();
1227 * }
1228 * }
1229 *
1230 * {@literal @}Override
1231 * public void onRequestPermissionsResult(int requestCode, String[] permissions,
1232 * int[] grantResults) {
1233 * if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS
1234 * && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
1235 * doShowContacts();
1236 * }
1237 * }
1238 * </code></pre></p>
1239 *
Svet Ganovf66381c2016-02-18 20:02:36 -08001240 * @param permissions The requested permissions. Must me non-null and not empty.
Svetoslavc6d1c342015-02-26 14:44:43 -08001241 * @param requestCode Application specific request code to match with a result
1242 * reported to {@link #onRequestPermissionsResult(int, String[], int[])}.
Svet Ganov255aece2015-07-03 16:03:53 -07001243 * Should be >= 0.
Svetoslavc6d1c342015-02-26 14:44:43 -08001244 *
1245 * @see #onRequestPermissionsResult(int, String[], int[])
1246 * @see android.content.Context#checkSelfPermission(String)
1247 */
1248 public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001249 if (mHost == null) {
Svetoslavc6d1c342015-02-26 14:44:43 -08001250 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1251 }
Svetoslav970b59c2015-06-09 16:05:21 -07001252 mHost.onRequestPermissionsFromFragment(this, permissions,requestCode);
Svetoslavc6d1c342015-02-26 14:44:43 -08001253 }
1254
1255 /**
1256 * Callback for the result from requesting permissions. This method
1257 * is invoked for every call on {@link #requestPermissions(String[], int)}.
Svet Ganov0f45c7c2015-07-16 16:28:51 -07001258 * <p>
1259 * <strong>Note:</strong> It is possible that the permissions request interaction
1260 * with the user is interrupted. In this case you will receive empty permissions
1261 * and results arrays which should be treated as a cancellation.
1262 * </p>
Svetoslavc6d1c342015-02-26 14:44:43 -08001263 *
1264 * @param requestCode The request code passed in {@link #requestPermissions(String[], int)}.
1265 * @param permissions The requested permissions. Never null.
1266 * @param grantResults The grant results for the corresponding permissions
1267 * which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
1268 * or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
1269 *
1270 * @see #requestPermissions(String[], int)
1271 */
1272 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
1273 @NonNull int[] grantResults) {
1274 /* callback - do nothing */
1275 }
1276
1277 /**
Svetoslava2011d22015-06-03 15:43:59 -07001278 * Gets whether you should show UI with rationale for requesting a permission.
1279 * You should do this only if you do not have the permission and the context in
1280 * which the permission is requested does not clearly communicate to the user
1281 * what would be the benefit from granting this permission.
1282 * <p>
1283 * For example, if you write a camera app, requesting the camera permission
1284 * would be expected by the user and no rationale for why it is requested is
1285 * needed. If however, the app needs location for tagging photos then a non-tech
1286 * savvy user may wonder how location is related to taking photos. In this case
1287 * you may choose to show UI with rationale of requesting this permission.
1288 * </p>
1289 *
1290 * @param permission A permission your app wants to request.
1291 * @return Whether you can show permission rationale UI.
1292 *
1293 * @see Context#checkSelfPermission(String)
1294 * @see #requestPermissions(String[], int)
1295 * @see #onRequestPermissionsResult(int, String[], int[])
1296 */
1297 public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
1298 if (mHost != null) {
Svet Ganov9e136b82015-07-16 09:58:38 -07001299 return mHost.getContext().getPackageManager()
1300 .shouldShowRequestPermissionRationale(permission);
Svetoslava2011d22015-06-03 15:43:59 -07001301 }
1302 return false;
1303 }
1304
1305 /**
George Mountc7146be2017-03-29 14:13:03 +00001306 * Returns the LayoutInflater used to inflate Views of this Fragment. The default
1307 * implementation will throw an exception if the Fragment is not attached.
1308 *
1309 * @return The LayoutInflater used to inflate Views of this Fragment.
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001310 */
George Mountc7146be2017-03-29 14:13:03 +00001311 public LayoutInflater onGetLayoutInflater(Bundle savedInstanceState) {
1312 if (mHost == null) {
1313 throw new IllegalStateException("onGetLayoutInflater() cannot be executed until the "
1314 + "Fragment is attached to the FragmentManager.");
1315 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001316 final LayoutInflater result = mHost.onGetLayoutInflater();
1317 if (mHost.onUseFragmentManagerInflaterFactory()) {
Adam Powell371a8092014-06-20 12:51:12 -07001318 getChildFragmentManager(); // Init if needed; use raw implementation below.
1319 result.setPrivateFactory(mChildFragmentManager.getLayoutInflaterFactory());
Adam Powell371a8092014-06-20 12:51:12 -07001320 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001321 return result;
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001322 }
George Mountc03da0e2014-08-22 17:04:02 -07001323
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001324 /**
George Mount3fd81292017-04-17 16:50:26 -07001325 * Returns the cached LayoutInflater used to inflate Views of this Fragment. If
1326 * {@link #onGetLayoutInflater(Bundle)} has not been called {@link #onGetLayoutInflater(Bundle)}
1327 * will be called with a {@code null} argument and that value will be cached.
1328 * <p>
1329 * The cached LayoutInflater will be replaced immediately prior to
1330 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} and cleared immediately after
1331 * {@link #onDetach()}.
1332 *
1333 * @return The LayoutInflater used to inflate Views of this Fragment.
1334 */
1335 public final LayoutInflater getLayoutInflater() {
1336 if (mLayoutInflater == null) {
1337 return performGetLayoutInflater(null);
1338 }
1339 return mLayoutInflater;
1340 }
1341
1342 /**
1343 * Calls {@link #onGetLayoutInflater(Bundle)} and caches the result for use by
1344 * {@link #getLayoutInflater()}.
1345 *
1346 * @param savedInstanceState If the fragment is being re-created from
1347 * a previous saved state, this is the state.
1348 * @return The LayoutInflater used to inflate Views of this Fragment.
1349 */
1350 LayoutInflater performGetLayoutInflater(Bundle savedInstanceState) {
1351 LayoutInflater layoutInflater = onGetLayoutInflater(savedInstanceState);
1352 mLayoutInflater = layoutInflater;
1353 return mLayoutInflater;
1354 }
1355
1356 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001357 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead.
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001358 */
1359 @Deprecated
Adam Powella9bab982016-04-21 11:04:41 -07001360 @CallSuper
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001361 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
1362 mCalled = true;
1363 }
1364
1365 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001366 * Called when a fragment is being created as part of a view layout
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001367 * inflation, typically from setting the content view of an activity. This
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001368 * may be called immediately after the fragment is created from a <fragment>
Dianne Hackborndef15372010-08-15 12:43:52 -07001369 * tag in a layout file. Note this is <em>before</em> the fragment's
1370 * {@link #onAttach(Activity)} has been called; all you should do here is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001371 * parse the attributes and save them away.
George Mountc03da0e2014-08-22 17:04:02 -07001372 *
Dianne Hackborndef15372010-08-15 12:43:52 -07001373 * <p>This is called every time the fragment is inflated, even if it is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001374 * being inflated into a new instance with saved state. It typically makes
1375 * sense to re-parse the parameters each time, to allow them to change with
1376 * different configurations.</p>
1377 *
1378 * <p>Here is a typical implementation of a fragment that can take parameters
1379 * both through attributes supplied here as well from {@link #getArguments()}:</p>
1380 *
1381 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1382 * fragment}
1383 *
1384 * <p>Note that parsing the XML attributes uses a "styleable" resource. The
1385 * declaration for the styleable used here is:</p>
1386 *
1387 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments}
Aurimas Liutikas6acbe072017-02-08 10:48:40 -08001388 *
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001389 * <p>The fragment can then be declared within its activity's content layout
1390 * through a tag like this:</p>
1391 *
1392 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes}
1393 *
1394 * <p>This fragment can also be created dynamically from arguments given
1395 * at runtime in the arguments Bundle; here is an example of doing so at
1396 * creation of the containing activity:</p>
1397 *
1398 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1399 * create}
1400 *
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001401 * @param context The Context that is inflating this fragment.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001402 * @param attrs The attributes at the tag where the fragment is
1403 * being created.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001404 * @param savedInstanceState If the fragment is being re-created from
1405 * a previous saved state, this is the state.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001406 */
Adam Powella9bab982016-04-21 11:04:41 -07001407 @CallSuper
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001408 public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001409 onInflate(attrs, savedInstanceState);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001410 mCalled = true;
George Mountc03da0e2014-08-22 17:04:02 -07001411
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001412 TypedArray a = context.obtainStyledAttributes(attrs,
George Mountc03da0e2014-08-22 17:04:02 -07001413 com.android.internal.R.styleable.Fragment);
George Mount86bfc662016-07-12 16:06:06 -07001414 setEnterTransition(loadTransition(context, a, getEnterTransition(), null,
1415 com.android.internal.R.styleable.Fragment_fragmentEnterTransition));
1416 setReturnTransition(loadTransition(context, a, getReturnTransition(),
George Mountc03da0e2014-08-22 17:04:02 -07001417 USE_DEFAULT_TRANSITION,
George Mount86bfc662016-07-12 16:06:06 -07001418 com.android.internal.R.styleable.Fragment_fragmentReturnTransition));
1419 setExitTransition(loadTransition(context, a, getExitTransition(), null,
1420 com.android.internal.R.styleable.Fragment_fragmentExitTransition));
1421
1422 setReenterTransition(loadTransition(context, a, getReenterTransition(),
1423 USE_DEFAULT_TRANSITION,
1424 com.android.internal.R.styleable.Fragment_fragmentReenterTransition));
1425 setSharedElementEnterTransition(loadTransition(context, a,
1426 getSharedElementEnterTransition(), null,
1427 com.android.internal.R.styleable.Fragment_fragmentSharedElementEnterTransition));
1428 setSharedElementReturnTransition(loadTransition(context, a,
1429 getSharedElementReturnTransition(), USE_DEFAULT_TRANSITION,
1430 com.android.internal.R.styleable.Fragment_fragmentSharedElementReturnTransition));
1431 boolean isEnterSet;
1432 boolean isReturnSet;
1433 if (mAnimationInfo == null) {
1434 isEnterSet = false;
1435 isReturnSet = false;
1436 } else {
1437 isEnterSet = mAnimationInfo.mAllowEnterTransitionOverlap != null;
1438 isReturnSet = mAnimationInfo.mAllowReturnTransitionOverlap != null;
George Mountc03da0e2014-08-22 17:04:02 -07001439 }
George Mount86bfc662016-07-12 16:06:06 -07001440 if (!isEnterSet) {
1441 setAllowEnterTransitionOverlap(a.getBoolean(
1442 com.android.internal.R.styleable.Fragment_fragmentAllowEnterTransitionOverlap,
1443 true));
1444 }
1445 if (!isReturnSet) {
1446 setAllowReturnTransitionOverlap(a.getBoolean(
1447 com.android.internal.R.styleable.Fragment_fragmentAllowReturnTransitionOverlap,
1448 true));
George Mountc03da0e2014-08-22 17:04:02 -07001449 }
1450 a.recycle();
Todd Kennedy434bd652015-05-04 12:29:50 -07001451
1452 final Activity hostActivity = mHost == null ? null : mHost.getActivity();
1453 if (hostActivity != null) {
1454 mCalled = false;
1455 onInflate(hostActivity, attrs, savedInstanceState);
1456 }
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001457 }
George Mountc03da0e2014-08-22 17:04:02 -07001458
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001459 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001460 * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead.
1461 */
1462 @Deprecated
Adam Powella9bab982016-04-21 11:04:41 -07001463 @CallSuper
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001464 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
1465 mCalled = true;
1466 }
1467
1468 /**
Adam Powelle30299f2016-05-13 11:04:39 -07001469 * Called when a fragment is attached as a child of this fragment.
1470 *
1471 * <p>This is called after the attached fragment's <code>onAttach</code> and before
1472 * the attached fragment's <code>onCreate</code> if the fragment has not yet had a previous
1473 * call to <code>onCreate</code>.</p>
1474 *
1475 * @param childFragment child fragment being attached
1476 */
1477 public void onAttachFragment(Fragment childFragment) {
1478 }
1479
1480 /**
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001481 * Called when a fragment is first attached to its context.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001482 * {@link #onCreate(Bundle)} will be called after this.
1483 */
Adam Powella9bab982016-04-21 11:04:41 -07001484 @CallSuper
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001485 public void onAttach(Context context) {
1486 mCalled = true;
1487 final Activity hostActivity = mHost == null ? null : mHost.getActivity();
1488 if (hostActivity != null) {
1489 mCalled = false;
1490 onAttach(hostActivity);
1491 }
1492 }
1493
1494 /**
1495 * @deprecated Use {@link #onAttach(Context)} instead.
1496 */
1497 @Deprecated
Adam Powella9bab982016-04-21 11:04:41 -07001498 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001499 public void onAttach(Activity activity) {
1500 mCalled = true;
1501 }
George Mountc03da0e2014-08-22 17:04:02 -07001502
Chet Haase811ed1062010-08-06 10:38:15 -07001503 /**
1504 * Called when a fragment loads an animation.
1505 */
Chet Haasea18a86b2010-09-07 13:20:00 -07001506 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001507 return null;
1508 }
George Mountc03da0e2014-08-22 17:04:02 -07001509
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001510 /**
1511 * Called to do initial creation of a fragment. This is called after
Dianne Hackbornc8017682010-07-06 13:34:38 -07001512 * {@link #onAttach(Activity)} and before
Felipe Lemeb1444572015-11-03 17:39:07 -08001513 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, but is not called if the fragment
1514 * instance is retained across Activity re-creation (see {@link #setRetainInstance(boolean)}).
George Mountc03da0e2014-08-22 17:04:02 -07001515 *
Dianne Hackbornc8017682010-07-06 13:34:38 -07001516 * <p>Note that this can be called while the fragment's activity is
1517 * still in the process of being created. As such, you can not rely
1518 * on things like the activity's content view hierarchy being initialized
1519 * at this point. If you want to do work once the activity itself is
1520 * created, see {@link #onActivityCreated(Bundle)}.
George Mountc03da0e2014-08-22 17:04:02 -07001521 *
Dianne Hackbornb34cbed2016-08-09 13:18:21 -07001522 * <p>If your app's <code>targetSdkVersion</code> is {@link android.os.Build.VERSION_CODES#M}
1523 * or lower, child fragments being restored from the savedInstanceState are restored after
Dianne Hackborn5f1649e2016-08-09 18:00:31 -07001524 * <code>onCreate</code> returns. When targeting {@link android.os.Build.VERSION_CODES#N} or
Dianne Hackbornb34cbed2016-08-09 13:18:21 -07001525 * above and running on an N or newer platform version
Adam Powell592df7d2016-01-14 12:58:23 -08001526 * they are restored by <code>Fragment.onCreate</code>.</p>
1527 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001528 * @param savedInstanceState If the fragment is being re-created from
1529 * a previous saved state, this is the state.
1530 */
Adam Powella9bab982016-04-21 11:04:41 -07001531 @CallSuper
Scott Kennedy3c32b622015-02-22 16:58:58 -08001532 public void onCreate(@Nullable Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001533 mCalled = true;
Adam Powell592df7d2016-01-14 12:58:23 -08001534 final Context context = getContext();
1535 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
1536 if (version >= Build.VERSION_CODES.N) {
Adam Powelleacacb52016-03-23 13:07:27 -07001537 restoreChildFragmentState(savedInstanceState, true);
Adam Powell467cc6f2016-05-11 13:45:33 -07001538 if (mChildFragmentManager != null
1539 && !mChildFragmentManager.isStateAtLeast(Fragment.CREATED)) {
1540 mChildFragmentManager.dispatchCreate();
1541 }
Adam Powelleacacb52016-03-23 13:07:27 -07001542 }
1543 }
1544
1545 void restoreChildFragmentState(@Nullable Bundle savedInstanceState, boolean provideNonConfig) {
1546 if (savedInstanceState != null) {
1547 Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG);
1548 if (p != null) {
1549 if (mChildFragmentManager == null) {
1550 instantiateChildFragmentManager();
Adam Powell592df7d2016-01-14 12:58:23 -08001551 }
Adam Powelleacacb52016-03-23 13:07:27 -07001552 mChildFragmentManager.restoreAllState(p, provideNonConfig ? mChildNonConfig : null);
1553 mChildNonConfig = null;
1554 mChildFragmentManager.dispatchCreate();
Adam Powell592df7d2016-01-14 12:58:23 -08001555 }
1556 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001557 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001558
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001559 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001560 * Called to have the fragment instantiate its user interface view.
1561 * This is optional, and non-graphical fragments can return null (which
1562 * is the default implementation). This will be called between
Dianne Hackbornc8017682010-07-06 13:34:38 -07001563 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
George Mountc03da0e2014-08-22 17:04:02 -07001564 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001565 * <p>If you return a View from here, you will later be called in
1566 * {@link #onDestroyView} when the view is being released.
George Mountc03da0e2014-08-22 17:04:02 -07001567 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001568 * @param inflater The LayoutInflater object that can be used to inflate
1569 * any views in the fragment,
1570 * @param container If non-null, this is the parent view that the fragment's
1571 * UI should be attached to. The fragment should not add the view itself,
1572 * but this can be used to generate the LayoutParams of the view.
1573 * @param savedInstanceState If non-null, this fragment is being re-constructed
1574 * from a previous saved state as given here.
George Mountc03da0e2014-08-22 17:04:02 -07001575 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001576 * @return Return the View for the fragment's UI, or null.
1577 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001578 @Nullable
1579 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001580 Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001581 return null;
1582 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001583
1584 /**
1585 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
1586 * has returned, but before any saved state has been restored in to the view.
1587 * This gives subclasses a chance to initialize themselves once
1588 * they know their view hierarchy has been completely created. The fragment's
1589 * view hierarchy is not however attached to its parent at this point.
1590 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1591 * @param savedInstanceState If non-null, this fragment is being re-constructed
1592 * from a previous saved state as given here.
1593 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001594 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001595 }
George Mountc03da0e2014-08-22 17:04:02 -07001596
Scott Main662cc7a42011-01-17 15:02:07 -08001597 /**
1598 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
1599 * if provided.
George Mountc03da0e2014-08-22 17:04:02 -07001600 *
Scott Main662cc7a42011-01-17 15:02:07 -08001601 * @return The fragment's root view, or null if it has no layout.
1602 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001603 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001604 public View getView() {
1605 return mView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001606 }
George Mountc03da0e2014-08-22 17:04:02 -07001607
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001608 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001609 * Called when the fragment's activity has been created and this
1610 * fragment's view hierarchy instantiated. It can be used to do final
1611 * initialization once these pieces are in place, such as retrieving
1612 * views or restoring state. It is also useful for fragments that use
1613 * {@link #setRetainInstance(boolean)} to retain their instance,
1614 * as this callback tells the fragment when it is fully associated with
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001615 * the new activity instance. This is called after {@link #onCreateView}
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001616 * and before {@link #onViewStateRestored(Bundle)}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001617 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001618 * @param savedInstanceState If the fragment is being re-created from
1619 * a previous saved state, this is the state.
1620 */
Adam Powella9bab982016-04-21 11:04:41 -07001621 @CallSuper
Scott Kennedy3c32b622015-02-22 16:58:58 -08001622 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001623 mCalled = true;
1624 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001625
1626 /**
1627 * Called when all saved state has been restored into the view hierarchy
1628 * of the fragment. This can be used to do initialization based on saved
1629 * state that you are letting the view hierarchy track itself, such as
1630 * whether check box widgets are currently checked. This is called
1631 * after {@link #onActivityCreated(Bundle)} and before
1632 * {@link #onStart()}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001633 *
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001634 * @param savedInstanceState If the fragment is being re-created from
1635 * a previous saved state, this is the state.
1636 */
Adam Powella9bab982016-04-21 11:04:41 -07001637 @CallSuper
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001638 public void onViewStateRestored(Bundle savedInstanceState) {
1639 mCalled = true;
1640 }
1641
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001642 /**
1643 * Called when the Fragment is visible to the user. This is generally
1644 * tied to {@link Activity#onStart() Activity.onStart} of the containing
1645 * Activity's lifecycle.
1646 */
Adam Powella9bab982016-04-21 11:04:41 -07001647 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001648 public void onStart() {
1649 mCalled = true;
George Mountc03da0e2014-08-22 17:04:02 -07001650
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001651 if (!mLoadersStarted) {
1652 mLoadersStarted = true;
1653 if (!mCheckedForLoaderManager) {
1654 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001655 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
George Mount550df702017-03-23 09:08:49 -07001656 } else if (mLoaderManager != null) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001657 mLoaderManager.doStart();
1658 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001659 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001660 }
George Mountc03da0e2014-08-22 17:04:02 -07001661
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001662 /**
1663 * Called when the fragment is visible to the user and actively running.
1664 * This is generally
1665 * tied to {@link Activity#onResume() Activity.onResume} of the containing
1666 * Activity's lifecycle.
1667 */
Adam Powella9bab982016-04-21 11:04:41 -07001668 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001669 public void onResume() {
1670 mCalled = true;
1671 }
George Mountc03da0e2014-08-22 17:04:02 -07001672
Dianne Hackborn72778202010-08-20 18:26:01 -07001673 /**
1674 * Called to ask the fragment to save its current dynamic state, so it
1675 * can later be reconstructed in a new instance of its process is
1676 * restarted. If a new instance of the fragment later needs to be
1677 * created, the data you place in the Bundle here will be available
1678 * in the Bundle given to {@link #onCreate(Bundle)},
1679 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
1680 * {@link #onActivityCreated(Bundle)}.
1681 *
1682 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
Daisuke Miyakawa2f761762010-09-12 16:53:17 -07001683 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
Dianne Hackborn72778202010-08-20 18:26:01 -07001684 * applies here as well. Note however: <em>this method may be called
1685 * at any time before {@link #onDestroy()}</em>. There are many situations
1686 * where a fragment may be mostly torn down (such as when placed on the
1687 * back stack with no UI showing), but its state will not be saved until
1688 * its owning activity actually needs to save its state.
1689 *
1690 * @param outState Bundle in which to place your saved state.
1691 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001692 public void onSaveInstanceState(Bundle outState) {
1693 }
George Mountc03da0e2014-08-22 17:04:02 -07001694
Wale Ogunwale7c796812016-01-29 21:13:50 -08001695 /**
1696 * Called when the Fragment's activity changes from fullscreen mode to multi-window mode and
Andrii Kulian933076d2016-03-29 17:04:42 -07001697 * visa-versa. This is generally tied to {@link Activity#onMultiWindowModeChanged} of the
Winson Chung5af42fc2017-03-24 17:11:33 -07001698 * containing Activity. This method provides the same configuration that will be sent in the
1699 * following {@link #onConfigurationChanged(Configuration)} call after the activity enters this
1700 * mode.
1701 *
1702 * @param isInMultiWindowMode True if the activity is in multi-window mode.
1703 * @param newConfig The new configuration of the activity with the state
1704 * {@param isInMultiWindowMode}.
1705 */
1706 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
1707 onMultiWindowModeChanged(isInMultiWindowMode);
1708 }
1709
1710 /**
1711 * Called when the Fragment's activity changes from fullscreen mode to multi-window mode and
1712 * visa-versa. This is generally tied to {@link Activity#onMultiWindowModeChanged} of the
Andrii Kulian933076d2016-03-29 17:04:42 -07001713 * containing Activity.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001714 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001715 * @param isInMultiWindowMode True if the activity is in multi-window mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07001716 *
1717 * @deprecated Use {@link #onMultiWindowModeChanged(boolean, Configuration)} instead.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001718 */
Winson Chung5af42fc2017-03-24 17:11:33 -07001719 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07001720 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
Wale Ogunwale7c796812016-01-29 21:13:50 -08001721 }
1722
1723 /**
1724 * Called by the system when the activity changes to and from picture-in-picture mode. This is
Andrii Kulian933076d2016-03-29 17:04:42 -07001725 * generally tied to {@link Activity#onPictureInPictureModeChanged} of the containing Activity.
Winson Chung5af42fc2017-03-24 17:11:33 -07001726 * This method provides the same configuration that will be sent in the following
1727 * {@link #onConfigurationChanged(Configuration)} call after the activity enters this mode.
1728 *
1729 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
1730 * @param newConfig The new configuration of the activity with the state
1731 * {@param isInPictureInPictureMode}.
1732 */
1733 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode,
1734 Configuration newConfig) {
1735 onPictureInPictureModeChanged(isInPictureInPictureMode);
1736 }
1737
1738 /**
1739 * Called by the system when the activity changes to and from picture-in-picture mode. This is
1740 * generally tied to {@link Activity#onPictureInPictureModeChanged} of the containing Activity.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001741 *
Andrii Kulian933076d2016-03-29 17:04:42 -07001742 * @param isInPictureInPictureMode True if the activity is in picture-in-picture mode.
Winson Chung5af42fc2017-03-24 17:11:33 -07001743 *
1744 * @deprecated Use {@link #onPictureInPictureModeChanged(boolean, Configuration)} instead.
Wale Ogunwale7c796812016-01-29 21:13:50 -08001745 */
Winson Chung5af42fc2017-03-24 17:11:33 -07001746 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07001747 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
Wale Ogunwale7c796812016-01-29 21:13:50 -08001748 }
1749
Adam Powella9bab982016-04-21 11:04:41 -07001750 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001751 public void onConfigurationChanged(Configuration newConfig) {
1752 mCalled = true;
1753 }
George Mountc03da0e2014-08-22 17:04:02 -07001754
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001755 /**
1756 * Called when the Fragment is no longer resumed. This is generally
1757 * tied to {@link Activity#onPause() Activity.onPause} of the containing
1758 * Activity's lifecycle.
1759 */
Adam Powella9bab982016-04-21 11:04:41 -07001760 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001761 public void onPause() {
1762 mCalled = true;
1763 }
George Mountc03da0e2014-08-22 17:04:02 -07001764
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001765 /**
1766 * Called when the Fragment is no longer started. This is generally
1767 * tied to {@link Activity#onStop() Activity.onStop} of the containing
1768 * Activity's lifecycle.
1769 */
Adam Powella9bab982016-04-21 11:04:41 -07001770 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001771 public void onStop() {
1772 mCalled = true;
1773 }
George Mountc03da0e2014-08-22 17:04:02 -07001774
Adam Powella9bab982016-04-21 11:04:41 -07001775 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001776 public void onLowMemory() {
1777 mCalled = true;
1778 }
George Mountc03da0e2014-08-22 17:04:02 -07001779
Adam Powella9bab982016-04-21 11:04:41 -07001780 @CallSuper
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001781 public void onTrimMemory(int level) {
1782 mCalled = true;
1783 }
1784
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001785 /**
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001786 * Called when the view previously created by {@link #onCreateView} has
1787 * been detached from the fragment. The next time the fragment needs
1788 * to be displayed, a new view will be created. This is called
Dianne Hackborndef15372010-08-15 12:43:52 -07001789 * after {@link #onStop()} and before {@link #onDestroy()}. It is called
1790 * <em>regardless</em> of whether {@link #onCreateView} returned a
1791 * non-null view. Internally it is called after the view's state has
1792 * been saved but before it has been removed from its parent.
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001793 */
Adam Powella9bab982016-04-21 11:04:41 -07001794 @CallSuper
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001795 public void onDestroyView() {
1796 mCalled = true;
1797 }
George Mountc03da0e2014-08-22 17:04:02 -07001798
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001799 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001800 * Called when the fragment is no longer in use. This is called
1801 * after {@link #onStop()} and before {@link #onDetach()}.
1802 */
Adam Powella9bab982016-04-21 11:04:41 -07001803 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001804 public void onDestroy() {
1805 mCalled = true;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001806 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager
1807 // + " mLoaderManager=" + mLoaderManager);
1808 if (!mCheckedForLoaderManager) {
1809 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001810 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001811 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001812 if (mLoaderManager != null) {
1813 mLoaderManager.doDestroy();
1814 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001815 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001816
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001817 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001818 * Called by the fragment manager once this fragment has been removed,
1819 * so that we don't have any left-over state if the application decides
1820 * to re-use the instance. This only clears state that the framework
1821 * internally manages, not things the application sets.
1822 */
1823 void initState() {
1824 mIndex = -1;
1825 mWho = null;
1826 mAdded = false;
1827 mRemoving = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001828 mFromLayout = false;
1829 mInLayout = false;
1830 mRestored = false;
1831 mBackStackNesting = 0;
1832 mFragmentManager = null;
Tim Kilbourn70717862014-02-28 01:00:06 -08001833 mChildFragmentManager = null;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07001834 mHost = null;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001835 mFragmentId = 0;
1836 mContainerId = 0;
1837 mTag = null;
1838 mHidden = false;
1839 mDetached = false;
1840 mRetaining = false;
1841 mLoaderManager = null;
1842 mLoadersStarted = false;
1843 mCheckedForLoaderManager = false;
1844 }
1845
1846 /**
Felipe Lemeb1444572015-11-03 17:39:07 -08001847 * Called when the fragment is no longer attached to its activity. This is called after
1848 * {@link #onDestroy()}, except in the cases where the fragment instance is retained across
1849 * Activity re-creation (see {@link #setRetainInstance(boolean)}), in which case it is called
1850 * after {@link #onStop()}.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001851 */
Adam Powella9bab982016-04-21 11:04:41 -07001852 @CallSuper
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001853 public void onDetach() {
1854 mCalled = true;
1855 }
George Mountc03da0e2014-08-22 17:04:02 -07001856
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001857 /**
1858 * Initialize the contents of the Activity's standard options menu. You
1859 * should place your menu items in to <var>menu</var>. For this method
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001860 * to be called, you must have first called {@link #setHasOptionsMenu}. See
1861 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
1862 * for more information.
George Mountc03da0e2014-08-22 17:04:02 -07001863 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001864 * @param menu The options menu in which you place your items.
George Mountc03da0e2014-08-22 17:04:02 -07001865 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001866 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001867 * @see #onPrepareOptionsMenu
1868 * @see #onOptionsItemSelected
1869 */
1870 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1871 }
1872
1873 /**
1874 * Prepare the Screen's standard options menu to be displayed. This is
1875 * called right before the menu is shown, every time it is shown. You can
1876 * use this method to efficiently enable/disable items or otherwise
1877 * dynamically modify the contents. See
1878 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu}
1879 * for more information.
George Mountc03da0e2014-08-22 17:04:02 -07001880 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001881 * @param menu The options menu as last shown or first initialized by
1882 * onCreateOptionsMenu().
George Mountc03da0e2014-08-22 17:04:02 -07001883 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001884 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001885 * @see #onCreateOptionsMenu
1886 */
1887 public void onPrepareOptionsMenu(Menu menu) {
1888 }
1889
1890 /**
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001891 * Called when this fragment's option menu items are no longer being
1892 * included in the overall options menu. Receiving this call means that
1893 * the menu needed to be rebuilt, but this fragment's items were not
1894 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
1895 * was not called).
1896 */
1897 public void onDestroyOptionsMenu() {
1898 }
George Mountc03da0e2014-08-22 17:04:02 -07001899
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001900 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001901 * This hook is called whenever an item in your options menu is selected.
1902 * The default implementation simply returns false to have the normal
1903 * processing happen (calling the item's Runnable or sending a message to
1904 * its Handler as appropriate). You can use this method for any items
1905 * for which you would like to do processing without those other
1906 * facilities.
George Mountc03da0e2014-08-22 17:04:02 -07001907 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001908 * <p>Derived classes should call through to the base class for it to
1909 * perform the default menu handling.
George Mountc03da0e2014-08-22 17:04:02 -07001910 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001911 * @param item The menu item that was selected.
George Mountc03da0e2014-08-22 17:04:02 -07001912 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001913 * @return boolean Return false to allow normal menu processing to
1914 * proceed, true to consume it here.
George Mountc03da0e2014-08-22 17:04:02 -07001915 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001916 * @see #onCreateOptionsMenu
1917 */
1918 public boolean onOptionsItemSelected(MenuItem item) {
1919 return false;
1920 }
1921
1922 /**
1923 * This hook is called whenever the options menu is being closed (either by the user canceling
1924 * the menu with the back/menu button, or when an item is selected).
George Mountc03da0e2014-08-22 17:04:02 -07001925 *
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001926 * @param menu The options menu as last shown or first initialized by
1927 * onCreateOptionsMenu().
1928 */
1929 public void onOptionsMenuClosed(Menu menu) {
1930 }
George Mountc03da0e2014-08-22 17:04:02 -07001931
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001932 /**
1933 * Called when a context menu for the {@code view} is about to be shown.
1934 * Unlike {@link #onCreateOptionsMenu}, this will be called every
1935 * time the context menu is about to be shown and should be populated for
1936 * the view (or item inside the view for {@link AdapterView} subclasses,
1937 * this can be found in the {@code menuInfo})).
1938 * <p>
1939 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
1940 * item has been selected.
1941 * <p>
1942 * The default implementation calls up to
1943 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
1944 * you can not call this implementation if you don't want that behavior.
1945 * <p>
1946 * It is not safe to hold onto the context menu after this method returns.
1947 * {@inheritDoc}
1948 */
1949 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1950 getActivity().onCreateContextMenu(menu, v, menuInfo);
1951 }
1952
1953 /**
1954 * Registers a context menu to be shown for the given view (multiple views
1955 * can show the context menu). This method will set the
1956 * {@link OnCreateContextMenuListener} on the view to this fragment, so
1957 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
1958 * called when it is time to show the context menu.
George Mountc03da0e2014-08-22 17:04:02 -07001959 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001960 * @see #unregisterForContextMenu(View)
1961 * @param view The view that should show a context menu.
1962 */
1963 public void registerForContextMenu(View view) {
1964 view.setOnCreateContextMenuListener(this);
1965 }
George Mountc03da0e2014-08-22 17:04:02 -07001966
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001967 /**
1968 * Prevents a context menu to be shown for the given view. This method will
1969 * remove the {@link OnCreateContextMenuListener} on the view.
George Mountc03da0e2014-08-22 17:04:02 -07001970 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001971 * @see #registerForContextMenu(View)
1972 * @param view The view that should stop showing a context menu.
1973 */
1974 public void unregisterForContextMenu(View view) {
1975 view.setOnCreateContextMenuListener(null);
1976 }
George Mountc03da0e2014-08-22 17:04:02 -07001977
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001978 /**
1979 * This hook is called whenever an item in a context menu is selected. The
1980 * default implementation simply returns false to have the normal processing
1981 * happen (calling the item's Runnable or sending a message to its Handler
1982 * as appropriate). You can use this method for any items for which you
1983 * would like to do processing without those other facilities.
1984 * <p>
1985 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
1986 * View that added this menu item.
1987 * <p>
1988 * Derived classes should call through to the base class for it to perform
1989 * the default menu handling.
George Mountc03da0e2014-08-22 17:04:02 -07001990 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001991 * @param item The context menu item that was selected.
1992 * @return boolean Return false to allow normal context menu processing to
1993 * proceed, true to consume it here.
1994 */
1995 public boolean onContextItemSelected(MenuItem item) {
1996 return false;
1997 }
George Mountc03da0e2014-08-22 17:04:02 -07001998
1999 /**
George Mount65580562014-08-29 08:15:48 -07002000 * When custom transitions are used with Fragments, the enter transition callback
George Mountc03da0e2014-08-22 17:04:02 -07002001 * is called when this Fragment is attached or detached when not popping the back stack.
2002 *
George Mount65580562014-08-29 08:15:48 -07002003 * @param callback Used to manipulate the shared element transitions on this Fragment
George Mountc03da0e2014-08-22 17:04:02 -07002004 * when added not as a pop from the back stack.
2005 */
George Mount85ea4982014-09-12 14:16:18 -07002006 public void setEnterSharedElementCallback(SharedElementCallback callback) {
George Mount65580562014-08-29 08:15:48 -07002007 if (callback == null) {
George Mount86bfc662016-07-12 16:06:06 -07002008 if (mAnimationInfo == null) {
2009 return; // already a null callback
2010 }
George Mount65580562014-08-29 08:15:48 -07002011 callback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -07002012 }
George Mount86bfc662016-07-12 16:06:06 -07002013 ensureAnimationInfo().mEnterTransitionCallback = callback;
George Mount85ea4982014-09-12 14:16:18 -07002014 }
2015
2016 /**
George Mount65580562014-08-29 08:15:48 -07002017 * When custom transitions are used with Fragments, the exit transition callback
George Mountc03da0e2014-08-22 17:04:02 -07002018 * is called when this Fragment is attached or detached when popping the back stack.
2019 *
George Mount65580562014-08-29 08:15:48 -07002020 * @param callback Used to manipulate the shared element transitions on this Fragment
George Mountc03da0e2014-08-22 17:04:02 -07002021 * when added as a pop from the back stack.
2022 */
George Mount85ea4982014-09-12 14:16:18 -07002023 public void setExitSharedElementCallback(SharedElementCallback callback) {
George Mount65580562014-08-29 08:15:48 -07002024 if (callback == null) {
George Mount86bfc662016-07-12 16:06:06 -07002025 if (mAnimationInfo == null) {
2026 return; // already a null callback
2027 }
George Mount65580562014-08-29 08:15:48 -07002028 callback = SharedElementCallback.NULL_CALLBACK;
George Mountc03da0e2014-08-22 17:04:02 -07002029 }
George Mount86bfc662016-07-12 16:06:06 -07002030 ensureAnimationInfo().mExitTransitionCallback = callback;
George Mount85ea4982014-09-12 14:16:18 -07002031 }
2032
2033 /**
George Mountc03da0e2014-08-22 17:04:02 -07002034 * Sets the Transition that will be used to move Views into the initial scene. The entering
2035 * Views will be those that are regular Views or ViewGroups that have
2036 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2037 * {@link android.transition.Visibility} as entering is governed by changing visibility from
2038 * {@link View#INVISIBLE} to {@link View#VISIBLE}. If <code>transition</code> is null,
2039 * entering Views will remain unaffected.
2040 *
2041 * @param transition The Transition to use to move Views into the initial Scene.
2042 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition
2043 */
2044 public void setEnterTransition(Transition transition) {
George Mount86bfc662016-07-12 16:06:06 -07002045 if (shouldChangeTransition(transition, null)) {
2046 ensureAnimationInfo().mEnterTransition = transition;
2047 }
George Mountc03da0e2014-08-22 17:04:02 -07002048 }
2049
2050 /**
2051 * Returns the Transition that will be used to move Views into the initial scene. The entering
2052 * Views will be those that are regular Views or ViewGroups that have
2053 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2054 * {@link android.transition.Visibility} as entering is governed by changing visibility from
2055 * {@link View#INVISIBLE} to {@link View#VISIBLE}.
2056 *
2057 * @return the Transition to use to move Views into the initial Scene.
2058 * @attr ref android.R.styleable#Fragment_fragmentEnterTransition
2059 */
2060 public Transition getEnterTransition() {
George Mount86bfc662016-07-12 16:06:06 -07002061 if (mAnimationInfo == null) {
2062 return null;
2063 }
2064 return mAnimationInfo.mEnterTransition;
George Mountc03da0e2014-08-22 17:04:02 -07002065 }
2066
2067 /**
2068 * Sets the Transition that will be used to move Views out of the scene when the Fragment is
2069 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting
2070 * Views will be those that are regular Views or ViewGroups that have
2071 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2072 * {@link android.transition.Visibility} as entering is governed by changing visibility from
2073 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null,
2074 * entering Views will remain unaffected. If nothing is set, the default will be to
2075 * use the same value as set in {@link #setEnterTransition(android.transition.Transition)}.
2076 *
2077 * @param transition The Transition to use to move Views out of the Scene when the Fragment
2078 * is preparing to close.
2079 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2080 */
2081 public void setReturnTransition(Transition transition) {
George Mount86bfc662016-07-12 16:06:06 -07002082 if (shouldChangeTransition(transition, USE_DEFAULT_TRANSITION)) {
2083 ensureAnimationInfo().mReturnTransition = transition;
2084 }
George Mountc03da0e2014-08-22 17:04:02 -07002085 }
2086
2087 /**
2088 * Returns the Transition that will be used to move Views out of the scene when the Fragment is
2089 * preparing to be removed, hidden, or detached because of popping the back stack. The exiting
2090 * Views will be those that are regular Views or ViewGroups that have
2091 * {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2092 * {@link android.transition.Visibility} as entering is governed by changing visibility from
2093 * {@link View#VISIBLE} to {@link View#INVISIBLE}. If <code>transition</code> is null,
2094 * entering Views will remain unaffected.
2095 *
2096 * @return the Transition to use to move Views out of the Scene when the Fragment
2097 * is preparing to close.
2098 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2099 */
2100 public Transition getReturnTransition() {
George Mount86bfc662016-07-12 16:06:06 -07002101 if (mAnimationInfo == null) {
2102 return null;
2103 }
2104 return mAnimationInfo.mReturnTransition == USE_DEFAULT_TRANSITION ? getEnterTransition()
2105 : mAnimationInfo.mReturnTransition;
George Mountc03da0e2014-08-22 17:04:02 -07002106 }
2107
2108 /**
2109 * Sets the Transition that will be used to move Views out of the scene when the
2110 * fragment is removed, hidden, or detached when not popping the back stack.
2111 * The exiting Views will be those that are regular Views or ViewGroups that
2112 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2113 * {@link android.transition.Visibility} as exiting is governed by changing visibility
2114 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will
2115 * remain unaffected.
2116 *
2117 * @param transition The Transition to use to move Views out of the Scene when the Fragment
2118 * is being closed not due to popping the back stack.
2119 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2120 */
2121 public void setExitTransition(Transition transition) {
George Mount86bfc662016-07-12 16:06:06 -07002122 if (shouldChangeTransition(transition, null)) {
2123 ensureAnimationInfo().mExitTransition = transition;
2124 }
George Mountc03da0e2014-08-22 17:04:02 -07002125 }
2126
2127 /**
2128 * Returns the Transition that will be used to move Views out of the scene when the
2129 * fragment is removed, hidden, or detached when not popping the back stack.
2130 * The exiting Views will be those that are regular Views or ViewGroups that
2131 * have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions will extend
2132 * {@link android.transition.Visibility} as exiting is governed by changing visibility
2133 * from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null, the views will
2134 * remain unaffected.
2135 *
2136 * @return the Transition to use to move Views out of the Scene when the Fragment
2137 * is being closed not due to popping the back stack.
2138 * @attr ref android.R.styleable#Fragment_fragmentExitTransition
2139 */
2140 public Transition getExitTransition() {
George Mount86bfc662016-07-12 16:06:06 -07002141 if (mAnimationInfo == null) {
2142 return null;
2143 }
2144 return mAnimationInfo.mExitTransition;
George Mountc03da0e2014-08-22 17:04:02 -07002145 }
2146
2147 /**
2148 * Sets the Transition that will be used to move Views in to the scene when returning due
2149 * to popping a back stack. The entering Views will be those that are regular Views
2150 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions
2151 * will extend {@link android.transition.Visibility} as exiting is governed by changing
2152 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null,
2153 * the views will remain unaffected. If nothing is set, the default will be to use the same
2154 * transition as {@link #setExitTransition(android.transition.Transition)}.
2155 *
2156 * @param transition The Transition to use to move Views into the scene when reentering from a
2157 * previously-started Activity.
2158 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition
2159 */
2160 public void setReenterTransition(Transition transition) {
George Mount86bfc662016-07-12 16:06:06 -07002161 if (shouldChangeTransition(transition, USE_DEFAULT_TRANSITION)) {
2162 ensureAnimationInfo().mReenterTransition = transition;
2163 }
George Mountc03da0e2014-08-22 17:04:02 -07002164 }
2165
2166 /**
2167 * Returns the Transition that will be used to move Views in to the scene when returning due
2168 * to popping a back stack. The entering Views will be those that are regular Views
2169 * or ViewGroups that have {@link ViewGroup#isTransitionGroup} return true. Typical Transitions
2170 * will extend {@link android.transition.Visibility} as exiting is governed by changing
2171 * visibility from {@link View#VISIBLE} to {@link View#INVISIBLE}. If transition is null,
2172 * the views will remain unaffected. If nothing is set, the default will be to use the same
2173 * transition as {@link #setExitTransition(android.transition.Transition)}.
2174 *
2175 * @return the Transition to use to move Views into the scene when reentering from a
2176 * previously-started Activity.
2177 * @attr ref android.R.styleable#Fragment_fragmentReenterTransition
2178 */
2179 public Transition getReenterTransition() {
George Mount86bfc662016-07-12 16:06:06 -07002180 if (mAnimationInfo == null) {
2181 return null;
2182 }
2183 return mAnimationInfo.mReenterTransition == USE_DEFAULT_TRANSITION ? getExitTransition()
2184 : mAnimationInfo.mReenterTransition;
George Mountc03da0e2014-08-22 17:04:02 -07002185 }
2186
2187 /**
2188 * Sets the Transition that will be used for shared elements transferred into the content
2189 * Scene. Typical Transitions will affect size and location, such as
2190 * {@link android.transition.ChangeBounds}. A null
2191 * value will cause transferred shared elements to blink to the final position.
2192 *
2193 * @param transition The Transition to use for shared elements transferred into the content
2194 * Scene.
2195 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition
2196 */
2197 public void setSharedElementEnterTransition(Transition transition) {
George Mount86bfc662016-07-12 16:06:06 -07002198 if (shouldChangeTransition(transition, null)) {
2199 ensureAnimationInfo().mSharedElementEnterTransition = transition;
2200 }
George Mountc03da0e2014-08-22 17:04:02 -07002201 }
2202
2203 /**
2204 * Returns the Transition that will be used for shared elements transferred into the content
2205 * Scene. Typical Transitions will affect size and location, such as
2206 * {@link android.transition.ChangeBounds}. A null
2207 * value will cause transferred shared elements to blink to the final position.
2208 *
2209 * @return The Transition to use for shared elements transferred into the content
2210 * Scene.
2211 * @attr ref android.R.styleable#Fragment_fragmentSharedElementEnterTransition
2212 */
2213 public Transition getSharedElementEnterTransition() {
George Mount86bfc662016-07-12 16:06:06 -07002214 if (mAnimationInfo == null) {
2215 return null;
2216 }
2217 return mAnimationInfo.mSharedElementEnterTransition;
George Mountc03da0e2014-08-22 17:04:02 -07002218 }
2219
2220 /**
2221 * Sets the Transition that will be used for shared elements transferred back during a
2222 * pop of the back stack. This Transition acts in the leaving Fragment.
2223 * Typical Transitions will affect size and location, such as
2224 * {@link android.transition.ChangeBounds}. A null
2225 * value will cause transferred shared elements to blink to the final position.
2226 * If no value is set, the default will be to use the same value as
2227 * {@link #setSharedElementEnterTransition(android.transition.Transition)}.
2228 *
2229 * @param transition The Transition to use for shared elements transferred out of the content
2230 * Scene.
2231 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition
2232 */
2233 public void setSharedElementReturnTransition(Transition transition) {
George Mount86bfc662016-07-12 16:06:06 -07002234 if (shouldChangeTransition(transition, USE_DEFAULT_TRANSITION)) {
2235 ensureAnimationInfo().mSharedElementReturnTransition = transition;
2236 }
George Mountc03da0e2014-08-22 17:04:02 -07002237 }
2238
2239 /**
2240 * Return the Transition that will be used for shared elements transferred back during a
2241 * pop of the back stack. This Transition acts in the leaving Fragment.
2242 * Typical Transitions will affect size and location, such as
2243 * {@link android.transition.ChangeBounds}. A null
2244 * value will cause transferred shared elements to blink to the final position.
2245 * If no value is set, the default will be to use the same value as
2246 * {@link #setSharedElementEnterTransition(android.transition.Transition)}.
2247 *
2248 * @return The Transition to use for shared elements transferred out of the content
2249 * Scene.
2250 * @attr ref android.R.styleable#Fragment_fragmentSharedElementReturnTransition
2251 */
2252 public Transition getSharedElementReturnTransition() {
George Mount86bfc662016-07-12 16:06:06 -07002253 if (mAnimationInfo == null) {
2254 return null;
2255 }
2256 return mAnimationInfo.mSharedElementReturnTransition == USE_DEFAULT_TRANSITION
2257 ? getSharedElementEnterTransition()
2258 : mAnimationInfo.mSharedElementReturnTransition;
George Mountc03da0e2014-08-22 17:04:02 -07002259 }
2260
2261 /**
koprivaa1a78482018-10-09 10:09:23 -07002262 * Sets whether the exit transition and enter transition overlap or not.
George Mountc03da0e2014-08-22 17:04:02 -07002263 * When true, the enter transition will start as soon as possible. When false, the
2264 * enter transition will wait until the exit transition completes before starting.
2265 *
2266 * @param allow true to start the enter transition when possible or false to
2267 * wait until the exiting transition completes.
2268 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap
2269 */
2270 public void setAllowEnterTransitionOverlap(boolean allow) {
George Mount86bfc662016-07-12 16:06:06 -07002271 ensureAnimationInfo().mAllowEnterTransitionOverlap = allow;
George Mountc03da0e2014-08-22 17:04:02 -07002272 }
2273
2274 /**
koprivaa1a78482018-10-09 10:09:23 -07002275 * Returns whether the exit transition and enter transition overlap or not.
George Mountc03da0e2014-08-22 17:04:02 -07002276 * When true, the enter transition will start as soon as possible. When false, the
2277 * enter transition will wait until the exit transition completes before starting.
2278 *
2279 * @return true when the enter transition should start as soon as possible or false to
2280 * when it should wait until the exiting transition completes.
2281 * @attr ref android.R.styleable#Fragment_fragmentAllowEnterTransitionOverlap
2282 */
2283 public boolean getAllowEnterTransitionOverlap() {
George Mount86bfc662016-07-12 16:06:06 -07002284 return (mAnimationInfo == null || mAnimationInfo.mAllowEnterTransitionOverlap == null)
2285 ? true : mAnimationInfo.mAllowEnterTransitionOverlap;
George Mountc03da0e2014-08-22 17:04:02 -07002286 }
2287
2288 /**
koprivaa1a78482018-10-09 10:09:23 -07002289 * Sets whether the return transition and reenter transition overlap or not.
George Mountc03da0e2014-08-22 17:04:02 -07002290 * When true, the reenter transition will start as soon as possible. When false, the
2291 * reenter transition will wait until the return transition completes before starting.
2292 *
2293 * @param allow true to start the reenter transition when possible or false to wait until the
2294 * return transition completes.
2295 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap
2296 */
2297 public void setAllowReturnTransitionOverlap(boolean allow) {
George Mount86bfc662016-07-12 16:06:06 -07002298 ensureAnimationInfo().mAllowReturnTransitionOverlap = allow;
George Mountc03da0e2014-08-22 17:04:02 -07002299 }
2300
2301 /**
koprivaa1a78482018-10-09 10:09:23 -07002302 * Returns whether the return transition and reenter transition overlap or not.
George Mountc03da0e2014-08-22 17:04:02 -07002303 * When true, the reenter transition will start as soon as possible. When false, the
2304 * reenter transition will wait until the return transition completes before starting.
2305 *
2306 * @return true to start the reenter transition when possible or false to wait until the
2307 * return transition completes.
2308 * @attr ref android.R.styleable#Fragment_fragmentAllowReturnTransitionOverlap
2309 */
2310 public boolean getAllowReturnTransitionOverlap() {
George Mount86bfc662016-07-12 16:06:06 -07002311 return (mAnimationInfo == null || mAnimationInfo.mAllowReturnTransitionOverlap == null)
2312 ? true : mAnimationInfo.mAllowReturnTransitionOverlap;
2313 }
2314
2315 /**
2316 * Postpone the entering Fragment transition until {@link #startPostponedEnterTransition()}
2317 * or {@link FragmentManager#executePendingTransactions()} has been called.
2318 * <p>
2319 * This method gives the Fragment the ability to delay Fragment animations
2320 * until all data is loaded. Until then, the added, shown, and
2321 * attached Fragments will be INVISIBLE and removed, hidden, and detached Fragments won't
2322 * be have their Views removed. The transaction runs when all postponed added Fragments in the
2323 * transaction have called {@link #startPostponedEnterTransition()}.
2324 * <p>
2325 * This method should be called before being added to the FragmentTransaction or
Scott Mainbda0cc92018-03-06 14:57:07 -08002326 * in {@link #onCreate(Bundle)}, {@link #onAttach(Context)}, or
George Mount86bfc662016-07-12 16:06:06 -07002327 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}}.
2328 * {@link #startPostponedEnterTransition()} must be called to allow the Fragment to
2329 * start the transitions.
2330 * <p>
2331 * When a FragmentTransaction is started that may affect a postponed FragmentTransaction,
2332 * based on which containers are in their operations, the postponed FragmentTransaction
2333 * will have its start triggered. The early triggering may result in faulty or nonexistent
2334 * animations in the postponed transaction. FragmentTransactions that operate only on
2335 * independent containers will not interfere with each other's postponement.
2336 * <p>
2337 * Calling postponeEnterTransition on Fragments with a null View will not postpone the
2338 * transition. Likewise, postponement only works if FragmentTransaction optimizations are
2339 * enabled.
2340 *
2341 * @see Activity#postponeEnterTransition()
George Mounta0ffaff2017-04-26 13:10:59 -07002342 * @see FragmentTransaction#setReorderingAllowed(boolean)
George Mount86bfc662016-07-12 16:06:06 -07002343 */
2344 public void postponeEnterTransition() {
2345 ensureAnimationInfo().mEnterTransitionPostponed = true;
2346 }
2347
2348 /**
2349 * Begin postponed transitions after {@link #postponeEnterTransition()} was called.
2350 * If postponeEnterTransition() was called, you must call startPostponedEnterTransition()
2351 * or {@link FragmentManager#executePendingTransactions()} to complete the FragmentTransaction.
2352 * If postponement was interrupted with {@link FragmentManager#executePendingTransactions()},
2353 * before {@code startPostponedEnterTransition()}, animations may not run or may execute
2354 * improperly.
2355 *
2356 * @see Activity#startPostponedEnterTransition()
2357 */
2358 public void startPostponedEnterTransition() {
2359 if (mFragmentManager == null || mFragmentManager.mHost == null) {
2360 ensureAnimationInfo().mEnterTransitionPostponed = false;
2361 } else if (Looper.myLooper() != mFragmentManager.mHost.getHandler().getLooper()) {
2362 mFragmentManager.mHost.getHandler().
2363 postAtFrontOfQueue(this::callStartTransitionListener);
2364 } else {
2365 callStartTransitionListener();
2366 }
2367 }
2368
2369 /**
2370 * Calls the start transition listener. This must be called on the UI thread.
2371 */
2372 private void callStartTransitionListener() {
2373 final OnStartEnterTransitionListener listener;
2374 if (mAnimationInfo == null) {
2375 listener = null;
2376 } else {
2377 mAnimationInfo.mEnterTransitionPostponed = false;
2378 listener = mAnimationInfo.mStartEnterTransitionListener;
2379 mAnimationInfo.mStartEnterTransitionListener = null;
2380 }
2381 if (listener != null) {
2382 listener.onStartEnterTransition();
2383 }
2384 }
2385
2386 /**
2387 * Returns true if mAnimationInfo is not null or the transition differs from the default value.
2388 * This is broken out to ensure mAnimationInfo is properly locked when checking.
2389 */
2390 private boolean shouldChangeTransition(Transition transition, Transition defaultValue) {
2391 if (transition == defaultValue) {
2392 return mAnimationInfo != null;
2393 }
2394 return true;
George Mountc03da0e2014-08-22 17:04:02 -07002395 }
2396
Dianne Hackborn625ac272010-09-17 18:29:22 -07002397 /**
2398 * Print the Fragments's state into the given stream.
2399 *
2400 * @param prefix Text to print at the front of each line.
2401 * @param fd The raw file descriptor that the dump is being sent to.
2402 * @param writer The PrintWriter to which you should dump your state. This will be
2403 * closed for you after you return.
2404 * @param args additional arguments to the dump request.
2405 */
2406 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08002407 writer.print(prefix); writer.print("mFragmentId=#");
George Mountc03da0e2014-08-22 17:04:02 -07002408 writer.print(Integer.toHexString(mFragmentId));
2409 writer.print(" mContainerId=#");
2410 writer.print(Integer.toHexString(mContainerId));
2411 writer.print(" mTag="); writer.println(mTag);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002412 writer.print(prefix); writer.print("mState="); writer.print(mState);
George Mountc03da0e2014-08-22 17:04:02 -07002413 writer.print(" mIndex="); writer.print(mIndex);
2414 writer.print(" mWho="); writer.print(mWho);
2415 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002416 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded);
George Mountc03da0e2014-08-22 17:04:02 -07002417 writer.print(" mRemoving="); writer.print(mRemoving);
George Mountc03da0e2014-08-22 17:04:02 -07002418 writer.print(" mFromLayout="); writer.print(mFromLayout);
2419 writer.print(" mInLayout="); writer.println(mInLayout);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002420 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
George Mountc03da0e2014-08-22 17:04:02 -07002421 writer.print(" mDetached="); writer.print(mDetached);
2422 writer.print(" mMenuVisible="); writer.print(mMenuVisible);
2423 writer.print(" mHasMenu="); writer.println(mHasMenu);
Dianne Hackborn6c285972011-08-29 16:53:49 -07002424 writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance);
George Mountc03da0e2014-08-22 17:04:02 -07002425 writer.print(" mRetaining="); writer.print(mRetaining);
2426 writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002427 if (mFragmentManager != null) {
2428 writer.print(prefix); writer.print("mFragmentManager=");
George Mountc03da0e2014-08-22 17:04:02 -07002429 writer.println(mFragmentManager);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002430 }
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002431 if (mHost != null) {
2432 writer.print(prefix); writer.print("mHost=");
2433 writer.println(mHost);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002434 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002435 if (mParentFragment != null) {
2436 writer.print(prefix); writer.print("mParentFragment=");
George Mountc03da0e2014-08-22 17:04:02 -07002437 writer.println(mParentFragment);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002438 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002439 if (mArguments != null) {
2440 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments);
2441 }
2442 if (mSavedFragmentState != null) {
2443 writer.print(prefix); writer.print("mSavedFragmentState=");
George Mountc03da0e2014-08-22 17:04:02 -07002444 writer.println(mSavedFragmentState);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002445 }
2446 if (mSavedViewState != null) {
2447 writer.print(prefix); writer.print("mSavedViewState=");
George Mountc03da0e2014-08-22 17:04:02 -07002448 writer.println(mSavedViewState);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002449 }
2450 if (mTarget != null) {
2451 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget);
George Mountc03da0e2014-08-22 17:04:02 -07002452 writer.print(" mTargetRequestCode=");
2453 writer.println(mTargetRequestCode);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002454 }
George Mount86bfc662016-07-12 16:06:06 -07002455 if (getNextAnim() != 0) {
2456 writer.print(prefix); writer.print("mNextAnim="); writer.println(getNextAnim());
Dianne Hackborn625ac272010-09-17 18:29:22 -07002457 }
2458 if (mContainer != null) {
2459 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer);
2460 }
2461 if (mView != null) {
2462 writer.print(prefix); writer.print("mView="); writer.println(mView);
2463 }
George Mount86bfc662016-07-12 16:06:06 -07002464 if (getAnimatingAway() != null) {
2465 writer.print(prefix); writer.print("mAnimatingAway=");
2466 writer.println(getAnimatingAway());
Dianne Hackbornd173fa32010-12-23 13:58:22 -08002467 writer.print(prefix); writer.print("mStateAfterAnimating=");
George Mount86bfc662016-07-12 16:06:06 -07002468 writer.println(getStateAfterAnimating());
Dianne Hackbornd173fa32010-12-23 13:58:22 -08002469 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002470 if (mLoaderManager != null) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08002471 writer.print(prefix); writer.println("Loader Manager:");
2472 mLoaderManager.dump(prefix + " ", fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002473 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002474 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002475 writer.print(prefix); writer.println("Child " + mChildFragmentManager + ":");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002476 mChildFragmentManager.dump(prefix + " ", fd, writer, args);
2477 }
2478 }
2479
2480 Fragment findFragmentByWho(String who) {
2481 if (who.equals(mWho)) {
2482 return this;
2483 }
2484 if (mChildFragmentManager != null) {
2485 return mChildFragmentManager.findFragmentByWho(who);
2486 }
2487 return null;
2488 }
2489
2490 void instantiateChildFragmentManager() {
2491 mChildFragmentManager = new FragmentManagerImpl();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002492 mChildFragmentManager.attachController(mHost, new FragmentContainer() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002493 @Override
Scott Kennedyc0519552015-02-11 15:33:10 -08002494 @Nullable
Alan Viverette04fd4702017-04-13 16:37:06 -04002495 public <T extends View> T onFindViewById(int id) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002496 if (mView == null) {
2497 throw new IllegalStateException("Fragment does not have a view");
2498 }
2499 return mView.findViewById(id);
2500 }
George Mount0b26e4d2014-09-17 16:36:42 -07002501
2502 @Override
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002503 public boolean onHasView() {
George Mount0b26e4d2014-09-17 16:36:42 -07002504 return (mView != null);
2505 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002506 }, this);
2507 }
2508
2509 void performCreate(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002510 if (mChildFragmentManager != null) {
2511 mChildFragmentManager.noteStateNotSaved();
2512 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002513 mState = CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002514 mCalled = false;
2515 onCreate(savedInstanceState);
George Mount15c21ff2017-07-19 10:47:48 -07002516 mIsCreated = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002517 if (!mCalled) {
2518 throw new SuperNotCalledException("Fragment " + this
2519 + " did not call through to super.onCreate()");
2520 }
Adam Powell592df7d2016-01-14 12:58:23 -08002521 final Context context = getContext();
2522 final int version = context != null ? context.getApplicationInfo().targetSdkVersion : 0;
2523 if (version < Build.VERSION_CODES.N) {
Adam Powelleacacb52016-03-23 13:07:27 -07002524 restoreChildFragmentState(savedInstanceState, false);
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002525 }
2526 }
2527
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002528 View performCreateView(LayoutInflater inflater, ViewGroup container,
2529 Bundle savedInstanceState) {
2530 if (mChildFragmentManager != null) {
2531 mChildFragmentManager.noteStateNotSaved();
2532 }
Adam Powellbb3aa342016-11-16 10:34:57 -08002533 mPerformedCreateView = true;
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002534 return onCreateView(inflater, container, savedInstanceState);
2535 }
2536
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002537 void performActivityCreated(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002538 if (mChildFragmentManager != null) {
2539 mChildFragmentManager.noteStateNotSaved();
2540 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002541 mState = ACTIVITY_CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002542 mCalled = false;
2543 onActivityCreated(savedInstanceState);
2544 if (!mCalled) {
2545 throw new SuperNotCalledException("Fragment " + this
2546 + " did not call through to super.onActivityCreated()");
2547 }
2548 if (mChildFragmentManager != null) {
2549 mChildFragmentManager.dispatchActivityCreated();
2550 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07002551 }
2552
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002553 void performStart() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002554 if (mChildFragmentManager != null) {
2555 mChildFragmentManager.noteStateNotSaved();
2556 mChildFragmentManager.execPendingActions();
2557 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002558 mState = STARTED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002559 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002560 onStart();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002561 if (!mCalled) {
2562 throw new SuperNotCalledException("Fragment " + this
2563 + " did not call through to super.onStart()");
2564 }
2565 if (mChildFragmentManager != null) {
2566 mChildFragmentManager.dispatchStart();
2567 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002568 if (mLoaderManager != null) {
2569 mLoaderManager.doReportStart();
2570 }
2571 }
2572
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002573 void performResume() {
2574 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07002575 mChildFragmentManager.noteStateNotSaved();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002576 mChildFragmentManager.execPendingActions();
2577 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002578 mState = RESUMED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002579 mCalled = false;
2580 onResume();
2581 if (!mCalled) {
2582 throw new SuperNotCalledException("Fragment " + this
2583 + " did not call through to super.onResume()");
2584 }
2585 if (mChildFragmentManager != null) {
2586 mChildFragmentManager.dispatchResume();
2587 mChildFragmentManager.execPendingActions();
2588 }
2589 }
2590
George Mount74af0762017-04-04 17:25:15 -07002591 void noteStateNotSaved() {
2592 if (mChildFragmentManager != null) {
2593 mChildFragmentManager.noteStateNotSaved();
2594 }
2595 }
2596
Winson Chung5af42fc2017-03-24 17:11:33 -07002597 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002598 void performMultiWindowModeChanged(boolean isInMultiWindowMode) {
2599 onMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002600 if (mChildFragmentManager != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002601 mChildFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002602 }
2603 }
2604
Winson Chung5af42fc2017-03-24 17:11:33 -07002605 void performMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
2606 onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
2607 if (mChildFragmentManager != null) {
2608 mChildFragmentManager.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
2609 }
2610 }
2611
2612 @Deprecated
Andrii Kulian933076d2016-03-29 17:04:42 -07002613 void performPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
2614 onPictureInPictureModeChanged(isInPictureInPictureMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002615 if (mChildFragmentManager != null) {
Andrii Kulian933076d2016-03-29 17:04:42 -07002616 mChildFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode);
Wale Ogunwale7c796812016-01-29 21:13:50 -08002617 }
2618 }
2619
Winson Chung5af42fc2017-03-24 17:11:33 -07002620 void performPictureInPictureModeChanged(boolean isInPictureInPictureMode,
2621 Configuration newConfig) {
2622 onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
2623 if (mChildFragmentManager != null) {
2624 mChildFragmentManager.dispatchPictureInPictureModeChanged(isInPictureInPictureMode,
2625 newConfig);
2626 }
2627 }
2628
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002629 void performConfigurationChanged(Configuration newConfig) {
2630 onConfigurationChanged(newConfig);
2631 if (mChildFragmentManager != null) {
2632 mChildFragmentManager.dispatchConfigurationChanged(newConfig);
2633 }
2634 }
2635
2636 void performLowMemory() {
2637 onLowMemory();
2638 if (mChildFragmentManager != null) {
2639 mChildFragmentManager.dispatchLowMemory();
2640 }
2641 }
2642
2643 void performTrimMemory(int level) {
2644 onTrimMemory(level);
2645 if (mChildFragmentManager != null) {
2646 mChildFragmentManager.dispatchTrimMemory(level);
2647 }
2648 }
2649
2650 boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) {
2651 boolean show = false;
2652 if (!mHidden) {
2653 if (mHasMenu && mMenuVisible) {
2654 show = true;
2655 onCreateOptionsMenu(menu, inflater);
2656 }
2657 if (mChildFragmentManager != null) {
2658 show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater);
2659 }
2660 }
2661 return show;
2662 }
2663
2664 boolean performPrepareOptionsMenu(Menu menu) {
2665 boolean show = false;
2666 if (!mHidden) {
2667 if (mHasMenu && mMenuVisible) {
2668 show = true;
2669 onPrepareOptionsMenu(menu);
2670 }
2671 if (mChildFragmentManager != null) {
2672 show |= mChildFragmentManager.dispatchPrepareOptionsMenu(menu);
2673 }
2674 }
2675 return show;
2676 }
2677
2678 boolean performOptionsItemSelected(MenuItem item) {
2679 if (!mHidden) {
2680 if (mHasMenu && mMenuVisible) {
2681 if (onOptionsItemSelected(item)) {
2682 return true;
2683 }
2684 }
2685 if (mChildFragmentManager != null) {
2686 if (mChildFragmentManager.dispatchOptionsItemSelected(item)) {
2687 return true;
2688 }
2689 }
2690 }
2691 return false;
2692 }
2693
2694 boolean performContextItemSelected(MenuItem item) {
2695 if (!mHidden) {
2696 if (onContextItemSelected(item)) {
2697 return true;
2698 }
2699 if (mChildFragmentManager != null) {
2700 if (mChildFragmentManager.dispatchContextItemSelected(item)) {
2701 return true;
2702 }
2703 }
2704 }
2705 return false;
2706 }
2707
2708 void performOptionsMenuClosed(Menu menu) {
2709 if (!mHidden) {
2710 if (mHasMenu && mMenuVisible) {
2711 onOptionsMenuClosed(menu);
2712 }
2713 if (mChildFragmentManager != null) {
2714 mChildFragmentManager.dispatchOptionsMenuClosed(menu);
2715 }
2716 }
2717 }
2718
2719 void performSaveInstanceState(Bundle outState) {
2720 onSaveInstanceState(outState);
2721 if (mChildFragmentManager != null) {
2722 Parcelable p = mChildFragmentManager.saveAllState();
2723 if (p != null) {
2724 outState.putParcelable(Activity.FRAGMENTS_TAG, p);
2725 }
2726 }
2727 }
2728
2729 void performPause() {
2730 if (mChildFragmentManager != null) {
2731 mChildFragmentManager.dispatchPause();
2732 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002733 mState = STARTED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002734 mCalled = false;
2735 onPause();
2736 if (!mCalled) {
2737 throw new SuperNotCalledException("Fragment " + this
2738 + " did not call through to super.onPause()");
2739 }
2740 }
2741
Dianne Hackborn2707d602010-07-09 18:01:20 -07002742 void performStop() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002743 if (mChildFragmentManager != null) {
2744 mChildFragmentManager.dispatchStop();
2745 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002746 mState = STOPPED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002747 mCalled = false;
Dianne Hackborn2707d602010-07-09 18:01:20 -07002748 onStop();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002749 if (!mCalled) {
2750 throw new SuperNotCalledException("Fragment " + this
2751 + " did not call through to super.onStop()");
2752 }
George Mountc03da0e2014-08-22 17:04:02 -07002753
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002754 if (mLoadersStarted) {
2755 mLoadersStarted = false;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07002756 if (!mCheckedForLoaderManager) {
2757 mCheckedForLoaderManager = true;
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002758 mLoaderManager = mHost.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07002759 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07002760 if (mLoaderManager != null) {
Todd Kennedy2ee19c7f2015-10-19 15:31:51 -07002761 if (mHost.getRetainLoaders()) {
Dianne Hackborn2707d602010-07-09 18:01:20 -07002762 mLoaderManager.doRetain();
Todd Kennedya5fc6f02015-04-14 18:22:54 -07002763 } else {
2764 mLoaderManager.doStop();
Dianne Hackborn2707d602010-07-09 18:01:20 -07002765 }
2766 }
2767 }
2768 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002769
2770 void performDestroyView() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002771 if (mChildFragmentManager != null) {
2772 mChildFragmentManager.dispatchDestroyView();
2773 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002774 mState = CREATED;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002775 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002776 onDestroyView();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002777 if (!mCalled) {
2778 throw new SuperNotCalledException("Fragment " + this
2779 + " did not call through to super.onDestroyView()");
2780 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002781 if (mLoaderManager != null) {
2782 mLoaderManager.doReportNextStart();
2783 }
Adam Powellbb3aa342016-11-16 10:34:57 -08002784 mPerformedCreateView = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07002785 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002786
2787 void performDestroy() {
2788 if (mChildFragmentManager != null) {
2789 mChildFragmentManager.dispatchDestroy();
2790 }
Adam Powelld1d4d9c2016-01-12 10:11:42 -08002791 mState = INITIALIZING;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002792 mCalled = false;
George Mount15c21ff2017-07-19 10:47:48 -07002793 mIsCreated = false;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002794 onDestroy();
2795 if (!mCalled) {
2796 throw new SuperNotCalledException("Fragment " + this
2797 + " did not call through to super.onDestroy()");
2798 }
George Mount4c656b32016-02-17 12:59:49 -08002799 mChildFragmentManager = null;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07002800 }
George Mountc03da0e2014-08-22 17:04:02 -07002801
Adam Powella9bab982016-04-21 11:04:41 -07002802 void performDetach() {
2803 mCalled = false;
2804 onDetach();
George Mount3fd81292017-04-17 16:50:26 -07002805 mLayoutInflater = null;
Adam Powella9bab982016-04-21 11:04:41 -07002806 if (!mCalled) {
2807 throw new SuperNotCalledException("Fragment " + this
2808 + " did not call through to super.onDetach()");
2809 }
2810
2811 // Destroy the child FragmentManager if we still have it here.
2812 // We won't unless we're retaining our instance and if we do,
2813 // our child FragmentManager instance state will have already been saved.
2814 if (mChildFragmentManager != null) {
2815 if (!mRetaining) {
2816 throw new IllegalStateException("Child FragmentManager of " + this + " was not "
2817 + " destroyed and this fragment is not retaining instance");
2818 }
2819 mChildFragmentManager.dispatchDestroy();
2820 mChildFragmentManager = null;
2821 }
2822 }
2823
George Mount86bfc662016-07-12 16:06:06 -07002824 void setOnStartEnterTransitionListener(OnStartEnterTransitionListener listener) {
2825 ensureAnimationInfo();
2826 if (listener == mAnimationInfo.mStartEnterTransitionListener) {
2827 return;
2828 }
2829 if (listener != null && mAnimationInfo.mStartEnterTransitionListener != null) {
2830 throw new IllegalStateException("Trying to set a replacement " +
2831 "startPostponedEnterTransition on " + this);
2832 }
2833 if (mAnimationInfo.mEnterTransitionPostponed) {
2834 mAnimationInfo.mStartEnterTransitionListener = listener;
2835 }
2836 if (listener != null) {
2837 listener.startListening();
2838 }
2839 }
2840
George Mountc03da0e2014-08-22 17:04:02 -07002841 private static Transition loadTransition(Context context, TypedArray typedArray,
2842 Transition currentValue, Transition defaultValue, int id) {
2843 if (currentValue != defaultValue) {
2844 return currentValue;
2845 }
2846 int transitionId = typedArray.getResourceId(id, 0);
2847 Transition transition = defaultValue;
2848 if (transitionId != 0 && transitionId != com.android.internal.R.transition.no_transition) {
2849 TransitionInflater inflater = TransitionInflater.from(context);
2850 transition = inflater.inflateTransition(transitionId);
2851 if (transition instanceof TransitionSet &&
2852 ((TransitionSet)transition).getTransitionCount() == 0) {
2853 transition = null;
2854 }
2855 }
2856 return transition;
2857 }
2858
George Mount86bfc662016-07-12 16:06:06 -07002859 private AnimationInfo ensureAnimationInfo() {
2860 if (mAnimationInfo == null) {
2861 mAnimationInfo = new AnimationInfo();
2862 }
2863 return mAnimationInfo;
2864 }
2865
2866 int getNextAnim() {
2867 if (mAnimationInfo == null) {
2868 return 0;
2869 }
2870 return mAnimationInfo.mNextAnim;
2871 }
2872
2873 void setNextAnim(int animResourceId) {
2874 if (mAnimationInfo == null && animResourceId == 0) {
2875 return; // no change!
2876 }
2877 ensureAnimationInfo().mNextAnim = animResourceId;
2878 }
2879
2880 int getNextTransition() {
2881 if (mAnimationInfo == null) {
2882 return 0;
2883 }
2884 return mAnimationInfo.mNextTransition;
2885 }
2886
2887 void setNextTransition(int nextTransition, int nextTransitionStyle) {
2888 if (mAnimationInfo == null && nextTransition == 0 && nextTransitionStyle == 0) {
2889 return; // no change!
2890 }
2891 ensureAnimationInfo();
2892 mAnimationInfo.mNextTransition = nextTransition;
2893 mAnimationInfo.mNextTransitionStyle = nextTransitionStyle;
2894 }
2895
2896 int getNextTransitionStyle() {
2897 if (mAnimationInfo == null) {
2898 return 0;
2899 }
2900 return mAnimationInfo.mNextTransitionStyle;
2901 }
2902
2903 SharedElementCallback getEnterTransitionCallback() {
2904 if (mAnimationInfo == null) {
2905 return SharedElementCallback.NULL_CALLBACK;
2906 }
2907 return mAnimationInfo.mEnterTransitionCallback;
2908 }
2909
2910 SharedElementCallback getExitTransitionCallback() {
2911 if (mAnimationInfo == null) {
2912 return SharedElementCallback.NULL_CALLBACK;
2913 }
2914 return mAnimationInfo.mExitTransitionCallback;
2915 }
2916
2917 Animator getAnimatingAway() {
2918 if (mAnimationInfo == null) {
2919 return null;
2920 }
2921 return mAnimationInfo.mAnimatingAway;
2922 }
2923
2924 void setAnimatingAway(Animator animator) {
2925 ensureAnimationInfo().mAnimatingAway = animator;
2926 }
2927
2928 int getStateAfterAnimating() {
2929 if (mAnimationInfo == null) {
2930 return 0;
2931 }
2932 return mAnimationInfo.mStateAfterAnimating;
2933 }
2934
2935 void setStateAfterAnimating(int state) {
2936 ensureAnimationInfo().mStateAfterAnimating = state;
2937 }
2938
2939 boolean isPostponed() {
2940 if (mAnimationInfo == null) {
2941 return false;
2942 }
2943 return mAnimationInfo.mEnterTransitionPostponed;
2944 }
2945
George Mount4fe47112016-10-27 08:12:36 -07002946 boolean isHideReplaced() {
2947 if (mAnimationInfo == null) {
2948 return false;
2949 }
2950 return mAnimationInfo.mIsHideReplaced;
2951 }
2952
2953 void setHideReplaced(boolean replaced) {
2954 ensureAnimationInfo().mIsHideReplaced = replaced;
2955 }
2956
George Mount86bfc662016-07-12 16:06:06 -07002957 /**
2958 * Used internally to be notified when {@link #startPostponedEnterTransition()} has
2959 * been called. This listener will only be called once and then be removed from the
2960 * listeners.
2961 */
2962 interface OnStartEnterTransitionListener {
2963 void onStartEnterTransition();
2964 void startListening();
2965 }
2966
2967 /**
2968 * Contains all the animation and transition information for a fragment. This will only
2969 * be instantiated for Fragments that have Views.
2970 */
2971 static class AnimationInfo {
2972 // Non-null if the fragment's view hierarchy is currently animating away,
2973 // meaning we need to wait a bit on completely destroying it. This is the
2974 // animation that is running.
2975 Animator mAnimatingAway;
2976
2977 // If mAnimatingAway != null, this is the state we should move to once the
2978 // animation is done.
2979 int mStateAfterAnimating;
2980
2981 // If app has requested a specific animation, this is the one to use.
2982 int mNextAnim;
2983
2984 // If app has requested a specific transition, this is the one to use.
2985 int mNextTransition;
2986
2987 // If app has requested a specific transition style, this is the one to use.
2988 int mNextTransitionStyle;
2989
2990 private Transition mEnterTransition = null;
2991 private Transition mReturnTransition = USE_DEFAULT_TRANSITION;
2992 private Transition mExitTransition = null;
2993 private Transition mReenterTransition = USE_DEFAULT_TRANSITION;
2994 private Transition mSharedElementEnterTransition = null;
2995 private Transition mSharedElementReturnTransition = USE_DEFAULT_TRANSITION;
2996 private Boolean mAllowReturnTransitionOverlap;
2997 private Boolean mAllowEnterTransitionOverlap;
2998
2999 SharedElementCallback mEnterTransitionCallback = SharedElementCallback.NULL_CALLBACK;
3000 SharedElementCallback mExitTransitionCallback = SharedElementCallback.NULL_CALLBACK;
3001
3002 // True when postponeEnterTransition has been called and startPostponeEnterTransition
3003 // hasn't been called yet.
3004 boolean mEnterTransitionPostponed;
3005
3006 // Listener to wait for startPostponeEnterTransition. After being called, it will
3007 // be set to null
3008 OnStartEnterTransitionListener mStartEnterTransitionListener;
3009
George Mount4fe47112016-10-27 08:12:36 -07003010 // True if the View was hidden, but the transition is handling the hide
3011 boolean mIsHideReplaced;
George Mount86bfc662016-07-12 16:06:06 -07003012 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003013}