blob: f427e78db96ab116cd03edb2ebe349eabc964a33 [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
Dianne Hackborn7871bad2011-12-12 15:19:26 -0800184 * at is also available for use on older platforms through
185 * {@link android.support.v4.app.FragmentActivity}. See the blog post
Dianne Hackbornf9dd34f2011-04-19 18:44:03 -0700186 * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">
187 * Fragments For All</a> for more details.
188 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700189 * <a name="Lifecycle"></a>
190 * <h3>Lifecycle</h3>
191 *
192 * <p>Though a Fragment's lifecycle is tied to its owning activity, it has
193 * its own wrinkle on the standard activity lifecycle. It includes basic
194 * activity lifecycle methods such as {@link #onResume}, but also important
195 * are methods related to interactions with the activity and UI generation.
196 *
197 * <p>The core series of lifecycle methods that are called to bring a fragment
198 * up to resumed state (interacting with the user) are:
199 *
200 * <ol>
201 * <li> {@link #onAttach} called once the fragment is associated with its activity.
202 * <li> {@link #onCreate} called to do initial creation of the fragment.
203 * <li> {@link #onCreateView} creates and returns the view hierarchy associated
204 * with the fragment.
205 * <li> {@link #onActivityCreated} tells the fragment that its activity has
206 * completed its own {@link Activity#onCreate Activity.onCreaate}.
207 * <li> {@link #onStart} makes the fragment visible to the user (based on its
208 * containing activity being started).
209 * <li> {@link #onResume} makes the fragment interacting with the user (based on its
210 * containing activity being resumed).
211 * </ol>
212 *
213 * <p>As a fragment is no longer being used, it goes through a reverse
214 * series of callbacks:
215 *
216 * <ol>
217 * <li> {@link #onPause} fragment is no longer interacting with the user either
218 * because its activity is being paused or a fragment operation is modifying it
219 * in the activity.
220 * <li> {@link #onStop} fragment is no longer visible to the user either
221 * because its activity is being stopped or a fragment operation is modifying it
222 * in the activity.
223 * <li> {@link #onDestroyView} allows the fragment to clean up resources
224 * associated with its View.
225 * <li> {@link #onDestroy} called to do final cleanup of the fragment's state.
226 * <li> {@link #onDetach} called immediately prior to the fragment no longer
227 * being associated with its activity.
228 * </ol>
229 *
230 * <a name="Layout"></a>
231 * <h3>Layout</h3>
232 *
233 * <p>Fragments can be used as part of your application's layout, allowing
234 * you to better modularize your code and more easily adjust your user
235 * interface to the screen it is running on. As an example, we can look
236 * at a simple program consisting of a list of items, and display of the
237 * details of each item.</p>
238 *
239 * <p>An activity's layout XML can include <code>&lt;fragment&gt;</code> tags
240 * to embed fragment instances inside of the layout. For example, here is
Dianne Hackborn625ac272010-09-17 18:29:22 -0700241 * a simple layout that embeds one fragment:</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700242 *
243 * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout}
244 *
245 * <p>The layout is installed in the activity in the normal way:</p>
246 *
247 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
248 * main}
249 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800250 * <p>The titles fragment, showing a list of titles, is fairly simple, relying
Dianne Hackborn291905e2010-08-17 15:17:15 -0700251 * on {@link ListFragment} for most of its work. Note the implementation of
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800252 * clicking an item: depending on the current activity's layout, it can either
253 * create and display a new fragment to show the details in-place (more about
Ben Dodson542f2402011-06-14 16:40:23 -0700254 * this later), or start a new activity to show the details.</p>
Dianne Hackborn291905e2010-08-17 15:17:15 -0700255 *
256 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
257 * titles}
258 *
Ben Dodson542f2402011-06-14 16:40:23 -0700259 * <p>The details fragment showing the contents of a selected item just
Dianne Hackborn291905e2010-08-17 15:17:15 -0700260 * displays a string of text based on an index of a string array built in to
261 * the app:</p>
262 *
263 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
264 * details}
265 *
266 * <p>In this case when the user clicks on a title, there is no details
Ben Dodson542f2402011-06-14 16:40:23 -0700267 * container in the current activity, so the titles fragment's click code will
Dianne Hackborn291905e2010-08-17 15:17:15 -0700268 * launch a new activity to display the details fragment:</p>
269 *
270 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java
271 * details_activity}
272 *
273 * <p>However the screen may be large enough to show both the list of titles
274 * and details about the currently selected title. To use such a layout on
275 * a landscape screen, this alternative layout can be placed under layout-land:</p>
276 *
277 * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout}
278 *
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800279 * <p>Note how the prior code will adjust to this alternative UI flow: the titles
280 * fragment will now embed the details fragment inside of this activity, and the
281 * details activity will finish itself if it is running in a configuration
282 * where the details can be shown in-place.
Dianne Hackborn291905e2010-08-17 15:17:15 -0700283 *
Dianne Hackborn625ac272010-09-17 18:29:22 -0700284 * <p>When a configuration change causes the activity hosting these fragments
285 * to restart, its new instance may use a different layout that doesn't
286 * include the same fragments as the previous layout. In this case all of
287 * the previous fragments will still be instantiated and running in the new
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800288 * instance. However, any that are no longer associated with a &lt;fragment&gt;
289 * tag in the view hierarchy will not have their content view created
290 * and will return false from {@link #isInLayout}. (The code here also shows
291 * how you can determine if a fragment placed in a container is no longer
292 * running in a layout with that container and avoid creating its view hierarchy
293 * in that case.)
Dianne Hackborn625ac272010-09-17 18:29:22 -0700294 *
295 * <p>The attributes of the &lt;fragment&gt; tag are used to control the
Dianne Hackborn87121ac2011-01-04 17:26:23 -0800296 * LayoutParams provided when attaching the fragment's view to the parent
297 * container. They can also be parsed by the fragment in {@link #onInflate}
Dianne Hackborn625ac272010-09-17 18:29:22 -0700298 * as parameters.
299 *
300 * <p>The fragment being instantiated must have some kind of unique identifier
301 * so that it can be re-associated with a previous instance if the parent
302 * activity needs to be destroyed and recreated. This can be provided these
303 * ways:
304 *
305 * <ul>
306 * <li>If nothing is explicitly supplied, the view ID of the container will
307 * be used.
308 * <li><code>android:tag</code> can be used in &lt;fragment&gt; to provide
309 * a specific tag name for the fragment.
310 * <li><code>android:id</code> can be used in &lt;fragment&gt; to provide
311 * a specific identifier for the fragment.
312 * </ul>
313 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700314 * <a name="BackStack"></a>
315 * <h3>Back Stack</h3>
316 *
317 * <p>The transaction in which fragments are modified can be placed on an
318 * internal back-stack of the owning activity. When the user presses back
319 * in the activity, any transactions on the back stack are popped off before
320 * the activity itself is finished.
321 *
322 * <p>For example, consider this simple fragment that is instantiated with
323 * an integer argument and displays that in a TextView in its UI:</p>
324 *
325 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
326 * fragment}
327 *
328 * <p>A function that creates a new instance of the fragment, replacing
329 * whatever current fragment instance is being shown and pushing that change
330 * on to the back stack could be written as:
331 *
332 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java
333 * add_stack}
334 *
335 * <p>After each call to this function, a new entry is on the stack, and
336 * pressing back will pop it to return the user to whatever previous state
337 * the activity UI was in.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700338 */
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700339public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener {
Dianne Hackbornc8017682010-07-06 13:34:38 -0700340 private static final HashMap<String, Class<?>> sClassMap =
341 new HashMap<String, Class<?>>();
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700342
Adam Powell635c60a2011-10-26 10:22:16 -0700343 static final int INVALID_STATE = -1; // Invalid state used as a null value.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700344 static final int INITIALIZING = 0; // Not yet created.
345 static final int CREATED = 1; // Created.
346 static final int ACTIVITY_CREATED = 2; // The activity has finished its creation.
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700347 static final int STOPPED = 3; // Fully created, not started.
348 static final int STARTED = 4; // Created and started, not resumed.
349 static final int RESUMED = 5; // Created started and resumed.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700350
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700351 int mState = INITIALIZING;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700352
Dianne Hackbornd173fa32010-12-23 13:58:22 -0800353 // Non-null if the fragment's view hierarchy is currently animating away,
354 // meaning we need to wait a bit on completely destroying it. This is the
355 // animation that is running.
356 Animator mAnimatingAway;
357
358 // If mAnimatingAway != null, this is the state we should move to once the
359 // animation is done.
360 int mStateAfterAnimating;
361
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700362 // When instantiated from saved state, this is the saved state.
363 Bundle mSavedFragmentState;
364 SparseArray<Parcelable> mSavedViewState;
365
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700366 // Index into active fragment array.
367 int mIndex = -1;
368
369 // Internal unique name for this fragment;
370 String mWho;
371
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700372 // Construction arguments;
373 Bundle mArguments;
374
Dianne Hackborndef15372010-08-15 12:43:52 -0700375 // Target fragment.
376 Fragment mTarget;
377
Dianne Hackbornf9302322011-06-14 18:36:14 -0700378 // For use when retaining a fragment: this is the index of the last mTarget.
379 int mTargetIndex = -1;
380
Dianne Hackborndef15372010-08-15 12:43:52 -0700381 // Target request code.
382 int mTargetRequestCode;
383
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700384 // True if the fragment is in the list of added fragments.
385 boolean mAdded;
386
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800387 // If set this fragment is being removed from its activity.
388 boolean mRemoving;
389
Dianne Hackborn2707d602010-07-09 18:01:20 -0700390 // True if the fragment is in the resumed state.
391 boolean mResumed;
392
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700393 // Set to true if this fragment was instantiated from a layout file.
394 boolean mFromLayout;
395
Dianne Hackborn625ac272010-09-17 18:29:22 -0700396 // Set to true when the view has actually been inflated in its layout.
397 boolean mInLayout;
398
Dianne Hackborn352cc982011-01-04 11:34:18 -0800399 // True if this fragment has been restored from previously saved state.
400 boolean mRestored;
401
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700402 // Number of active back stack entries this fragment is in.
Dianne Hackbornf121be72010-05-06 14:10:32 -0700403 int mBackStackNesting;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700404
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700405 // The fragment manager we are associated with. Set as soon as the
406 // fragment is used in a transaction; cleared after it has been removed
407 // from all transactions.
Adam Powell635c60a2011-10-26 10:22:16 -0700408 FragmentManagerImpl mFragmentManager;
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700409
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700410 // Activity this fragment is attached to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700411 Activity mActivity;
412
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700413 // The optional identifier for this fragment -- either the container ID if it
414 // was dynamically added to the view hierarchy, or the ID supplied in
415 // layout.
416 int mFragmentId;
417
418 // When a fragment is being dynamically added to the view hierarchy, this
419 // is the identifier of the parent container it is being added to.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700420 int mContainerId;
421
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700422 // The optional named tag for this fragment -- usually used to find
423 // fragments that are not part of the layout.
424 String mTag;
425
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700426 // Set to true when the app has requested that this fragment be hidden
427 // from the user.
428 boolean mHidden;
429
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700430 // Set to true when the app has requested that this fragment be detached.
431 boolean mDetached;
432
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700433 // If set this fragment would like its instance retained across
434 // configuration changes.
435 boolean mRetainInstance;
436
437 // If set this fragment is being retained across the current config change.
438 boolean mRetaining;
439
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700440 // If set this fragment has menu items to contribute.
441 boolean mHasMenu;
Dianne Hackborn6c285972011-08-29 16:53:49 -0700442
443 // Set to true to allow the fragment's menu to be shown.
444 boolean mMenuVisible = true;
445
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700446 // Used to verify that subclasses call through to super class.
447 boolean mCalled;
448
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700449 // If app has requested a specific animation, this is the one to use.
450 int mNextAnim;
451
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700452 // The parent container of the fragment after dynamically added to UI.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700453 ViewGroup mContainer;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700454
455 // The View generated for this fragment.
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700456 View mView;
457
Adam Powell635c60a2011-10-26 10:22:16 -0700458 // Whether this fragment should defer starting until after other fragments
459 // have been started and their loaders are finished.
460 boolean mDeferStart;
461
Adam Powell78fed9b2011-11-07 10:45:34 -0800462 // Hint provided by the app that this fragment is currently visible to the user.
463 boolean mUserVisibleHint = true;
464
Dianne Hackborn4911b782010-07-15 12:54:39 -0700465 LoaderManagerImpl mLoaderManager;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700466 boolean mLoadersStarted;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700467 boolean mCheckedForLoaderManager;
Dianne Hackbornc8017682010-07-06 13:34:38 -0700468
469 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700470 * State information that has been retrieved from a fragment instance
471 * through {@link FragmentManager#saveFragmentInstanceState(Fragment)
472 * FragmentManager.saveFragmentInstanceState}.
473 */
474 public static class SavedState implements Parcelable {
475 final Bundle mState;
476
477 SavedState(Bundle state) {
478 mState = state;
479 }
480
481 SavedState(Parcel in, ClassLoader loader) {
482 mState = in.readBundle();
483 if (loader != null && mState != null) {
484 mState.setClassLoader(loader);
485 }
486 }
487
488 @Override
489 public int describeContents() {
490 return 0;
491 }
492
493 @Override
494 public void writeToParcel(Parcel dest, int flags) {
495 dest.writeBundle(mState);
496 }
497
498 public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR
499 = new Parcelable.ClassLoaderCreator<SavedState>() {
500 public SavedState createFromParcel(Parcel in) {
501 return new SavedState(in, null);
502 }
503
504 public SavedState createFromParcel(Parcel in, ClassLoader loader) {
505 return new SavedState(in, loader);
506 }
507
508 public SavedState[] newArray(int size) {
509 return new SavedState[size];
510 }
511 };
512 }
513
514 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700515 * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when
516 * there is an instantiation failure.
517 */
518 static public class InstantiationException extends AndroidRuntimeException {
519 public InstantiationException(String msg, Exception cause) {
520 super(msg, cause);
521 }
522 }
523
524 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700525 * Default constructor. <strong>Every</strong> fragment must have an
Dianne Hackbornc8017682010-07-06 13:34:38 -0700526 * empty constructor, so it can be instantiated when restoring its
527 * activity's state. It is strongly recommended that subclasses do not
528 * have other constructors with parameters, since these constructors
529 * will not be called when the fragment is re-instantiated; instead,
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700530 * arguments can be supplied by the caller with {@link #setArguments}
531 * and later retrieved by the Fragment with {@link #getArguments}.
532 *
Dianne Hackborn291905e2010-08-17 15:17:15 -0700533 * <p>Applications should generally not implement a constructor. The
534 * first place application code an run where the fragment is ready to
535 * be used is in {@link #onAttach(Activity)}, the point where the fragment
536 * is actually associated with its activity. Some applications may also
537 * want to implement {@link #onInflate} to retrieve attributes from a
538 * layout resource, though should take care here because this happens for
539 * the fragment is attached to its activity.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700540 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700541 public Fragment() {
542 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700543
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700544 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700545 * Like {@link #instantiate(Context, String, Bundle)} but with a null
546 * argument Bundle.
547 */
548 public static Fragment instantiate(Context context, String fname) {
549 return instantiate(context, fname, null);
550 }
551
552 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700553 * Create a new instance of a Fragment with the given class name. This is
554 * the same as calling its empty constructor.
555 *
556 * @param context The calling context being used to instantiate the fragment.
557 * This is currently just used to get its ClassLoader.
558 * @param fname The class name of the fragment to instantiate.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700559 * @param args Bundle of arguments to supply to the fragment, which it
560 * can retrieve with {@link #getArguments()}. May be null.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700561 * @return Returns a new fragment instance.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700562 * @throws InstantiationException If there is a failure in instantiating
563 * the given fragment class. This is a runtime exception; it is not
564 * normally expected to happen.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700565 */
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700566 public static Fragment instantiate(Context context, String fname, Bundle args) {
567 try {
568 Class<?> clazz = sClassMap.get(fname);
569 if (clazz == null) {
570 // Class not found in the cache, see if it's real, and try to add it
571 clazz = context.getClassLoader().loadClass(fname);
572 sClassMap.put(fname, clazz);
573 }
574 Fragment f = (Fragment)clazz.newInstance();
575 if (args != null) {
576 args.setClassLoader(f.getClass().getClassLoader());
577 f.mArguments = args;
578 }
579 return f;
580 } catch (ClassNotFoundException e) {
581 throw new InstantiationException("Unable to instantiate fragment " + fname
582 + ": make sure class name exists, is public, and has an"
583 + " empty constructor that is public", e);
584 } catch (java.lang.InstantiationException e) {
585 throw new InstantiationException("Unable to instantiate fragment " + fname
586 + ": make sure class name exists, is public, and has an"
587 + " empty constructor that is public", e);
588 } catch (IllegalAccessException e) {
589 throw new InstantiationException("Unable to instantiate fragment " + fname
590 + ": make sure class name exists, is public, and has an"
591 + " empty constructor that is public", e);
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700592 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700593 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700594
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700595 final void restoreViewState() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700596 if (mSavedViewState != null) {
597 mView.restoreHierarchyState(mSavedViewState);
598 mSavedViewState = null;
599 }
600 }
601
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700602 final void setIndex(int index) {
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700603 mIndex = index;
604 mWho = "android:fragment:" + mIndex;
605 }
606
Dianne Hackborn16f6e892011-04-15 19:00:20 -0700607 final boolean isInBackStack() {
608 return mBackStackNesting > 0;
609 }
610
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700611 /**
612 * Subclasses can not override equals().
613 */
614 @Override final public boolean equals(Object o) {
615 return super.equals(o);
616 }
617
618 /**
619 * Subclasses can not override hashCode().
620 */
621 @Override final public int hashCode() {
622 return super.hashCode();
623 }
624
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700625 @Override
626 public String toString() {
627 StringBuilder sb = new StringBuilder(128);
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800628 DebugUtils.buildShortClassTag(this, sb);
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700629 if (mIndex >= 0) {
630 sb.append(" #");
631 sb.append(mIndex);
632 }
633 if (mFragmentId != 0) {
634 sb.append(" id=0x");
635 sb.append(Integer.toHexString(mFragmentId));
636 }
637 if (mTag != null) {
638 sb.append(" ");
639 sb.append(mTag);
640 }
641 sb.append('}');
642 return sb.toString();
643 }
644
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700645 /**
646 * Return the identifier this fragment is known by. This is either
647 * the android:id value supplied in a layout or the container view ID
648 * supplied when adding the fragment.
649 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700650 final public int getId() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700651 return mFragmentId;
652 }
653
654 /**
655 * Get the tag name of the fragment, if specified.
656 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700657 final public String getTag() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700658 return mTag;
659 }
660
661 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700662 * Supply the construction arguments for this fragment. This can only
663 * be called before the fragment has been attached to its activity; that
664 * is, you should call it immediately after constructing the fragment. The
665 * arguments supplied here will be retained across fragment destroy and
666 * creation.
667 */
Dianne Hackborndef15372010-08-15 12:43:52 -0700668 public void setArguments(Bundle args) {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700669 if (mIndex >= 0) {
670 throw new IllegalStateException("Fragment already active");
671 }
672 mArguments = args;
673 }
674
675 /**
676 * Return the arguments supplied when the fragment was instantiated,
677 * if any.
678 */
679 final public Bundle getArguments() {
680 return mArguments;
681 }
682
683 /**
Dianne Hackbornb46ed762011-06-02 18:33:15 -0700684 * Set the initial saved state that this Fragment should restore itself
685 * from when first being constructed, as returned by
686 * {@link FragmentManager#saveFragmentInstanceState(Fragment)
687 * FragmentManager.saveFragmentInstanceState}.
688 *
689 * @param state The state the fragment should be restored from.
690 */
691 public void setInitialSavedState(SavedState state) {
692 if (mIndex >= 0) {
693 throw new IllegalStateException("Fragment already active");
694 }
695 mSavedFragmentState = state != null && state.mState != null
696 ? state.mState : null;
697 }
698
699 /**
Dianne Hackborndef15372010-08-15 12:43:52 -0700700 * Optional target for this fragment. This may be used, for example,
701 * if this fragment is being started by another, and when done wants to
702 * give a result back to the first. The target set here is retained
703 * across instances via {@link FragmentManager#putFragment
704 * FragmentManager.putFragment()}.
705 *
706 * @param fragment The fragment that is the target of this one.
707 * @param requestCode Optional request code, for convenience if you
708 * are going to call back with {@link #onActivityResult(int, int, Intent)}.
709 */
710 public void setTargetFragment(Fragment fragment, int requestCode) {
711 mTarget = fragment;
712 mTargetRequestCode = requestCode;
713 }
714
715 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700716 * Return the target fragment set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700717 */
718 final public Fragment getTargetFragment() {
719 return mTarget;
720 }
721
722 /**
Dianne Hackborn3f00be52010-08-15 18:03:27 -0700723 * Return the target request code set by {@link #setTargetFragment}.
Dianne Hackborndef15372010-08-15 12:43:52 -0700724 */
725 final public int getTargetRequestCode() {
726 return mTargetRequestCode;
727 }
728
729 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700730 * Return the Activity this fragment is currently associated with.
731 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700732 final public Activity getActivity() {
Dianne Hackborn2dedce62010-04-15 14:45:25 -0700733 return mActivity;
734 }
735
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700736 /**
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800737 * Return <code>getActivity().getResources()</code>.
738 */
739 final public Resources getResources() {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800740 if (mActivity == null) {
741 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
742 }
Dianne Hackbornbdd19bc2010-11-10 23:27:54 -0800743 return mActivity.getResources();
744 }
745
746 /**
747 * Return a localized, styled CharSequence from the application's package's
748 * default string table.
749 *
750 * @param resId Resource id for the CharSequence text
751 */
752 public final CharSequence getText(int resId) {
753 return getResources().getText(resId);
754 }
755
756 /**
757 * Return a localized string from the application's package's
758 * default string table.
759 *
760 * @param resId Resource id for the string
761 */
762 public final String getString(int resId) {
763 return getResources().getString(resId);
764 }
765
766 /**
767 * Return a localized formatted string from the application's package's
768 * default string table, substituting the format arguments as defined in
769 * {@link java.util.Formatter} and {@link java.lang.String#format}.
770 *
771 * @param resId Resource id for the format string
772 * @param formatArgs The format arguments that will be used for substitution.
773 */
774
775 public final String getString(int resId, Object... formatArgs) {
776 return getResources().getString(resId, formatArgs);
777 }
778
779 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700780 * Return the FragmentManager for interacting with fragments associated
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700781 * with this fragment's activity. Note that this will be non-null slightly
Dianne Hackborn625ac272010-09-17 18:29:22 -0700782 * before {@link #getActivity()}, during the time from when the fragment is
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700783 * placed in a {@link FragmentTransaction} until it is committed and
784 * attached to its activity.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700785 */
786 final public FragmentManager getFragmentManager() {
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700787 return mFragmentManager;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700788 }
789
790 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700791 * Return true if the fragment is currently added to its activity.
792 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700793 final public boolean isAdded() {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800794 return mActivity != null && mAdded;
795 }
796
797 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -0700798 * Return true if the fragment has been explicitly detached from the UI.
799 * That is, {@link FragmentTransaction#detach(Fragment)
800 * FragmentTransaction.detach(Fragment)} has been used on it.
801 */
802 final public boolean isDetached() {
803 return mDetached;
804 }
805
806 /**
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800807 * Return true if this fragment is currently being removed from its
808 * activity. This is <em>not</em> whether its activity is finishing, but
809 * rather whether it is in the process of being removed from its activity.
810 */
811 final public boolean isRemoving() {
812 return mRemoving;
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700813 }
814
815 /**
Dianne Hackborn625ac272010-09-17 18:29:22 -0700816 * Return true if the layout is included as part of an activity view
817 * hierarchy via the &lt;fragment&gt; tag. This will always be true when
818 * fragments are created through the &lt;fragment&gt; tag, <em>except</em>
819 * in the case where an old fragment is restored from a previous state and
820 * it does not appear in the layout of the current state.
821 */
822 final public boolean isInLayout() {
823 return mInLayout;
824 }
825
826 /**
Dianne Hackborn2707d602010-07-09 18:01:20 -0700827 * Return true if the fragment is in the resumed state. This is true
828 * for the duration of {@link #onResume()} and {@link #onPause()} as well.
829 */
830 final public boolean isResumed() {
831 return mResumed;
832 }
833
834 /**
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700835 * Return true if the fragment is currently visible to the user. This means
836 * it: (1) has been added, (2) has its view attached to the window, and
837 * (3) is not hidden.
838 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700839 final public boolean isVisible() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700840 return isAdded() && !isHidden() && mView != null
841 && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
842 }
843
844 /**
845 * Return true if the fragment has been hidden. By default fragments
846 * are shown. You can find out about changes to this state with
Dianne Hackborncddfa6d2010-05-19 22:56:37 -0700847 * {@link #onHiddenChanged}. Note that the hidden state is orthogonal
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700848 * to other states -- that is, to be visible to the user, a fragment
849 * must be both started and not hidden.
850 */
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700851 final public boolean isHidden() {
Dianne Hackborn5ae74d62010-05-19 19:14:57 -0700852 return mHidden;
853 }
854
855 /**
856 * Called when the hidden state (as returned by {@link #isHidden()} of
857 * the fragment has changed. Fragments start out not hidden; this will
858 * be called whenever the fragment changes state from that.
859 * @param hidden True if the fragment is now hidden, false if it is not
860 * visible.
861 */
862 public void onHiddenChanged(boolean hidden) {
863 }
864
865 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700866 * Control whether a fragment instance is retained across Activity
867 * re-creation (such as from a configuration change). This can only
868 * be used with fragments not in the back stack. If set, the fragment
869 * lifecycle will be slightly different when an activity is recreated:
870 * <ul>
871 * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still
872 * will be, because the fragment is being detached from its current activity).
873 * <li> {@link #onCreate(Bundle)} will not be called since the fragment
874 * is not being re-created.
Dianne Hackbornc8017682010-07-06 13:34:38 -0700875 * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b>
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700876 * still be called.
877 * </ul>
878 */
879 public void setRetainInstance(boolean retain) {
880 mRetainInstance = retain;
881 }
882
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700883 final public boolean getRetainInstance() {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700884 return mRetainInstance;
885 }
886
887 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700888 * Report that this fragment would like to participate in populating
Wink Saville4dc643e2010-06-12 22:16:41 -0700889 * the options menu by receiving a call to {@link #onCreateOptionsMenu}
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700890 * and related methods.
891 *
892 * @param hasMenu If true, the fragment has menu items to contribute.
893 */
894 public void setHasOptionsMenu(boolean hasMenu) {
895 if (mHasMenu != hasMenu) {
896 mHasMenu = hasMenu;
Adam Powellf0f5fff2011-08-01 13:42:50 -0700897 if (isAdded() && !isHidden()) {
898 mFragmentManager.invalidateOptionsMenu();
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700899 }
900 }
901 }
Dianne Hackborn6c285972011-08-29 16:53:49 -0700902
903 /**
904 * Set a hint for whether this fragment's menu should be visible. This
905 * is useful if you know that a fragment has been placed in your view
906 * hierarchy so that the user can not currently seen it, so any menu items
907 * it has should also not be shown.
908 *
909 * @param menuVisible The default is true, meaning the fragment's menu will
910 * be shown as usual. If false, the user will not see the menu.
911 */
912 public void setMenuVisibility(boolean menuVisible) {
913 if (mMenuVisible != menuVisible) {
914 mMenuVisible = menuVisible;
915 if (mHasMenu && isAdded() && !isHidden()) {
916 mFragmentManager.invalidateOptionsMenu();
917 }
918 }
919 }
920
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -0700921 /**
Adam Powell78fed9b2011-11-07 10:45:34 -0800922 * Set a hint to the system about whether this fragment's UI is currently visible
923 * to the user. This hint defaults to true and is persistent across fragment instance
924 * state save and restore.
Adam Powell635c60a2011-10-26 10:22:16 -0700925 *
Adam Powell78fed9b2011-11-07 10:45:34 -0800926 * <p>An app may set this to false to indicate that the fragment's UI is
927 * scrolled out of visibility or is otherwise not directly visible to the user.
928 * This may be used by the system to prioritize operations such as fragment lifecycle updates
929 * or loader ordering behavior.</p>
Adam Powell635c60a2011-10-26 10:22:16 -0700930 *
Adam Powell78fed9b2011-11-07 10:45:34 -0800931 * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
932 * false if it is not.
Adam Powell635c60a2011-10-26 10:22:16 -0700933 */
Adam Powell78fed9b2011-11-07 10:45:34 -0800934 public void setUserVisibleHint(boolean isVisibleToUser) {
935 if (!mUserVisibleHint && isVisibleToUser && mState < STARTED) {
Adam Powell635c60a2011-10-26 10:22:16 -0700936 mFragmentManager.performPendingDeferredStart(this);
937 }
Adam Powell78fed9b2011-11-07 10:45:34 -0800938 mUserVisibleHint = isVisibleToUser;
939 mDeferStart = !isVisibleToUser;
Adam Powell635c60a2011-10-26 10:22:16 -0700940 }
941
942 /**
Adam Powell78fed9b2011-11-07 10:45:34 -0800943 * @return The current value of the user-visible hint on this fragment.
944 * @see #setUserVisibleHint(boolean)
Adam Powell635c60a2011-10-26 10:22:16 -0700945 */
Adam Powell78fed9b2011-11-07 10:45:34 -0800946 public boolean getUserVisibleHint() {
947 return mUserVisibleHint;
Adam Powell635c60a2011-10-26 10:22:16 -0700948 }
949
950 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -0700951 * Return the LoaderManager for this fragment, creating it if needed.
952 */
953 public LoaderManager getLoaderManager() {
954 if (mLoaderManager != null) {
955 return mLoaderManager;
956 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800957 if (mActivity == null) {
958 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
959 }
Dianne Hackborn5e0d5952010-08-05 13:45:35 -0700960 mCheckedForLoaderManager = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -0700961 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, true);
Dianne Hackbornc8017682010-07-06 13:34:38 -0700962 return mLoaderManager;
963 }
964
965 /**
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700966 * Call {@link Activity#startActivity(Intent)} on the fragment's
967 * containing Activity.
968 */
969 public void startActivity(Intent intent) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800970 if (mActivity == null) {
971 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
972 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700973 mActivity.startActivityFromFragment(this, intent, -1);
974 }
975
976 /**
977 * Call {@link Activity#startActivityForResult(Intent, int)} on the fragment's
978 * containing Activity.
979 */
980 public void startActivityForResult(Intent intent, int requestCode) {
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -0800981 if (mActivity == null) {
982 throw new IllegalStateException("Fragment " + this + " not attached to Activity");
983 }
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700984 mActivity.startActivityFromFragment(this, intent, requestCode);
985 }
986
987 /**
988 * Receive the result from a previous call to
989 * {@link #startActivityForResult(Intent, int)}. This follows the
990 * related Activity API as described there in
991 * {@link Activity#onActivityResult(int, int, Intent)}.
992 *
993 * @param requestCode The integer request code originally supplied to
994 * startActivityForResult(), allowing you to identify who this
995 * result came from.
996 * @param resultCode The integer result code returned by the child activity
997 * through its setResult().
998 * @param data An Intent, which can return result data to the caller
999 * (various data can be attached to Intent "extras").
1000 */
1001 public void onActivityResult(int requestCode, int resultCode, Intent data) {
1002 }
1003
1004 /**
Dianne Hackborn7187ccb2011-01-24 23:58:13 -08001005 * @hide Hack so that DialogFragment can make its Dialog before creating
1006 * its views, and the view construction can use the dialog's context for
1007 * inflation. Maybe this should become a public API. Note sure.
1008 */
1009 public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
1010 return mActivity.getLayoutInflater();
1011 }
1012
1013 /**
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001014 * @deprecated Use {@link #onInflate(Activity, AttributeSet, Bundle)} instead.
1015 */
1016 @Deprecated
1017 public void onInflate(AttributeSet attrs, Bundle savedInstanceState) {
1018 mCalled = true;
1019 }
1020
1021 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001022 * Called when a fragment is being created as part of a view layout
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001023 * inflation, typically from setting the content view of an activity. This
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001024 * may be called immediately after the fragment is created from a <fragment>
Dianne Hackborndef15372010-08-15 12:43:52 -07001025 * tag in a layout file. Note this is <em>before</em> the fragment's
1026 * {@link #onAttach(Activity)} has been called; all you should do here is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001027 * parse the attributes and save them away.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001028 *
Dianne Hackborndef15372010-08-15 12:43:52 -07001029 * <p>This is called every time the fragment is inflated, even if it is
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001030 * being inflated into a new instance with saved state. It typically makes
1031 * sense to re-parse the parameters each time, to allow them to change with
1032 * different configurations.</p>
1033 *
1034 * <p>Here is a typical implementation of a fragment that can take parameters
1035 * both through attributes supplied here as well from {@link #getArguments()}:</p>
1036 *
1037 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1038 * fragment}
1039 *
1040 * <p>Note that parsing the XML attributes uses a "styleable" resource. The
1041 * declaration for the styleable used here is:</p>
1042 *
1043 * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments}
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001044 *
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001045 * <p>The fragment can then be declared within its activity's content layout
1046 * through a tag like this:</p>
1047 *
1048 * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes}
1049 *
1050 * <p>This fragment can also be created dynamically from arguments given
1051 * at runtime in the arguments Bundle; here is an example of doing so at
1052 * creation of the containing activity:</p>
1053 *
1054 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java
1055 * create}
1056 *
1057 * @param activity The Activity that is inflating this fragment.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001058 * @param attrs The attributes at the tag where the fragment is
1059 * being created.
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001060 * @param savedInstanceState If the fragment is being re-created from
1061 * a previous saved state, this is the state.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001062 */
Dianne Hackborne3a7f622011-03-03 21:48:24 -08001063 public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
1064 onInflate(attrs, savedInstanceState);
Dianne Hackbornba51c3d2010-05-05 18:49:48 -07001065 mCalled = true;
1066 }
1067
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001068 /**
1069 * Called when a fragment is first attached to its activity.
1070 * {@link #onCreate(Bundle)} will be called after this.
1071 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001072 public void onAttach(Activity activity) {
1073 mCalled = true;
1074 }
1075
Chet Haase811ed1062010-08-06 10:38:15 -07001076 /**
1077 * Called when a fragment loads an animation.
1078 */
Chet Haasea18a86b2010-09-07 13:20:00 -07001079 public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Dianne Hackbornf121be72010-05-06 14:10:32 -07001080 return null;
1081 }
1082
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001083 /**
1084 * Called to do initial creation of a fragment. This is called after
Dianne Hackbornc8017682010-07-06 13:34:38 -07001085 * {@link #onAttach(Activity)} and before
1086 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1087 *
1088 * <p>Note that this can be called while the fragment's activity is
1089 * still in the process of being created. As such, you can not rely
1090 * on things like the activity's content view hierarchy being initialized
1091 * at this point. If you want to do work once the activity itself is
1092 * created, see {@link #onActivityCreated(Bundle)}.
1093 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001094 * @param savedInstanceState If the fragment is being re-created from
1095 * a previous saved state, this is the state.
1096 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001097 public void onCreate(Bundle savedInstanceState) {
1098 mCalled = true;
1099 }
1100
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001101 /**
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001102 * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
1103 * has returned, but before any saved state has been restored in to the view.
1104 * This gives subclasses a chance to initialize themselves once
1105 * they know their view hierarchy has been completely created. The fragment's
1106 * view hierarchy is not however attached to its parent at this point.
1107 * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}.
1108 * @param savedInstanceState If non-null, this fragment is being re-constructed
1109 * from a previous saved state as given here.
1110 */
1111 public void onViewCreated(View view, Bundle savedInstanceState) {
1112 }
1113
1114 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001115 * Called to have the fragment instantiate its user interface view.
1116 * This is optional, and non-graphical fragments can return null (which
1117 * is the default implementation). This will be called between
Dianne Hackbornc8017682010-07-06 13:34:38 -07001118 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001119 *
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001120 * <p>If you return a View from here, you will later be called in
1121 * {@link #onDestroyView} when the view is being released.
1122 *
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001123 * @param inflater The LayoutInflater object that can be used to inflate
1124 * any views in the fragment,
1125 * @param container If non-null, this is the parent view that the fragment's
1126 * UI should be attached to. The fragment should not add the view itself,
1127 * but this can be used to generate the LayoutParams of the view.
1128 * @param savedInstanceState If non-null, this fragment is being re-constructed
1129 * from a previous saved state as given here.
1130 *
1131 * @return Return the View for the fragment's UI, or null.
1132 */
1133 public View onCreateView(LayoutInflater inflater, ViewGroup container,
1134 Bundle savedInstanceState) {
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001135 return null;
1136 }
1137
Scott Main662cc7a42011-01-17 15:02:07 -08001138 /**
1139 * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}),
1140 * if provided.
1141 *
1142 * @return The fragment's root view, or null if it has no layout.
1143 */
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001144 public View getView() {
1145 return mView;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001146 }
1147
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001148 /**
Dianne Hackbornc8017682010-07-06 13:34:38 -07001149 * Called when the fragment's activity has been created and this
1150 * fragment's view hierarchy instantiated. It can be used to do final
1151 * initialization once these pieces are in place, such as retrieving
1152 * views or restoring state. It is also useful for fragments that use
1153 * {@link #setRetainInstance(boolean)} to retain their instance,
1154 * as this callback tells the fragment when it is fully associated with
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001155 * the new activity instance. This is called after {@link #onCreateView}
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001156 * and before {@link #onStart()}.
1157 *
1158 * @param savedInstanceState If the fragment is being re-created from
1159 * a previous saved state, this is the state.
1160 */
Dianne Hackbornc8017682010-07-06 13:34:38 -07001161 public void onActivityCreated(Bundle savedInstanceState) {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001162 mCalled = true;
1163 }
1164
1165 /**
1166 * Called when the Fragment is visible to the user. This is generally
1167 * tied to {@link Activity#onStart() Activity.onStart} of the containing
1168 * Activity's lifecycle.
1169 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001170 public void onStart() {
1171 mCalled = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001172
1173 if (!mLoadersStarted) {
1174 mLoadersStarted = true;
1175 if (!mCheckedForLoaderManager) {
1176 mCheckedForLoaderManager = true;
1177 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
1178 }
1179 if (mLoaderManager != null) {
1180 mLoaderManager.doStart();
1181 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001182 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001183 }
1184
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001185 /**
1186 * Called when the fragment is visible to the user and actively running.
1187 * This is generally
1188 * tied to {@link Activity#onResume() Activity.onResume} of the containing
1189 * Activity's lifecycle.
1190 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001191 public void onResume() {
1192 mCalled = true;
1193 }
1194
Dianne Hackborn72778202010-08-20 18:26:01 -07001195 /**
1196 * Called to ask the fragment to save its current dynamic state, so it
1197 * can later be reconstructed in a new instance of its process is
1198 * restarted. If a new instance of the fragment later needs to be
1199 * created, the data you place in the Bundle here will be available
1200 * in the Bundle given to {@link #onCreate(Bundle)},
1201 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
1202 * {@link #onActivityCreated(Bundle)}.
1203 *
1204 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
Daisuke Miyakawa2f761762010-09-12 16:53:17 -07001205 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
Dianne Hackborn72778202010-08-20 18:26:01 -07001206 * applies here as well. Note however: <em>this method may be called
1207 * at any time before {@link #onDestroy()}</em>. There are many situations
1208 * where a fragment may be mostly torn down (such as when placed on the
1209 * back stack with no UI showing), but its state will not be saved until
1210 * its owning activity actually needs to save its state.
1211 *
1212 * @param outState Bundle in which to place your saved state.
1213 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001214 public void onSaveInstanceState(Bundle outState) {
1215 }
1216
1217 public void onConfigurationChanged(Configuration newConfig) {
1218 mCalled = true;
1219 }
1220
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001221 /**
1222 * Called when the Fragment is no longer resumed. This is generally
1223 * tied to {@link Activity#onPause() Activity.onPause} of the containing
1224 * Activity's lifecycle.
1225 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001226 public void onPause() {
1227 mCalled = true;
1228 }
1229
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001230 /**
1231 * Called when the Fragment is no longer started. This is generally
1232 * tied to {@link Activity#onStop() Activity.onStop} of the containing
1233 * Activity's lifecycle.
1234 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001235 public void onStop() {
1236 mCalled = true;
1237 }
1238
1239 public void onLowMemory() {
1240 mCalled = true;
1241 }
1242
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001243 public void onTrimMemory(int level) {
1244 mCalled = true;
1245 }
1246
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001247 /**
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001248 * Called when the view previously created by {@link #onCreateView} has
1249 * been detached from the fragment. The next time the fragment needs
1250 * to be displayed, a new view will be created. This is called
Dianne Hackborndef15372010-08-15 12:43:52 -07001251 * after {@link #onStop()} and before {@link #onDestroy()}. It is called
1252 * <em>regardless</em> of whether {@link #onCreateView} returned a
1253 * non-null view. Internally it is called after the view's state has
1254 * been saved but before it has been removed from its parent.
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001255 */
1256 public void onDestroyView() {
1257 mCalled = true;
1258 }
1259
1260 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001261 * Called when the fragment is no longer in use. This is called
1262 * after {@link #onStop()} and before {@link #onDetach()}.
1263 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001264 public void onDestroy() {
1265 mCalled = true;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001266 //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager
1267 // + " mLoaderManager=" + mLoaderManager);
1268 if (!mCheckedForLoaderManager) {
1269 mCheckedForLoaderManager = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001270 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001271 }
Dianne Hackbornc8017682010-07-06 13:34:38 -07001272 if (mLoaderManager != null) {
1273 mLoaderManager.doDestroy();
1274 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001275 }
Dianne Hackborn5ae74d62010-05-19 19:14:57 -07001276
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001277 /**
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001278 * Called by the fragment manager once this fragment has been removed,
1279 * so that we don't have any left-over state if the application decides
1280 * to re-use the instance. This only clears state that the framework
1281 * internally manages, not things the application sets.
1282 */
1283 void initState() {
1284 mIndex = -1;
1285 mWho = null;
1286 mAdded = false;
1287 mRemoving = false;
1288 mResumed = false;
1289 mFromLayout = false;
1290 mInLayout = false;
1291 mRestored = false;
1292 mBackStackNesting = 0;
1293 mFragmentManager = null;
Dianne Hackborn6c285972011-08-29 16:53:49 -07001294 mActivity = null;
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001295 mFragmentId = 0;
1296 mContainerId = 0;
1297 mTag = null;
1298 mHidden = false;
1299 mDetached = false;
1300 mRetaining = false;
1301 mLoaderManager = null;
1302 mLoadersStarted = false;
1303 mCheckedForLoaderManager = false;
1304 }
1305
1306 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001307 * Called when the fragment is no longer attached to its activity. This
1308 * is called after {@link #onDestroy()}.
1309 */
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001310 public void onDetach() {
1311 mCalled = true;
1312 }
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001313
1314 /**
1315 * Initialize the contents of the Activity's standard options menu. You
1316 * should place your menu items in to <var>menu</var>. For this method
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001317 * to be called, you must have first called {@link #setHasOptionsMenu}. See
1318 * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
1319 * for more information.
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001320 *
1321 * @param menu The options menu in which you place your items.
1322 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001323 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001324 * @see #onPrepareOptionsMenu
1325 * @see #onOptionsItemSelected
1326 */
1327 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
1328 }
1329
1330 /**
1331 * Prepare the Screen's standard options menu to be displayed. This is
1332 * called right before the menu is shown, every time it is shown. You can
1333 * use this method to efficiently enable/disable items or otherwise
1334 * dynamically modify the contents. See
1335 * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu}
1336 * for more information.
1337 *
1338 * @param menu The options menu as last shown or first initialized by
1339 * onCreateOptionsMenu().
1340 *
Wink Saville4dc643e2010-06-12 22:16:41 -07001341 * @see #setHasOptionsMenu
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001342 * @see #onCreateOptionsMenu
1343 */
1344 public void onPrepareOptionsMenu(Menu menu) {
1345 }
1346
1347 /**
Dianne Hackborn8eb2e242010-11-01 12:31:24 -07001348 * Called when this fragment's option menu items are no longer being
1349 * included in the overall options menu. Receiving this call means that
1350 * the menu needed to be rebuilt, but this fragment's items were not
1351 * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
1352 * was not called).
1353 */
1354 public void onDestroyOptionsMenu() {
1355 }
1356
1357 /**
Dianne Hackbornb31e84bc2010-06-08 18:04:35 -07001358 * This hook is called whenever an item in your options menu is selected.
1359 * The default implementation simply returns false to have the normal
1360 * processing happen (calling the item's Runnable or sending a message to
1361 * its Handler as appropriate). You can use this method for any items
1362 * for which you would like to do processing without those other
1363 * facilities.
1364 *
1365 * <p>Derived classes should call through to the base class for it to
1366 * perform the default menu handling.
1367 *
1368 * @param item The menu item that was selected.
1369 *
1370 * @return boolean Return false to allow normal menu processing to
1371 * proceed, true to consume it here.
1372 *
1373 * @see #onCreateOptionsMenu
1374 */
1375 public boolean onOptionsItemSelected(MenuItem item) {
1376 return false;
1377 }
1378
1379 /**
1380 * This hook is called whenever the options menu is being closed (either by the user canceling
1381 * the menu with the back/menu button, or when an item is selected).
1382 *
1383 * @param menu The options menu as last shown or first initialized by
1384 * onCreateOptionsMenu().
1385 */
1386 public void onOptionsMenuClosed(Menu menu) {
1387 }
Dianne Hackborn5ddd1272010-06-12 10:15:28 -07001388
1389 /**
1390 * Called when a context menu for the {@code view} is about to be shown.
1391 * Unlike {@link #onCreateOptionsMenu}, this will be called every
1392 * time the context menu is about to be shown and should be populated for
1393 * the view (or item inside the view for {@link AdapterView} subclasses,
1394 * this can be found in the {@code menuInfo})).
1395 * <p>
1396 * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
1397 * item has been selected.
1398 * <p>
1399 * The default implementation calls up to
1400 * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
1401 * you can not call this implementation if you don't want that behavior.
1402 * <p>
1403 * It is not safe to hold onto the context menu after this method returns.
1404 * {@inheritDoc}
1405 */
1406 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1407 getActivity().onCreateContextMenu(menu, v, menuInfo);
1408 }
1409
1410 /**
1411 * Registers a context menu to be shown for the given view (multiple views
1412 * can show the context menu). This method will set the
1413 * {@link OnCreateContextMenuListener} on the view to this fragment, so
1414 * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be
1415 * called when it is time to show the context menu.
1416 *
1417 * @see #unregisterForContextMenu(View)
1418 * @param view The view that should show a context menu.
1419 */
1420 public void registerForContextMenu(View view) {
1421 view.setOnCreateContextMenuListener(this);
1422 }
1423
1424 /**
1425 * Prevents a context menu to be shown for the given view. This method will
1426 * remove the {@link OnCreateContextMenuListener} on the view.
1427 *
1428 * @see #registerForContextMenu(View)
1429 * @param view The view that should stop showing a context menu.
1430 */
1431 public void unregisterForContextMenu(View view) {
1432 view.setOnCreateContextMenuListener(null);
1433 }
1434
1435 /**
1436 * This hook is called whenever an item in a context menu is selected. The
1437 * default implementation simply returns false to have the normal processing
1438 * happen (calling the item's Runnable or sending a message to its Handler
1439 * as appropriate). You can use this method for any items for which you
1440 * would like to do processing without those other facilities.
1441 * <p>
1442 * Use {@link MenuItem#getMenuInfo()} to get extra information set by the
1443 * View that added this menu item.
1444 * <p>
1445 * Derived classes should call through to the base class for it to perform
1446 * the default menu handling.
1447 *
1448 * @param item The context menu item that was selected.
1449 * @return boolean Return false to allow normal context menu processing to
1450 * proceed, true to consume it here.
1451 */
1452 public boolean onContextItemSelected(MenuItem item) {
1453 return false;
1454 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07001455
Dianne Hackborn625ac272010-09-17 18:29:22 -07001456 /**
1457 * Print the Fragments's state into the given stream.
1458 *
1459 * @param prefix Text to print at the front of each line.
1460 * @param fd The raw file descriptor that the dump is being sent to.
1461 * @param writer The PrintWriter to which you should dump your state. This will be
1462 * closed for you after you return.
1463 * @param args additional arguments to the dump request.
1464 */
1465 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08001466 writer.print(prefix); writer.print("mFragmentId=#");
1467 writer.print(Integer.toHexString(mFragmentId));
1468 writer.print(" mContainerId#=");
1469 writer.print(Integer.toHexString(mContainerId));
Dianne Hackborn625ac272010-09-17 18:29:22 -07001470 writer.print(" mTag="); writer.println(mTag);
1471 writer.print(prefix); writer.print("mState="); writer.print(mState);
1472 writer.print(" mIndex="); writer.print(mIndex);
1473 writer.print(" mWho="); writer.print(mWho);
1474 writer.print(" mBackStackNesting="); writer.println(mBackStackNesting);
1475 writer.print(prefix); writer.print("mAdded="); writer.print(mAdded);
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08001476 writer.print(" mRemoving="); writer.print(mRemoving);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001477 writer.print(" mResumed="); writer.print(mResumed);
1478 writer.print(" mFromLayout="); writer.print(mFromLayout);
1479 writer.print(" mInLayout="); writer.println(mInLayout);
1480 writer.print(prefix); writer.print("mHidden="); writer.print(mHidden);
Dianne Hackborn16f6e892011-04-15 19:00:20 -07001481 writer.print(" mDetached="); writer.print(mDetached);
Dianne Hackborn6c285972011-08-29 16:53:49 -07001482 writer.print(" mMenuVisible="); writer.print(mMenuVisible);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001483 writer.print(" mHasMenu="); writer.println(mHasMenu);
Dianne Hackborn6c285972011-08-29 16:53:49 -07001484 writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance);
Adam Powell78fed9b2011-11-07 10:45:34 -08001485 writer.print(" mRetaining="); writer.print(mRetaining);
1486 writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001487 if (mFragmentManager != null) {
1488 writer.print(prefix); writer.print("mFragmentManager=");
1489 writer.println(mFragmentManager);
1490 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001491 if (mActivity != null) {
1492 writer.print(prefix); writer.print("mActivity=");
1493 writer.println(mActivity);
1494 }
1495 if (mArguments != null) {
1496 writer.print(prefix); writer.print("mArguments="); writer.println(mArguments);
1497 }
1498 if (mSavedFragmentState != null) {
1499 writer.print(prefix); writer.print("mSavedFragmentState=");
1500 writer.println(mSavedFragmentState);
1501 }
1502 if (mSavedViewState != null) {
1503 writer.print(prefix); writer.print("mSavedViewState=");
1504 writer.println(mSavedViewState);
1505 }
1506 if (mTarget != null) {
1507 writer.print(prefix); writer.print("mTarget="); writer.print(mTarget);
1508 writer.print(" mTargetRequestCode=");
1509 writer.println(mTargetRequestCode);
1510 }
1511 if (mNextAnim != 0) {
1512 writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim);
1513 }
1514 if (mContainer != null) {
1515 writer.print(prefix); writer.print("mContainer="); writer.println(mContainer);
1516 }
1517 if (mView != null) {
1518 writer.print(prefix); writer.print("mView="); writer.println(mView);
1519 }
Dianne Hackbornd173fa32010-12-23 13:58:22 -08001520 if (mAnimatingAway != null) {
1521 writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway);
1522 writer.print(prefix); writer.print("mStateAfterAnimating=");
1523 writer.println(mStateAfterAnimating);
1524 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001525 if (mLoaderManager != null) {
Dianne Hackborn30d71892010-12-11 10:37:55 -08001526 writer.print(prefix); writer.println("Loader Manager:");
1527 mLoaderManager.dump(prefix + " ", fd, writer, args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001528 }
1529 }
1530
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001531 void performStart() {
1532 onStart();
1533 if (mLoaderManager != null) {
1534 mLoaderManager.doReportStart();
1535 }
1536 }
1537
Dianne Hackborn2707d602010-07-09 18:01:20 -07001538 void performStop() {
1539 onStop();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001540
1541 if (mLoadersStarted) {
1542 mLoadersStarted = false;
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001543 if (!mCheckedForLoaderManager) {
1544 mCheckedForLoaderManager = true;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001545 mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false);
Dianne Hackborn5e0d5952010-08-05 13:45:35 -07001546 }
Dianne Hackborn2707d602010-07-09 18:01:20 -07001547 if (mLoaderManager != null) {
1548 if (mActivity == null || !mActivity.mChangingConfigurations) {
1549 mLoaderManager.doStop();
1550 } else {
1551 mLoaderManager.doRetain();
1552 }
1553 }
1554 }
1555 }
Dianne Hackbornafc4b282011-06-10 17:03:42 -07001556
1557 void performDestroyView() {
1558 onDestroyView();
1559 if (mLoaderManager != null) {
1560 mLoaderManager.doReportNextStart();
1561 }
1562 }
Dianne Hackborn2dedce62010-04-15 14:45:25 -07001563}