Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 20 | import android.content.ComponentCallbacks2; |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 23 | import android.content.res.Configuration; |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 24 | import android.content.res.Resources; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 28 | import android.util.AndroidRuntimeException; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 30 | import android.util.DebugUtils; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 31 | import android.util.SparseArray; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 32 | import android.view.ContextMenu; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 33 | import android.view.ContextMenu.ContextMenuInfo; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 34 | import android.view.LayoutInflater; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 35 | import android.view.Menu; |
| 36 | import android.view.MenuInflater; |
| 37 | import android.view.MenuItem; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 38 | import android.view.View; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 39 | import android.view.View.OnCreateContextMenuListener; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 40 | import android.view.ViewGroup; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 41 | import android.widget.AdapterView; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 42 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 43 | import java.io.FileDescriptor; |
| 44 | import java.io.PrintWriter; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 45 | import java.util.HashMap; |
| 46 | |
| 47 | final class FragmentState implements Parcelable { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 48 | final String mClassName; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 49 | final int mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 50 | final boolean mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 51 | final int mFragmentId; |
| 52 | final int mContainerId; |
| 53 | final String mTag; |
| 54 | final boolean mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 55 | final boolean mDetached; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 56 | final Bundle mArguments; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 57 | |
| 58 | Bundle mSavedFragmentState; |
| 59 | |
| 60 | Fragment mInstance; |
| 61 | |
| 62 | public FragmentState(Fragment frag) { |
| 63 | mClassName = frag.getClass().getName(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 64 | mIndex = frag.mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 65 | mFromLayout = frag.mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 66 | mFragmentId = frag.mFragmentId; |
| 67 | mContainerId = frag.mContainerId; |
| 68 | mTag = frag.mTag; |
| 69 | mRetainInstance = frag.mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 70 | mDetached = frag.mDetached; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 71 | mArguments = frag.mArguments; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public FragmentState(Parcel in) { |
| 75 | mClassName = in.readString(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 76 | mIndex = in.readInt(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 77 | mFromLayout = in.readInt() != 0; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 78 | mFragmentId = in.readInt(); |
| 79 | mContainerId = in.readInt(); |
| 80 | mTag = in.readString(); |
| 81 | mRetainInstance = in.readInt() != 0; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 82 | mDetached = in.readInt() != 0; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 83 | mArguments = in.readBundle(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 84 | mSavedFragmentState = in.readBundle(); |
| 85 | } |
| 86 | |
| 87 | public Fragment instantiate(Activity activity) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 88 | if (mInstance != null) { |
| 89 | return mInstance; |
| 90 | } |
| 91 | |
Dianne Hackborn | 5164246 | 2010-10-28 10:32:37 -0700 | [diff] [blame] | 92 | if (mArguments != null) { |
| 93 | mArguments.setClassLoader(activity.getClassLoader()); |
| 94 | } |
| 95 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 96 | mInstance = Fragment.instantiate(activity, mClassName, mArguments); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 97 | |
| 98 | if (mSavedFragmentState != null) { |
| 99 | mSavedFragmentState.setClassLoader(activity.getClassLoader()); |
| 100 | mInstance.mSavedFragmentState = mSavedFragmentState; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 101 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 102 | mInstance.setIndex(mIndex); |
| 103 | mInstance.mFromLayout = mFromLayout; |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 104 | mInstance.mRestored = true; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 105 | mInstance.mFragmentId = mFragmentId; |
| 106 | mInstance.mContainerId = mContainerId; |
| 107 | mInstance.mTag = mTag; |
| 108 | mInstance.mRetainInstance = mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 109 | mInstance.mDetached = mDetached; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 110 | mInstance.mFragmentManager = activity.mFragments; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 111 | |
| 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 Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 121 | dest.writeInt(mIndex); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 122 | dest.writeInt(mFromLayout ? 1 : 0); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 123 | dest.writeInt(mFragmentId); |
| 124 | dest.writeInt(mContainerId); |
| 125 | dest.writeString(mTag); |
| 126 | dest.writeInt(mRetainInstance ? 1 : 0); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 127 | dest.writeInt(mDetached ? 1 : 0); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 128 | dest.writeBundle(mArguments); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 129 | 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 144 | /** |
| 145 | * A Fragment is a piece of an application's user interface or behavior |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 146 | * 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 Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 167 | * <li><a href="#OlderPlatforms">Older Platforms</a> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 168 | * <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 Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 173 | * <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 Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 179 | * <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 Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 188 | * <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><fragment></code> tags |
| 239 | * to embed fragment instances inside of the layout. For example, here is |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 240 | * a simple layout that embeds one fragment:</p> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 241 | * |
| 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 Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 249 | * <p>The titles fragment, showing a list of titles, is fairly simple, relying |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 250 | * on {@link ListFragment} for most of its work. Note the implementation of |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 251 | * 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 Dodson | 542f240 | 2011-06-14 16:40:23 -0700 | [diff] [blame] | 253 | * this later), or start a new activity to show the details.</p> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 254 | * |
| 255 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 256 | * titles} |
| 257 | * |
Ben Dodson | 542f240 | 2011-06-14 16:40:23 -0700 | [diff] [blame] | 258 | * <p>The details fragment showing the contents of a selected item just |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 259 | * 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 Dodson | 542f240 | 2011-06-14 16:40:23 -0700 | [diff] [blame] | 266 | * container in the current activity, so the titles fragment's click code will |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 267 | * 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 Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 278 | * <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 Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 282 | * |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 283 | * <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 Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 287 | * instance. However, any that are no longer associated with a <fragment> |
| 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 Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 293 | * |
| 294 | * <p>The attributes of the <fragment> tag are used to control the |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 295 | * 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 Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 297 | * 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 <fragment> to provide |
| 308 | * a specific tag name for the fragment. |
| 309 | * <li><code>android:id</code> can be used in <fragment> to provide |
| 310 | * a specific identifier for the fragment. |
| 311 | * </ul> |
| 312 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 313 | * <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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 337 | */ |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 338 | public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener { |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 339 | private static final HashMap<String, Class<?>> sClassMap = |
| 340 | new HashMap<String, Class<?>>(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 341 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 342 | static final int INVALID_STATE = -1; // Invalid state used as a null value. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 343 | 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 Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 346 | 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 349 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 350 | int mState = INITIALIZING; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 351 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 352 | // 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 361 | // When instantiated from saved state, this is the saved state. |
| 362 | Bundle mSavedFragmentState; |
| 363 | SparseArray<Parcelable> mSavedViewState; |
| 364 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 365 | // Index into active fragment array. |
| 366 | int mIndex = -1; |
| 367 | |
| 368 | // Internal unique name for this fragment; |
| 369 | String mWho; |
| 370 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 371 | // Construction arguments; |
| 372 | Bundle mArguments; |
| 373 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 374 | // Target fragment. |
| 375 | Fragment mTarget; |
| 376 | |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 377 | // For use when retaining a fragment: this is the index of the last mTarget. |
| 378 | int mTargetIndex = -1; |
| 379 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 380 | // Target request code. |
| 381 | int mTargetRequestCode; |
| 382 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 383 | // True if the fragment is in the list of added fragments. |
| 384 | boolean mAdded; |
| 385 | |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 386 | // If set this fragment is being removed from its activity. |
| 387 | boolean mRemoving; |
| 388 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 389 | // True if the fragment is in the resumed state. |
| 390 | boolean mResumed; |
| 391 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 392 | // Set to true if this fragment was instantiated from a layout file. |
| 393 | boolean mFromLayout; |
| 394 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 395 | // Set to true when the view has actually been inflated in its layout. |
| 396 | boolean mInLayout; |
| 397 | |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 398 | // True if this fragment has been restored from previously saved state. |
| 399 | boolean mRestored; |
| 400 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 401 | // Number of active back stack entries this fragment is in. |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 402 | int mBackStackNesting; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 403 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 404 | // 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 Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 407 | FragmentManagerImpl mFragmentManager; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 408 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 409 | // Activity this fragment is attached to. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 410 | Activity mActivity; |
| 411 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 412 | // 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 419 | int mContainerId; |
| 420 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 421 | // 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 Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 425 | // Set to true when the app has requested that this fragment be hidden |
| 426 | // from the user. |
| 427 | boolean mHidden; |
| 428 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 429 | // Set to true when the app has requested that this fragment be detached. |
| 430 | boolean mDetached; |
| 431 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 432 | // 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 Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 439 | // If set this fragment has menu items to contribute. |
| 440 | boolean mHasMenu; |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 441 | |
| 442 | // Set to true to allow the fragment's menu to be shown. |
| 443 | boolean mMenuVisible = true; |
| 444 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 445 | // Used to verify that subclasses call through to super class. |
| 446 | boolean mCalled; |
| 447 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 448 | // If app has requested a specific animation, this is the one to use. |
| 449 | int mNextAnim; |
| 450 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 451 | // The parent container of the fragment after dynamically added to UI. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 452 | ViewGroup mContainer; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 453 | |
| 454 | // The View generated for this fragment. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 455 | View mView; |
| 456 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 457 | // Whether this fragment should defer starting until after other fragments |
| 458 | // have been started and their loaders are finished. |
| 459 | boolean mDeferStart; |
| 460 | |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 461 | // Hint provided by the app that this fragment is currently visible to the user. |
| 462 | boolean mUserVisibleHint = true; |
| 463 | |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 464 | LoaderManagerImpl mLoaderManager; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 465 | boolean mLoadersStarted; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 466 | boolean mCheckedForLoaderManager; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 467 | |
| 468 | /** |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 469 | * 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 Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 514 | * 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 Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 524 | * Default constructor. <strong>Every</strong> fragment must have an |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 525 | * 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 Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 529 | * arguments can be supplied by the caller with {@link #setArguments} |
| 530 | * and later retrieved by the Fragment with {@link #getArguments}. |
| 531 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 532 | * <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 Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 539 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 540 | public Fragment() { |
| 541 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 542 | |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 543 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 544 | * 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 Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 552 | * 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 Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 558 | * @param args Bundle of arguments to supply to the fragment, which it |
| 559 | * can retrieve with {@link #getArguments()}. May be null. |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 560 | * @return Returns a new fragment instance. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 561 | * @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 Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 564 | */ |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 565 | 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 591 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 592 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 593 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 594 | final void restoreViewState() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 595 | if (mSavedViewState != null) { |
| 596 | mView.restoreHierarchyState(mSavedViewState); |
| 597 | mSavedViewState = null; |
| 598 | } |
| 599 | } |
| 600 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 601 | final void setIndex(int index) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 602 | mIndex = index; |
| 603 | mWho = "android:fragment:" + mIndex; |
| 604 | } |
| 605 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 606 | final boolean isInBackStack() { |
| 607 | return mBackStackNesting > 0; |
| 608 | } |
| 609 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 610 | /** |
| 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 Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 624 | @Override |
| 625 | public String toString() { |
| 626 | StringBuilder sb = new StringBuilder(128); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 627 | DebugUtils.buildShortClassTag(this, sb); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 628 | 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 644 | /** |
| 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 Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 649 | final public int getId() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 650 | return mFragmentId; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Get the tag name of the fragment, if specified. |
| 655 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 656 | final public String getTag() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 657 | return mTag; |
| 658 | } |
| 659 | |
| 660 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 661 | * 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 Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 667 | public void setArguments(Bundle args) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 668 | 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 Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 683 | * 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 Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 699 | * 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 Hackborn | 3f00be5 | 2010-08-15 18:03:27 -0700 | [diff] [blame] | 715 | * Return the target fragment set by {@link #setTargetFragment}. |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 716 | */ |
| 717 | final public Fragment getTargetFragment() { |
| 718 | return mTarget; |
| 719 | } |
| 720 | |
| 721 | /** |
Dianne Hackborn | 3f00be5 | 2010-08-15 18:03:27 -0700 | [diff] [blame] | 722 | * Return the target request code set by {@link #setTargetFragment}. |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 723 | */ |
| 724 | final public int getTargetRequestCode() { |
| 725 | return mTargetRequestCode; |
| 726 | } |
| 727 | |
| 728 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 729 | * Return the Activity this fragment is currently associated with. |
| 730 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 731 | final public Activity getActivity() { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 732 | return mActivity; |
| 733 | } |
| 734 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 735 | /** |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 736 | * Return <code>getActivity().getResources()</code>. |
| 737 | */ |
| 738 | final public Resources getResources() { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 739 | if (mActivity == null) { |
| 740 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 741 | } |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 742 | 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 Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 779 | * Return the FragmentManager for interacting with fragments associated |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 780 | * with this fragment's activity. Note that this will be non-null slightly |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 781 | * before {@link #getActivity()}, during the time from when the fragment is |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 782 | * placed in a {@link FragmentTransaction} until it is committed and |
| 783 | * attached to its activity. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 784 | */ |
| 785 | final public FragmentManager getFragmentManager() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 786 | return mFragmentManager; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /** |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 790 | * Return true if the fragment is currently added to its activity. |
| 791 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 792 | final public boolean isAdded() { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 793 | return mActivity != null && mAdded; |
| 794 | } |
| 795 | |
| 796 | /** |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 797 | * 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 Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 806 | * 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 Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 815 | * Return true if the layout is included as part of an activity view |
| 816 | * hierarchy via the <fragment> tag. This will always be true when |
| 817 | * fragments are created through the <fragment> 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 Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 826 | * 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 Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 834 | * 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 Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 838 | final public boolean isVisible() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 839 | 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 Hackborn | cddfa6d | 2010-05-19 22:56:37 -0700 | [diff] [blame] | 846 | * {@link #onHiddenChanged}. Note that the hidden state is orthogonal |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 847 | * to other states -- that is, to be visible to the user, a fragment |
| 848 | * must be both started and not hidden. |
| 849 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 850 | final public boolean isHidden() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 851 | 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 865 | * 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 Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 874 | * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b> |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 875 | * still be called. |
| 876 | * </ul> |
| 877 | */ |
| 878 | public void setRetainInstance(boolean retain) { |
| 879 | mRetainInstance = retain; |
| 880 | } |
| 881 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 882 | final public boolean getRetainInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 883 | return mRetainInstance; |
| 884 | } |
| 885 | |
| 886 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 887 | * Report that this fragment would like to participate in populating |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 888 | * the options menu by receiving a call to {@link #onCreateOptionsMenu} |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 889 | * 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 Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 896 | if (isAdded() && !isHidden()) { |
| 897 | mFragmentManager.invalidateOptionsMenu(); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 901 | |
| 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 Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 920 | /** |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 921 | * 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 Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 924 | * |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 925 | * <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 Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 929 | * |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 930 | * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default), |
| 931 | * false if it is not. |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 932 | */ |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 933 | public void setUserVisibleHint(boolean isVisibleToUser) { |
| 934 | if (!mUserVisibleHint && isVisibleToUser && mState < STARTED) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 935 | mFragmentManager.performPendingDeferredStart(this); |
| 936 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 937 | mUserVisibleHint = isVisibleToUser; |
| 938 | mDeferStart = !isVisibleToUser; |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 942 | * @return The current value of the user-visible hint on this fragment. |
| 943 | * @see #setUserVisibleHint(boolean) |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 944 | */ |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 945 | public boolean getUserVisibleHint() { |
| 946 | return mUserVisibleHint; |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 950 | * Return the LoaderManager for this fragment, creating it if needed. |
| 951 | */ |
| 952 | public LoaderManager getLoaderManager() { |
| 953 | if (mLoaderManager != null) { |
| 954 | return mLoaderManager; |
| 955 | } |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 956 | if (mActivity == null) { |
| 957 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 958 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 959 | mCheckedForLoaderManager = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 960 | mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, true); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 961 | return mLoaderManager; |
| 962 | } |
| 963 | |
| 964 | /** |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 965 | * Call {@link Activity#startActivity(Intent)} on the fragment's |
| 966 | * containing Activity. |
| 967 | */ |
| 968 | public void startActivity(Intent intent) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 969 | if (mActivity == null) { |
| 970 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 971 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 972 | 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 Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 980 | if (mActivity == null) { |
| 981 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 982 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 983 | 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 Hackborn | 7187ccb | 2011-01-24 23:58:13 -0800 | [diff] [blame] | 1004 | * @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 Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1013 | * @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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1021 | * Called when a fragment is being created as part of a view layout |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1022 | * inflation, typically from setting the content view of an activity. This |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1023 | * may be called immediately after the fragment is created from a <fragment> |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1024 | * 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 Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1026 | * parse the attributes and save them away. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1027 | * |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1028 | * <p>This is called every time the fragment is inflated, even if it is |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1029 | * 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1043 | * |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1044 | * <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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1057 | * @param attrs The attributes at the tag where the fragment is |
| 1058 | * being created. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1059 | * @param savedInstanceState If the fragment is being re-created from |
| 1060 | * a previous saved state, this is the state. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1061 | */ |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1062 | public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { |
| 1063 | onInflate(attrs, savedInstanceState); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1064 | mCalled = true; |
| 1065 | } |
| 1066 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1067 | /** |
| 1068 | * Called when a fragment is first attached to its activity. |
| 1069 | * {@link #onCreate(Bundle)} will be called after this. |
| 1070 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1071 | public void onAttach(Activity activity) { |
| 1072 | mCalled = true; |
| 1073 | } |
| 1074 | |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1075 | /** |
| 1076 | * Called when a fragment loads an animation. |
| 1077 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1078 | public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1079 | return null; |
| 1080 | } |
| 1081 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1082 | /** |
| 1083 | * Called to do initial creation of a fragment. This is called after |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1084 | * {@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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1093 | * @param savedInstanceState If the fragment is being re-created from |
| 1094 | * a previous saved state, this is the state. |
| 1095 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1096 | public void onCreate(Bundle savedInstanceState) { |
| 1097 | mCalled = true; |
| 1098 | } |
| 1099 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1100 | /** |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1101 | * 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1114 | * 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 Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1117 | * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1118 | * |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1119 | * <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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1122 | * @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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1134 | return null; |
| 1135 | } |
| 1136 | |
Scott Main | 662cc7a4 | 2011-01-17 15:02:07 -0800 | [diff] [blame] | 1137 | /** |
| 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1143 | public View getView() { |
| 1144 | return mView; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1147 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1148 | * 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 Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1154 | * the new activity instance. This is called after {@link #onCreateView} |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1155 | * 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 Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1160 | public void onActivityCreated(Bundle savedInstanceState) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1161 | 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1169 | public void onStart() { |
| 1170 | mCalled = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1171 | |
| 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 Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1181 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1184 | /** |
| 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1190 | public void onResume() { |
| 1191 | mCalled = true; |
| 1192 | } |
| 1193 | |
Dianne Hackborn | 7277820 | 2010-08-20 18:26:01 -0700 | [diff] [blame] | 1194 | /** |
| 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 Miyakawa | 2f76176 | 2010-09-12 16:53:17 -0700 | [diff] [blame] | 1204 | * Activity.onSaveInstanceState(Bundle)} and most of the discussion there |
Dianne Hackborn | 7277820 | 2010-08-20 18:26:01 -0700 | [diff] [blame] | 1205 | * 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1213 | public void onSaveInstanceState(Bundle outState) { |
| 1214 | } |
| 1215 | |
| 1216 | public void onConfigurationChanged(Configuration newConfig) { |
| 1217 | mCalled = true; |
| 1218 | } |
| 1219 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1220 | /** |
| 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1225 | public void onPause() { |
| 1226 | mCalled = true; |
| 1227 | } |
| 1228 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1229 | /** |
| 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 Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1234 | public void onStop() { |
| 1235 | mCalled = true; |
| 1236 | } |
| 1237 | |
| 1238 | public void onLowMemory() { |
| 1239 | mCalled = true; |
| 1240 | } |
| 1241 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1242 | public void onTrimMemory(int level) { |
| 1243 | mCalled = true; |
| 1244 | } |
| 1245 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1246 | /** |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1247 | * 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 Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1250 | * 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 Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1254 | */ |
| 1255 | public void onDestroyView() { |
| 1256 | mCalled = true; |
| 1257 | } |
| 1258 | |
| 1259 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1260 | * Called when the fragment is no longer in use. This is called |
| 1261 | * after {@link #onStop()} and before {@link #onDetach()}. |
| 1262 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1263 | public void onDestroy() { |
| 1264 | mCalled = true; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1265 | //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager |
| 1266 | // + " mLoaderManager=" + mLoaderManager); |
| 1267 | if (!mCheckedForLoaderManager) { |
| 1268 | mCheckedForLoaderManager = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1269 | mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1270 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1271 | if (mLoaderManager != null) { |
| 1272 | mLoaderManager.doDestroy(); |
| 1273 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1274 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1275 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1276 | /** |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1277 | * 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 Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1293 | mActivity = null; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1294 | 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 Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1306 | * Called when the fragment is no longer attached to its activity. This |
| 1307 | * is called after {@link #onDestroy()}. |
| 1308 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1309 | public void onDetach() { |
| 1310 | mCalled = true; |
| 1311 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1312 | |
| 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 Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1316 | * to be called, you must have first called {@link #setHasOptionsMenu}. See |
| 1317 | * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu} |
| 1318 | * for more information. |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1319 | * |
| 1320 | * @param menu The options menu in which you place your items. |
| 1321 | * |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 1322 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1323 | * @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 Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 1340 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1341 | * @see #onCreateOptionsMenu |
| 1342 | */ |
| 1343 | public void onPrepareOptionsMenu(Menu menu) { |
| 1344 | } |
| 1345 | |
| 1346 | /** |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1347 | * 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 Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1357 | * 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 Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1387 | |
| 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 Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1454 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1455 | /** |
| 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 Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 1465 | writer.print(prefix); writer.print("mFragmentId=#"); |
| 1466 | writer.print(Integer.toHexString(mFragmentId)); |
| 1467 | writer.print(" mContainerId#="); |
| 1468 | writer.print(Integer.toHexString(mContainerId)); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1469 | 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 Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1475 | writer.print(" mRemoving="); writer.print(mRemoving); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1476 | 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 Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1480 | writer.print(" mDetached="); writer.print(mDetached); |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1481 | writer.print(" mMenuVisible="); writer.print(mMenuVisible); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1482 | writer.print(" mHasMenu="); writer.println(mHasMenu); |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1483 | writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance); |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1484 | writer.print(" mRetaining="); writer.print(mRetaining); |
| 1485 | writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1486 | if (mFragmentManager != null) { |
| 1487 | writer.print(prefix); writer.print("mFragmentManager="); |
| 1488 | writer.println(mFragmentManager); |
| 1489 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1490 | 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 Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1519 | 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 Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1524 | if (mLoaderManager != null) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 1525 | writer.print(prefix); writer.println("Loader Manager:"); |
| 1526 | mLoaderManager.dump(prefix + " ", fd, writer, args); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1530 | void performStart() { |
| 1531 | onStart(); |
| 1532 | if (mLoaderManager != null) { |
| 1533 | mLoaderManager.doReportStart(); |
| 1534 | } |
| 1535 | } |
| 1536 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1537 | void performStop() { |
| 1538 | onStop(); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1539 | |
| 1540 | if (mLoadersStarted) { |
| 1541 | mLoadersStarted = false; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1542 | if (!mCheckedForLoaderManager) { |
| 1543 | mCheckedForLoaderManager = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1544 | mLoaderManager = mActivity.getLoaderManager(mIndex, mLoadersStarted, false); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1545 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1546 | if (mLoaderManager != null) { |
| 1547 | if (mActivity == null || !mActivity.mChangingConfigurations) { |
| 1548 | mLoaderManager.doStop(); |
| 1549 | } else { |
| 1550 | mLoaderManager.doRetain(); |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1555 | |
| 1556 | void performDestroyView() { |
| 1557 | onDestroyView(); |
| 1558 | if (mLoaderManager != null) { |
| 1559 | mLoaderManager.doReportNextStart(); |
| 1560 | } |
| 1561 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1562 | } |