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 | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 29 | import android.util.ArrayMap; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 31 | import android.util.DebugUtils; |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 32 | import android.util.Log; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 33 | import android.util.SparseArray; |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 34 | import android.util.SuperNotCalledException; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 35 | import android.view.ContextMenu; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 36 | import android.view.ContextMenu.ContextMenuInfo; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 37 | import android.view.LayoutInflater; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 38 | import android.view.Menu; |
| 39 | import android.view.MenuInflater; |
| 40 | import android.view.MenuItem; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 41 | import android.view.View; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 42 | import android.view.View.OnCreateContextMenuListener; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 43 | import android.view.ViewGroup; |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 44 | import android.widget.AdapterView; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 45 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 46 | import java.io.FileDescriptor; |
| 47 | import java.io.PrintWriter; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 48 | |
| 49 | final class FragmentState implements Parcelable { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 50 | final String mClassName; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 51 | final int mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 52 | final boolean mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 53 | final int mFragmentId; |
| 54 | final int mContainerId; |
| 55 | final String mTag; |
| 56 | final boolean mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 57 | final boolean mDetached; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 58 | final Bundle mArguments; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 59 | |
| 60 | Bundle mSavedFragmentState; |
| 61 | |
| 62 | Fragment mInstance; |
| 63 | |
| 64 | public FragmentState(Fragment frag) { |
| 65 | mClassName = frag.getClass().getName(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 66 | mIndex = frag.mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 67 | mFromLayout = frag.mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 68 | mFragmentId = frag.mFragmentId; |
| 69 | mContainerId = frag.mContainerId; |
| 70 | mTag = frag.mTag; |
| 71 | mRetainInstance = frag.mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 72 | mDetached = frag.mDetached; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 73 | mArguments = frag.mArguments; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public FragmentState(Parcel in) { |
| 77 | mClassName = in.readString(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 78 | mIndex = in.readInt(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 79 | mFromLayout = in.readInt() != 0; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 80 | mFragmentId = in.readInt(); |
| 81 | mContainerId = in.readInt(); |
| 82 | mTag = in.readString(); |
| 83 | mRetainInstance = in.readInt() != 0; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 84 | mDetached = in.readInt() != 0; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 85 | mArguments = in.readBundle(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 86 | mSavedFragmentState = in.readBundle(); |
| 87 | } |
| 88 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 89 | public Fragment instantiate(Activity activity, Fragment parent) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 90 | if (mInstance != null) { |
| 91 | return mInstance; |
| 92 | } |
| 93 | |
Dianne Hackborn | 5164246 | 2010-10-28 10:32:37 -0700 | [diff] [blame] | 94 | if (mArguments != null) { |
| 95 | mArguments.setClassLoader(activity.getClassLoader()); |
| 96 | } |
| 97 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 98 | mInstance = Fragment.instantiate(activity, mClassName, mArguments); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 99 | |
| 100 | if (mSavedFragmentState != null) { |
| 101 | mSavedFragmentState.setClassLoader(activity.getClassLoader()); |
| 102 | mInstance.mSavedFragmentState = mSavedFragmentState; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 103 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 104 | mInstance.setIndex(mIndex, parent); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 105 | mInstance.mFromLayout = mFromLayout; |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 106 | mInstance.mRestored = true; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 107 | mInstance.mFragmentId = mFragmentId; |
| 108 | mInstance.mContainerId = mContainerId; |
| 109 | mInstance.mTag = mTag; |
| 110 | mInstance.mRetainInstance = mRetainInstance; |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 111 | mInstance.mDetached = mDetached; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 112 | mInstance.mFragmentManager = activity.mFragments; |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 113 | if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG, |
| 114 | "Instantiated fragment " + mInstance); |
| 115 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 116 | return mInstance; |
| 117 | } |
| 118 | |
| 119 | public int describeContents() { |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | public void writeToParcel(Parcel dest, int flags) { |
| 124 | dest.writeString(mClassName); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 125 | dest.writeInt(mIndex); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 126 | dest.writeInt(mFromLayout ? 1 : 0); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 127 | dest.writeInt(mFragmentId); |
| 128 | dest.writeInt(mContainerId); |
| 129 | dest.writeString(mTag); |
| 130 | dest.writeInt(mRetainInstance ? 1 : 0); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 131 | dest.writeInt(mDetached ? 1 : 0); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 132 | dest.writeBundle(mArguments); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 133 | dest.writeBundle(mSavedFragmentState); |
| 134 | } |
| 135 | |
| 136 | public static final Parcelable.Creator<FragmentState> CREATOR |
| 137 | = new Parcelable.Creator<FragmentState>() { |
| 138 | public FragmentState createFromParcel(Parcel in) { |
| 139 | return new FragmentState(in); |
| 140 | } |
| 141 | |
| 142 | public FragmentState[] newArray(int size) { |
| 143 | return new FragmentState[size]; |
| 144 | } |
| 145 | }; |
| 146 | } |
| 147 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 148 | /** |
| 149 | * 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] | 150 | * that can be placed in an {@link Activity}. Interaction with fragments |
| 151 | * is done through {@link FragmentManager}, which can be obtained via |
| 152 | * {@link Activity#getFragmentManager() Activity.getFragmentManager()} and |
| 153 | * {@link Fragment#getFragmentManager() Fragment.getFragmentManager()}. |
| 154 | * |
| 155 | * <p>The Fragment class can be used many ways to achieve a wide variety of |
felipeal | 1b4e4b6 | 2012-02-27 12:09:13 -0800 | [diff] [blame] | 156 | * results. In its core, it represents a particular operation or interface |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 157 | * that is running within a larger {@link Activity}. A Fragment is closely |
| 158 | * tied to the Activity it is in, and can not be used apart from one. Though |
| 159 | * Fragment defines its own lifecycle, that lifecycle is dependent on its |
| 160 | * activity: if the activity is stopped, no fragments inside of it can be |
| 161 | * started; when the activity is destroyed, all fragments will be destroyed. |
| 162 | * |
| 163 | * <p>All subclasses of Fragment must include a public empty constructor. |
| 164 | * The framework will often re-instantiate a fragment class when needed, |
| 165 | * in particular during state restore, and needs to be able to find this |
| 166 | * constructor to instantiate it. If the empty constructor is not available, |
| 167 | * a runtime exception will occur in some cases during state restore. |
| 168 | * |
| 169 | * <p>Topics covered here: |
| 170 | * <ol> |
Dianne Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 171 | * <li><a href="#OlderPlatforms">Older Platforms</a> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 172 | * <li><a href="#Lifecycle">Lifecycle</a> |
| 173 | * <li><a href="#Layout">Layout</a> |
| 174 | * <li><a href="#BackStack">Back Stack</a> |
| 175 | * </ol> |
| 176 | * |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 177 | * <div class="special reference"> |
| 178 | * <h3>Developer Guides</h3> |
| 179 | * <p>For more information about using fragments, read the |
| 180 | * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p> |
| 181 | * </div> |
| 182 | * |
Dianne Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 183 | * <a name="OlderPlatforms"></a> |
| 184 | * <h3>Older Platforms</h3> |
| 185 | * |
| 186 | * While the Fragment API was introduced in |
| 187 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 188 | * at is also available for use on older platforms through |
| 189 | * {@link android.support.v4.app.FragmentActivity}. See the blog post |
Dianne Hackborn | f9dd34f | 2011-04-19 18:44:03 -0700 | [diff] [blame] | 190 | * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html"> |
| 191 | * Fragments For All</a> for more details. |
| 192 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 193 | * <a name="Lifecycle"></a> |
| 194 | * <h3>Lifecycle</h3> |
| 195 | * |
| 196 | * <p>Though a Fragment's lifecycle is tied to its owning activity, it has |
| 197 | * its own wrinkle on the standard activity lifecycle. It includes basic |
| 198 | * activity lifecycle methods such as {@link #onResume}, but also important |
| 199 | * are methods related to interactions with the activity and UI generation. |
| 200 | * |
| 201 | * <p>The core series of lifecycle methods that are called to bring a fragment |
| 202 | * up to resumed state (interacting with the user) are: |
| 203 | * |
| 204 | * <ol> |
| 205 | * <li> {@link #onAttach} called once the fragment is associated with its activity. |
| 206 | * <li> {@link #onCreate} called to do initial creation of the fragment. |
| 207 | * <li> {@link #onCreateView} creates and returns the view hierarchy associated |
| 208 | * with the fragment. |
| 209 | * <li> {@link #onActivityCreated} tells the fragment that its activity has |
Quddus Chong | 3790001 | 2012-04-11 11:57:50 -0700 | [diff] [blame] | 210 | * completed its own {@link Activity#onCreate Activity.onCreate()}. |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 211 | * <li> {@link #onViewStateRestored} tells the fragment that all of the saved |
| 212 | * state of its view hierarchy has been restored. |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 213 | * <li> {@link #onStart} makes the fragment visible to the user (based on its |
| 214 | * containing activity being started). |
| 215 | * <li> {@link #onResume} makes the fragment interacting with the user (based on its |
| 216 | * containing activity being resumed). |
| 217 | * </ol> |
| 218 | * |
| 219 | * <p>As a fragment is no longer being used, it goes through a reverse |
| 220 | * series of callbacks: |
| 221 | * |
| 222 | * <ol> |
| 223 | * <li> {@link #onPause} fragment is no longer interacting with the user either |
| 224 | * because its activity is being paused or a fragment operation is modifying it |
| 225 | * in the activity. |
| 226 | * <li> {@link #onStop} fragment is no longer visible to the user either |
| 227 | * because its activity is being stopped or a fragment operation is modifying it |
| 228 | * in the activity. |
| 229 | * <li> {@link #onDestroyView} allows the fragment to clean up resources |
| 230 | * associated with its View. |
| 231 | * <li> {@link #onDestroy} called to do final cleanup of the fragment's state. |
| 232 | * <li> {@link #onDetach} called immediately prior to the fragment no longer |
| 233 | * being associated with its activity. |
| 234 | * </ol> |
| 235 | * |
| 236 | * <a name="Layout"></a> |
| 237 | * <h3>Layout</h3> |
| 238 | * |
| 239 | * <p>Fragments can be used as part of your application's layout, allowing |
| 240 | * you to better modularize your code and more easily adjust your user |
| 241 | * interface to the screen it is running on. As an example, we can look |
| 242 | * at a simple program consisting of a list of items, and display of the |
| 243 | * details of each item.</p> |
| 244 | * |
| 245 | * <p>An activity's layout XML can include <code><fragment></code> tags |
| 246 | * to embed fragment instances inside of the layout. For example, here is |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 247 | * a simple layout that embeds one fragment:</p> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 248 | * |
| 249 | * {@sample development/samples/ApiDemos/res/layout/fragment_layout.xml layout} |
| 250 | * |
| 251 | * <p>The layout is installed in the activity in the normal way:</p> |
| 252 | * |
| 253 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 254 | * main} |
| 255 | * |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 256 | * <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] | 257 | * on {@link ListFragment} for most of its work. Note the implementation of |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 258 | * clicking an item: depending on the current activity's layout, it can either |
| 259 | * 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] | 260 | * this later), or start a new activity to show the details.</p> |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 261 | * |
| 262 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 263 | * titles} |
| 264 | * |
Ben Dodson | 542f240 | 2011-06-14 16:40:23 -0700 | [diff] [blame] | 265 | * <p>The details fragment showing the contents of a selected item just |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 266 | * displays a string of text based on an index of a string array built in to |
| 267 | * the app:</p> |
| 268 | * |
| 269 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 270 | * details} |
| 271 | * |
| 272 | * <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] | 273 | * 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] | 274 | * launch a new activity to display the details fragment:</p> |
| 275 | * |
| 276 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java |
| 277 | * details_activity} |
| 278 | * |
| 279 | * <p>However the screen may be large enough to show both the list of titles |
| 280 | * and details about the currently selected title. To use such a layout on |
| 281 | * a landscape screen, this alternative layout can be placed under layout-land:</p> |
| 282 | * |
| 283 | * {@sample development/samples/ApiDemos/res/layout-land/fragment_layout.xml layout} |
| 284 | * |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 285 | * <p>Note how the prior code will adjust to this alternative UI flow: the titles |
| 286 | * fragment will now embed the details fragment inside of this activity, and the |
| 287 | * details activity will finish itself if it is running in a configuration |
| 288 | * where the details can be shown in-place. |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 289 | * |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 290 | * <p>When a configuration change causes the activity hosting these fragments |
| 291 | * to restart, its new instance may use a different layout that doesn't |
| 292 | * include the same fragments as the previous layout. In this case all of |
| 293 | * the previous fragments will still be instantiated and running in the new |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 294 | * instance. However, any that are no longer associated with a <fragment> |
| 295 | * tag in the view hierarchy will not have their content view created |
| 296 | * and will return false from {@link #isInLayout}. (The code here also shows |
| 297 | * how you can determine if a fragment placed in a container is no longer |
| 298 | * running in a layout with that container and avoid creating its view hierarchy |
| 299 | * in that case.) |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 300 | * |
| 301 | * <p>The attributes of the <fragment> tag are used to control the |
Dianne Hackborn | 87121ac | 2011-01-04 17:26:23 -0800 | [diff] [blame] | 302 | * LayoutParams provided when attaching the fragment's view to the parent |
| 303 | * container. They can also be parsed by the fragment in {@link #onInflate} |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 304 | * as parameters. |
| 305 | * |
| 306 | * <p>The fragment being instantiated must have some kind of unique identifier |
| 307 | * so that it can be re-associated with a previous instance if the parent |
| 308 | * activity needs to be destroyed and recreated. This can be provided these |
| 309 | * ways: |
| 310 | * |
| 311 | * <ul> |
| 312 | * <li>If nothing is explicitly supplied, the view ID of the container will |
| 313 | * be used. |
| 314 | * <li><code>android:tag</code> can be used in <fragment> to provide |
| 315 | * a specific tag name for the fragment. |
| 316 | * <li><code>android:id</code> can be used in <fragment> to provide |
| 317 | * a specific identifier for the fragment. |
| 318 | * </ul> |
| 319 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 320 | * <a name="BackStack"></a> |
| 321 | * <h3>Back Stack</h3> |
| 322 | * |
| 323 | * <p>The transaction in which fragments are modified can be placed on an |
| 324 | * internal back-stack of the owning activity. When the user presses back |
| 325 | * in the activity, any transactions on the back stack are popped off before |
| 326 | * the activity itself is finished. |
| 327 | * |
| 328 | * <p>For example, consider this simple fragment that is instantiated with |
| 329 | * an integer argument and displays that in a TextView in its UI:</p> |
| 330 | * |
| 331 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java |
| 332 | * fragment} |
| 333 | * |
| 334 | * <p>A function that creates a new instance of the fragment, replacing |
| 335 | * whatever current fragment instance is being shown and pushing that change |
| 336 | * on to the back stack could be written as: |
| 337 | * |
| 338 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java |
| 339 | * add_stack} |
| 340 | * |
| 341 | * <p>After each call to this function, a new entry is on the stack, and |
| 342 | * pressing back will pop it to return the user to whatever previous state |
| 343 | * the activity UI was in. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 344 | */ |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 345 | public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 346 | private static final ArrayMap<String, Class<?>> sClassMap = |
| 347 | new ArrayMap<String, Class<?>>(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 348 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 349 | 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] | 350 | static final int INITIALIZING = 0; // Not yet created. |
| 351 | static final int CREATED = 1; // Created. |
| 352 | static final int ACTIVITY_CREATED = 2; // The activity has finished its creation. |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 353 | static final int STOPPED = 3; // Fully created, not started. |
| 354 | static final int STARTED = 4; // Created and started, not resumed. |
| 355 | static final int RESUMED = 5; // Created started and resumed. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 356 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 357 | int mState = INITIALIZING; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 358 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 359 | // Non-null if the fragment's view hierarchy is currently animating away, |
| 360 | // meaning we need to wait a bit on completely destroying it. This is the |
| 361 | // animation that is running. |
| 362 | Animator mAnimatingAway; |
| 363 | |
| 364 | // If mAnimatingAway != null, this is the state we should move to once the |
| 365 | // animation is done. |
| 366 | int mStateAfterAnimating; |
| 367 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 368 | // When instantiated from saved state, this is the saved state. |
| 369 | Bundle mSavedFragmentState; |
| 370 | SparseArray<Parcelable> mSavedViewState; |
| 371 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 372 | // Index into active fragment array. |
| 373 | int mIndex = -1; |
| 374 | |
| 375 | // Internal unique name for this fragment; |
| 376 | String mWho; |
| 377 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 378 | // Construction arguments; |
| 379 | Bundle mArguments; |
| 380 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 381 | // Target fragment. |
| 382 | Fragment mTarget; |
| 383 | |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 384 | // For use when retaining a fragment: this is the index of the last mTarget. |
| 385 | int mTargetIndex = -1; |
| 386 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 387 | // Target request code. |
| 388 | int mTargetRequestCode; |
| 389 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 390 | // True if the fragment is in the list of added fragments. |
| 391 | boolean mAdded; |
| 392 | |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 393 | // If set this fragment is being removed from its activity. |
| 394 | boolean mRemoving; |
| 395 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 396 | // True if the fragment is in the resumed state. |
| 397 | boolean mResumed; |
| 398 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 399 | // Set to true if this fragment was instantiated from a layout file. |
| 400 | boolean mFromLayout; |
| 401 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 402 | // Set to true when the view has actually been inflated in its layout. |
| 403 | boolean mInLayout; |
| 404 | |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 405 | // True if this fragment has been restored from previously saved state. |
| 406 | boolean mRestored; |
| 407 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 408 | // Number of active back stack entries this fragment is in. |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 409 | int mBackStackNesting; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 410 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 411 | // The fragment manager we are associated with. Set as soon as the |
| 412 | // fragment is used in a transaction; cleared after it has been removed |
| 413 | // from all transactions. |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 414 | FragmentManagerImpl mFragmentManager; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 415 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 416 | // Activity this fragment is attached to. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 417 | Activity mActivity; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 418 | |
| 419 | // Private fragment manager for child fragments inside of this one. |
| 420 | FragmentManagerImpl mChildFragmentManager; |
| 421 | |
| 422 | // If this Fragment is contained in another Fragment, this is that container. |
| 423 | Fragment mParentFragment; |
| 424 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 425 | // The optional identifier for this fragment -- either the container ID if it |
| 426 | // was dynamically added to the view hierarchy, or the ID supplied in |
| 427 | // layout. |
| 428 | int mFragmentId; |
| 429 | |
| 430 | // When a fragment is being dynamically added to the view hierarchy, this |
| 431 | // is the identifier of the parent container it is being added to. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 432 | int mContainerId; |
| 433 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 434 | // The optional named tag for this fragment -- usually used to find |
| 435 | // fragments that are not part of the layout. |
| 436 | String mTag; |
| 437 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 438 | // Set to true when the app has requested that this fragment be hidden |
| 439 | // from the user. |
| 440 | boolean mHidden; |
| 441 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 442 | // Set to true when the app has requested that this fragment be detached. |
| 443 | boolean mDetached; |
| 444 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 445 | // If set this fragment would like its instance retained across |
| 446 | // configuration changes. |
| 447 | boolean mRetainInstance; |
| 448 | |
| 449 | // If set this fragment is being retained across the current config change. |
| 450 | boolean mRetaining; |
| 451 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 452 | // If set this fragment has menu items to contribute. |
| 453 | boolean mHasMenu; |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 454 | |
| 455 | // Set to true to allow the fragment's menu to be shown. |
| 456 | boolean mMenuVisible = true; |
| 457 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 458 | // Used to verify that subclasses call through to super class. |
| 459 | boolean mCalled; |
| 460 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 461 | // If app has requested a specific animation, this is the one to use. |
| 462 | int mNextAnim; |
| 463 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 464 | // The parent container of the fragment after dynamically added to UI. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 465 | ViewGroup mContainer; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 466 | |
| 467 | // The View generated for this fragment. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 468 | View mView; |
| 469 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 470 | // Whether this fragment should defer starting until after other fragments |
| 471 | // have been started and their loaders are finished. |
| 472 | boolean mDeferStart; |
| 473 | |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 474 | // Hint provided by the app that this fragment is currently visible to the user. |
| 475 | boolean mUserVisibleHint = true; |
| 476 | |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 477 | LoaderManagerImpl mLoaderManager; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 478 | boolean mLoadersStarted; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 479 | boolean mCheckedForLoaderManager; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 480 | |
| 481 | /** |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 482 | * State information that has been retrieved from a fragment instance |
| 483 | * through {@link FragmentManager#saveFragmentInstanceState(Fragment) |
| 484 | * FragmentManager.saveFragmentInstanceState}. |
| 485 | */ |
| 486 | public static class SavedState implements Parcelable { |
| 487 | final Bundle mState; |
| 488 | |
| 489 | SavedState(Bundle state) { |
| 490 | mState = state; |
| 491 | } |
| 492 | |
| 493 | SavedState(Parcel in, ClassLoader loader) { |
| 494 | mState = in.readBundle(); |
| 495 | if (loader != null && mState != null) { |
| 496 | mState.setClassLoader(loader); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public int describeContents() { |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void writeToParcel(Parcel dest, int flags) { |
| 507 | dest.writeBundle(mState); |
| 508 | } |
| 509 | |
| 510 | public static final Parcelable.ClassLoaderCreator<SavedState> CREATOR |
| 511 | = new Parcelable.ClassLoaderCreator<SavedState>() { |
| 512 | public SavedState createFromParcel(Parcel in) { |
| 513 | return new SavedState(in, null); |
| 514 | } |
| 515 | |
| 516 | public SavedState createFromParcel(Parcel in, ClassLoader loader) { |
| 517 | return new SavedState(in, loader); |
| 518 | } |
| 519 | |
| 520 | public SavedState[] newArray(int size) { |
| 521 | return new SavedState[size]; |
| 522 | } |
| 523 | }; |
| 524 | } |
| 525 | |
| 526 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 527 | * Thrown by {@link Fragment#instantiate(Context, String, Bundle)} when |
| 528 | * there is an instantiation failure. |
| 529 | */ |
| 530 | static public class InstantiationException extends AndroidRuntimeException { |
| 531 | public InstantiationException(String msg, Exception cause) { |
| 532 | super(msg, cause); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 537 | * Default constructor. <strong>Every</strong> fragment must have an |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 538 | * empty constructor, so it can be instantiated when restoring its |
| 539 | * activity's state. It is strongly recommended that subclasses do not |
| 540 | * have other constructors with parameters, since these constructors |
| 541 | * will not be called when the fragment is re-instantiated; instead, |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 542 | * arguments can be supplied by the caller with {@link #setArguments} |
| 543 | * and later retrieved by the Fragment with {@link #getArguments}. |
| 544 | * |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 545 | * <p>Applications should generally not implement a constructor. The |
| 546 | * first place application code an run where the fragment is ready to |
| 547 | * be used is in {@link #onAttach(Activity)}, the point where the fragment |
| 548 | * is actually associated with its activity. Some applications may also |
| 549 | * want to implement {@link #onInflate} to retrieve attributes from a |
| 550 | * layout resource, though should take care here because this happens for |
| 551 | * the fragment is attached to its activity. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 552 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 553 | public Fragment() { |
| 554 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 555 | |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 556 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 557 | * Like {@link #instantiate(Context, String, Bundle)} but with a null |
| 558 | * argument Bundle. |
| 559 | */ |
| 560 | public static Fragment instantiate(Context context, String fname) { |
| 561 | return instantiate(context, fname, null); |
| 562 | } |
| 563 | |
| 564 | /** |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 565 | * Create a new instance of a Fragment with the given class name. This is |
| 566 | * the same as calling its empty constructor. |
| 567 | * |
| 568 | * @param context The calling context being used to instantiate the fragment. |
| 569 | * This is currently just used to get its ClassLoader. |
| 570 | * @param fname The class name of the fragment to instantiate. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 571 | * @param args Bundle of arguments to supply to the fragment, which it |
| 572 | * can retrieve with {@link #getArguments()}. May be null. |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 573 | * @return Returns a new fragment instance. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 574 | * @throws InstantiationException If there is a failure in instantiating |
| 575 | * the given fragment class. This is a runtime exception; it is not |
| 576 | * normally expected to happen. |
Dianne Hackborn | b1ad597 | 2010-08-02 17:30:33 -0700 | [diff] [blame] | 577 | */ |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 578 | public static Fragment instantiate(Context context, String fname, Bundle args) { |
| 579 | try { |
| 580 | Class<?> clazz = sClassMap.get(fname); |
| 581 | if (clazz == null) { |
| 582 | // Class not found in the cache, see if it's real, and try to add it |
| 583 | clazz = context.getClassLoader().loadClass(fname); |
Amith Yamasani | 364ed4d | 2013-07-26 13:37:56 -0700 | [diff] [blame] | 584 | if (!Fragment.class.isAssignableFrom(clazz)) { |
| 585 | throw new InstantiationException("Trying to instantiate a class " + fname |
| 586 | + " that is not a Fragment", new ClassCastException()); |
| 587 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 588 | sClassMap.put(fname, clazz); |
| 589 | } |
| 590 | Fragment f = (Fragment)clazz.newInstance(); |
| 591 | if (args != null) { |
| 592 | args.setClassLoader(f.getClass().getClassLoader()); |
| 593 | f.mArguments = args; |
| 594 | } |
| 595 | return f; |
| 596 | } catch (ClassNotFoundException e) { |
| 597 | throw new InstantiationException("Unable to instantiate fragment " + fname |
| 598 | + ": make sure class name exists, is public, and has an" |
| 599 | + " empty constructor that is public", e); |
| 600 | } catch (java.lang.InstantiationException e) { |
| 601 | throw new InstantiationException("Unable to instantiate fragment " + fname |
| 602 | + ": make sure class name exists, is public, and has an" |
| 603 | + " empty constructor that is public", e); |
| 604 | } catch (IllegalAccessException e) { |
| 605 | throw new InstantiationException("Unable to instantiate fragment " + fname |
| 606 | + ": make sure class name exists, is public, and has an" |
| 607 | + " empty constructor that is public", e); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 608 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 609 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 610 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 611 | final void restoreViewState(Bundle savedInstanceState) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 612 | if (mSavedViewState != null) { |
| 613 | mView.restoreHierarchyState(mSavedViewState); |
| 614 | mSavedViewState = null; |
| 615 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 616 | mCalled = false; |
| 617 | onViewStateRestored(savedInstanceState); |
| 618 | if (!mCalled) { |
| 619 | throw new SuperNotCalledException("Fragment " + this |
| 620 | + " did not call through to super.onViewStateRestored()"); |
| 621 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 622 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 623 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 624 | final void setIndex(int index, Fragment parent) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 625 | mIndex = index; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 626 | if (parent != null) { |
| 627 | mWho = parent.mWho + ":" + mIndex; |
| 628 | } else { |
| 629 | mWho = "android:fragment:" + mIndex; |
| 630 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 633 | final boolean isInBackStack() { |
| 634 | return mBackStackNesting > 0; |
| 635 | } |
| 636 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 637 | /** |
| 638 | * Subclasses can not override equals(). |
| 639 | */ |
| 640 | @Override final public boolean equals(Object o) { |
| 641 | return super.equals(o); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Subclasses can not override hashCode(). |
| 646 | */ |
| 647 | @Override final public int hashCode() { |
| 648 | return super.hashCode(); |
| 649 | } |
| 650 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 651 | @Override |
| 652 | public String toString() { |
| 653 | StringBuilder sb = new StringBuilder(128); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 654 | DebugUtils.buildShortClassTag(this, sb); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 655 | if (mIndex >= 0) { |
| 656 | sb.append(" #"); |
| 657 | sb.append(mIndex); |
| 658 | } |
| 659 | if (mFragmentId != 0) { |
| 660 | sb.append(" id=0x"); |
| 661 | sb.append(Integer.toHexString(mFragmentId)); |
| 662 | } |
| 663 | if (mTag != null) { |
| 664 | sb.append(" "); |
| 665 | sb.append(mTag); |
| 666 | } |
| 667 | sb.append('}'); |
| 668 | return sb.toString(); |
| 669 | } |
| 670 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 671 | /** |
| 672 | * Return the identifier this fragment is known by. This is either |
| 673 | * the android:id value supplied in a layout or the container view ID |
| 674 | * supplied when adding the fragment. |
| 675 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 676 | final public int getId() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 677 | return mFragmentId; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Get the tag name of the fragment, if specified. |
| 682 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 683 | final public String getTag() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 684 | return mTag; |
| 685 | } |
| 686 | |
| 687 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 688 | * Supply the construction arguments for this fragment. This can only |
| 689 | * be called before the fragment has been attached to its activity; that |
| 690 | * is, you should call it immediately after constructing the fragment. The |
| 691 | * arguments supplied here will be retained across fragment destroy and |
| 692 | * creation. |
| 693 | */ |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 694 | public void setArguments(Bundle args) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 695 | if (mIndex >= 0) { |
| 696 | throw new IllegalStateException("Fragment already active"); |
| 697 | } |
| 698 | mArguments = args; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Return the arguments supplied when the fragment was instantiated, |
| 703 | * if any. |
| 704 | */ |
| 705 | final public Bundle getArguments() { |
| 706 | return mArguments; |
| 707 | } |
| 708 | |
| 709 | /** |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 710 | * Set the initial saved state that this Fragment should restore itself |
| 711 | * from when first being constructed, as returned by |
| 712 | * {@link FragmentManager#saveFragmentInstanceState(Fragment) |
| 713 | * FragmentManager.saveFragmentInstanceState}. |
| 714 | * |
| 715 | * @param state The state the fragment should be restored from. |
| 716 | */ |
| 717 | public void setInitialSavedState(SavedState state) { |
| 718 | if (mIndex >= 0) { |
| 719 | throw new IllegalStateException("Fragment already active"); |
| 720 | } |
| 721 | mSavedFragmentState = state != null && state.mState != null |
| 722 | ? state.mState : null; |
| 723 | } |
| 724 | |
| 725 | /** |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 726 | * Optional target for this fragment. This may be used, for example, |
| 727 | * if this fragment is being started by another, and when done wants to |
| 728 | * give a result back to the first. The target set here is retained |
| 729 | * across instances via {@link FragmentManager#putFragment |
| 730 | * FragmentManager.putFragment()}. |
| 731 | * |
| 732 | * @param fragment The fragment that is the target of this one. |
| 733 | * @param requestCode Optional request code, for convenience if you |
| 734 | * are going to call back with {@link #onActivityResult(int, int, Intent)}. |
| 735 | */ |
| 736 | public void setTargetFragment(Fragment fragment, int requestCode) { |
| 737 | mTarget = fragment; |
| 738 | mTargetRequestCode = requestCode; |
| 739 | } |
| 740 | |
| 741 | /** |
Dianne Hackborn | 3f00be5 | 2010-08-15 18:03:27 -0700 | [diff] [blame] | 742 | * Return the target fragment set by {@link #setTargetFragment}. |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 743 | */ |
| 744 | final public Fragment getTargetFragment() { |
| 745 | return mTarget; |
| 746 | } |
| 747 | |
| 748 | /** |
Dianne Hackborn | 3f00be5 | 2010-08-15 18:03:27 -0700 | [diff] [blame] | 749 | * Return the target request code set by {@link #setTargetFragment}. |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 750 | */ |
| 751 | final public int getTargetRequestCode() { |
| 752 | return mTargetRequestCode; |
| 753 | } |
| 754 | |
| 755 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 756 | * Return the Activity this fragment is currently associated with. |
| 757 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 758 | final public Activity getActivity() { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 759 | return mActivity; |
| 760 | } |
| 761 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 762 | /** |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 763 | * Return <code>getActivity().getResources()</code>. |
| 764 | */ |
| 765 | final public Resources getResources() { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 766 | if (mActivity == null) { |
| 767 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 768 | } |
Dianne Hackborn | bdd19bc | 2010-11-10 23:27:54 -0800 | [diff] [blame] | 769 | return mActivity.getResources(); |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Return a localized, styled CharSequence from the application's package's |
| 774 | * default string table. |
| 775 | * |
| 776 | * @param resId Resource id for the CharSequence text |
| 777 | */ |
| 778 | public final CharSequence getText(int resId) { |
| 779 | return getResources().getText(resId); |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Return a localized string from the application's package's |
| 784 | * default string table. |
| 785 | * |
| 786 | * @param resId Resource id for the string |
| 787 | */ |
| 788 | public final String getString(int resId) { |
| 789 | return getResources().getString(resId); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Return a localized formatted string from the application's package's |
| 794 | * default string table, substituting the format arguments as defined in |
| 795 | * {@link java.util.Formatter} and {@link java.lang.String#format}. |
| 796 | * |
| 797 | * @param resId Resource id for the format string |
| 798 | * @param formatArgs The format arguments that will be used for substitution. |
| 799 | */ |
| 800 | |
| 801 | public final String getString(int resId, Object... formatArgs) { |
| 802 | return getResources().getString(resId, formatArgs); |
| 803 | } |
| 804 | |
| 805 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 806 | * Return the FragmentManager for interacting with fragments associated |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 807 | * 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] | 808 | * before {@link #getActivity()}, during the time from when the fragment is |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 809 | * placed in a {@link FragmentTransaction} until it is committed and |
| 810 | * attached to its activity. |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 811 | * |
| 812 | * <p>If this Fragment is a child of another Fragment, the FragmentManager |
| 813 | * returned here will be the parent's {@link #getChildFragmentManager()}. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 814 | */ |
| 815 | final public FragmentManager getFragmentManager() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 816 | return mFragmentManager; |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /** |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 820 | * Return a private FragmentManager for placing and managing Fragments |
| 821 | * inside of this Fragment. |
| 822 | */ |
| 823 | final public FragmentManager getChildFragmentManager() { |
| 824 | if (mChildFragmentManager == null) { |
| 825 | instantiateChildFragmentManager(); |
| 826 | if (mState >= RESUMED) { |
| 827 | mChildFragmentManager.dispatchResume(); |
| 828 | } else if (mState >= STARTED) { |
| 829 | mChildFragmentManager.dispatchStart(); |
| 830 | } else if (mState >= ACTIVITY_CREATED) { |
| 831 | mChildFragmentManager.dispatchActivityCreated(); |
| 832 | } else if (mState >= CREATED) { |
| 833 | mChildFragmentManager.dispatchCreate(); |
| 834 | } |
| 835 | } |
| 836 | return mChildFragmentManager; |
| 837 | } |
| 838 | |
| 839 | /** |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 840 | * Returns the parent Fragment containing this Fragment. If this Fragment |
| 841 | * is attached directly to an Activity, returns null. |
| 842 | */ |
| 843 | final public Fragment getParentFragment() { |
| 844 | return mParentFragment; |
| 845 | } |
| 846 | |
| 847 | /** |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 848 | * Return true if the fragment is currently added to its activity. |
| 849 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 850 | final public boolean isAdded() { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 851 | return mActivity != null && mAdded; |
| 852 | } |
| 853 | |
| 854 | /** |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 855 | * Return true if the fragment has been explicitly detached from the UI. |
| 856 | * That is, {@link FragmentTransaction#detach(Fragment) |
| 857 | * FragmentTransaction.detach(Fragment)} has been used on it. |
| 858 | */ |
| 859 | final public boolean isDetached() { |
| 860 | return mDetached; |
| 861 | } |
| 862 | |
| 863 | /** |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 864 | * Return true if this fragment is currently being removed from its |
| 865 | * activity. This is <em>not</em> whether its activity is finishing, but |
| 866 | * rather whether it is in the process of being removed from its activity. |
| 867 | */ |
| 868 | final public boolean isRemoving() { |
| 869 | return mRemoving; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 873 | * Return true if the layout is included as part of an activity view |
| 874 | * hierarchy via the <fragment> tag. This will always be true when |
| 875 | * fragments are created through the <fragment> tag, <em>except</em> |
| 876 | * in the case where an old fragment is restored from a previous state and |
| 877 | * it does not appear in the layout of the current state. |
| 878 | */ |
| 879 | final public boolean isInLayout() { |
| 880 | return mInLayout; |
| 881 | } |
| 882 | |
| 883 | /** |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 884 | * Return true if the fragment is in the resumed state. This is true |
| 885 | * for the duration of {@link #onResume()} and {@link #onPause()} as well. |
| 886 | */ |
| 887 | final public boolean isResumed() { |
| 888 | return mResumed; |
| 889 | } |
| 890 | |
| 891 | /** |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 892 | * Return true if the fragment is currently visible to the user. This means |
| 893 | * it: (1) has been added, (2) has its view attached to the window, and |
| 894 | * (3) is not hidden. |
| 895 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 896 | final public boolean isVisible() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 897 | return isAdded() && !isHidden() && mView != null |
| 898 | && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Return true if the fragment has been hidden. By default fragments |
| 903 | * are shown. You can find out about changes to this state with |
Dianne Hackborn | cddfa6d | 2010-05-19 22:56:37 -0700 | [diff] [blame] | 904 | * {@link #onHiddenChanged}. Note that the hidden state is orthogonal |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 905 | * to other states -- that is, to be visible to the user, a fragment |
| 906 | * must be both started and not hidden. |
| 907 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 908 | final public boolean isHidden() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 909 | return mHidden; |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Called when the hidden state (as returned by {@link #isHidden()} of |
| 914 | * the fragment has changed. Fragments start out not hidden; this will |
| 915 | * be called whenever the fragment changes state from that. |
| 916 | * @param hidden True if the fragment is now hidden, false if it is not |
| 917 | * visible. |
| 918 | */ |
| 919 | public void onHiddenChanged(boolean hidden) { |
| 920 | } |
| 921 | |
| 922 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 923 | * Control whether a fragment instance is retained across Activity |
| 924 | * re-creation (such as from a configuration change). This can only |
| 925 | * be used with fragments not in the back stack. If set, the fragment |
| 926 | * lifecycle will be slightly different when an activity is recreated: |
| 927 | * <ul> |
| 928 | * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still |
| 929 | * will be, because the fragment is being detached from its current activity). |
| 930 | * <li> {@link #onCreate(Bundle)} will not be called since the fragment |
| 931 | * is not being re-created. |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 932 | * <li> {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} <b>will</b> |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 933 | * still be called. |
| 934 | * </ul> |
| 935 | */ |
| 936 | public void setRetainInstance(boolean retain) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 937 | if (retain && mParentFragment != null) { |
| 938 | throw new IllegalStateException( |
| 939 | "Can't retain fragements that are nested in other fragments"); |
| 940 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 941 | mRetainInstance = retain; |
| 942 | } |
| 943 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 944 | final public boolean getRetainInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 945 | return mRetainInstance; |
| 946 | } |
| 947 | |
| 948 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 949 | * Report that this fragment would like to participate in populating |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 950 | * the options menu by receiving a call to {@link #onCreateOptionsMenu} |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 951 | * and related methods. |
| 952 | * |
| 953 | * @param hasMenu If true, the fragment has menu items to contribute. |
| 954 | */ |
| 955 | public void setHasOptionsMenu(boolean hasMenu) { |
| 956 | if (mHasMenu != hasMenu) { |
| 957 | mHasMenu = hasMenu; |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 958 | if (isAdded() && !isHidden()) { |
| 959 | mFragmentManager.invalidateOptionsMenu(); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 963 | |
| 964 | /** |
| 965 | * Set a hint for whether this fragment's menu should be visible. This |
| 966 | * is useful if you know that a fragment has been placed in your view |
| 967 | * hierarchy so that the user can not currently seen it, so any menu items |
| 968 | * it has should also not be shown. |
| 969 | * |
| 970 | * @param menuVisible The default is true, meaning the fragment's menu will |
| 971 | * be shown as usual. If false, the user will not see the menu. |
| 972 | */ |
| 973 | public void setMenuVisibility(boolean menuVisible) { |
| 974 | if (mMenuVisible != menuVisible) { |
| 975 | mMenuVisible = menuVisible; |
| 976 | if (mHasMenu && isAdded() && !isHidden()) { |
| 977 | mFragmentManager.invalidateOptionsMenu(); |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 982 | /** |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 983 | * Set a hint to the system about whether this fragment's UI is currently visible |
| 984 | * to the user. This hint defaults to true and is persistent across fragment instance |
| 985 | * state save and restore. |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 986 | * |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 987 | * <p>An app may set this to false to indicate that the fragment's UI is |
| 988 | * scrolled out of visibility or is otherwise not directly visible to the user. |
| 989 | * This may be used by the system to prioritize operations such as fragment lifecycle updates |
| 990 | * or loader ordering behavior.</p> |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 991 | * |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 992 | * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default), |
| 993 | * false if it is not. |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 994 | */ |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 995 | public void setUserVisibleHint(boolean isVisibleToUser) { |
| 996 | if (!mUserVisibleHint && isVisibleToUser && mState < STARTED) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 997 | mFragmentManager.performPendingDeferredStart(this); |
| 998 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 999 | mUserVisibleHint = isVisibleToUser; |
| 1000 | mDeferStart = !isVisibleToUser; |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /** |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1004 | * @return The current value of the user-visible hint on this fragment. |
| 1005 | * @see #setUserVisibleHint(boolean) |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1006 | */ |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1007 | public boolean getUserVisibleHint() { |
| 1008 | return mUserVisibleHint; |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1012 | * Return the LoaderManager for this fragment, creating it if needed. |
| 1013 | */ |
| 1014 | public LoaderManager getLoaderManager() { |
| 1015 | if (mLoaderManager != null) { |
| 1016 | return mLoaderManager; |
| 1017 | } |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1018 | if (mActivity == null) { |
| 1019 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 1020 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1021 | mCheckedForLoaderManager = true; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1022 | mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, true); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1023 | return mLoaderManager; |
| 1024 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1025 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1026 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1027 | * Call {@link Activity#startActivity(Intent)} from the fragment's |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1028 | * containing Activity. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1029 | * |
| 1030 | * @param intent The intent to start. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1031 | */ |
| 1032 | public void startActivity(Intent intent) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1033 | startActivity(intent, null); |
| 1034 | } |
| 1035 | |
| 1036 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1037 | * Call {@link Activity#startActivity(Intent, Bundle)} from the fragment's |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1038 | * containing Activity. |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 1039 | * |
| 1040 | * @param intent The intent to start. |
| 1041 | * @param options Additional options for how the Activity should be started. |
| 1042 | * See {@link android.content.Context#startActivity(Intent, Bundle) |
| 1043 | * Context.startActivity(Intent, Bundle)} for more details. |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1044 | */ |
| 1045 | public void startActivity(Intent intent, Bundle options) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1046 | if (mActivity == null) { |
| 1047 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 1048 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1049 | if (options != null) { |
| 1050 | mActivity.startActivityFromFragment(this, intent, -1, options); |
| 1051 | } else { |
| 1052 | // Note we want to go through this call for compatibility with |
| 1053 | // applications that may have overridden the method. |
| 1054 | mActivity.startActivityFromFragment(this, intent, -1); |
| 1055 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1056 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1057 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1058 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1059 | * Call {@link Activity#startActivityForResult(Intent, int)} from the fragment's |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1060 | * containing Activity. |
| 1061 | */ |
| 1062 | public void startActivityForResult(Intent intent, int requestCode) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1063 | startActivityForResult(intent, requestCode, null); |
| 1064 | } |
| 1065 | |
| 1066 | /** |
Scott Main | 87bff97 | 2013-02-27 15:46:34 -0800 | [diff] [blame] | 1067 | * Call {@link Activity#startActivityForResult(Intent, int, Bundle)} from the fragment's |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1068 | * containing Activity. |
| 1069 | */ |
| 1070 | public void startActivityForResult(Intent intent, int requestCode, Bundle options) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1071 | if (mActivity == null) { |
| 1072 | throw new IllegalStateException("Fragment " + this + " not attached to Activity"); |
| 1073 | } |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1074 | if (options != null) { |
| 1075 | mActivity.startActivityFromFragment(this, intent, requestCode, options); |
| 1076 | } else { |
| 1077 | // Note we want to go through this call for compatibility with |
| 1078 | // applications that may have overridden the method. |
| 1079 | mActivity.startActivityFromFragment(this, intent, requestCode, options); |
| 1080 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * Receive the result from a previous call to |
| 1085 | * {@link #startActivityForResult(Intent, int)}. This follows the |
| 1086 | * related Activity API as described there in |
| 1087 | * {@link Activity#onActivityResult(int, int, Intent)}. |
| 1088 | * |
| 1089 | * @param requestCode The integer request code originally supplied to |
| 1090 | * startActivityForResult(), allowing you to identify who this |
| 1091 | * result came from. |
| 1092 | * @param resultCode The integer result code returned by the child activity |
| 1093 | * through its setResult(). |
| 1094 | * @param data An Intent, which can return result data to the caller |
| 1095 | * (various data can be attached to Intent "extras"). |
| 1096 | */ |
| 1097 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 1098 | } |
| 1099 | |
| 1100 | /** |
Dianne Hackborn | 7187ccb | 2011-01-24 23:58:13 -0800 | [diff] [blame] | 1101 | * @hide Hack so that DialogFragment can make its Dialog before creating |
| 1102 | * its views, and the view construction can use the dialog's context for |
| 1103 | * inflation. Maybe this should become a public API. Note sure. |
| 1104 | */ |
| 1105 | public LayoutInflater getLayoutInflater(Bundle savedInstanceState) { |
| 1106 | return mActivity.getLayoutInflater(); |
| 1107 | } |
| 1108 | |
| 1109 | /** |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1110 | * @deprecated Use {@link #onInflate(Activity, AttributeSet, Bundle)} instead. |
| 1111 | */ |
| 1112 | @Deprecated |
| 1113 | public void onInflate(AttributeSet attrs, Bundle savedInstanceState) { |
| 1114 | mCalled = true; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1118 | * 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] | 1119 | * inflation, typically from setting the content view of an activity. This |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1120 | * may be called immediately after the fragment is created from a <fragment> |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1121 | * tag in a layout file. Note this is <em>before</em> the fragment's |
| 1122 | * {@link #onAttach(Activity)} has been called; all you should do here is |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1123 | * parse the attributes and save them away. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1124 | * |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1125 | * <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] | 1126 | * being inflated into a new instance with saved state. It typically makes |
| 1127 | * sense to re-parse the parameters each time, to allow them to change with |
| 1128 | * different configurations.</p> |
| 1129 | * |
| 1130 | * <p>Here is a typical implementation of a fragment that can take parameters |
| 1131 | * both through attributes supplied here as well from {@link #getArguments()}:</p> |
| 1132 | * |
| 1133 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java |
| 1134 | * fragment} |
| 1135 | * |
| 1136 | * <p>Note that parsing the XML attributes uses a "styleable" resource. The |
| 1137 | * declaration for the styleable used here is:</p> |
| 1138 | * |
| 1139 | * {@sample development/samples/ApiDemos/res/values/attrs.xml fragment_arguments} |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1140 | * |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1141 | * <p>The fragment can then be declared within its activity's content layout |
| 1142 | * through a tag like this:</p> |
| 1143 | * |
| 1144 | * {@sample development/samples/ApiDemos/res/layout/fragment_arguments.xml from_attributes} |
| 1145 | * |
| 1146 | * <p>This fragment can also be created dynamically from arguments given |
| 1147 | * at runtime in the arguments Bundle; here is an example of doing so at |
| 1148 | * creation of the containing activity:</p> |
| 1149 | * |
| 1150 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentArguments.java |
| 1151 | * create} |
| 1152 | * |
| 1153 | * @param activity The Activity that is inflating this fragment. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1154 | * @param attrs The attributes at the tag where the fragment is |
| 1155 | * being created. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1156 | * @param savedInstanceState If the fragment is being re-created from |
| 1157 | * a previous saved state, this is the state. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1158 | */ |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 1159 | public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { |
| 1160 | onInflate(attrs, savedInstanceState); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1161 | mCalled = true; |
| 1162 | } |
| 1163 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1164 | /** |
| 1165 | * Called when a fragment is first attached to its activity. |
| 1166 | * {@link #onCreate(Bundle)} will be called after this. |
| 1167 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1168 | public void onAttach(Activity activity) { |
| 1169 | mCalled = true; |
| 1170 | } |
| 1171 | |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1172 | /** |
| 1173 | * Called when a fragment loads an animation. |
| 1174 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1175 | public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1176 | return null; |
| 1177 | } |
| 1178 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1179 | /** |
| 1180 | * Called to do initial creation of a fragment. This is called after |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1181 | * {@link #onAttach(Activity)} and before |
| 1182 | * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. |
| 1183 | * |
| 1184 | * <p>Note that this can be called while the fragment's activity is |
| 1185 | * still in the process of being created. As such, you can not rely |
| 1186 | * on things like the activity's content view hierarchy being initialized |
| 1187 | * at this point. If you want to do work once the activity itself is |
| 1188 | * created, see {@link #onActivityCreated(Bundle)}. |
| 1189 | * |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1190 | * @param savedInstanceState If the fragment is being re-created from |
| 1191 | * a previous saved state, this is the state. |
| 1192 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1193 | public void onCreate(Bundle savedInstanceState) { |
| 1194 | mCalled = true; |
| 1195 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1196 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1197 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1198 | * Called to have the fragment instantiate its user interface view. |
| 1199 | * This is optional, and non-graphical fragments can return null (which |
| 1200 | * is the default implementation). This will be called between |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1201 | * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1202 | * |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1203 | * <p>If you return a View from here, you will later be called in |
| 1204 | * {@link #onDestroyView} when the view is being released. |
| 1205 | * |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1206 | * @param inflater The LayoutInflater object that can be used to inflate |
| 1207 | * any views in the fragment, |
| 1208 | * @param container If non-null, this is the parent view that the fragment's |
| 1209 | * UI should be attached to. The fragment should not add the view itself, |
| 1210 | * but this can be used to generate the LayoutParams of the view. |
| 1211 | * @param savedInstanceState If non-null, this fragment is being re-constructed |
| 1212 | * from a previous saved state as given here. |
| 1213 | * |
| 1214 | * @return Return the View for the fragment's UI, or null. |
| 1215 | */ |
| 1216 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 1217 | Bundle savedInstanceState) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1218 | return null; |
| 1219 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1220 | |
| 1221 | /** |
| 1222 | * Called immediately after {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} |
| 1223 | * has returned, but before any saved state has been restored in to the view. |
| 1224 | * This gives subclasses a chance to initialize themselves once |
| 1225 | * they know their view hierarchy has been completely created. The fragment's |
| 1226 | * view hierarchy is not however attached to its parent at this point. |
| 1227 | * @param view The View returned by {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}. |
| 1228 | * @param savedInstanceState If non-null, this fragment is being re-constructed |
| 1229 | * from a previous saved state as given here. |
| 1230 | */ |
| 1231 | public void onViewCreated(View view, Bundle savedInstanceState) { |
| 1232 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1233 | |
Scott Main | 662cc7a4 | 2011-01-17 15:02:07 -0800 | [diff] [blame] | 1234 | /** |
| 1235 | * Get the root view for the fragment's layout (the one returned by {@link #onCreateView}), |
| 1236 | * if provided. |
| 1237 | * |
| 1238 | * @return The fragment's root view, or null if it has no layout. |
| 1239 | */ |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1240 | public View getView() { |
| 1241 | return mView; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1244 | /** |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1245 | * Called when the fragment's activity has been created and this |
| 1246 | * fragment's view hierarchy instantiated. It can be used to do final |
| 1247 | * initialization once these pieces are in place, such as retrieving |
| 1248 | * views or restoring state. It is also useful for fragments that use |
| 1249 | * {@link #setRetainInstance(boolean)} to retain their instance, |
| 1250 | * as this callback tells the fragment when it is fully associated with |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1251 | * the new activity instance. This is called after {@link #onCreateView} |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1252 | * and before {@link #onViewStateRestored(Bundle)}. |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1253 | * |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1254 | * @param savedInstanceState If the fragment is being re-created from |
| 1255 | * a previous saved state, this is the state. |
| 1256 | */ |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1257 | public void onActivityCreated(Bundle savedInstanceState) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1258 | mCalled = true; |
| 1259 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1260 | |
| 1261 | /** |
| 1262 | * Called when all saved state has been restored into the view hierarchy |
| 1263 | * of the fragment. This can be used to do initialization based on saved |
| 1264 | * state that you are letting the view hierarchy track itself, such as |
| 1265 | * whether check box widgets are currently checked. This is called |
| 1266 | * after {@link #onActivityCreated(Bundle)} and before |
| 1267 | * {@link #onStart()}. |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1268 | * |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1269 | * @param savedInstanceState If the fragment is being re-created from |
| 1270 | * a previous saved state, this is the state. |
| 1271 | */ |
| 1272 | public void onViewStateRestored(Bundle savedInstanceState) { |
| 1273 | mCalled = true; |
| 1274 | } |
| 1275 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1276 | /** |
| 1277 | * Called when the Fragment is visible to the user. This is generally |
| 1278 | * tied to {@link Activity#onStart() Activity.onStart} of the containing |
| 1279 | * Activity's lifecycle. |
| 1280 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1281 | public void onStart() { |
| 1282 | mCalled = true; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1283 | |
| 1284 | if (!mLoadersStarted) { |
| 1285 | mLoadersStarted = true; |
| 1286 | if (!mCheckedForLoaderManager) { |
| 1287 | mCheckedForLoaderManager = true; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1288 | mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, false); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1289 | } |
| 1290 | if (mLoaderManager != null) { |
| 1291 | mLoaderManager.doStart(); |
| 1292 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1293 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1296 | /** |
| 1297 | * Called when the fragment is visible to the user and actively running. |
| 1298 | * This is generally |
| 1299 | * tied to {@link Activity#onResume() Activity.onResume} of the containing |
| 1300 | * Activity's lifecycle. |
| 1301 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1302 | public void onResume() { |
| 1303 | mCalled = true; |
| 1304 | } |
| 1305 | |
Dianne Hackborn | 7277820 | 2010-08-20 18:26:01 -0700 | [diff] [blame] | 1306 | /** |
| 1307 | * Called to ask the fragment to save its current dynamic state, so it |
| 1308 | * can later be reconstructed in a new instance of its process is |
| 1309 | * restarted. If a new instance of the fragment later needs to be |
| 1310 | * created, the data you place in the Bundle here will be available |
| 1311 | * in the Bundle given to {@link #onCreate(Bundle)}, |
| 1312 | * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and |
| 1313 | * {@link #onActivityCreated(Bundle)}. |
| 1314 | * |
| 1315 | * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle) |
Daisuke Miyakawa | 2f76176 | 2010-09-12 16:53:17 -0700 | [diff] [blame] | 1316 | * Activity.onSaveInstanceState(Bundle)} and most of the discussion there |
Dianne Hackborn | 7277820 | 2010-08-20 18:26:01 -0700 | [diff] [blame] | 1317 | * applies here as well. Note however: <em>this method may be called |
| 1318 | * at any time before {@link #onDestroy()}</em>. There are many situations |
| 1319 | * where a fragment may be mostly torn down (such as when placed on the |
| 1320 | * back stack with no UI showing), but its state will not be saved until |
| 1321 | * its owning activity actually needs to save its state. |
| 1322 | * |
| 1323 | * @param outState Bundle in which to place your saved state. |
| 1324 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1325 | public void onSaveInstanceState(Bundle outState) { |
| 1326 | } |
| 1327 | |
| 1328 | public void onConfigurationChanged(Configuration newConfig) { |
| 1329 | mCalled = true; |
| 1330 | } |
| 1331 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1332 | /** |
| 1333 | * Called when the Fragment is no longer resumed. This is generally |
| 1334 | * tied to {@link Activity#onPause() Activity.onPause} of the containing |
| 1335 | * Activity's lifecycle. |
| 1336 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1337 | public void onPause() { |
| 1338 | mCalled = true; |
| 1339 | } |
| 1340 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1341 | /** |
| 1342 | * Called when the Fragment is no longer started. This is generally |
| 1343 | * tied to {@link Activity#onStop() Activity.onStop} of the containing |
| 1344 | * Activity's lifecycle. |
| 1345 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1346 | public void onStop() { |
| 1347 | mCalled = true; |
| 1348 | } |
| 1349 | |
| 1350 | public void onLowMemory() { |
| 1351 | mCalled = true; |
| 1352 | } |
| 1353 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1354 | public void onTrimMemory(int level) { |
| 1355 | mCalled = true; |
| 1356 | } |
| 1357 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1358 | /** |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1359 | * Called when the view previously created by {@link #onCreateView} has |
| 1360 | * been detached from the fragment. The next time the fragment needs |
| 1361 | * to be displayed, a new view will be created. This is called |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1362 | * after {@link #onStop()} and before {@link #onDestroy()}. It is called |
| 1363 | * <em>regardless</em> of whether {@link #onCreateView} returned a |
| 1364 | * non-null view. Internally it is called after the view's state has |
| 1365 | * been saved but before it has been removed from its parent. |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1366 | */ |
| 1367 | public void onDestroyView() { |
| 1368 | mCalled = true; |
| 1369 | } |
| 1370 | |
| 1371 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1372 | * Called when the fragment is no longer in use. This is called |
| 1373 | * after {@link #onStop()} and before {@link #onDetach()}. |
| 1374 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1375 | public void onDestroy() { |
| 1376 | mCalled = true; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1377 | //Log.v("foo", "onDestroy: mCheckedForLoaderManager=" + mCheckedForLoaderManager |
| 1378 | // + " mLoaderManager=" + mLoaderManager); |
| 1379 | if (!mCheckedForLoaderManager) { |
| 1380 | mCheckedForLoaderManager = true; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1381 | mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, false); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1382 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1383 | if (mLoaderManager != null) { |
| 1384 | mLoaderManager.doDestroy(); |
| 1385 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1386 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1387 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1388 | /** |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1389 | * Called by the fragment manager once this fragment has been removed, |
| 1390 | * so that we don't have any left-over state if the application decides |
| 1391 | * to re-use the instance. This only clears state that the framework |
| 1392 | * internally manages, not things the application sets. |
| 1393 | */ |
| 1394 | void initState() { |
| 1395 | mIndex = -1; |
| 1396 | mWho = null; |
| 1397 | mAdded = false; |
| 1398 | mRemoving = false; |
| 1399 | mResumed = false; |
| 1400 | mFromLayout = false; |
| 1401 | mInLayout = false; |
| 1402 | mRestored = false; |
| 1403 | mBackStackNesting = 0; |
| 1404 | mFragmentManager = null; |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1405 | mActivity = null; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1406 | mFragmentId = 0; |
| 1407 | mContainerId = 0; |
| 1408 | mTag = null; |
| 1409 | mHidden = false; |
| 1410 | mDetached = false; |
| 1411 | mRetaining = false; |
| 1412 | mLoaderManager = null; |
| 1413 | mLoadersStarted = false; |
| 1414 | mCheckedForLoaderManager = false; |
| 1415 | } |
| 1416 | |
| 1417 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1418 | * Called when the fragment is no longer attached to its activity. This |
| 1419 | * is called after {@link #onDestroy()}. |
| 1420 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1421 | public void onDetach() { |
| 1422 | mCalled = true; |
| 1423 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1424 | |
| 1425 | /** |
| 1426 | * Initialize the contents of the Activity's standard options menu. You |
| 1427 | * 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] | 1428 | * to be called, you must have first called {@link #setHasOptionsMenu}. See |
| 1429 | * {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu} |
| 1430 | * for more information. |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1431 | * |
| 1432 | * @param menu The options menu in which you place your items. |
| 1433 | * |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 1434 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1435 | * @see #onPrepareOptionsMenu |
| 1436 | * @see #onOptionsItemSelected |
| 1437 | */ |
| 1438 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * Prepare the Screen's standard options menu to be displayed. This is |
| 1443 | * called right before the menu is shown, every time it is shown. You can |
| 1444 | * use this method to efficiently enable/disable items or otherwise |
| 1445 | * dynamically modify the contents. See |
| 1446 | * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu} |
| 1447 | * for more information. |
| 1448 | * |
| 1449 | * @param menu The options menu as last shown or first initialized by |
| 1450 | * onCreateOptionsMenu(). |
| 1451 | * |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame] | 1452 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1453 | * @see #onCreateOptionsMenu |
| 1454 | */ |
| 1455 | public void onPrepareOptionsMenu(Menu menu) { |
| 1456 | } |
| 1457 | |
| 1458 | /** |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1459 | * Called when this fragment's option menu items are no longer being |
| 1460 | * included in the overall options menu. Receiving this call means that |
| 1461 | * the menu needed to be rebuilt, but this fragment's items were not |
| 1462 | * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)} |
| 1463 | * was not called). |
| 1464 | */ |
| 1465 | public void onDestroyOptionsMenu() { |
| 1466 | } |
| 1467 | |
| 1468 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1469 | * This hook is called whenever an item in your options menu is selected. |
| 1470 | * The default implementation simply returns false to have the normal |
| 1471 | * processing happen (calling the item's Runnable or sending a message to |
| 1472 | * its Handler as appropriate). You can use this method for any items |
| 1473 | * for which you would like to do processing without those other |
| 1474 | * facilities. |
| 1475 | * |
| 1476 | * <p>Derived classes should call through to the base class for it to |
| 1477 | * perform the default menu handling. |
| 1478 | * |
| 1479 | * @param item The menu item that was selected. |
| 1480 | * |
| 1481 | * @return boolean Return false to allow normal menu processing to |
| 1482 | * proceed, true to consume it here. |
| 1483 | * |
| 1484 | * @see #onCreateOptionsMenu |
| 1485 | */ |
| 1486 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 1492 | * the menu with the back/menu button, or when an item is selected). |
| 1493 | * |
| 1494 | * @param menu The options menu as last shown or first initialized by |
| 1495 | * onCreateOptionsMenu(). |
| 1496 | */ |
| 1497 | public void onOptionsMenuClosed(Menu menu) { |
| 1498 | } |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1499 | |
| 1500 | /** |
| 1501 | * Called when a context menu for the {@code view} is about to be shown. |
| 1502 | * Unlike {@link #onCreateOptionsMenu}, this will be called every |
| 1503 | * time the context menu is about to be shown and should be populated for |
| 1504 | * the view (or item inside the view for {@link AdapterView} subclasses, |
| 1505 | * this can be found in the {@code menuInfo})). |
| 1506 | * <p> |
| 1507 | * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an |
| 1508 | * item has been selected. |
| 1509 | * <p> |
| 1510 | * The default implementation calls up to |
| 1511 | * {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though |
| 1512 | * you can not call this implementation if you don't want that behavior. |
| 1513 | * <p> |
| 1514 | * It is not safe to hold onto the context menu after this method returns. |
| 1515 | * {@inheritDoc} |
| 1516 | */ |
| 1517 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 1518 | getActivity().onCreateContextMenu(menu, v, menuInfo); |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * Registers a context menu to be shown for the given view (multiple views |
| 1523 | * can show the context menu). This method will set the |
| 1524 | * {@link OnCreateContextMenuListener} on the view to this fragment, so |
| 1525 | * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be |
| 1526 | * called when it is time to show the context menu. |
| 1527 | * |
| 1528 | * @see #unregisterForContextMenu(View) |
| 1529 | * @param view The view that should show a context menu. |
| 1530 | */ |
| 1531 | public void registerForContextMenu(View view) { |
| 1532 | view.setOnCreateContextMenuListener(this); |
| 1533 | } |
| 1534 | |
| 1535 | /** |
| 1536 | * Prevents a context menu to be shown for the given view. This method will |
| 1537 | * remove the {@link OnCreateContextMenuListener} on the view. |
| 1538 | * |
| 1539 | * @see #registerForContextMenu(View) |
| 1540 | * @param view The view that should stop showing a context menu. |
| 1541 | */ |
| 1542 | public void unregisterForContextMenu(View view) { |
| 1543 | view.setOnCreateContextMenuListener(null); |
| 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * This hook is called whenever an item in a context menu is selected. The |
| 1548 | * default implementation simply returns false to have the normal processing |
| 1549 | * happen (calling the item's Runnable or sending a message to its Handler |
| 1550 | * as appropriate). You can use this method for any items for which you |
| 1551 | * would like to do processing without those other facilities. |
| 1552 | * <p> |
| 1553 | * Use {@link MenuItem#getMenuInfo()} to get extra information set by the |
| 1554 | * View that added this menu item. |
| 1555 | * <p> |
| 1556 | * Derived classes should call through to the base class for it to perform |
| 1557 | * the default menu handling. |
| 1558 | * |
| 1559 | * @param item The context menu item that was selected. |
| 1560 | * @return boolean Return false to allow normal context menu processing to |
| 1561 | * proceed, true to consume it here. |
| 1562 | */ |
| 1563 | public boolean onContextItemSelected(MenuItem item) { |
| 1564 | return false; |
| 1565 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1566 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1567 | /** |
| 1568 | * Print the Fragments's state into the given stream. |
| 1569 | * |
| 1570 | * @param prefix Text to print at the front of each line. |
| 1571 | * @param fd The raw file descriptor that the dump is being sent to. |
| 1572 | * @param writer The PrintWriter to which you should dump your state. This will be |
| 1573 | * closed for you after you return. |
| 1574 | * @param args additional arguments to the dump request. |
| 1575 | */ |
| 1576 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 1577 | writer.print(prefix); writer.print("mFragmentId=#"); |
| 1578 | writer.print(Integer.toHexString(mFragmentId)); |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1579 | writer.print(" mContainerId=#"); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 1580 | writer.print(Integer.toHexString(mContainerId)); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1581 | writer.print(" mTag="); writer.println(mTag); |
| 1582 | writer.print(prefix); writer.print("mState="); writer.print(mState); |
| 1583 | writer.print(" mIndex="); writer.print(mIndex); |
| 1584 | writer.print(" mWho="); writer.print(mWho); |
| 1585 | writer.print(" mBackStackNesting="); writer.println(mBackStackNesting); |
| 1586 | writer.print(prefix); writer.print("mAdded="); writer.print(mAdded); |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1587 | writer.print(" mRemoving="); writer.print(mRemoving); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1588 | writer.print(" mResumed="); writer.print(mResumed); |
| 1589 | writer.print(" mFromLayout="); writer.print(mFromLayout); |
| 1590 | writer.print(" mInLayout="); writer.println(mInLayout); |
| 1591 | writer.print(prefix); writer.print("mHidden="); writer.print(mHidden); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1592 | writer.print(" mDetached="); writer.print(mDetached); |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1593 | writer.print(" mMenuVisible="); writer.print(mMenuVisible); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1594 | writer.print(" mHasMenu="); writer.println(mHasMenu); |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1595 | writer.print(prefix); writer.print("mRetainInstance="); writer.print(mRetainInstance); |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1596 | writer.print(" mRetaining="); writer.print(mRetaining); |
| 1597 | writer.print(" mUserVisibleHint="); writer.println(mUserVisibleHint); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1598 | if (mFragmentManager != null) { |
| 1599 | writer.print(prefix); writer.print("mFragmentManager="); |
| 1600 | writer.println(mFragmentManager); |
| 1601 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1602 | if (mActivity != null) { |
| 1603 | writer.print(prefix); writer.print("mActivity="); |
| 1604 | writer.println(mActivity); |
| 1605 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1606 | if (mParentFragment != null) { |
| 1607 | writer.print(prefix); writer.print("mParentFragment="); |
| 1608 | writer.println(mParentFragment); |
| 1609 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1610 | if (mArguments != null) { |
| 1611 | writer.print(prefix); writer.print("mArguments="); writer.println(mArguments); |
| 1612 | } |
| 1613 | if (mSavedFragmentState != null) { |
| 1614 | writer.print(prefix); writer.print("mSavedFragmentState="); |
| 1615 | writer.println(mSavedFragmentState); |
| 1616 | } |
| 1617 | if (mSavedViewState != null) { |
| 1618 | writer.print(prefix); writer.print("mSavedViewState="); |
| 1619 | writer.println(mSavedViewState); |
| 1620 | } |
| 1621 | if (mTarget != null) { |
| 1622 | writer.print(prefix); writer.print("mTarget="); writer.print(mTarget); |
| 1623 | writer.print(" mTargetRequestCode="); |
| 1624 | writer.println(mTargetRequestCode); |
| 1625 | } |
| 1626 | if (mNextAnim != 0) { |
| 1627 | writer.print(prefix); writer.print("mNextAnim="); writer.println(mNextAnim); |
| 1628 | } |
| 1629 | if (mContainer != null) { |
| 1630 | writer.print(prefix); writer.print("mContainer="); writer.println(mContainer); |
| 1631 | } |
| 1632 | if (mView != null) { |
| 1633 | writer.print(prefix); writer.print("mView="); writer.println(mView); |
| 1634 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1635 | if (mAnimatingAway != null) { |
| 1636 | writer.print(prefix); writer.print("mAnimatingAway="); writer.println(mAnimatingAway); |
| 1637 | writer.print(prefix); writer.print("mStateAfterAnimating="); |
| 1638 | writer.println(mStateAfterAnimating); |
| 1639 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1640 | if (mLoaderManager != null) { |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 1641 | writer.print(prefix); writer.println("Loader Manager:"); |
| 1642 | mLoaderManager.dump(prefix + " ", fd, writer, args); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1643 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1644 | if (mChildFragmentManager != null) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1645 | writer.print(prefix); writer.println("Child " + mChildFragmentManager + ":"); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1646 | mChildFragmentManager.dump(prefix + " ", fd, writer, args); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | Fragment findFragmentByWho(String who) { |
| 1651 | if (who.equals(mWho)) { |
| 1652 | return this; |
| 1653 | } |
| 1654 | if (mChildFragmentManager != null) { |
| 1655 | return mChildFragmentManager.findFragmentByWho(who); |
| 1656 | } |
| 1657 | return null; |
| 1658 | } |
| 1659 | |
| 1660 | void instantiateChildFragmentManager() { |
| 1661 | mChildFragmentManager = new FragmentManagerImpl(); |
| 1662 | mChildFragmentManager.attachActivity(mActivity, new FragmentContainer() { |
| 1663 | @Override |
| 1664 | public View findViewById(int id) { |
| 1665 | if (mView == null) { |
| 1666 | throw new IllegalStateException("Fragment does not have a view"); |
| 1667 | } |
| 1668 | return mView.findViewById(id); |
| 1669 | } |
| 1670 | }, this); |
| 1671 | } |
| 1672 | |
| 1673 | void performCreate(Bundle savedInstanceState) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1674 | if (mChildFragmentManager != null) { |
| 1675 | mChildFragmentManager.noteStateNotSaved(); |
| 1676 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1677 | mCalled = false; |
| 1678 | onCreate(savedInstanceState); |
| 1679 | if (!mCalled) { |
| 1680 | throw new SuperNotCalledException("Fragment " + this |
| 1681 | + " did not call through to super.onCreate()"); |
| 1682 | } |
| 1683 | if (savedInstanceState != null) { |
| 1684 | Parcelable p = savedInstanceState.getParcelable(Activity.FRAGMENTS_TAG); |
| 1685 | if (p != null) { |
| 1686 | if (mChildFragmentManager == null) { |
| 1687 | instantiateChildFragmentManager(); |
| 1688 | } |
| 1689 | mChildFragmentManager.restoreAllState(p, null); |
| 1690 | mChildFragmentManager.dispatchCreate(); |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1695 | View performCreateView(LayoutInflater inflater, ViewGroup container, |
| 1696 | Bundle savedInstanceState) { |
| 1697 | if (mChildFragmentManager != null) { |
| 1698 | mChildFragmentManager.noteStateNotSaved(); |
| 1699 | } |
| 1700 | return onCreateView(inflater, container, savedInstanceState); |
| 1701 | } |
| 1702 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1703 | void performActivityCreated(Bundle savedInstanceState) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1704 | if (mChildFragmentManager != null) { |
| 1705 | mChildFragmentManager.noteStateNotSaved(); |
| 1706 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1707 | mCalled = false; |
| 1708 | onActivityCreated(savedInstanceState); |
| 1709 | if (!mCalled) { |
| 1710 | throw new SuperNotCalledException("Fragment " + this |
| 1711 | + " did not call through to super.onActivityCreated()"); |
| 1712 | } |
| 1713 | if (mChildFragmentManager != null) { |
| 1714 | mChildFragmentManager.dispatchActivityCreated(); |
| 1715 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1718 | void performStart() { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1719 | if (mChildFragmentManager != null) { |
| 1720 | mChildFragmentManager.noteStateNotSaved(); |
| 1721 | mChildFragmentManager.execPendingActions(); |
| 1722 | } |
| 1723 | mCalled = false; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1724 | onStart(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1725 | if (!mCalled) { |
| 1726 | throw new SuperNotCalledException("Fragment " + this |
| 1727 | + " did not call through to super.onStart()"); |
| 1728 | } |
| 1729 | if (mChildFragmentManager != null) { |
| 1730 | mChildFragmentManager.dispatchStart(); |
| 1731 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1732 | if (mLoaderManager != null) { |
| 1733 | mLoaderManager.doReportStart(); |
| 1734 | } |
| 1735 | } |
| 1736 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1737 | void performResume() { |
| 1738 | if (mChildFragmentManager != null) { |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1739 | mChildFragmentManager.noteStateNotSaved(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1740 | mChildFragmentManager.execPendingActions(); |
| 1741 | } |
| 1742 | mCalled = false; |
| 1743 | onResume(); |
| 1744 | if (!mCalled) { |
| 1745 | throw new SuperNotCalledException("Fragment " + this |
| 1746 | + " did not call through to super.onResume()"); |
| 1747 | } |
| 1748 | if (mChildFragmentManager != null) { |
| 1749 | mChildFragmentManager.dispatchResume(); |
| 1750 | mChildFragmentManager.execPendingActions(); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | void performConfigurationChanged(Configuration newConfig) { |
| 1755 | onConfigurationChanged(newConfig); |
| 1756 | if (mChildFragmentManager != null) { |
| 1757 | mChildFragmentManager.dispatchConfigurationChanged(newConfig); |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | void performLowMemory() { |
| 1762 | onLowMemory(); |
| 1763 | if (mChildFragmentManager != null) { |
| 1764 | mChildFragmentManager.dispatchLowMemory(); |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | void performTrimMemory(int level) { |
| 1769 | onTrimMemory(level); |
| 1770 | if (mChildFragmentManager != null) { |
| 1771 | mChildFragmentManager.dispatchTrimMemory(level); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 1776 | boolean show = false; |
| 1777 | if (!mHidden) { |
| 1778 | if (mHasMenu && mMenuVisible) { |
| 1779 | show = true; |
| 1780 | onCreateOptionsMenu(menu, inflater); |
| 1781 | } |
| 1782 | if (mChildFragmentManager != null) { |
| 1783 | show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater); |
| 1784 | } |
| 1785 | } |
| 1786 | return show; |
| 1787 | } |
| 1788 | |
| 1789 | boolean performPrepareOptionsMenu(Menu menu) { |
| 1790 | boolean show = false; |
| 1791 | if (!mHidden) { |
| 1792 | if (mHasMenu && mMenuVisible) { |
| 1793 | show = true; |
| 1794 | onPrepareOptionsMenu(menu); |
| 1795 | } |
| 1796 | if (mChildFragmentManager != null) { |
| 1797 | show |= mChildFragmentManager.dispatchPrepareOptionsMenu(menu); |
| 1798 | } |
| 1799 | } |
| 1800 | return show; |
| 1801 | } |
| 1802 | |
| 1803 | boolean performOptionsItemSelected(MenuItem item) { |
| 1804 | if (!mHidden) { |
| 1805 | if (mHasMenu && mMenuVisible) { |
| 1806 | if (onOptionsItemSelected(item)) { |
| 1807 | return true; |
| 1808 | } |
| 1809 | } |
| 1810 | if (mChildFragmentManager != null) { |
| 1811 | if (mChildFragmentManager.dispatchOptionsItemSelected(item)) { |
| 1812 | return true; |
| 1813 | } |
| 1814 | } |
| 1815 | } |
| 1816 | return false; |
| 1817 | } |
| 1818 | |
| 1819 | boolean performContextItemSelected(MenuItem item) { |
| 1820 | if (!mHidden) { |
| 1821 | if (onContextItemSelected(item)) { |
| 1822 | return true; |
| 1823 | } |
| 1824 | if (mChildFragmentManager != null) { |
| 1825 | if (mChildFragmentManager.dispatchContextItemSelected(item)) { |
| 1826 | return true; |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | return false; |
| 1831 | } |
| 1832 | |
| 1833 | void performOptionsMenuClosed(Menu menu) { |
| 1834 | if (!mHidden) { |
| 1835 | if (mHasMenu && mMenuVisible) { |
| 1836 | onOptionsMenuClosed(menu); |
| 1837 | } |
| 1838 | if (mChildFragmentManager != null) { |
| 1839 | mChildFragmentManager.dispatchOptionsMenuClosed(menu); |
| 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | void performSaveInstanceState(Bundle outState) { |
| 1845 | onSaveInstanceState(outState); |
| 1846 | if (mChildFragmentManager != null) { |
| 1847 | Parcelable p = mChildFragmentManager.saveAllState(); |
| 1848 | if (p != null) { |
| 1849 | outState.putParcelable(Activity.FRAGMENTS_TAG, p); |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | void performPause() { |
| 1855 | if (mChildFragmentManager != null) { |
| 1856 | mChildFragmentManager.dispatchPause(); |
| 1857 | } |
| 1858 | mCalled = false; |
| 1859 | onPause(); |
| 1860 | if (!mCalled) { |
| 1861 | throw new SuperNotCalledException("Fragment " + this |
| 1862 | + " did not call through to super.onPause()"); |
| 1863 | } |
| 1864 | } |
| 1865 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1866 | void performStop() { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1867 | if (mChildFragmentManager != null) { |
| 1868 | mChildFragmentManager.dispatchStop(); |
| 1869 | } |
| 1870 | mCalled = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1871 | onStop(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1872 | if (!mCalled) { |
| 1873 | throw new SuperNotCalledException("Fragment " + this |
| 1874 | + " did not call through to super.onStop()"); |
| 1875 | } |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1876 | |
| 1877 | if (mLoadersStarted) { |
| 1878 | mLoadersStarted = false; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1879 | if (!mCheckedForLoaderManager) { |
| 1880 | mCheckedForLoaderManager = true; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1881 | mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, false); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1882 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 1883 | if (mLoaderManager != null) { |
| 1884 | if (mActivity == null || !mActivity.mChangingConfigurations) { |
| 1885 | mLoaderManager.doStop(); |
| 1886 | } else { |
| 1887 | mLoaderManager.doRetain(); |
| 1888 | } |
| 1889 | } |
| 1890 | } |
| 1891 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1892 | |
| 1893 | void performDestroyView() { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1894 | if (mChildFragmentManager != null) { |
| 1895 | mChildFragmentManager.dispatchDestroyView(); |
| 1896 | } |
| 1897 | mCalled = false; |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1898 | onDestroyView(); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1899 | if (!mCalled) { |
| 1900 | throw new SuperNotCalledException("Fragment " + this |
| 1901 | + " did not call through to super.onDestroyView()"); |
| 1902 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1903 | if (mLoaderManager != null) { |
| 1904 | mLoaderManager.doReportNextStart(); |
| 1905 | } |
| 1906 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1907 | |
| 1908 | void performDestroy() { |
| 1909 | if (mChildFragmentManager != null) { |
| 1910 | mChildFragmentManager.dispatchDestroy(); |
| 1911 | } |
| 1912 | mCalled = false; |
| 1913 | onDestroy(); |
| 1914 | if (!mCalled) { |
| 1915 | throw new SuperNotCalledException("Fragment " + this |
| 1916 | + " did not call through to super.onDestroy()"); |
| 1917 | } |
| 1918 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1919 | } |