blob: 473a2d17ca39a1395706716d17c5096f03d5ef60 [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;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070020import android.content.ComponentCallbacks2;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070021import android.content.Context;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070022import android.content.Intent;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070023import android.content.res.Configuration;
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -080024import android.content.res.Resources;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070025import android.os.Bundle;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070026import android.os.Parcel;
27import android.os.Parcelable;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070028import android.util.AndroidRuntimeException;
Dianne Hackbornba51c3d2010-05-05 18:49:48 -070029import android.util.AttributeSet;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080030import android.util.DebugUtils;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070031import android.util.SparseArray;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070032import android.view.ContextMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -070033import android.view.ContextMenu.ContextMenuInfo;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070034import android.view.LayoutInflater;
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -070035import android.view.Menu;
36import android.view.MenuInflater;
37import android.view.MenuItem;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070038import android.view.View;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070039import android.view.View.OnCreateContextMenuListener;
Adam Powellf0f5fff2011-08-01 13:42:50 -070040import android.view.ViewGroup;
Dianne Hackborn5ddd1272010-06-12 10:15:28 -070041import android.widget.AdapterView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -070042
Dianne Hackborn625ac272010-09-17 18:29:22 -070043import java.io.FileDescriptor;
44import java.io.PrintWriter;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070045import java.util.HashMap;
46
47final class FragmentState implements Parcelable {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070048 final String mClassName;
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070049 final int mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070050 final boolean mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070051 final int mFragmentId;
52 final int mContainerId;
53 final String mTag;
54 final boolean mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070055 final boolean mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070056 final Bundle mArguments;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070057
58 Bundle mSavedFragmentState;
59
60 Fragment mInstance;
61
62 public FragmentState(Fragment frag) {
63 mClassName = frag.getClass().getName();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070064 mIndex = frag.mIndex;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070065 mFromLayout = frag.mFromLayout;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070066 mFragmentId = frag.mFragmentId;
67 mContainerId = frag.mContainerId;
68 mTag = frag.mTag;
69 mRetainInstance = frag.mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070070 mDetached = frag.mDetached;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070071 mArguments = frag.mArguments;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070072 }
73
74 public FragmentState(Parcel in) {
75 mClassName = in.readString();
Dianne Hackborn6e8304e2010-05-14 00:42:53 -070076 mIndex = in.readInt();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070077 mFromLayout = in.readInt() != 0;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070078 mFragmentId = in.readInt();
79 mContainerId = in.readInt();
80 mTag = in.readString();
81 mRetainInstance = in.readInt() != 0;
Dianne Hackborn16f6e892011-04-15 19:00:20 -070082 mDetached = in.readInt() != 0;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070083 mArguments = in.readBundle();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070084 mSavedFragmentState = in.readBundle();
85 }
86
87 public Fragment instantiate(Activity activity) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070088 if (mInstance != null) {
89 return mInstance;
90 }
91
Dianne Hackborn51642462010-10-28 10:32:37 -070092 if (mArguments != null) {
93 mArguments.setClassLoader(activity.getClassLoader());
94 }
95
Dianne Hackbornb7a2e472010-08-12 16:20:42 -070096 mInstance = Fragment.instantiate(activity, mClassName, mArguments);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -070097
98 if (mSavedFragmentState != null) {
99 mSavedFragmentState.setClassLoader(activity.getClassLoader());
100 mInstance.mSavedFragmentState = mSavedFragmentState;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700101 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700102 mInstance.setIndex(mIndex);
103 mInstance.mFromLayout = mFromLayout;
Dianne Hackborn352cc982011-01-04 11:34:18 -0800104 mInstance.mRestored = true;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700105 mInstance.mFragmentId = mFragmentId;
106 mInstance.mContainerId = mContainerId;
107 mInstance.mTag = mTag;
108 mInstance.mRetainInstance = mRetainInstance;
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700109 mInstance.mDetached = mDetached;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700110 mInstance.mFragmentManager = activity.mFragments;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700111
112 return mInstance;
113 }
114
115 public int describeContents() {
116 return 0;
117 }
118
119 public void writeToParcel(Parcel dest, int flags) {
120 dest.writeString(mClassName);
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700121 dest.writeInt(mIndex);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700122 dest.writeInt(mFromLayout ? 1 : 0);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700123 dest.writeInt(mFragmentId);
124 dest.writeInt(mContainerId);
125 dest.writeString(mTag);
126 dest.writeInt(mRetainInstance ? 1 : 0);
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700127 dest.writeInt(mDetached ? 1 : 0);
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700128 dest.writeBundle(mArguments);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700129 dest.writeBundle(mSavedFragmentState);
130 }
131
132 public static final Parcelable.Creator<FragmentState> CREATOR
133 = new Parcelable.Creator<FragmentState>() {
134 public FragmentState createFromParcel(Parcel in) {
135 return new FragmentState(in);
136 }
137
138 public FragmentState[] newArray(int size) {
139 return new FragmentState[size];
140 }
141 };
142}
143
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700144/**
145 * A Fragment is a piece of an application's user interface or behavior
Dianne Hackborn291905e2010-08-17 15:17:15 -0700146 * that can be placed in an {@link Activity}. Interaction with fragments
147 * is done through {@link FragmentManager}, which can be obtained via
148 * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and
149 * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}.
150 *
151 * <p>The Fragment class can be used many ways to achieve a wide variety of
152 * results. It is core, it represents a particular operation or interface
153 * that is running within a larger {@link Activity}. A Fragment is closely
154 * tied to the Activity it is in, and can not be used apart from one. Though
155 * Fragment defines its own lifecycle, that lifecycle is dependent on its
156 * activity: if the activity is stopped, no fragments inside of it can be
157 * started; when the activity is destroyed, all fragments will be destroyed.
158 *
159 * <p>All subclasses of Fragment must include a public empty constructor.
160 * The framework will often re-instantiate a fragment class when needed,
161 * in particular during state restore, and needs to be able to find this
162 * constructor to instantiate it. If the empty constructor is not available,
163 * a runtime exception will occur in some cases during state restore.
164 *
165 * <p>Topics covered here:
166 * <ol>
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700167 * <li><a href="#OlderPlatforms">Older Platforms</a>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700168 * <li><a href="#Lifecycle">Lifecycle</a>
169 * <li><a href="#Layout">Layout</a>
170 * <li><a href="#BackStack">Back Stack</a>
171 * </ol>
172 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700173 * <div class="special reference">
174 * <h3>Developer Guides</h3>
175 * <p>For more information about using fragments, read the
176 * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p>
177 * </div>
178 *
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700179 * <a name="OlderPlatforms"></a>
180 * <h3>Older Platforms</h3>
181 *
182 * While the Fragment API was introduced in
183 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API
184 * is also available for use on older platforms. See the blog post
185 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
186 * Fragments For All</a> for more details.
187 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700188 * <a name="Lifecycle"></a>
189 * <h3>Lifecycle</h3>
190 *
191 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has
192 * its own wrinkle on the standard activity lifecycle. It includes basic
193 * activity lifecycle methods such as {@link #onResume}, but also important
194 * are methods related to interactions with the activity and UI generation.
195 *
196 * <p>The core series of lifecycle methods that are called to bring a fragment
197 * up to resumed state (interacting with the user) are:
198 *
199 * <ol>
200 * <li> {@link #onAttach} called once the fragment is associated with its activity.
201 * <li> {@link #onCreate} called to do initial creation of the fragment.
202 * <li> {@link #onCreateView} creates and returns the view hierarchy associated
203 * with the fragment.
204 * <li> {@link #onActivityCreated} tells the fragment that its activity has
205 * completed its own {@link Activity#onCreate Activity.onCreaate}.
206 * <li> {@link #onStart} makes the fragment visible to the user (based on its
207 * containing activity being started).
208 * <li> {@link #onResume} makes the fragment interacting with the user (based on its
209 * containing activity being resumed).
210 * </ol>
211 *
212 * <p>As a fragment is no longer being used, it goes through a reverse
213 * series of callbacks:
214 *
215 * <ol>
216 * <li> {@link #onPause} fragment is no longer interacting with the user either
217 * because its activity is being paused or a fragment operation is modifying it
218 * in the activity.
219 * <li> {@link #onStop} fragment is no longer visible to the user either
220 * because its activity is being stopped or a fragment operation is modifying it
221 * in the activity.
222 * <li> {@link #onDestroyView} allows the fragment to clean up resources
223 * associated with its View.
224 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state.
225 * <li> {@link #onDetach} called immediately prior to the fragment no longer
226 * being associated with its activity.
227 * </ol>
228 *
229 * <a name="Layout"></a>
230 * <h3>Layout</h3>
231 *
232 * <p>Fragments can be used as part of your application's layout, allowing
233 * you to better modularize your code and more easily adjust your user
234 * interface to the screen it is running on. As an example, we can look
235 * at a simple program consisting of a list of items, and display of the
236 * details of each item.</p>
237 *
238 * <p>An activity's layout XML can include <code>&lt;fragment&gt;</code> tags
239 * to embed fragment instances inside of the layout. For example, here is
Dianne Hackborn625ac272010-09-17 18:29:22 -0700240 * a simple layout that embeds one fragment:</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700241 *
242 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout}
243 *
244 * <p>The layout is installed in the activity in the normal way:</p>
245 *
246 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
247 * main}
248 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800249 * <p>The titles fragment, showing a list of titles, is fairly simple, relying
Dianne Hackborn291905e2010-08-17 15:17:15 -0700250 * on {@link ListFragment} for most of its work. Note the implementation of
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800251 * clicking an item: depending on the current activity's layout, it can either
252 * create and display a new fragment to show the details in-place (more about
Ben Dodson542f2402011-06-14 16:40:23 -0700253 * this later), or start a new activity to show the details.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700254 *
255 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
256 * titles}
257 *
Ben Dodson542f2402011-06-14 16:40:23 -0700258 * <p>The details fragment showing the contents of a selected item just
Dianne Hackborn291905e2010-08-17 15:17:15 -0700259 * displays a string of text based on an index of a string array built in to
260 * the app:</p>
261 *
262 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
263 * details}
264 *
265 * <p>In this case when the user clicks on a title, there is no details
Ben Dodson542f2402011-06-14 16:40:23 -0700266 * container in the current activity, so the titles fragment's click code will
Dianne Hackborn291905e2010-08-17 15:17:15 -0700267 * launch a new activity to display the details fragment:</p>
268 *
269 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
270 * details_activity}
271 *
272 * <p>However the screen may be large enough to show both the list of titles
273 * and details about the currently selected title. To use such a layout on
274 * a landscape screen, this alternative layout can be placed under layout-land:</p>
275 *
276 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout}
277 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800278 * <p>Note how the prior code will adjust to this alternative UI flow: the titles
279 * fragment will now embed the details fragment inside of this activity, and the
280 * details activity will finish itself if it is running in a configuration
281 * where the details can be shown in-place.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700282 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700283 * <p>When a configuration change causes the activity hosting these fragments
284 * to restart, its new instance may use a different layout that doesn't
285 * include the same fragments as the previous layout. In this case all of
286 * the previous fragments will still be instantiated and running in the new
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800287 * instance. However, any that are no longer associated with a &lt;fragment&gt;
288 * tag in the view hierarchy will not have their content view created
289 * and will return false from {@link #isInLayout}. (The code here also shows
290 * how you can determine if a fragment placed in a container is no longer
291 * running in a layout with that container and avoid creating its view hierarchy
292 * in that case.)
Dianne Hackborn625ac272010-09-17 18:29:22 -0700293 *
294 * <p>The attributes of the &lt;fragment&gt; tag are used to control the
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800295 * LayoutParams provided when attaching the fragment's view to the parent
296 * container. They can also be parsed by the fragment in {@link #onInflate}
Dianne Hackborn625ac272010-09-17 18:29:22 -0700297 * as parameters.
298 *
299 * <p>The fragment being instantiated must have some kind of unique identifier
300 * so that it can be re-associated with a previous instance if the parent
301 * activity needs to be destroyed and recreated. This can be provided these
302 * ways:
303 *
304 * <ul>
305 * <li>If nothing is explicitly supplied, the view ID of the container will
306 * be used.
307 * <li><code>android:tag</code> can be used in &lt;fragment&gt; to provide
308 * a specific tag name for the fragment.
309 * <li><code>android:id</code> can be used in &lt;fragment&gt; to provide
310 * a specific identifier for the fragment.
311 * </ul>
312 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700313 * <a name="BackStack"></a>
314 * <h3>Back Stack</h3>
315 *
316 * <p>The transaction in which fragments are modified can be placed on an
317 * internal back-stack of the owning activity. When the user presses back
318 * in the activity, any transactions on the back stack are popped off before
319 * the activity itself is finished.
320 *
321 * <p>For example, consider this simple fragment that is instantiated with
322 * an integer argument and displays that in a TextView in its UI:</p>
323 *
324 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
325 * fragment}
326 *
327 * <p>A function that creates a new instance of the fragment, replacing
328 * whatever current fragment instance is being shown and pushing that change
329 * on to the back stack could be written as:
330 *
331 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
332 * add_stack}
333 *
334 * <p>After each call to this function, a new entry is on the stack, and
335 * pressing back will pop it to return the user to whatever previous state
336 * the activity UI was in.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700337 */
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700338public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener {
Dianne Hackbornc8017682010-07-06 13:34:38 -0700339 private static final HashMap<String, Class<?>> sClassMap =
340 new HashMap<String, Class<?>>();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700341
Adam Powell635c60a2011-10-26 10:22:16 -0700342 static final int INVALID_STATE = -1; // Invalid state used as a null value.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700343 static final int INITIALIZING = 0; // Not yet created.
344 static final int CREATED = 1; // Created.
345 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700346 static final int STOPPED = 3; // Fully created, not started.
347 static final int STARTED = 4; // Created and started, not resumed.
348 static final int RESUMED = 5; // Created started and resumed.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700349
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700350 int mState = INITIALIZING;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700351
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800352 // Non-null if the fragment's view hierarchy is currently animating away,
353 // meaning we need to wait a bit on completely destroying it. This is the
354 // animation that is running.
355 Animator mAnimatingAway;
356
357 // If mAnimatingAway != null, this is the state we should move to once the
358 // animation is done.
359 int mStateAfterAnimating;
360
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700361 // When instantiated from saved state, this is the saved state.
362 Bundle mSavedFragmentState;
363 SparseArray<Parcelable> mSavedViewState;
364
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700365 // Index into active fragment array.
366 int mIndex = -1;
367
368 // Internal unique name for this fragment;
369 String mWho;
370
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700371 // Construction arguments;
372 Bundle mArguments;
373
Dianne Hackborndef15372010-08-15 12:43:52 -0700374 // Target fragment.
375 Fragment mTarget;
376
Dianne Hackbornf9302322011-06-14 18:36:14 -0700377 // For use when retaining a fragment: this is the index of the last mTarget.
378 int mTargetIndex = -1;
379
Dianne Hackborndef15372010-08-15 12:43:52 -0700380 // Target request code.
381 int mTargetRequestCode;
382
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700383 // True if the fragment is in the list of added fragments.
384 boolean mAdded;
385
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800386 // If set this fragment is being removed from its activity.
387 boolean mRemoving;
388
Dianne Hackborn2707d602010-07-09 18:01:20 -0700389 // True if the fragment is in the resumed state.
390 boolean mResumed;
391
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700392 // Set to true if this fragment was instantiated from a layout file.
393 boolean mFromLayout;
394
Dianne Hackborn625ac272010-09-17 18:29:22 -0700395 // Set to true when the view has actually been inflated in its layout.
396 boolean mInLayout;
397
Dianne Hackborn352cc982011-01-04 11:34:18 -0800398 // True if this fragment has been restored from previously saved state.
399 boolean mRestored;
400
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700401 // Number of active back stack entries this fragment is in.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700402 int mBackStackNesting;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700403
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700404 // The fragment manager we are associated with. Set as soon as the
405 // fragment is used in a transaction; cleared after it has been removed
406 // from all transactions.
Adam Powell635c60a2011-10-26 10:22:16 -0700407 FragmentManagerImpl mFragmentManager;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700408
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700409 // Activity this fragment is attached to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700410 Activity mActivity;
411
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700412 // The optional identifier for this fragment -- either the container ID if it
413 // was dynamically added to the view hierarchy, or the ID supplied in
414 // layout.
415 int mFragmentId;
416
417 // When a fragment is being dynamically added to the view hierarchy, this
418 // is the identifier of the parent container it is being added to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700419 int mContainerId;
420
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700421 // The optional named tag for this fragment -- usually used to find
422 // fragments that are not part of the layout.
423 String mTag;
424
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700425 // Set to true when the app has requested that this fragment be hidden
426 // from the user.
427 boolean mHidden;
428
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700429 // Set to true when the app has requested that this fragment be detached.
430 boolean mDetached;
431
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700432 // If set this fragment would like its instance retained across
433 // configuration changes.
434 boolean mRetainInstance;
435
436 // If set this fragment is being retained across the current config change.
437 boolean mRetaining;
438
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700439 // If set this fragment has menu items to contribute.
440 boolean mHasMenu;
Dianne Hackborn6c285972011-08-29 16:53:49 -0700441
442 // Set to true to allow the fragment's menu to be shown.
443 boolean mMenuVisible = true;
444
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700445 // Used to verify that subclasses call through to super class.
446 boolean mCalled;
447
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700448 // If app has requested a specific animation, this is the one to use.
449 int mNextAnim;
450
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700451 // The parent container of the fragment after dynamically added to UI.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700452 ViewGroup mContainer;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700453
454 // The View generated for this fragment.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700455 View mView;
456
Adam Powell635c60a2011-10-26 10:22:16 -0700457 // Whether this fragment should defer starting until after other fragments
458 // have been started and their loaders are finished.
459 boolean mDeferStart;
460
Adam Powell78fed9b2011-11-07 10:45:34 -0800461 // Hint provided by the app that this fragment is currently visible to the user.
462 boolean mUserVisibleHint = true;
463
Dianne Hackborn4911b782010-07-15 12:54:39 -0700464 LoaderManagerImpl mLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700465 boolean mLoadersStarted;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700466 boolean mCheckedForLoaderManager;
Dianne Hackbornc8017682010-07-06 13:34:38 -0700467
468 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700469 * State information that has been retrieved from a fragment instance
470 * through {@link FragmentManager#saveFragmentInstanceState(Fragment)
471 * FragmentManager.saveFragmentInstanceState}.
472 */
473 public static class SavedState implements Parcelable {
474 final Bundle mState;
475
476 SavedState(Bundle state) {
477 mState = state;
478 }
479
480 SavedState(Parcel in, ClassLoader loader) {
481 mState = in.readBundle();
482 if (loader != null && mState != null) {
483 mState.setClassLoader(loader);
484 }
485 }
486
487 @Override
488 public int describeContents() {
489 return 0;
490 }
491
492 @Override
493 public void writeToParcel(Parcel dest, int flags) {
494 dest.writeBundle(mState);
495 }
496
497 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR
498 = new Parcelable.ClassLoaderCreator<SavedState>() {
499 public SavedState createFromParcel(Parcel in) {
500 return new SavedState(in, null);
501 }
502
503 public SavedState createFromParcel(Parcel in, ClassLoader loader) {
504 return new SavedState(in, loader);
505 }
506
507 public SavedState[] newArray(int size) {
508 return new SavedState[size];
509 }
510 };
511 }
512
513 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700514 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when
515 * there is an instantiation failure.
516 */
517 static public class InstantiationException extends AndroidRuntimeException {
518 public InstantiationException(String msg, Exception cause) {
519 super(msg, cause);
520 }
521 }
522
523 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700524 * Default constructor. <strong>Every</strong> fragment must have an
Dianne Hackbornc8017682010-07-06 13:34:38 -0700525 * empty constructor, so it can be instantiated when restoring its
526 * activity's state. It is strongly recommended that subclasses do not
527 * have other constructors with parameters, since these constructors
528 * will not be called when the fragment is re-instantiated; instead,
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700529 * arguments can be supplied by the caller with {@link #setArguments}
530 * and later retrieved by the Fragment with {@link #getArguments}.
531 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700532 * <p>Applications should generally not implement a constructor. The
533 * first place application code an run where the fragment is ready to
534 * be used is in {@link #onAttach(Activity)}, the point where the fragment
535 * is actually associated with its activity. Some applications may also
536 * want to implement {@link #onInflate} to retrieve attributes from a
537 * layout resource, though should take care here because this happens for
538 * the fragment is attached to its activity.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700539 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700540 public Fragment() {
541 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700542
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700543 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700544 * Like {@link #instantiate(Context, String, Bundle)} but with a null
545 * argument Bundle.
546 */
547 public static Fragment instantiate(Context context, String fname) {
548 return instantiate(context, fname, null);
549 }
550
551 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700552 * Create a new instance of a Fragment with the given class name. This is
553 * the same as calling its empty constructor.
554 *
555 * @param context The calling context being used to instantiate the fragment.
556 * This is currently just used to get its ClassLoader.
557 * @param fname The class name of the fragment to instantiate.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700558 * @param args Bundle of arguments to supply to the fragment, which it
559 * can retrieve with {@link #getArguments()}. May be null.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700560 * @return Returns a new fragment instance.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700561 * @throws InstantiationException If there is a failure in instantiating
562 * the given fragment class. This is a runtime exception; it is not
563 * normally expected to happen.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700564 */
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700565 public static Fragment instantiate(Context context, String fname, Bundle args) {
566 try {
567 Class<?> clazz = sClassMap.get(fname);
568 if (clazz == null) {
569 // Class not found in the cache, see if it's real, and try to add it
570 clazz = context.getClassLoader().loadClass(fname);
571 sClassMap.put(fname, clazz);
572 }
573 Fragment f = (Fragment)clazz.newInstance();
574 if (args != null) {
575 args.setClassLoader(f.getClass().getClassLoader());
576 f.mArguments = args;
577 }
578 return f;
579 } catch (ClassNotFoundException e) {
580 throw new InstantiationException("Unable to instantiate fragment " + fname
581 + ": make sure class name exists, is public, and has an"
582 + " empty constructor that is public", e);
583 } catch (java.lang.InstantiationException e) {
584 throw new InstantiationException("Unable to instantiate fragment " + fname
585 + ": make sure class name exists, is public, and has an"
586 + " empty constructor that is public", e);
587 } catch (IllegalAccessException e) {
588 throw new InstantiationException("Unable to instantiate fragment " + fname
589 + ": make sure class name exists, is public, and has an"
590 + " empty constructor that is public", e);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700591 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700592 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700593
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700594 final void restoreViewState() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700595 if (mSavedViewState != null) {
596 mView.restoreHierarchyState(mSavedViewState);
597 mSavedViewState = null;
598 }
599 }
600
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700601 final void setIndex(int index) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700602 mIndex = index;
603 mWho = "android:fragment:" + mIndex;
604 }
605
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700606 final boolean isInBackStack() {
607 return mBackStackNesting > 0;
608 }
609
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700610 /**
611 * Subclasses can not override equals().
612 */
613 @Override final public boolean equals(Object o) {
614 return super.equals(o);
615 }
616
617 /**
618 * Subclasses can not override hashCode().
619 */
620 @Override final public int hashCode() {
621 return super.hashCode();
622 }
623
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700624 @Override
625 public String toString() {
626 StringBuilder sb = new StringBuilder(128);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800627 DebugUtils.buildShortClassTag(this, sb);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700628 if (mIndex >= 0) {
629 sb.append(" #");
630 sb.append(mIndex);
631 }
632 if (mFragmentId != 0) {
633 sb.append(" id=0x");
634 sb.append(Integer.toHexString(mFragmentId));
635 }
636 if (mTag != null) {
637 sb.append(" ");
638 sb.append(mTag);
639 }
640 sb.append('}');
641 return sb.toString();
642 }
643
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700644 /**
645 * Return the identifier this fragment is known by. This is either
646 * the android:id value supplied in a layout or the container view ID
647 * supplied when adding the fragment.
648 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700649 final public int getId() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700650 return mFragmentId;
651 }
652
653 /**
654 * Get the tag name of the fragment, if specified.
655 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700656 final public String getTag() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700657 return mTag;
658 }
659
660 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700661 * Supply the construction arguments for this fragment. This can only
662 * be called before the fragment has been attached to its activity; that
663 * is, you should call it immediately after constructing the fragment. The
664 * arguments supplied here will be retained across fragment destroy and
665 * creation.
666 */
Dianne Hackborndef15372010-08-15 12:43:52 -0700667 public void setArguments(Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700668 if (mIndex >= 0) {
669 throw new IllegalStateException("Fragment already active");
670 }
671 mArguments = args;
672 }
673
674 /**
675 * Return the arguments supplied when the fragment was instantiated,
676 * if any.
677 */
678 final public Bundle getArguments() {
679 return mArguments;
680 }
681
682 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700683 * Set the initial saved state that this Fragment should restore itself
684 * from when first being constructed, as returned by
685 * {@link FragmentManager#saveFragmentInstanceState(Fragment)
686 * FragmentManager.saveFragmentInstanceState}.
687 *
688 * @param state The state the fragment should be restored from.
689 */
690 public void setInitialSavedState(SavedState state) {
691 if (mIndex >= 0) {
692 throw new IllegalStateException("Fragment already active");
693 }
694 mSavedFragmentState = state != null && state.mState != null
695 ? state.mState : null;
696 }
697
698 /**
Dianne Hackborndef15372010-08-15 12:43:52 -0700699 * Optional target for this fragment. This may be used, for example,
700 * if this fragment is being started by another, and when done wants to
701 * give a result back to the first. The target set here is retained
702 * across instances via {@link FragmentManager#putFragment
703 * FragmentManager.putFragment()}.
704 *
705 * @param fragment The fragment that is the target of this one.
706 * @param requestCode Optional request code, for convenience if you
707 * are going to call back with {@link #onActivityResult(int, int, Intent)}.
708 */
709 public void setTargetFragment(Fragment fragment, int requestCode) {
710 mTarget = fragment;
711 mTargetRequestCode = requestCode;
712 }
713
714 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700715 * Return the target fragment set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700716 */
717 final public Fragment getTargetFragment() {
718 return mTarget;
719 }
720
721 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700722 * Return the target request code set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700723 */
724 final public int getTargetRequestCode() {
725 return mTargetRequestCode;
726 }
727
728 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700729 * Return the Activity this fragment is currently associated with.
730 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700731 final public Activity getActivity() {
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700732 return mActivity;
733 }
734
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700735 /**
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800736 * Return <code>getActivity().getResources()</code>.
737 */
738 final public Resources getResources() {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800739 if (mActivity == null) {
740 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
741 }
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800742 return mActivity.getResources();
743 }
744
745 /**
746 * Return a localized, styled CharSequence from the application's package's
747 * default string table.
748 *
749 * @param resId Resource id for the CharSequence text
750 */
751 public final CharSequence getText(int resId) {
752 return getResources().getText(resId);
753 }
754
755 /**
756 * Return a localized string from the application's package's
757 * default string table.
758 *
759 * @param resId Resource id for the string
760 */
761 public final String getString(int resId) {
762 return getResources().getString(resId);
763 }
764
765 /**
766 * Return a localized formatted string from the application's package's
767 * default string table, substituting the format arguments as defined in
768 * {@link java.util.Formatter} and {@link java.lang.String#format}.
769 *
770 * @param resId Resource id for the format string
771 * @param formatArgs The format arguments that will be used for substitution.
772 */
773
774 public final String getString(int resId, Object... formatArgs) {
775 return getResources().getString(resId, formatArgs);
776 }
777
778 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700779 * Return the FragmentManager for interacting with fragments associated
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700780 * with this fragment's activity. Note that this will be non-null slightly
Dianne Hackborn625ac272010-09-17 18:29:22 -0700781 * before {@link #getActivity()}, during the time from when the fragment is
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700782 * placed in a {@link FragmentTransaction} until it is committed and
783 * attached to its activity.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700784 */
785 final public FragmentManager getFragmentManager() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700786 return mFragmentManager;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700787 }
788
789 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700790 * Return true if the fragment is currently added to its activity.
791 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700792 final public boolean isAdded() {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800793 return mActivity != null && mAdded;
794 }
795
796 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700797 * Return true if the fragment has been explicitly detached from the UI.
798 * That is, {@link FragmentTransaction#detach(Fragment)
799 * FragmentTransaction.detach(Fragment)} has been used on it.
800 */
801 final public boolean isDetached() {
802 return mDetached;
803 }
804
805 /**
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800806 * Return true if this fragment is currently being removed from its
807 * activity. This is <em>not</em> whether its activity is finishing, but
808 * rather whether it is in the process of being removed from its activity.
809 */
810 final public boolean isRemoving() {
811 return mRemoving;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700812 }
813
814 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700815 * Return true if the layout is included as part of an activity view
816 * hierarchy via the &lt;fragment&gt; tag. This will always be true when
817 * fragments are created through the &lt;fragment&gt; tag, <em>except</em>
818 * in the case where an old fragment is restored from a previous state and
819 * it does not appear in the layout of the current state.
820 */
821 final public boolean isInLayout() {
822 return mInLayout;
823 }
824
825 /**
Dianne Hackborn2707d602010-07-09 18:01:20 -0700826 * Return true if the fragment is in the resumed state. This is true
827 * for the duration of {@link #onResume()} and {@link #onPause()} as well.
828 */
829 final public boolean isResumed() {
830 return mResumed;
831 }
832
833 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700834 * Return true if the fragment is currently visible to the user. This means
835 * it: (1) has been added, (2) has its view attached to the window, and
836 * (3) is not hidden.
837 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700838 final public boolean isVisible() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700839 return isAdded() && !isHidden() && mView != null
840 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
841 }
842
843 /**
844 * Return true if the fragment has been hidden. By default fragments
845 * are shown. You can find out about changes to this state with
Dianne Hackborncddfa6d2010-05-19 22:56:37 -0700846 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700847 * to other states -- that is, to be visible to the user, a fragment
848 * must be both started and not hidden.
849 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700850 final public boolean isHidden() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700851 return mHidden;
852 }
853
854 /**
855 * Called when the hidden state (as returned by {@link #isHidden()} of
856 * the fragment has changed. Fragments start out not hidden; this will
857 * be called whenever the fragment changes state from that.
858 * @param hidden True if the fragment is now hidden, false if it is not
859 * visible.
860 */
861 public void onHiddenChanged(boolean hidden) {
862 }
863
864 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700865 * Control whether a fragment instance is retained across Activity
866 * re-creation (such as from a configuration change). This can only
867 * be used with fragments not in the back stack. If set, the fragment
868 * lifecycle will be slightly different when an activity is recreated:
869 * <ul>
870 * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still
871 * will be, because the fragment is being detached from its current activity).
872 * <li> {@link #onCreate(Bundle)} will not be called since the fragment
873 * is not being re-created.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700874 * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b>
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700875 * still be called.
876 * </ul>
877 */
878 public void setRetainInstance(boolean retain) {
879 mRetainInstance = retain;
880 }
881
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700882 final public boolean getRetainInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700883 return mRetainInstance;
884 }
885
886 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700887 * Report that this fragment would like to participate in populating
Wink Saville4dc643e2010-06-12 22:16:41 -0700888 * the options menu by receiving a call to {@link #onCreateOptionsMenu}
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700889 * and related methods.
890 *
891 * @param hasMenu If true, the fragment has menu items to contribute.
892 */
893 public void setHasOptionsMenu(boolean hasMenu) {
894 if (mHasMenu != hasMenu) {
895 mHasMenu = hasMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -0700896 if (isAdded() && !isHidden()) {
897 mFragmentManager.invalidateOptionsMenu();
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700898 }
899 }
900 }
Dianne Hackborn6c285972011-08-29 16:53:49 -0700901
902 /**
903 * Set a hint for whether this fragment's menu should be visible. This
904 * is useful if you know that a fragment has been placed in your view
905 * hierarchy so that the user can not currently seen it, so any menu items
906 * it has should also not be shown.
907 *
908 * @param menuVisible The default is true, meaning the fragment's menu will
909 * be shown as usual. If false, the user will not see the menu.
910 */
911 public void setMenuVisibility(boolean menuVisible) {
912 if (mMenuVisible != menuVisible) {
913 mMenuVisible = menuVisible;
914 if (mHasMenu && isAdded() && !isHidden()) {
915 mFragmentManager.invalidateOptionsMenu();
916 }
917 }
918 }
919
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700920 /**
Adam Powell78fed9b2011-11-07 10:45:34 -0800921 * Set a hint to the system about whether this fragment's UI is currently visible
922 * to the user. This hint defaults to true and is persistent across fragment instance
923 * state save and restore.
Adam Powell635c60a2011-10-26 10:22:16 -0700924 *
Adam Powell78fed9b2011-11-07 10:45:34 -0800925 * <p>An app may set this to false to indicate that the fragment's UI is
926 * scrolled out of visibility or is otherwise not directly visible to the user.
927 * This may be used by the system to prioritize operations such as fragment lifecycle updates
928 * or loader ordering behavior.</p>
Adam Powell635c60a2011-10-26 10:22:16 -0700929 *
Adam Powell78fed9b2011-11-07 10:45:34 -0800930 * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
931 * false if it is not.
Adam Powell635c60a2011-10-26 10:22:16 -0700932 */
Adam Powell78fed9b2011-11-07 10:45:34 -0800933 public void setUserVisibleHint(boolean isVisibleToUser) {
934 if (!mUserVisibleHint && isVisibleToUser && mState < STARTED) {
Adam Powell635c60a2011-10-26 10:22:16 -0700935 mFragmentManager.performPendingDeferredStart(this);
936 }
Adam Powell78fed9b2011-11-07 10:45:34 -0800937 mUserVisibleHint = isVisibleToUser;
938 mDeferStart = !isVisibleToUser;
Adam Powell635c60a2011-10-26 10:22:16 -0700939 }
940
941 /**
Adam Powell78fed9b2011-11-07 10:45:34 -0800942 * @return The current value of the user-visible hint on this fragment.
943 * @see #setUserVisibleHint(boolean)
Adam Powell635c60a2011-10-26 10:22:16 -0700944 */
Adam Powell78fed9b2011-11-07 10:45:34 -0800945 public boolean getUserVisibleHint() {
946 return mUserVisibleHint;
Adam Powell635c60a2011-10-26 10:22:16 -0700947 }
948
949 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -0700950 * Return the LoaderManager for this fragment, creating it if needed.
951 */
952 public LoaderManager getLoaderManager() {
953 if (mLoaderManager != null) {
954 return mLoaderManager;
955 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800956 if (mActivity == null) {
957 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
958 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700959 mCheckedForLoaderManager = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700960 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700961 return mLoaderManager;
962 }
963
964 /**
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700965 * Call {@link Activity#startActivity(Intent)} on the fragment's
966 * containing Activity.
967 */
968 public void startActivity(Intent intent) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800969 if (mActivity == null) {
970 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
971 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700972 mActivity.startActivityFromFragment(this, intent, -1);
973 }
974
975 /**
976 * Call {@link Activity#startActivityForResult(Intent, int)} on the fragment's
977 * containing Activity.
978 */
979 public void startActivityForResult(Intent intent, int requestCode) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800980 if (mActivity == null) {
981 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
982 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700983 mActivity.startActivityFromFragment(this, intent, requestCode);
984 }
985
986 /**
987 * Receive the result from a previous call to
988 * {@link #startActivityForResult(Intent, int)}. This follows the
989 * related Activity API as described there in
990 * {@link Activity#onActivityResult(int, int, Intent)}.
991 *
992 * @param requestCode The integer request code originally supplied to
993 * startActivityForResult(), allowing you to identify who this
994 * result came from.
995 * @param resultCode The integer result code returned by the child activity
996 * through its setResult().
997 * @param data An Intent, which can return result data to the caller
998 * (various data can be attached to Intent "extras").
999 */
1000 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1001 }
1002
1003 /**
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001004 * @hide Hack so that DialogFragment can make its Dialog before creating
1005 * its views, and the view construction can use the dialog's context for
1006 * inflation. Maybe this should become a public API. Note sure.
1007 */
1008 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
1009 return mActivity.getLayoutInflater();
1010 }
1011
1012 /**
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001013 * @deprecated Use {@link #onInflate(Activity, AttributeSet, Bundle)} instead.
1014 */
1015 @Deprecated
1016 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
1017 mCalled = true;
1018 }
1019
1020 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001021 * Called when a fragment is being created as part of a view layout
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001022 * inflation, typically from setting the content view of an activity. This
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001023 * may be called immediately after the fragment is created from a <fragment>
Dianne Hackborndef15372010-08-15 12:43:52 -07001024 * tag in a layout file. Note this is <em>before</em> the fragment's
1025 * {@link #onAttach(Activity)} has been called; all you should do here is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001026 * parse the attributes and save them away.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001027 *
Dianne Hackborndef15372010-08-15 12:43:52 -07001028 * <p>This is called every time the fragment is inflated, even if it is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001029 * being inflated into a new instance with saved state. It typically makes
1030 * sense to re-parse the parameters each time, to allow them to change with
1031 * different configurations.</p>
1032 *
1033 * <p>Here is a typical implementation of a fragment that can take parameters
1034 * both through attributes supplied here as well from {@link #getArguments()}:</p>
1035 *
1036 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1037 * fragment}
1038 *
1039 * <p>Note that parsing the XML attributes uses a "styleable" resource. The
1040 * declaration for the styleable used here is:</p>
1041 *
1042 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments}
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001043 *
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001044 * <p>The fragment can then be declared within its activity's content layout
1045 * through a tag like this:</p>
1046 *
1047 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes}
1048 *
1049 * <p>This fragment can also be created dynamically from arguments given
1050 * at runtime in the arguments Bundle; here is an example of doing so at
1051 * creation of the containing activity:</p>
1052 *
1053 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1054 * create}
1055 *
1056 * @param activity The Activity that is inflating this fragment.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001057 * @param attrs The attributes at the tag where the fragment is
1058 * being created.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001059 * @param savedInstanceState If the fragment is being re-created from
1060 * a previous saved state, this is the state.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001061 */
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001062 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
1063 onInflate(attrs, savedInstanceState);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001064 mCalled = true;
1065 }
1066
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001067 /**
1068 * Called when a fragment is first attached to its activity.
1069 * {@link #onCreate(Bundle)} will be called after this.
1070 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001071 public void onAttach(Activity activity) {
1072 mCalled = true;
1073 }
1074
Chet Haase811ed1062010-08-06 10:38:15 -07001075 /**
1076 * Called when a fragment loads an animation.
1077 */
Chet Haasea18a86b2010-09-07 13:20:00 -07001078 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001079 return null;
1080 }
1081
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001082 /**
1083 * Called to do initial creation of a fragment. This is called after
Dianne Hackbornc8017682010-07-06 13:34:38 -07001084 * {@link #onAttach(Activity)} and before
1085 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1086 *
1087 * <p>Note that this can be called while the fragment's activity is
1088 * still in the process of being created. As such, you can not rely
1089 * on things like the activity's content view hierarchy being initialized
1090 * at this point. If you want to do work once the activity itself is
1091 * created, see {@link #onActivityCreated(Bundle)}.
1092 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001093 * @param savedInstanceState If the fragment is being re-created from
1094 * a previous saved state, this is the state.
1095 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001096 public void onCreate(Bundle savedInstanceState) {
1097 mCalled = true;
1098 }
1099
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001100 /**
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001101 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
1102 * has returned, but before any saved state has been restored in to the view.
1103 * This gives subclasses a chance to initialize themselves once
1104 * they know their view hierarchy has been completely created. The fragment's
1105 * view hierarchy is not however attached to its parent at this point.
1106 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1107 * @param savedInstanceState If non-null, this fragment is being re-constructed
1108 * from a previous saved state as given here.
1109 */
1110 public void onViewCreated(View view, Bundle savedInstanceState) {
1111 }
1112
1113 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001114 * Called to have the fragment instantiate its user interface view.
1115 * This is optional, and non-graphical fragments can return null (which
1116 * is the default implementation). This will be called between
Dianne Hackbornc8017682010-07-06 13:34:38 -07001117 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001118 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001119 * <p>If you return a View from here, you will later be called in
1120 * {@link #onDestroyView} when the view is being released.
1121 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001122 * @param inflater The LayoutInflater object that can be used to inflate
1123 * any views in the fragment,
1124 * @param container If non-null, this is the parent view that the fragment's
1125 * UI should be attached to. The fragment should not add the view itself,
1126 * but this can be used to generate the LayoutParams of the view.
1127 * @param savedInstanceState If non-null, this fragment is being re-constructed
1128 * from a previous saved state as given here.
1129 *
1130 * @return Return the View for the fragment's UI, or null.
1131 */
1132 public View onCreateView(LayoutInflater inflater, ViewGroup container,
1133 Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001134 return null;
1135 }
1136
Scott Main662cc7a42011-01-17 15:02:07 -08001137 /**
1138 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
1139 * if provided.
1140 *
1141 * @return The fragment's root view, or null if it has no layout.
1142 */
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001143 public View getView() {
1144 return mView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001145 }
1146
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001147 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001148 * Called when the fragment's activity has been created and this
1149 * fragment's view hierarchy instantiated. It can be used to do final
1150 * initialization once these pieces are in place, such as retrieving
1151 * views or restoring state. It is also useful for fragments that use
1152 * {@link #setRetainInstance(boolean)} to retain their instance,
1153 * as this callback tells the fragment when it is fully associated with
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001154 * the new activity instance. This is called after {@link #onCreateView}
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001155 * and before {@link #onStart()}.
1156 *
1157 * @param savedInstanceState If the fragment is being re-created from
1158 * a previous saved state, this is the state.
1159 */
Dianne Hackbornc8017682010-07-06 13:34:38 -07001160 public void onActivityCreated(Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001161 mCalled = true;
1162 }
1163
1164 /**
1165 * Called when the Fragment is visible to the user. This is generally
1166 * tied to {@link Activity#onStart() Activity.onStart} of the containing
1167 * Activity's lifecycle.
1168 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001169 public void onStart() {
1170 mCalled = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001171
1172 if (!mLoadersStarted) {
1173 mLoadersStarted = true;
1174 if (!mCheckedForLoaderManager) {
1175 mCheckedForLoaderManager = true;
1176 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
1177 }
1178 if (mLoaderManager != null) {
1179 mLoaderManager.doStart();
1180 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001181 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001182 }
1183
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001184 /**
1185 * Called when the fragment is visible to the user and actively running.
1186 * This is generally
1187 * tied to {@link Activity#onResume() Activity.onResume} of the containing
1188 * Activity's lifecycle.
1189 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001190 public void onResume() {
1191 mCalled = true;
1192 }
1193
Dianne Hackborn72778202010-08-20 18:26:01 -07001194 /**
1195 * Called to ask the fragment to save its current dynamic state, so it
1196 * can later be reconstructed in a new instance of its process is
1197 * restarted. If a new instance of the fragment later needs to be
1198 * created, the data you place in the Bundle here will be available
1199 * in the Bundle given to {@link #onCreate(Bundle)},
1200 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
1201 * {@link #onActivityCreated(Bundle)}.
1202 *
1203 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
Daisuke Miyakawa2f761762010-09-12 16:53:17 -07001204 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
Dianne Hackborn72778202010-08-20 18:26:01 -07001205 * applies here as well. Note however: <em>this method may be called
1206 * at any time before {@link #onDestroy()}</em>. There are many situations
1207 * where a fragment may be mostly torn down (such as when placed on the
1208 * back stack with no UI showing), but its state will not be saved until
1209 * its owning activity actually needs to save its state.
1210 *
1211 * @param outState Bundle in which to place your saved state.
1212 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001213 public void onSaveInstanceState(Bundle outState) {
1214 }
1215
1216 public void onConfigurationChanged(Configuration newConfig) {
1217 mCalled = true;
1218 }
1219
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001220 /**
1221 * Called when the Fragment is no longer resumed. This is generally
1222 * tied to {@link Activity#onPause() Activity.onPause} of the containing
1223 * Activity's lifecycle.
1224 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001225 public void onPause() {
1226 mCalled = true;
1227 }
1228
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001229 /**
1230 * Called when the Fragment is no longer started. This is generally
1231 * tied to {@link Activity#onStop() Activity.onStop} of the containing
1232 * Activity's lifecycle.
1233 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001234 public void onStop() {
1235 mCalled = true;
1236 }
1237
1238 public void onLowMemory() {
1239 mCalled = true;
1240 }
1241
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001242 public void onTrimMemory(int level) {
1243 mCalled = true;
1244 }
1245
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001246 /**
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001247 * Called when the view previously created by {@link #onCreateView} has
1248 * been detached from the fragment. The next time the fragment needs
1249 * to be displayed, a new view will be created. This is called
Dianne Hackborndef15372010-08-15 12:43:52 -07001250 * after {@link #onStop()} and before {@link #onDestroy()}. It is called
1251 * <em>regardless</em> of whether {@link #onCreateView} returned a
1252 * non-null view. Internally it is called after the view's state has
1253 * been saved but before it has been removed from its parent.
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001254 */
1255 public void onDestroyView() {
1256 mCalled = true;
1257 }
1258
1259 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001260 * Called when the fragment is no longer in use. This is called
1261 * after {@link #onStop()} and before {@link #onDetach()}.
1262 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001263 public void onDestroy() {
1264 mCalled = true;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001265 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager
1266 // + " mLoaderManager=" + mLoaderManager);
1267 if (!mCheckedForLoaderManager) {
1268 mCheckedForLoaderManager = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001269 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001270 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001271 if (mLoaderManager != null) {
1272 mLoaderManager.doDestroy();
1273 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001274 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001275
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001276 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001277 * Called by the fragment manager once this fragment has been removed,
1278 * so that we don't have any left-over state if the application decides
1279 * to re-use the instance. This only clears state that the framework
1280 * internally manages, not things the application sets.
1281 */
1282 void initState() {
1283 mIndex = -1;
1284 mWho = null;
1285 mAdded = false;
1286 mRemoving = false;
1287 mResumed = false;
1288 mFromLayout = false;
1289 mInLayout = false;
1290 mRestored = false;
1291 mBackStackNesting = 0;
1292 mFragmentManager = null;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001293 mActivity = null;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001294 mFragmentId = 0;
1295 mContainerId = 0;
1296 mTag = null;
1297 mHidden = false;
1298 mDetached = false;
1299 mRetaining = false;
1300 mLoaderManager = null;
1301 mLoadersStarted = false;
1302 mCheckedForLoaderManager = false;
1303 }
1304
1305 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001306 * Called when the fragment is no longer attached to its activity. This
1307 * is called after {@link #onDestroy()}.
1308 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001309 public void onDetach() {
1310 mCalled = true;
1311 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001312
1313 /**
1314 * Initialize the contents of the Activity's standard options menu. You
1315 * should place your menu items in to <var>menu</var>. For this method
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001316 * to be called, you must have first called {@link #setHasOptionsMenu}. See
1317 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
1318 * for more information.
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001319 *
1320 * @param menu The options menu in which you place your items.
1321 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001322 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001323 * @see #onPrepareOptionsMenu
1324 * @see #onOptionsItemSelected
1325 */
1326 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1327 }
1328
1329 /**
1330 * Prepare the Screen's standard options menu to be displayed. This is
1331 * called right before the menu is shown, every time it is shown. You can
1332 * use this method to efficiently enable/disable items or otherwise
1333 * dynamically modify the contents. See
1334 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu}
1335 * for more information.
1336 *
1337 * @param menu The options menu as last shown or first initialized by
1338 * onCreateOptionsMenu().
1339 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001340 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001341 * @see #onCreateOptionsMenu
1342 */
1343 public void onPrepareOptionsMenu(Menu menu) {
1344 }
1345
1346 /**
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001347 * Called when this fragment's option menu items are no longer being
1348 * included in the overall options menu. Receiving this call means that
1349 * the menu needed to be rebuilt, but this fragment's items were not
1350 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
1351 * was not called).
1352 */
1353 public void onDestroyOptionsMenu() {
1354 }
1355
1356 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001357 * This hook is called whenever an item in your options menu is selected.
1358 * The default implementation simply returns false to have the normal
1359 * processing happen (calling the item's Runnable or sending a message to
1360 * its Handler as appropriate). You can use this method for any items
1361 * for which you would like to do processing without those other
1362 * facilities.
1363 *
1364 * <p>Derived classes should call through to the base class for it to
1365 * perform the default menu handling.
1366 *
1367 * @param item The menu item that was selected.
1368 *
1369 * @return boolean Return false to allow normal menu processing to
1370 * proceed, true to consume it here.
1371 *
1372 * @see #onCreateOptionsMenu
1373 */
1374 public boolean onOptionsItemSelected(MenuItem item) {
1375 return false;
1376 }
1377
1378 /**
1379 * This hook is called whenever the options menu is being closed (either by the user canceling
1380 * the menu with the back/menu button, or when an item is selected).
1381 *
1382 * @param menu The options menu as last shown or first initialized by
1383 * onCreateOptionsMenu().
1384 */
1385 public void onOptionsMenuClosed(Menu menu) {
1386 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001387
1388 /**
1389 * Called when a context menu for the {@code view} is about to be shown.
1390 * Unlike {@link #onCreateOptionsMenu}, this will be called every
1391 * time the context menu is about to be shown and should be populated for
1392 * the view (or item inside the view for {@link AdapterView} subclasses,
1393 * this can be found in the {@code menuInfo})).
1394 * <p>
1395 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
1396 * item has been selected.
1397 * <p>
1398 * The default implementation calls up to
1399 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
1400 * you can not call this implementation if you don't want that behavior.
1401 * <p>
1402 * It is not safe to hold onto the context menu after this method returns.
1403 * {@inheritDoc}
1404 */
1405 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1406 getActivity().onCreateContextMenu(menu, v, menuInfo);
1407 }
1408
1409 /**
1410 * Registers a context menu to be shown for the given view (multiple views
1411 * can show the context menu). This method will set the
1412 * {@link OnCreateContextMenuListener} on the view to this fragment, so
1413 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
1414 * called when it is time to show the context menu.
1415 *
1416 * @see #unregisterForContextMenu(View)
1417 * @param view The view that should show a context menu.
1418 */
1419 public void registerForContextMenu(View view) {
1420 view.setOnCreateContextMenuListener(this);
1421 }
1422
1423 /**
1424 * Prevents a context menu to be shown for the given view. This method will
1425 * remove the {@link OnCreateContextMenuListener} on the view.
1426 *
1427 * @see #registerForContextMenu(View)
1428 * @param view The view that should stop showing a context menu.
1429 */
1430 public void unregisterForContextMenu(View view) {
1431 view.setOnCreateContextMenuListener(null);
1432 }
1433
1434 /**
1435 * This hook is called whenever an item in a context menu is selected. The
1436 * default implementation simply returns false to have the normal processing
1437 * happen (calling the item's Runnable or sending a message to its Handler
1438 * as appropriate). You can use this method for any items for which you
1439 * would like to do processing without those other facilities.
1440 * <p>
1441 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
1442 * View that added this menu item.
1443 * <p>
1444 * Derived classes should call through to the base class for it to perform
1445 * the default menu handling.
1446 *
1447 * @param item The context menu item that was selected.
1448 * @return boolean Return false to allow normal context menu processing to
1449 * proceed, true to consume it here.
1450 */
1451 public boolean onContextItemSelected(MenuItem item) {
1452 return false;
1453 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07001454
Dianne Hackborn625ac272010-09-17 18:29:22 -07001455 /**
1456 * Print the Fragments's state into the given stream.
1457 *
1458 * @param prefix Text to print at the front of each line.
1459 * @param fd The raw file descriptor that the dump is being sent to.
1460 * @param writer The PrintWriter to which you should dump your state. This will be
1461 * closed for you after you return.
1462 * @param args additional arguments to the dump request.
1463 */
1464 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08001465 writer.print(prefix); writer.print("mFragmentId=#");
1466 writer.print(Integer.toHexString(mFragmentId));
1467 writer.print(" mContainerId#=");
1468 writer.print(Integer.toHexString(mContainerId));
Dianne Hackborn625ac272010-09-17 18:29:22 -07001469 writer.print(" mTag="); writer.println(mTag);
1470 writer.print(prefix); writer.print("mState="); writer.print(mState);
1471 writer.print(" mIndex="); writer.print(mIndex);
1472 writer.print(" mWho="); writer.print(mWho);
1473 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting);
1474 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded);
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001475 writer.print(" mRemoving="); writer.print(mRemoving);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001476 writer.print(" mResumed="); writer.print(mResumed);
1477 writer.print(" mFromLayout="); writer.print(mFromLayout);
1478 writer.print(" mInLayout="); writer.println(mInLayout);
1479 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001480 writer.print(" mDetached="); writer.print(mDetached);
Dianne Hackborn6c285972011-08-29 16:53:49 -07001481 writer.print(" mMenuVisible="); writer.print(mMenuVisible);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001482 writer.print(" mHasMenu="); writer.println(mHasMenu);
Dianne Hackborn6c285972011-08-29 16:53:49 -07001483 writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance);
Adam Powell78fed9b2011-11-07 10:45:34 -08001484 writer.print(" mRetaining="); writer.print(mRetaining);
1485 writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001486 if (mFragmentManager != null) {
1487 writer.print(prefix); writer.print("mFragmentManager=");
1488 writer.println(mFragmentManager);
1489 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001490 if (mActivity != null) {
1491 writer.print(prefix); writer.print("mActivity=");
1492 writer.println(mActivity);
1493 }
1494 if (mArguments != null) {
1495 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments);
1496 }
1497 if (mSavedFragmentState != null) {
1498 writer.print(prefix); writer.print("mSavedFragmentState=");
1499 writer.println(mSavedFragmentState);
1500 }
1501 if (mSavedViewState != null) {
1502 writer.print(prefix); writer.print("mSavedViewState=");
1503 writer.println(mSavedViewState);
1504 }
1505 if (mTarget != null) {
1506 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget);
1507 writer.print(" mTargetRequestCode=");
1508 writer.println(mTargetRequestCode);
1509 }
1510 if (mNextAnim != 0) {
1511 writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim);
1512 }
1513 if (mContainer != null) {
1514 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer);
1515 }
1516 if (mView != null) {
1517 writer.print(prefix); writer.print("mView="); writer.println(mView);
1518 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001519 if (mAnimatingAway != null) {
1520 writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway);
1521 writer.print(prefix); writer.print("mStateAfterAnimating=");
1522 writer.println(mStateAfterAnimating);
1523 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001524 if (mLoaderManager != null) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08001525 writer.print(prefix); writer.println("Loader Manager:");
1526 mLoaderManager.dump(prefix + " ", fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001527 }
1528 }
1529
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001530 void performStart() {
1531 onStart();
1532 if (mLoaderManager != null) {
1533 mLoaderManager.doReportStart();
1534 }
1535 }
1536
Dianne Hackborn2707d602010-07-09 18:01:20 -07001537 void performStop() {
1538 onStop();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001539
1540 if (mLoadersStarted) {
1541 mLoadersStarted = false;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001542 if (!mCheckedForLoaderManager) {
1543 mCheckedForLoaderManager = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001544 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001545 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07001546 if (mLoaderManager != null) {
1547 if (mActivity == null || !mActivity.mChangingConfigurations) {
1548 mLoaderManager.doStop();
1549 } else {
1550 mLoaderManager.doRetain();
1551 }
1552 }
1553 }
1554 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001555
1556 void performDestroyView() {
1557 onDestroyView();
1558 if (mLoaderManager != null) {
1559 mLoaderManager.doReportNextStart();
1560 }
1561 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001562}