blob: c09da8798c40b81a38d26c31cca78f41dfa8f7fe [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;
Tor Norbyed9273d62013-05-30 15:59:53 -070020import android.annotation.Nullable;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070021import android.content.ComponentCallbacks2;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070022import android.content.Context;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070023import android.content.Intent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070024import android.content.res.Configuration;
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -080025import android.content.res.Resources;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070026import android.os.Bundle;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070027import android.os.Parcel;
28import android.os.Parcelable;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070029import android.util.AndroidRuntimeException;
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070030import android.util.ArrayMap;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070031import android.util.AttributeSet;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080032import android.util.DebugUtils;
Dianne Hackborn03fcc332012-05-15 12:49:40 -070033import android.util.Log;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070034import android.util.SparseArray;
Adam Powell14874662013-07-18 19:42:41 -070035import android.util.SuperNotCalledException;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070036import android.view.ContextMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -070037import android.view.ContextMenu.ContextMenuInfo;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070038import android.view.LayoutInflater;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070039import android.view.Menu;
40import android.view.MenuInflater;
41import android.view.MenuItem;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070042import android.view.View;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070043import android.view.View.OnCreateContextMenuListener;
Adam Powellf0f5fff2011-08-01 13:42:50 -070044import android.view.ViewGroup;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070045import android.widget.AdapterView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070046
Dianne Hackborn625ac272010-09-17 18:29:22 -070047import java.io.FileDescriptor;
48import java.io.PrintWriter;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070049
50final class FragmentState implements Parcelable {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070051 final String mClassName;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070052 final int mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070053 final boolean mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070054 final int mFragmentId;
55 final int mContainerId;
56 final String mTag;
57 final boolean mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070058 final boolean mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070059 final Bundle mArguments;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070060
61 Bundle mSavedFragmentState;
62
63 Fragment mInstance;
64
65 public FragmentState(Fragment frag) {
66 mClassName = frag.getClass().getName();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070067 mIndex = frag.mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070068 mFromLayout = frag.mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070069 mFragmentId = frag.mFragmentId;
70 mContainerId = frag.mContainerId;
71 mTag = frag.mTag;
72 mRetainInstance = frag.mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070073 mDetached = frag.mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070074 mArguments = frag.mArguments;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070075 }
76
77 public FragmentState(Parcel in) {
78 mClassName = in.readString();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070079 mIndex = in.readInt();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070080 mFromLayout = in.readInt() != 0;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070081 mFragmentId = in.readInt();
82 mContainerId = in.readInt();
83 mTag = in.readString();
84 mRetainInstance = in.readInt() != 0;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070085 mDetached = in.readInt() != 0;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070086 mArguments = in.readBundle();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070087 mSavedFragmentState = in.readBundle();
88 }
89
Dianne Hackborn62bea2f2012-09-04 18:48:15 -070090 public Fragment instantiate(Activity activity, Fragment parent) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070091 if (mInstance != null) {
92 return mInstance;
93 }
94
Dianne Hackborn51642462010-10-28 10:32:37 -070095 if (mArguments != null) {
96 mArguments.setClassLoader(activity.getClassLoader());
97 }
98
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070099 mInstance = Fragment.instantiate(activity, mClassName, mArguments);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700100
101 if (mSavedFragmentState != null) {
102 mSavedFragmentState.setClassLoader(activity.getClassLoader());
103 mInstance.mSavedFragmentState = mSavedFragmentState;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700104 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700105 mInstance.setIndex(mIndex, parent);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700106 mInstance.mFromLayout = mFromLayout;
Dianne Hackborn352cc982011-01-04 11:34:18 -0800107 mInstance.mRestored = true;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700108 mInstance.mFragmentId = mFragmentId;
109 mInstance.mContainerId = mContainerId;
110 mInstance.mTag = mTag;
111 mInstance.mRetainInstance = mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700112 mInstance.mDetached = mDetached;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700113 mInstance.mFragmentManager = activity.mFragments;
Dianne Hackborn03fcc332012-05-15 12:49:40 -0700114 if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG,
115 "Instantiated fragment " + mInstance);
116
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700117 return mInstance;
118 }
119
120 public int describeContents() {
121 return 0;
122 }
123
124 public void writeToParcel(Parcel dest, int flags) {
125 dest.writeString(mClassName);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700126 dest.writeInt(mIndex);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700127 dest.writeInt(mFromLayout ? 1 : 0);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700128 dest.writeInt(mFragmentId);
129 dest.writeInt(mContainerId);
130 dest.writeString(mTag);
131 dest.writeInt(mRetainInstance ? 1 : 0);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700132 dest.writeInt(mDetached ? 1 : 0);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700133 dest.writeBundle(mArguments);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700134 dest.writeBundle(mSavedFragmentState);
135 }
136
137 public static final Parcelable.Creator<FragmentState> CREATOR
138 = new Parcelable.Creator<FragmentState>() {
139 public FragmentState createFromParcel(Parcel in) {
140 return new FragmentState(in);
141 }
142
143 public FragmentState[] newArray(int size) {
144 return new FragmentState[size];
145 }
146 };
147}
148
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700149/**
150 * A Fragment is a piece of an application's user interface or behavior
Dianne Hackborn291905e2010-08-17 15:17:15 -0700151 * that can be placed in an {@link Activity}. Interaction with fragments
152 * is done through {@link FragmentManager}, which can be obtained via
153 * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and
154 * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}.
155 *
156 * <p>The Fragment class can be used many ways to achieve a wide variety of
felipeal1b4e4b62012-02-27 12:09:13 -0800157 * results. In its core, it represents a particular operation or interface
Dianne Hackborn291905e2010-08-17 15:17:15 -0700158 * that is running within a larger {@link Activity}. A Fragment is closely
159 * tied to the Activity it is in, and can not be used apart from one. Though
160 * Fragment defines its own lifecycle, that lifecycle is dependent on its
161 * activity: if the activity is stopped, no fragments inside of it can be
162 * started; when the activity is destroyed, all fragments will be destroyed.
163 *
164 * <p>All subclasses of Fragment must include a public empty constructor.
165 * The framework will often re-instantiate a fragment class when needed,
166 * in particular during state restore, and needs to be able to find this
167 * constructor to instantiate it. If the empty constructor is not available,
168 * a runtime exception will occur in some cases during state restore.
169 *
170 * <p>Topics covered here:
171 * <ol>
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700172 * <li><a href="#OlderPlatforms">Older Platforms</a>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700173 * <li><a href="#Lifecycle">Lifecycle</a>
174 * <li><a href="#Layout">Layout</a>
175 * <li><a href="#BackStack">Back Stack</a>
176 * </ol>
177 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700178 * <div class="special reference">
179 * <h3>Developer Guides</h3>
180 * <p>For more information about using fragments, read the
181 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
182 * </div>
183 *
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700184 * <a name="OlderPlatforms"></a>
185 * <h3>Older Platforms</h3>
186 *
187 * While the Fragment API was introduced in
188 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
Dianne Hackborn7871bad2011-12-12 15:19:26 -0800189 * at is also available for use on older platforms through
190 * {@link android.support.v4.app.FragmentActivity}. See the blog post
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700191 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
192 * Fragments For All</a> for more details.
193 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700194 * <a name="Lifecycle"></a>
195 * <h3>Lifecycle</h3>
196 *
197 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has
198 * its own wrinkle on the standard activity lifecycle. It includes basic
199 * activity lifecycle methods such as {@link #onResume}, but also important
200 * are methods related to interactions with the activity and UI generation.
201 *
202 * <p>The core series of lifecycle methods that are called to bring a fragment
203 * up to resumed state (interacting with the user) are:
204 *
205 * <ol>
206 * <li> {@link #onAttach} called once the fragment is associated with its activity.
207 * <li> {@link #onCreate} called to do initial creation of the fragment.
208 * <li> {@link #onCreateView} creates and returns the view hierarchy associated
209 * with the fragment.
210 * <li> {@link #onActivityCreated} tells the fragment that its activity has
Quddus Chong37900012012-04-11 11:57:50 -0700211 * completed its own {@link Activity#onCreate Activity.onCreate()}.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700212 * <li> {@link #onViewStateRestored} tells the fragment that all of the saved
213 * state of its view hierarchy has been restored.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700214 * <li> {@link #onStart} makes the fragment visible to the user (based on its
215 * containing activity being started).
216 * <li> {@link #onResume} makes the fragment interacting with the user (based on its
217 * containing activity being resumed).
218 * </ol>
219 *
220 * <p>As a fragment is no longer being used, it goes through a reverse
221 * series of callbacks:
222 *
223 * <ol>
224 * <li> {@link #onPause} fragment is no longer interacting with the user either
225 * because its activity is being paused or a fragment operation is modifying it
226 * in the activity.
227 * <li> {@link #onStop} fragment is no longer visible to the user either
228 * because its activity is being stopped or a fragment operation is modifying it
229 * in the activity.
230 * <li> {@link #onDestroyView} allows the fragment to clean up resources
231 * associated with its View.
232 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state.
233 * <li> {@link #onDetach} called immediately prior to the fragment no longer
234 * being associated with its activity.
235 * </ol>
236 *
237 * <a name="Layout"></a>
238 * <h3>Layout</h3>
239 *
240 * <p>Fragments can be used as part of your application's layout, allowing
241 * you to better modularize your code and more easily adjust your user
242 * interface to the screen it is running on. As an example, we can look
243 * at a simple program consisting of a list of items, and display of the
244 * details of each item.</p>
245 *
246 * <p>An activity's layout XML can include <code>&lt;fragment&gt;</code> tags
247 * to embed fragment instances inside of the layout. For example, here is
Dianne Hackborn625ac272010-09-17 18:29:22 -0700248 * a simple layout that embeds one fragment:</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700249 *
250 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout}
251 *
252 * <p>The layout is installed in the activity in the normal way:</p>
253 *
254 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
255 * main}
256 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800257 * <p>The titles fragment, showing a list of titles, is fairly simple, relying
Dianne Hackborn291905e2010-08-17 15:17:15 -0700258 * on {@link ListFragment} for most of its work. Note the implementation of
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800259 * clicking an item: depending on the current activity's layout, it can either
260 * create and display a new fragment to show the details in-place (more about
Ben Dodson542f2402011-06-14 16:40:23 -0700261 * this later), or start a new activity to show the details.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700262 *
263 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
264 * titles}
265 *
Ben Dodson542f2402011-06-14 16:40:23 -0700266 * <p>The details fragment showing the contents of a selected item just
Dianne Hackborn291905e2010-08-17 15:17:15 -0700267 * displays a string of text based on an index of a string array built in to
268 * the app:</p>
269 *
270 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
271 * details}
272 *
273 * <p>In this case when the user clicks on a title, there is no details
Ben Dodson542f2402011-06-14 16:40:23 -0700274 * container in the current activity, so the titles fragment's click code will
Dianne Hackborn291905e2010-08-17 15:17:15 -0700275 * launch a new activity to display the details fragment:</p>
276 *
277 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
278 * details_activity}
279 *
280 * <p>However the screen may be large enough to show both the list of titles
281 * and details about the currently selected title. To use such a layout on
282 * a landscape screen, this alternative layout can be placed under layout-land:</p>
283 *
284 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout}
285 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800286 * <p>Note how the prior code will adjust to this alternative UI flow: the titles
287 * fragment will now embed the details fragment inside of this activity, and the
288 * details activity will finish itself if it is running in a configuration
289 * where the details can be shown in-place.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700290 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700291 * <p>When a configuration change causes the activity hosting these fragments
292 * to restart, its new instance may use a different layout that doesn't
293 * include the same fragments as the previous layout. In this case all of
294 * the previous fragments will still be instantiated and running in the new
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800295 * instance. However, any that are no longer associated with a &lt;fragment&gt;
296 * tag in the view hierarchy will not have their content view created
297 * and will return false from {@link #isInLayout}. (The code here also shows
298 * how you can determine if a fragment placed in a container is no longer
299 * running in a layout with that container and avoid creating its view hierarchy
300 * in that case.)
Dianne Hackborn625ac272010-09-17 18:29:22 -0700301 *
302 * <p>The attributes of the &lt;fragment&gt; tag are used to control the
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800303 * LayoutParams provided when attaching the fragment's view to the parent
304 * container. They can also be parsed by the fragment in {@link #onInflate}
Dianne Hackborn625ac272010-09-17 18:29:22 -0700305 * as parameters.
306 *
307 * <p>The fragment being instantiated must have some kind of unique identifier
308 * so that it can be re-associated with a previous instance if the parent
309 * activity needs to be destroyed and recreated. This can be provided these
310 * ways:
311 *
312 * <ul>
313 * <li>If nothing is explicitly supplied, the view ID of the container will
314 * be used.
315 * <li><code>android:tag</code> can be used in &lt;fragment&gt; to provide
316 * a specific tag name for the fragment.
317 * <li><code>android:id</code> can be used in &lt;fragment&gt; to provide
318 * a specific identifier for the fragment.
319 * </ul>
320 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700321 * <a name="BackStack"></a>
322 * <h3>Back Stack</h3>
323 *
324 * <p>The transaction in which fragments are modified can be placed on an
325 * internal back-stack of the owning activity. When the user presses back
326 * in the activity, any transactions on the back stack are popped off before
327 * the activity itself is finished.
328 *
329 * <p>For example, consider this simple fragment that is instantiated with
330 * an integer argument and displays that in a TextView in its UI:</p>
331 *
332 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
333 * fragment}
334 *
335 * <p>A function that creates a new instance of the fragment, replacing
336 * whatever current fragment instance is being shown and pushing that change
337 * on to the back stack could be written as:
338 *
339 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
340 * add_stack}
341 *
342 * <p>After each call to this function, a new entry is on the stack, and
343 * pressing back will pop it to return the user to whatever previous state
344 * the activity UI was in.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700345 */
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700346public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700347 private static final ArrayMap<String, Class<?>> sClassMap =
348 new ArrayMap<String, Class<?>>();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700349
Adam Powell635c60a2011-10-26 10:22:16 -0700350 static final int INVALID_STATE = -1; // Invalid state used as a null value.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700351 static final int INITIALIZING = 0; // Not yet created.
352 static final int CREATED = 1; // Created.
353 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700354 static final int STOPPED = 3; // Fully created, not started.
355 static final int STARTED = 4; // Created and started, not resumed.
356 static final int RESUMED = 5; // Created started and resumed.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700357
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700358 int mState = INITIALIZING;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700359
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800360 // Non-null if the fragment's view hierarchy is currently animating away,
361 // meaning we need to wait a bit on completely destroying it. This is the
362 // animation that is running.
363 Animator mAnimatingAway;
364
365 // If mAnimatingAway != null, this is the state we should move to once the
366 // animation is done.
367 int mStateAfterAnimating;
368
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700369 // When instantiated from saved state, this is the saved state.
370 Bundle mSavedFragmentState;
371 SparseArray<Parcelable> mSavedViewState;
372
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700373 // Index into active fragment array.
374 int mIndex = -1;
375
376 // Internal unique name for this fragment;
377 String mWho;
378
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700379 // Construction arguments;
380 Bundle mArguments;
381
Dianne Hackborndef15372010-08-15 12:43:52 -0700382 // Target fragment.
383 Fragment mTarget;
384
Dianne Hackbornf9302322011-06-14 18:36:14 -0700385 // For use when retaining a fragment: this is the index of the last mTarget.
386 int mTargetIndex = -1;
387
Dianne Hackborndef15372010-08-15 12:43:52 -0700388 // Target request code.
389 int mTargetRequestCode;
390
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700391 // True if the fragment is in the list of added fragments.
392 boolean mAdded;
393
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800394 // If set this fragment is being removed from its activity.
395 boolean mRemoving;
396
Dianne Hackborn2707d602010-07-09 18:01:20 -0700397 // True if the fragment is in the resumed state.
398 boolean mResumed;
399
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700400 // Set to true if this fragment was instantiated from a layout file.
401 boolean mFromLayout;
402
Dianne Hackborn625ac272010-09-17 18:29:22 -0700403 // Set to true when the view has actually been inflated in its layout.
404 boolean mInLayout;
405
Dianne Hackborn352cc982011-01-04 11:34:18 -0800406 // True if this fragment has been restored from previously saved state.
407 boolean mRestored;
408
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700409 // Number of active back stack entries this fragment is in.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700410 int mBackStackNesting;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700411
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700412 // The fragment manager we are associated with. Set as soon as the
413 // fragment is used in a transaction; cleared after it has been removed
414 // from all transactions.
Adam Powell635c60a2011-10-26 10:22:16 -0700415 FragmentManagerImpl mFragmentManager;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700416
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700417 // Activity this fragment is attached to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700418 Activity mActivity;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700419
420 // Private fragment manager for child fragments inside of this one.
421 FragmentManagerImpl mChildFragmentManager;
422
423 // If this Fragment is contained in another Fragment, this is that container.
424 Fragment mParentFragment;
425
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700426 // The optional identifier for this fragment -- either the container ID if it
427 // was dynamically added to the view hierarchy, or the ID supplied in
428 // layout.
429 int mFragmentId;
430
431 // When a fragment is being dynamically added to the view hierarchy, this
432 // is the identifier of the parent container it is being added to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700433 int mContainerId;
434
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700435 // The optional named tag for this fragment -- usually used to find
436 // fragments that are not part of the layout.
437 String mTag;
438
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700439 // Set to true when the app has requested that this fragment be hidden
440 // from the user.
441 boolean mHidden;
442
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700443 // Set to true when the app has requested that this fragment be detached.
444 boolean mDetached;
445
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700446 // If set this fragment would like its instance retained across
447 // configuration changes.
448 boolean mRetainInstance;
449
450 // If set this fragment is being retained across the current config change.
451 boolean mRetaining;
452
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700453 // If set this fragment has menu items to contribute.
454 boolean mHasMenu;
Dianne Hackborn6c285972011-08-29 16:53:49 -0700455
456 // Set to true to allow the fragment's menu to be shown.
457 boolean mMenuVisible = true;
458
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700459 // Used to verify that subclasses call through to super class.
460 boolean mCalled;
461
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700462 // If app has requested a specific animation, this is the one to use.
463 int mNextAnim;
464
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700465 // The parent container of the fragment after dynamically added to UI.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700466 ViewGroup mContainer;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700467
468 // The View generated for this fragment.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700469 View mView;
470
Adam Powell635c60a2011-10-26 10:22:16 -0700471 // Whether this fragment should defer starting until after other fragments
472 // have been started and their loaders are finished.
473 boolean mDeferStart;
474
Adam Powell78fed9b2011-11-07 10:45:34 -0800475 // Hint provided by the app that this fragment is currently visible to the user.
476 boolean mUserVisibleHint = true;
477
Dianne Hackborn4911b782010-07-15 12:54:39 -0700478 LoaderManagerImpl mLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700479 boolean mLoadersStarted;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700480 boolean mCheckedForLoaderManager;
Dianne Hackbornc8017682010-07-06 13:34:38 -0700481
482 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700483 * State information that has been retrieved from a fragment instance
484 * through {@link FragmentManager#saveFragmentInstanceState(Fragment)
485 * FragmentManager.saveFragmentInstanceState}.
486 */
487 public static class SavedState implements Parcelable {
488 final Bundle mState;
489
490 SavedState(Bundle state) {
491 mState = state;
492 }
493
494 SavedState(Parcel in, ClassLoader loader) {
495 mState = in.readBundle();
496 if (loader != null && mState != null) {
497 mState.setClassLoader(loader);
498 }
499 }
500
501 @Override
502 public int describeContents() {
503 return 0;
504 }
505
506 @Override
507 public void writeToParcel(Parcel dest, int flags) {
508 dest.writeBundle(mState);
509 }
510
511 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR
512 = new Parcelable.ClassLoaderCreator<SavedState>() {
513 public SavedState createFromParcel(Parcel in) {
514 return new SavedState(in, null);
515 }
516
517 public SavedState createFromParcel(Parcel in, ClassLoader loader) {
518 return new SavedState(in, loader);
519 }
520
521 public SavedState[] newArray(int size) {
522 return new SavedState[size];
523 }
524 };
525 }
526
527 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700528 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when
529 * there is an instantiation failure.
530 */
531 static public class InstantiationException extends AndroidRuntimeException {
532 public InstantiationException(String msg, Exception cause) {
533 super(msg, cause);
534 }
535 }
536
537 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700538 * Default constructor. <strong>Every</strong> fragment must have an
Dianne Hackbornc8017682010-07-06 13:34:38 -0700539 * empty constructor, so it can be instantiated when restoring its
540 * activity's state. It is strongly recommended that subclasses do not
541 * have other constructors with parameters, since these constructors
542 * will not be called when the fragment is re-instantiated; instead,
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700543 * arguments can be supplied by the caller with {@link #setArguments}
544 * and later retrieved by the Fragment with {@link #getArguments}.
545 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700546 * <p>Applications should generally not implement a constructor. The
547 * first place application code an run where the fragment is ready to
548 * be used is in {@link #onAttach(Activity)}, the point where the fragment
549 * is actually associated with its activity. Some applications may also
550 * want to implement {@link #onInflate} to retrieve attributes from a
551 * layout resource, though should take care here because this happens for
552 * the fragment is attached to its activity.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700553 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700554 public Fragment() {
555 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700556
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700557 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700558 * Like {@link #instantiate(Context, String, Bundle)} but with a null
559 * argument Bundle.
560 */
561 public static Fragment instantiate(Context context, String fname) {
562 return instantiate(context, fname, null);
563 }
564
565 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700566 * Create a new instance of a Fragment with the given class name. This is
567 * the same as calling its empty constructor.
568 *
569 * @param context The calling context being used to instantiate the fragment.
570 * This is currently just used to get its ClassLoader.
571 * @param fname The class name of the fragment to instantiate.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700572 * @param args Bundle of arguments to supply to the fragment, which it
573 * can retrieve with {@link #getArguments()}. May be null.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700574 * @return Returns a new fragment instance.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700575 * @throws InstantiationException If there is a failure in instantiating
576 * the given fragment class. This is a runtime exception; it is not
577 * normally expected to happen.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700578 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700579 public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700580 try {
581 Class<?> clazz = sClassMap.get(fname);
582 if (clazz == null) {
583 // Class not found in the cache, see if it's real, and try to add it
584 clazz = context.getClassLoader().loadClass(fname);
Amith Yamasani364ed4d2013-07-26 13:37:56 -0700585 if (!Fragment.class.isAssignableFrom(clazz)) {
586 throw new InstantiationException("Trying to instantiate a class " + fname
587 + " that is not a Fragment", new ClassCastException());
588 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700589 sClassMap.put(fname, clazz);
590 }
591 Fragment f = (Fragment)clazz.newInstance();
592 if (args != null) {
593 args.setClassLoader(f.getClass().getClassLoader());
594 f.mArguments = args;
595 }
596 return f;
597 } catch (ClassNotFoundException e) {
598 throw new InstantiationException("Unable to instantiate fragment " + fname
599 + ": make sure class name exists, is public, and has an"
600 + " empty constructor that is public", e);
601 } catch (java.lang.InstantiationException e) {
602 throw new InstantiationException("Unable to instantiate fragment " + fname
603 + ": make sure class name exists, is public, and has an"
604 + " empty constructor that is public", e);
605 } catch (IllegalAccessException e) {
606 throw new InstantiationException("Unable to instantiate fragment " + fname
607 + ": make sure class name exists, is public, and has an"
608 + " empty constructor that is public", e);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700609 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700610 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700611
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700612 final void restoreViewState(Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700613 if (mSavedViewState != null) {
614 mView.restoreHierarchyState(mSavedViewState);
615 mSavedViewState = null;
616 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700617 mCalled = false;
618 onViewStateRestored(savedInstanceState);
619 if (!mCalled) {
620 throw new SuperNotCalledException("Fragment " + this
621 + " did not call through to super.onViewStateRestored()");
622 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700623 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700624
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700625 final void setIndex(int index, Fragment parent) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700626 mIndex = index;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700627 if (parent != null) {
628 mWho = parent.mWho + ":" + mIndex;
629 } else {
630 mWho = "android:fragment:" + mIndex;
631 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700632 }
633
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700634 final boolean isInBackStack() {
635 return mBackStackNesting > 0;
636 }
637
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700638 /**
639 * Subclasses can not override equals().
640 */
641 @Override final public boolean equals(Object o) {
642 return super.equals(o);
643 }
644
645 /**
646 * Subclasses can not override hashCode().
647 */
648 @Override final public int hashCode() {
649 return super.hashCode();
650 }
651
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700652 @Override
653 public String toString() {
654 StringBuilder sb = new StringBuilder(128);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800655 DebugUtils.buildShortClassTag(this, sb);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700656 if (mIndex >= 0) {
657 sb.append(" #");
658 sb.append(mIndex);
659 }
660 if (mFragmentId != 0) {
661 sb.append(" id=0x");
662 sb.append(Integer.toHexString(mFragmentId));
663 }
664 if (mTag != null) {
665 sb.append(" ");
666 sb.append(mTag);
667 }
668 sb.append('}');
669 return sb.toString();
670 }
671
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700672 /**
673 * Return the identifier this fragment is known by. This is either
674 * the android:id value supplied in a layout or the container view ID
675 * supplied when adding the fragment.
676 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700677 final public int getId() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700678 return mFragmentId;
679 }
680
681 /**
682 * Get the tag name of the fragment, if specified.
683 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700684 final public String getTag() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700685 return mTag;
686 }
687
688 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700689 * Supply the construction arguments for this fragment. This can only
690 * be called before the fragment has been attached to its activity; that
691 * is, you should call it immediately after constructing the fragment. The
692 * arguments supplied here will be retained across fragment destroy and
693 * creation.
694 */
Dianne Hackborndef15372010-08-15 12:43:52 -0700695 public void setArguments(Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700696 if (mIndex >= 0) {
697 throw new IllegalStateException("Fragment already active");
698 }
699 mArguments = args;
700 }
701
702 /**
703 * Return the arguments supplied when the fragment was instantiated,
704 * if any.
705 */
706 final public Bundle getArguments() {
707 return mArguments;
708 }
709
710 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700711 * Set the initial saved state that this Fragment should restore itself
712 * from when first being constructed, as returned by
713 * {@link FragmentManager#saveFragmentInstanceState(Fragment)
714 * FragmentManager.saveFragmentInstanceState}.
715 *
716 * @param state The state the fragment should be restored from.
717 */
718 public void setInitialSavedState(SavedState state) {
719 if (mIndex >= 0) {
720 throw new IllegalStateException("Fragment already active");
721 }
722 mSavedFragmentState = state != null && state.mState != null
723 ? state.mState : null;
724 }
725
726 /**
Dianne Hackborndef15372010-08-15 12:43:52 -0700727 * Optional target for this fragment. This may be used, for example,
728 * if this fragment is being started by another, and when done wants to
729 * give a result back to the first. The target set here is retained
730 * across instances via {@link FragmentManager#putFragment
731 * FragmentManager.putFragment()}.
732 *
733 * @param fragment The fragment that is the target of this one.
734 * @param requestCode Optional request code, for convenience if you
735 * are going to call back with {@link #onActivityResult(int, int, Intent)}.
736 */
737 public void setTargetFragment(Fragment fragment, int requestCode) {
738 mTarget = fragment;
739 mTargetRequestCode = requestCode;
740 }
741
742 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700743 * Return the target fragment set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700744 */
745 final public Fragment getTargetFragment() {
746 return mTarget;
747 }
748
749 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700750 * Return the target request code set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700751 */
752 final public int getTargetRequestCode() {
753 return mTargetRequestCode;
754 }
755
756 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700757 * Return the Activity this fragment is currently associated with.
758 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700759 final public Activity getActivity() {
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700760 return mActivity;
761 }
762
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700763 /**
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800764 * Return <code>getActivity().getResources()</code>.
765 */
766 final public Resources getResources() {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800767 if (mActivity == null) {
768 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
769 }
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800770 return mActivity.getResources();
771 }
772
773 /**
774 * Return a localized, styled CharSequence from the application's package's
775 * default string table.
776 *
777 * @param resId Resource id for the CharSequence text
778 */
779 public final CharSequence getText(int resId) {
780 return getResources().getText(resId);
781 }
782
783 /**
784 * Return a localized string from the application's package's
785 * default string table.
786 *
787 * @param resId Resource id for the string
788 */
789 public final String getString(int resId) {
790 return getResources().getString(resId);
791 }
792
793 /**
794 * Return a localized formatted string from the application's package's
795 * default string table, substituting the format arguments as defined in
796 * {@link java.util.Formatter} and {@link java.lang.String#format}.
797 *
798 * @param resId Resource id for the format string
799 * @param formatArgs The format arguments that will be used for substitution.
800 */
801
802 public final String getString(int resId, Object... formatArgs) {
803 return getResources().getString(resId, formatArgs);
804 }
805
806 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700807 * Return the FragmentManager for interacting with fragments associated
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700808 * with this fragment's activity. Note that this will be non-null slightly
Dianne Hackborn625ac272010-09-17 18:29:22 -0700809 * before {@link #getActivity()}, during the time from when the fragment is
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700810 * placed in a {@link FragmentTransaction} until it is committed and
811 * attached to its activity.
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700812 *
813 * <p>If this Fragment is a child of another Fragment, the FragmentManager
814 * returned here will be the parent's {@link #getChildFragmentManager()}.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700815 */
816 final public FragmentManager getFragmentManager() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700817 return mFragmentManager;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700818 }
819
820 /**
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700821 * Return a private FragmentManager for placing and managing Fragments
822 * inside of this Fragment.
823 */
824 final public FragmentManager getChildFragmentManager() {
825 if (mChildFragmentManager == null) {
826 instantiateChildFragmentManager();
827 if (mState >= RESUMED) {
828 mChildFragmentManager.dispatchResume();
829 } else if (mState >= STARTED) {
830 mChildFragmentManager.dispatchStart();
831 } else if (mState >= ACTIVITY_CREATED) {
832 mChildFragmentManager.dispatchActivityCreated();
833 } else if (mState >= CREATED) {
834 mChildFragmentManager.dispatchCreate();
835 }
836 }
837 return mChildFragmentManager;
838 }
839
840 /**
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -0700841 * Returns the parent Fragment containing this Fragment. If this Fragment
842 * is attached directly to an Activity, returns null.
843 */
844 final public Fragment getParentFragment() {
845 return mParentFragment;
846 }
847
848 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700849 * Return true if the fragment is currently added to its activity.
850 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700851 final public boolean isAdded() {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800852 return mActivity != null && mAdded;
853 }
854
855 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700856 * Return true if the fragment has been explicitly detached from the UI.
857 * That is, {@link FragmentTransaction#detach(Fragment)
858 * FragmentTransaction.detach(Fragment)} has been used on it.
859 */
860 final public boolean isDetached() {
861 return mDetached;
862 }
863
864 /**
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800865 * Return true if this fragment is currently being removed from its
866 * activity. This is <em>not</em> whether its activity is finishing, but
867 * rather whether it is in the process of being removed from its activity.
868 */
869 final public boolean isRemoving() {
870 return mRemoving;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700871 }
872
873 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700874 * Return true if the layout is included as part of an activity view
875 * hierarchy via the &lt;fragment&gt; tag. This will always be true when
876 * fragments are created through the &lt;fragment&gt; tag, <em>except</em>
877 * in the case where an old fragment is restored from a previous state and
878 * it does not appear in the layout of the current state.
879 */
880 final public boolean isInLayout() {
881 return mInLayout;
882 }
883
884 /**
Dianne Hackborn2707d602010-07-09 18:01:20 -0700885 * Return true if the fragment is in the resumed state. This is true
886 * for the duration of {@link #onResume()} and {@link #onPause()} as well.
887 */
888 final public boolean isResumed() {
889 return mResumed;
890 }
891
892 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700893 * Return true if the fragment is currently visible to the user. This means
894 * it: (1) has been added, (2) has its view attached to the window, and
895 * (3) is not hidden.
896 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700897 final public boolean isVisible() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700898 return isAdded() && !isHidden() && mView != null
899 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
900 }
901
902 /**
903 * Return true if the fragment has been hidden. By default fragments
904 * are shown. You can find out about changes to this state with
Dianne Hackborncddfa6d2010-05-19 22:56:37 -0700905 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700906 * to other states -- that is, to be visible to the user, a fragment
907 * must be both started and not hidden.
908 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700909 final public boolean isHidden() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700910 return mHidden;
911 }
912
913 /**
914 * Called when the hidden state (as returned by {@link #isHidden()} of
915 * the fragment has changed. Fragments start out not hidden; this will
916 * be called whenever the fragment changes state from that.
917 * @param hidden True if the fragment is now hidden, false if it is not
918 * visible.
919 */
920 public void onHiddenChanged(boolean hidden) {
921 }
922
923 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700924 * Control whether a fragment instance is retained across Activity
925 * re-creation (such as from a configuration change). This can only
926 * be used with fragments not in the back stack. If set, the fragment
927 * lifecycle will be slightly different when an activity is recreated:
928 * <ul>
929 * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still
930 * will be, because the fragment is being detached from its current activity).
931 * <li> {@link #onCreate(Bundle)} will not be called since the fragment
932 * is not being re-created.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700933 * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b>
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700934 * still be called.
935 * </ul>
936 */
937 public void setRetainInstance(boolean retain) {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -0700938 if (retain && mParentFragment != null) {
939 throw new IllegalStateException(
940 "Can't retain fragements that are nested in other fragments");
941 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700942 mRetainInstance = retain;
943 }
944
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700945 final public boolean getRetainInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700946 return mRetainInstance;
947 }
948
949 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700950 * Report that this fragment would like to participate in populating
Wink Saville4dc643e2010-06-12 22:16:41 -0700951 * the options menu by receiving a call to {@link #onCreateOptionsMenu}
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700952 * and related methods.
953 *
954 * @param hasMenu If true, the fragment has menu items to contribute.
955 */
956 public void setHasOptionsMenu(boolean hasMenu) {
957 if (mHasMenu != hasMenu) {
958 mHasMenu = hasMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -0700959 if (isAdded() && !isHidden()) {
960 mFragmentManager.invalidateOptionsMenu();
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700961 }
962 }
963 }
Dianne Hackborn6c285972011-08-29 16:53:49 -0700964
965 /**
966 * Set a hint for whether this fragment's menu should be visible. This
967 * is useful if you know that a fragment has been placed in your view
968 * hierarchy so that the user can not currently seen it, so any menu items
969 * it has should also not be shown.
970 *
971 * @param menuVisible The default is true, meaning the fragment's menu will
972 * be shown as usual. If false, the user will not see the menu.
973 */
974 public void setMenuVisibility(boolean menuVisible) {
975 if (mMenuVisible != menuVisible) {
976 mMenuVisible = menuVisible;
977 if (mHasMenu && isAdded() && !isHidden()) {
978 mFragmentManager.invalidateOptionsMenu();
979 }
980 }
981 }
982
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700983 /**
Adam Powell78fed9b2011-11-07 10:45:34 -0800984 * Set a hint to the system about whether this fragment's UI is currently visible
985 * to the user. This hint defaults to true and is persistent across fragment instance
986 * state save and restore.
Adam Powell635c60a2011-10-26 10:22:16 -0700987 *
Adam Powell78fed9b2011-11-07 10:45:34 -0800988 * <p>An app may set this to false to indicate that the fragment's UI is
989 * scrolled out of visibility or is otherwise not directly visible to the user.
990 * This may be used by the system to prioritize operations such as fragment lifecycle updates
991 * or loader ordering behavior.</p>
Adam Powell635c60a2011-10-26 10:22:16 -0700992 *
Adam Powell78fed9b2011-11-07 10:45:34 -0800993 * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
994 * false if it is not.
Adam Powell635c60a2011-10-26 10:22:16 -0700995 */
Adam Powell78fed9b2011-11-07 10:45:34 -0800996 public void setUserVisibleHint(boolean isVisibleToUser) {
997 if (!mUserVisibleHint && isVisibleToUser && mState < STARTED) {
Adam Powell635c60a2011-10-26 10:22:16 -0700998 mFragmentManager.performPendingDeferredStart(this);
999 }
Adam Powell78fed9b2011-11-07 10:45:34 -08001000 mUserVisibleHint = isVisibleToUser;
1001 mDeferStart = !isVisibleToUser;
Adam Powell635c60a2011-10-26 10:22:16 -07001002 }
1003
1004 /**
Adam Powell78fed9b2011-11-07 10:45:34 -08001005 * @return The current value of the user-visible hint on this fragment.
1006 * @see #setUserVisibleHint(boolean)
Adam Powell635c60a2011-10-26 10:22:16 -07001007 */
Adam Powell78fed9b2011-11-07 10:45:34 -08001008 public boolean getUserVisibleHint() {
1009 return mUserVisibleHint;
Adam Powell635c60a2011-10-26 10:22:16 -07001010 }
1011
1012 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001013 * Return the LoaderManager for this fragment, creating it if needed.
1014 */
1015 public LoaderManager getLoaderManager() {
1016 if (mLoaderManager != null) {
1017 return mLoaderManager;
1018 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001019 if (mActivity == null) {
1020 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1021 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001022 mCheckedForLoaderManager = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001023 mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -07001024 return mLoaderManager;
1025 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001026
Dianne Hackbornc8017682010-07-06 13:34:38 -07001027 /**
Scott Main87bff972013-02-27 15:46:34 -08001028 * Call {@link Activity#startActivity(Intent)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001029 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001030 *
1031 * @param intent The intent to start.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001032 */
1033 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001034 startActivity(intent, null);
1035 }
1036
1037 /**
Scott Main87bff972013-02-27 15:46:34 -08001038 * Call {@link Activity#startActivity(Intent, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001039 * containing Activity.
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07001040 *
1041 * @param intent The intent to start.
1042 * @param options Additional options for how the Activity should be started.
1043 * See {@link android.content.Context#startActivity(Intent, Bundle)
1044 * Context.startActivity(Intent, Bundle)} for more details.
Dianne Hackborna4972e92012-03-14 10:38:05 -07001045 */
1046 public void startActivity(Intent intent, Bundle options) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001047 if (mActivity == null) {
1048 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1049 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001050 if (options != null) {
1051 mActivity.startActivityFromFragment(this, intent, -1, options);
1052 } else {
1053 // Note we want to go through this call for compatibility with
1054 // applications that may have overridden the method.
1055 mActivity.startActivityFromFragment(this, intent, -1);
1056 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001057 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001058
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001059 /**
Scott Main87bff972013-02-27 15:46:34 -08001060 * Call {@link Activity#startActivityForResult(Intent, int)} from the fragment's
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001061 * containing Activity.
1062 */
1063 public void startActivityForResult(Intent intent, int requestCode) {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001064 startActivityForResult(intent, requestCode, null);
1065 }
1066
1067 /**
Scott Main87bff972013-02-27 15:46:34 -08001068 * Call {@link Activity#startActivityForResult(Intent, int, Bundle)} from the fragment's
Dianne Hackborna4972e92012-03-14 10:38:05 -07001069 * containing Activity.
1070 */
1071 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001072 if (mActivity == null) {
1073 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
1074 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07001075 if (options != null) {
1076 mActivity.startActivityFromFragment(this, intent, requestCode, options);
1077 } else {
1078 // Note we want to go through this call for compatibility with
1079 // applications that may have overridden the method.
1080 mActivity.startActivityFromFragment(this, intent, requestCode, options);
1081 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001082 }
1083
1084 /**
1085 * Receive the result from a previous call to
1086 * {@link #startActivityForResult(Intent, int)}. This follows the
1087 * related Activity API as described there in
1088 * {@link Activity#onActivityResult(int, int, Intent)}.
1089 *
1090 * @param requestCode The integer request code originally supplied to
1091 * startActivityForResult(), allowing you to identify who this
1092 * result came from.
1093 * @param resultCode The integer result code returned by the child activity
1094 * through its setResult().
1095 * @param data An Intent, which can return result data to the caller
1096 * (various data can be attached to Intent "extras").
1097 */
1098 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1099 }
1100
1101 /**
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001102 * @hide Hack so that DialogFragment can make its Dialog before creating
1103 * its views, and the view construction can use the dialog's context for
1104 * inflation. Maybe this should become a public API. Note sure.
1105 */
1106 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
1107 return mActivity.getLayoutInflater();
1108 }
1109
1110 /**
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001111 * @deprecated Use {@link #onInflate(Activity, AttributeSet, Bundle)} instead.
1112 */
1113 @Deprecated
1114 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
1115 mCalled = true;
1116 }
1117
1118 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001119 * Called when a fragment is being created as part of a view layout
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001120 * inflation, typically from setting the content view of an activity. This
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001121 * may be called immediately after the fragment is created from a <fragment>
Dianne Hackborndef15372010-08-15 12:43:52 -07001122 * tag in a layout file. Note this is <em>before</em> the fragment's
1123 * {@link #onAttach(Activity)} has been called; all you should do here is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001124 * parse the attributes and save them away.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001125 *
Dianne Hackborndef15372010-08-15 12:43:52 -07001126 * <p>This is called every time the fragment is inflated, even if it is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001127 * being inflated into a new instance with saved state. It typically makes
1128 * sense to re-parse the parameters each time, to allow them to change with
1129 * different configurations.</p>
1130 *
1131 * <p>Here is a typical implementation of a fragment that can take parameters
1132 * both through attributes supplied here as well from {@link #getArguments()}:</p>
1133 *
1134 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1135 * fragment}
1136 *
1137 * <p>Note that parsing the XML attributes uses a "styleable" resource. The
1138 * declaration for the styleable used here is:</p>
1139 *
1140 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments}
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001141 *
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001142 * <p>The fragment can then be declared within its activity's content layout
1143 * through a tag like this:</p>
1144 *
1145 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes}
1146 *
1147 * <p>This fragment can also be created dynamically from arguments given
1148 * at runtime in the arguments Bundle; here is an example of doing so at
1149 * creation of the containing activity:</p>
1150 *
1151 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1152 * create}
1153 *
1154 * @param activity The Activity that is inflating this fragment.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001155 * @param attrs The attributes at the tag where the fragment is
1156 * being created.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001157 * @param savedInstanceState If the fragment is being re-created from
1158 * a previous saved state, this is the state.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001159 */
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001160 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
1161 onInflate(attrs, savedInstanceState);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001162 mCalled = true;
1163 }
1164
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001165 /**
1166 * Called when a fragment is first attached to its activity.
1167 * {@link #onCreate(Bundle)} will be called after this.
1168 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001169 public void onAttach(Activity activity) {
1170 mCalled = true;
1171 }
1172
Chet Haase811ed1062010-08-06 10:38:15 -07001173 /**
1174 * Called when a fragment loads an animation.
1175 */
Chet Haasea18a86b2010-09-07 13:20:00 -07001176 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001177 return null;
1178 }
1179
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001180 /**
1181 * Called to do initial creation of a fragment. This is called after
Dianne Hackbornc8017682010-07-06 13:34:38 -07001182 * {@link #onAttach(Activity)} and before
1183 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1184 *
1185 * <p>Note that this can be called while the fragment's activity is
1186 * still in the process of being created. As such, you can not rely
1187 * on things like the activity's content view hierarchy being initialized
1188 * at this point. If you want to do work once the activity itself is
1189 * created, see {@link #onActivityCreated(Bundle)}.
1190 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001191 * @param savedInstanceState If the fragment is being re-created from
1192 * a previous saved state, this is the state.
1193 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001194 public void onCreate(Bundle savedInstanceState) {
1195 mCalled = true;
1196 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001197
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001198 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001199 * Called to have the fragment instantiate its user interface view.
1200 * This is optional, and non-graphical fragments can return null (which
1201 * is the default implementation). This will be called between
Dianne Hackbornc8017682010-07-06 13:34:38 -07001202 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001203 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001204 * <p>If you return a View from here, you will later be called in
1205 * {@link #onDestroyView} when the view is being released.
1206 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001207 * @param inflater The LayoutInflater object that can be used to inflate
1208 * any views in the fragment,
1209 * @param container If non-null, this is the parent view that the fragment's
1210 * UI should be attached to. The fragment should not add the view itself,
1211 * but this can be used to generate the LayoutParams of the view.
1212 * @param savedInstanceState If non-null, this fragment is being re-constructed
1213 * from a previous saved state as given here.
1214 *
1215 * @return Return the View for the fragment's UI, or null.
1216 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001217 @Nullable
1218 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001219 Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001220 return null;
1221 }
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001222
1223 /**
1224 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
1225 * has returned, but before any saved state has been restored in to the view.
1226 * This gives subclasses a chance to initialize themselves once
1227 * they know their view hierarchy has been completely created. The fragment's
1228 * view hierarchy is not however attached to its parent at this point.
1229 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1230 * @param savedInstanceState If non-null, this fragment is being re-constructed
1231 * from a previous saved state as given here.
1232 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001233 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001234 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001235
Scott Main662cc7a42011-01-17 15:02:07 -08001236 /**
1237 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
1238 * if provided.
1239 *
1240 * @return The fragment's root view, or null if it has no layout.
1241 */
Tor Norbyed9273d62013-05-30 15:59:53 -07001242 @Nullable
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001243 public View getView() {
1244 return mView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001245 }
1246
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001247 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001248 * Called when the fragment's activity has been created and this
1249 * fragment's view hierarchy instantiated. It can be used to do final
1250 * initialization once these pieces are in place, such as retrieving
1251 * views or restoring state. It is also useful for fragments that use
1252 * {@link #setRetainInstance(boolean)} to retain their instance,
1253 * as this callback tells the fragment when it is fully associated with
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001254 * the new activity instance. This is called after {@link #onCreateView}
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001255 * and before {@link #onViewStateRestored(Bundle)}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001256 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001257 * @param savedInstanceState If the fragment is being re-created from
1258 * a previous saved state, this is the state.
1259 */
Dianne Hackbornc8017682010-07-06 13:34:38 -07001260 public void onActivityCreated(Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001261 mCalled = true;
1262 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001263
1264 /**
1265 * Called when all saved state has been restored into the view hierarchy
1266 * of the fragment. This can be used to do initialization based on saved
1267 * state that you are letting the view hierarchy track itself, such as
1268 * whether check box widgets are currently checked. This is called
1269 * after {@link #onActivityCreated(Bundle)} and before
1270 * {@link #onStart()}.
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001271 *
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001272 * @param savedInstanceState If the fragment is being re-created from
1273 * a previous saved state, this is the state.
1274 */
1275 public void onViewStateRestored(Bundle savedInstanceState) {
1276 mCalled = true;
1277 }
1278
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001279 /**
1280 * Called when the Fragment is visible to the user. This is generally
1281 * tied to {@link Activity#onStart() Activity.onStart} of the containing
1282 * Activity's lifecycle.
1283 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001284 public void onStart() {
1285 mCalled = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001286
1287 if (!mLoadersStarted) {
1288 mLoadersStarted = true;
1289 if (!mCheckedForLoaderManager) {
1290 mCheckedForLoaderManager = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001291 mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001292 }
1293 if (mLoaderManager != null) {
1294 mLoaderManager.doStart();
1295 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001296 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001297 }
1298
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001299 /**
1300 * Called when the fragment is visible to the user and actively running.
1301 * This is generally
1302 * tied to {@link Activity#onResume() Activity.onResume} of the containing
1303 * Activity's lifecycle.
1304 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001305 public void onResume() {
1306 mCalled = true;
1307 }
1308
Dianne Hackborn72778202010-08-20 18:26:01 -07001309 /**
1310 * Called to ask the fragment to save its current dynamic state, so it
1311 * can later be reconstructed in a new instance of its process is
1312 * restarted. If a new instance of the fragment later needs to be
1313 * created, the data you place in the Bundle here will be available
1314 * in the Bundle given to {@link #onCreate(Bundle)},
1315 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
1316 * {@link #onActivityCreated(Bundle)}.
1317 *
1318 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
Daisuke Miyakawa2f761762010-09-12 16:53:17 -07001319 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
Dianne Hackborn72778202010-08-20 18:26:01 -07001320 * applies here as well. Note however: <em>this method may be called
1321 * at any time before {@link #onDestroy()}</em>. There are many situations
1322 * where a fragment may be mostly torn down (such as when placed on the
1323 * back stack with no UI showing), but its state will not be saved until
1324 * its owning activity actually needs to save its state.
1325 *
1326 * @param outState Bundle in which to place your saved state.
1327 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001328 public void onSaveInstanceState(Bundle outState) {
1329 }
1330
1331 public void onConfigurationChanged(Configuration newConfig) {
1332 mCalled = true;
1333 }
1334
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001335 /**
1336 * Called when the Fragment is no longer resumed. This is generally
1337 * tied to {@link Activity#onPause() Activity.onPause} of the containing
1338 * Activity's lifecycle.
1339 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001340 public void onPause() {
1341 mCalled = true;
1342 }
1343
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001344 /**
1345 * Called when the Fragment is no longer started. This is generally
1346 * tied to {@link Activity#onStop() Activity.onStop} of the containing
1347 * Activity's lifecycle.
1348 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001349 public void onStop() {
1350 mCalled = true;
1351 }
1352
1353 public void onLowMemory() {
1354 mCalled = true;
1355 }
1356
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001357 public void onTrimMemory(int level) {
1358 mCalled = true;
1359 }
1360
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001361 /**
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001362 * Called when the view previously created by {@link #onCreateView} has
1363 * been detached from the fragment. The next time the fragment needs
1364 * to be displayed, a new view will be created. This is called
Dianne Hackborndef15372010-08-15 12:43:52 -07001365 * after {@link #onStop()} and before {@link #onDestroy()}. It is called
1366 * <em>regardless</em> of whether {@link #onCreateView} returned a
1367 * non-null view. Internally it is called after the view's state has
1368 * been saved but before it has been removed from its parent.
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001369 */
1370 public void onDestroyView() {
1371 mCalled = true;
1372 }
1373
1374 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001375 * Called when the fragment is no longer in use. This is called
1376 * after {@link #onStop()} and before {@link #onDetach()}.
1377 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001378 public void onDestroy() {
1379 mCalled = true;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001380 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager
1381 // + " mLoaderManager=" + mLoaderManager);
1382 if (!mCheckedForLoaderManager) {
1383 mCheckedForLoaderManager = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001384 mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001385 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001386 if (mLoaderManager != null) {
1387 mLoaderManager.doDestroy();
1388 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001389 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001390
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001391 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001392 * Called by the fragment manager once this fragment has been removed,
1393 * so that we don't have any left-over state if the application decides
1394 * to re-use the instance. This only clears state that the framework
1395 * internally manages, not things the application sets.
1396 */
1397 void initState() {
1398 mIndex = -1;
1399 mWho = null;
1400 mAdded = false;
1401 mRemoving = false;
1402 mResumed = false;
1403 mFromLayout = false;
1404 mInLayout = false;
1405 mRestored = false;
1406 mBackStackNesting = 0;
1407 mFragmentManager = null;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001408 mActivity = null;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001409 mFragmentId = 0;
1410 mContainerId = 0;
1411 mTag = null;
1412 mHidden = false;
1413 mDetached = false;
1414 mRetaining = false;
1415 mLoaderManager = null;
1416 mLoadersStarted = false;
1417 mCheckedForLoaderManager = false;
1418 }
1419
1420 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001421 * Called when the fragment is no longer attached to its activity. This
1422 * is called after {@link #onDestroy()}.
1423 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001424 public void onDetach() {
1425 mCalled = true;
1426 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001427
1428 /**
1429 * Initialize the contents of the Activity's standard options menu. You
1430 * should place your menu items in to <var>menu</var>. For this method
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001431 * to be called, you must have first called {@link #setHasOptionsMenu}. See
1432 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
1433 * for more information.
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001434 *
1435 * @param menu The options menu in which you place your items.
1436 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001437 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001438 * @see #onPrepareOptionsMenu
1439 * @see #onOptionsItemSelected
1440 */
1441 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1442 }
1443
1444 /**
1445 * Prepare the Screen's standard options menu to be displayed. This is
1446 * called right before the menu is shown, every time it is shown. You can
1447 * use this method to efficiently enable/disable items or otherwise
1448 * dynamically modify the contents. See
1449 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu}
1450 * for more information.
1451 *
1452 * @param menu The options menu as last shown or first initialized by
1453 * onCreateOptionsMenu().
1454 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001455 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001456 * @see #onCreateOptionsMenu
1457 */
1458 public void onPrepareOptionsMenu(Menu menu) {
1459 }
1460
1461 /**
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001462 * Called when this fragment's option menu items are no longer being
1463 * included in the overall options menu. Receiving this call means that
1464 * the menu needed to be rebuilt, but this fragment's items were not
1465 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
1466 * was not called).
1467 */
1468 public void onDestroyOptionsMenu() {
1469 }
1470
1471 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001472 * This hook is called whenever an item in your options menu is selected.
1473 * The default implementation simply returns false to have the normal
1474 * processing happen (calling the item's Runnable or sending a message to
1475 * its Handler as appropriate). You can use this method for any items
1476 * for which you would like to do processing without those other
1477 * facilities.
1478 *
1479 * <p>Derived classes should call through to the base class for it to
1480 * perform the default menu handling.
1481 *
1482 * @param item The menu item that was selected.
1483 *
1484 * @return boolean Return false to allow normal menu processing to
1485 * proceed, true to consume it here.
1486 *
1487 * @see #onCreateOptionsMenu
1488 */
1489 public boolean onOptionsItemSelected(MenuItem item) {
1490 return false;
1491 }
1492
1493 /**
1494 * This hook is called whenever the options menu is being closed (either by the user canceling
1495 * the menu with the back/menu button, or when an item is selected).
1496 *
1497 * @param menu The options menu as last shown or first initialized by
1498 * onCreateOptionsMenu().
1499 */
1500 public void onOptionsMenuClosed(Menu menu) {
1501 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001502
1503 /**
1504 * Called when a context menu for the {@code view} is about to be shown.
1505 * Unlike {@link #onCreateOptionsMenu}, this will be called every
1506 * time the context menu is about to be shown and should be populated for
1507 * the view (or item inside the view for {@link AdapterView} subclasses,
1508 * this can be found in the {@code menuInfo})).
1509 * <p>
1510 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
1511 * item has been selected.
1512 * <p>
1513 * The default implementation calls up to
1514 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
1515 * you can not call this implementation if you don't want that behavior.
1516 * <p>
1517 * It is not safe to hold onto the context menu after this method returns.
1518 * {@inheritDoc}
1519 */
1520 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1521 getActivity().onCreateContextMenu(menu, v, menuInfo);
1522 }
1523
1524 /**
1525 * Registers a context menu to be shown for the given view (multiple views
1526 * can show the context menu). This method will set the
1527 * {@link OnCreateContextMenuListener} on the view to this fragment, so
1528 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
1529 * called when it is time to show the context menu.
1530 *
1531 * @see #unregisterForContextMenu(View)
1532 * @param view The view that should show a context menu.
1533 */
1534 public void registerForContextMenu(View view) {
1535 view.setOnCreateContextMenuListener(this);
1536 }
1537
1538 /**
1539 * Prevents a context menu to be shown for the given view. This method will
1540 * remove the {@link OnCreateContextMenuListener} on the view.
1541 *
1542 * @see #registerForContextMenu(View)
1543 * @param view The view that should stop showing a context menu.
1544 */
1545 public void unregisterForContextMenu(View view) {
1546 view.setOnCreateContextMenuListener(null);
1547 }
1548
1549 /**
1550 * This hook is called whenever an item in a context menu is selected. The
1551 * default implementation simply returns false to have the normal processing
1552 * happen (calling the item's Runnable or sending a message to its Handler
1553 * as appropriate). You can use this method for any items for which you
1554 * would like to do processing without those other facilities.
1555 * <p>
1556 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
1557 * View that added this menu item.
1558 * <p>
1559 * Derived classes should call through to the base class for it to perform
1560 * the default menu handling.
1561 *
1562 * @param item The context menu item that was selected.
1563 * @return boolean Return false to allow normal context menu processing to
1564 * proceed, true to consume it here.
1565 */
1566 public boolean onContextItemSelected(MenuItem item) {
1567 return false;
1568 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07001569
Dianne Hackborn625ac272010-09-17 18:29:22 -07001570 /**
1571 * Print the Fragments's state into the given stream.
1572 *
1573 * @param prefix Text to print at the front of each line.
1574 * @param fd The raw file descriptor that the dump is being sent to.
1575 * @param writer The PrintWriter to which you should dump your state. This will be
1576 * closed for you after you return.
1577 * @param args additional arguments to the dump request.
1578 */
1579 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08001580 writer.print(prefix); writer.print("mFragmentId=#");
1581 writer.print(Integer.toHexString(mFragmentId));
Dianne Hackbornee76efb2012-06-05 10:27:40 -07001582 writer.print(" mContainerId=#");
Dianne Hackborn30d71892010-12-11 10:37:55 -08001583 writer.print(Integer.toHexString(mContainerId));
Dianne Hackborn625ac272010-09-17 18:29:22 -07001584 writer.print(" mTag="); writer.println(mTag);
1585 writer.print(prefix); writer.print("mState="); writer.print(mState);
1586 writer.print(" mIndex="); writer.print(mIndex);
1587 writer.print(" mWho="); writer.print(mWho);
1588 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting);
1589 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded);
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001590 writer.print(" mRemoving="); writer.print(mRemoving);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001591 writer.print(" mResumed="); writer.print(mResumed);
1592 writer.print(" mFromLayout="); writer.print(mFromLayout);
1593 writer.print(" mInLayout="); writer.println(mInLayout);
1594 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001595 writer.print(" mDetached="); writer.print(mDetached);
Dianne Hackborn6c285972011-08-29 16:53:49 -07001596 writer.print(" mMenuVisible="); writer.print(mMenuVisible);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001597 writer.print(" mHasMenu="); writer.println(mHasMenu);
Dianne Hackborn6c285972011-08-29 16:53:49 -07001598 writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance);
Adam Powell78fed9b2011-11-07 10:45:34 -08001599 writer.print(" mRetaining="); writer.print(mRetaining);
1600 writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001601 if (mFragmentManager != null) {
1602 writer.print(prefix); writer.print("mFragmentManager=");
1603 writer.println(mFragmentManager);
1604 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001605 if (mActivity != null) {
1606 writer.print(prefix); writer.print("mActivity=");
1607 writer.println(mActivity);
1608 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001609 if (mParentFragment != null) {
1610 writer.print(prefix); writer.print("mParentFragment=");
1611 writer.println(mParentFragment);
1612 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001613 if (mArguments != null) {
1614 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments);
1615 }
1616 if (mSavedFragmentState != null) {
1617 writer.print(prefix); writer.print("mSavedFragmentState=");
1618 writer.println(mSavedFragmentState);
1619 }
1620 if (mSavedViewState != null) {
1621 writer.print(prefix); writer.print("mSavedViewState=");
1622 writer.println(mSavedViewState);
1623 }
1624 if (mTarget != null) {
1625 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget);
1626 writer.print(" mTargetRequestCode=");
1627 writer.println(mTargetRequestCode);
1628 }
1629 if (mNextAnim != 0) {
1630 writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim);
1631 }
1632 if (mContainer != null) {
1633 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer);
1634 }
1635 if (mView != null) {
1636 writer.print(prefix); writer.print("mView="); writer.println(mView);
1637 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001638 if (mAnimatingAway != null) {
1639 writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway);
1640 writer.print(prefix); writer.print("mStateAfterAnimating=");
1641 writer.println(mStateAfterAnimating);
1642 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001643 if (mLoaderManager != null) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08001644 writer.print(prefix); writer.println("Loader Manager:");
1645 mLoaderManager.dump(prefix + " ", fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001646 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001647 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001648 writer.print(prefix); writer.println("Child " + mChildFragmentManager + ":");
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001649 mChildFragmentManager.dump(prefix + " ", fd, writer, args);
1650 }
1651 }
1652
1653 Fragment findFragmentByWho(String who) {
1654 if (who.equals(mWho)) {
1655 return this;
1656 }
1657 if (mChildFragmentManager != null) {
1658 return mChildFragmentManager.findFragmentByWho(who);
1659 }
1660 return null;
1661 }
1662
1663 void instantiateChildFragmentManager() {
1664 mChildFragmentManager = new FragmentManagerImpl();
1665 mChildFragmentManager.attachActivity(mActivity, new FragmentContainer() {
1666 @Override
1667 public View findViewById(int id) {
1668 if (mView == null) {
1669 throw new IllegalStateException("Fragment does not have a view");
1670 }
1671 return mView.findViewById(id);
1672 }
1673 }, this);
1674 }
1675
1676 void performCreate(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001677 if (mChildFragmentManager != null) {
1678 mChildFragmentManager.noteStateNotSaved();
1679 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001680 mCalled = false;
1681 onCreate(savedInstanceState);
1682 if (!mCalled) {
1683 throw new SuperNotCalledException("Fragment " + this
1684 + " did not call through to super.onCreate()");
1685 }
1686 if (savedInstanceState != null) {
1687 Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG);
1688 if (p != null) {
1689 if (mChildFragmentManager == null) {
1690 instantiateChildFragmentManager();
1691 }
1692 mChildFragmentManager.restoreAllState(p, null);
1693 mChildFragmentManager.dispatchCreate();
1694 }
1695 }
1696 }
1697
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001698 View performCreateView(LayoutInflater inflater, ViewGroup container,
1699 Bundle savedInstanceState) {
1700 if (mChildFragmentManager != null) {
1701 mChildFragmentManager.noteStateNotSaved();
1702 }
1703 return onCreateView(inflater, container, savedInstanceState);
1704 }
1705
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001706 void performActivityCreated(Bundle savedInstanceState) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001707 if (mChildFragmentManager != null) {
1708 mChildFragmentManager.noteStateNotSaved();
1709 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001710 mCalled = false;
1711 onActivityCreated(savedInstanceState);
1712 if (!mCalled) {
1713 throw new SuperNotCalledException("Fragment " + this
1714 + " did not call through to super.onActivityCreated()");
1715 }
1716 if (mChildFragmentManager != null) {
1717 mChildFragmentManager.dispatchActivityCreated();
1718 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001719 }
1720
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001721 void performStart() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001722 if (mChildFragmentManager != null) {
1723 mChildFragmentManager.noteStateNotSaved();
1724 mChildFragmentManager.execPendingActions();
1725 }
1726 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001727 onStart();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001728 if (!mCalled) {
1729 throw new SuperNotCalledException("Fragment " + this
1730 + " did not call through to super.onStart()");
1731 }
1732 if (mChildFragmentManager != null) {
1733 mChildFragmentManager.dispatchStart();
1734 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001735 if (mLoaderManager != null) {
1736 mLoaderManager.doReportStart();
1737 }
1738 }
1739
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001740 void performResume() {
1741 if (mChildFragmentManager != null) {
Dianne Hackborn1b8ecc52012-09-08 17:03:52 -07001742 mChildFragmentManager.noteStateNotSaved();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001743 mChildFragmentManager.execPendingActions();
1744 }
1745 mCalled = false;
1746 onResume();
1747 if (!mCalled) {
1748 throw new SuperNotCalledException("Fragment " + this
1749 + " did not call through to super.onResume()");
1750 }
1751 if (mChildFragmentManager != null) {
1752 mChildFragmentManager.dispatchResume();
1753 mChildFragmentManager.execPendingActions();
1754 }
1755 }
1756
1757 void performConfigurationChanged(Configuration newConfig) {
1758 onConfigurationChanged(newConfig);
1759 if (mChildFragmentManager != null) {
1760 mChildFragmentManager.dispatchConfigurationChanged(newConfig);
1761 }
1762 }
1763
1764 void performLowMemory() {
1765 onLowMemory();
1766 if (mChildFragmentManager != null) {
1767 mChildFragmentManager.dispatchLowMemory();
1768 }
1769 }
1770
1771 void performTrimMemory(int level) {
1772 onTrimMemory(level);
1773 if (mChildFragmentManager != null) {
1774 mChildFragmentManager.dispatchTrimMemory(level);
1775 }
1776 }
1777
1778 boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1779 boolean show = false;
1780 if (!mHidden) {
1781 if (mHasMenu && mMenuVisible) {
1782 show = true;
1783 onCreateOptionsMenu(menu, inflater);
1784 }
1785 if (mChildFragmentManager != null) {
1786 show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater);
1787 }
1788 }
1789 return show;
1790 }
1791
1792 boolean performPrepareOptionsMenu(Menu menu) {
1793 boolean show = false;
1794 if (!mHidden) {
1795 if (mHasMenu && mMenuVisible) {
1796 show = true;
1797 onPrepareOptionsMenu(menu);
1798 }
1799 if (mChildFragmentManager != null) {
1800 show |= mChildFragmentManager.dispatchPrepareOptionsMenu(menu);
1801 }
1802 }
1803 return show;
1804 }
1805
1806 boolean performOptionsItemSelected(MenuItem item) {
1807 if (!mHidden) {
1808 if (mHasMenu && mMenuVisible) {
1809 if (onOptionsItemSelected(item)) {
1810 return true;
1811 }
1812 }
1813 if (mChildFragmentManager != null) {
1814 if (mChildFragmentManager.dispatchOptionsItemSelected(item)) {
1815 return true;
1816 }
1817 }
1818 }
1819 return false;
1820 }
1821
1822 boolean performContextItemSelected(MenuItem item) {
1823 if (!mHidden) {
1824 if (onContextItemSelected(item)) {
1825 return true;
1826 }
1827 if (mChildFragmentManager != null) {
1828 if (mChildFragmentManager.dispatchContextItemSelected(item)) {
1829 return true;
1830 }
1831 }
1832 }
1833 return false;
1834 }
1835
1836 void performOptionsMenuClosed(Menu menu) {
1837 if (!mHidden) {
1838 if (mHasMenu && mMenuVisible) {
1839 onOptionsMenuClosed(menu);
1840 }
1841 if (mChildFragmentManager != null) {
1842 mChildFragmentManager.dispatchOptionsMenuClosed(menu);
1843 }
1844 }
1845 }
1846
1847 void performSaveInstanceState(Bundle outState) {
1848 onSaveInstanceState(outState);
1849 if (mChildFragmentManager != null) {
1850 Parcelable p = mChildFragmentManager.saveAllState();
1851 if (p != null) {
1852 outState.putParcelable(Activity.FRAGMENTS_TAG, p);
1853 }
1854 }
1855 }
1856
1857 void performPause() {
1858 if (mChildFragmentManager != null) {
1859 mChildFragmentManager.dispatchPause();
1860 }
1861 mCalled = false;
1862 onPause();
1863 if (!mCalled) {
1864 throw new SuperNotCalledException("Fragment " + this
1865 + " did not call through to super.onPause()");
1866 }
1867 }
1868
Dianne Hackborn2707d602010-07-09 18:01:20 -07001869 void performStop() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001870 if (mChildFragmentManager != null) {
1871 mChildFragmentManager.dispatchStop();
1872 }
1873 mCalled = false;
Dianne Hackborn2707d602010-07-09 18:01:20 -07001874 onStop();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001875 if (!mCalled) {
1876 throw new SuperNotCalledException("Fragment " + this
1877 + " did not call through to super.onStop()");
1878 }
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001879
1880 if (mLoadersStarted) {
1881 mLoadersStarted = false;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001882 if (!mCheckedForLoaderManager) {
1883 mCheckedForLoaderManager = true;
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001884 mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001885 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07001886 if (mLoaderManager != null) {
1887 if (mActivity == null || !mActivity.mChangingConfigurations) {
1888 mLoaderManager.doStop();
1889 } else {
1890 mLoaderManager.doRetain();
1891 }
1892 }
1893 }
1894 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001895
1896 void performDestroyView() {
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001897 if (mChildFragmentManager != null) {
1898 mChildFragmentManager.dispatchDestroyView();
1899 }
1900 mCalled = false;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001901 onDestroyView();
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001902 if (!mCalled) {
1903 throw new SuperNotCalledException("Fragment " + this
1904 + " did not call through to super.onDestroyView()");
1905 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001906 if (mLoaderManager != null) {
1907 mLoaderManager.doReportNextStart();
1908 }
1909 }
Dianne Hackborn62bea2f2012-09-04 18:48:15 -07001910
1911 void performDestroy() {
1912 if (mChildFragmentManager != null) {
1913 mChildFragmentManager.dispatchDestroy();
1914 }
1915 mCalled = false;
1916 onDestroy();
1917 if (!mCalled) {
1918 throw new SuperNotCalledException("Fragment " + this
1919 + " did not call through to super.onDestroy()");
1920 }
1921 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001922}