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