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 | |
| 19 | import android.content.ComponentCallbacks; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 21 | import android.content.res.Configuration; |
| 22 | import android.os.Bundle; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 26 | import android.util.SparseArray; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 28 | import android.view.Menu; |
| 29 | import android.view.MenuInflater; |
| 30 | import android.view.MenuItem; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.ViewGroup; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 33 | import android.view.animation.Animation; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 34 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 35 | import java.lang.reflect.InvocationTargetException; |
| 36 | import java.util.HashMap; |
| 37 | |
| 38 | final class FragmentState implements Parcelable { |
| 39 | static final String VIEW_STATE_TAG = "android:view_state"; |
| 40 | |
| 41 | final String mClassName; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 42 | final int mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 43 | final boolean mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 44 | final int mFragmentId; |
| 45 | final int mContainerId; |
| 46 | final String mTag; |
| 47 | final boolean mRetainInstance; |
| 48 | |
| 49 | Bundle mSavedFragmentState; |
| 50 | |
| 51 | Fragment mInstance; |
| 52 | |
| 53 | public FragmentState(Fragment frag) { |
| 54 | mClassName = frag.getClass().getName(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 55 | mIndex = frag.mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 56 | mFromLayout = frag.mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 57 | mFragmentId = frag.mFragmentId; |
| 58 | mContainerId = frag.mContainerId; |
| 59 | mTag = frag.mTag; |
| 60 | mRetainInstance = frag.mRetainInstance; |
| 61 | } |
| 62 | |
| 63 | public FragmentState(Parcel in) { |
| 64 | mClassName = in.readString(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 65 | mIndex = in.readInt(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 66 | mFromLayout = in.readInt() != 0; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 67 | mFragmentId = in.readInt(); |
| 68 | mContainerId = in.readInt(); |
| 69 | mTag = in.readString(); |
| 70 | mRetainInstance = in.readInt() != 0; |
| 71 | mSavedFragmentState = in.readBundle(); |
| 72 | } |
| 73 | |
| 74 | public Fragment instantiate(Activity activity) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 75 | if (mInstance != null) { |
| 76 | return mInstance; |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | mInstance = Fragment.instantiate(activity, mClassName); |
| 81 | } catch (Exception e) { |
| 82 | throw new RuntimeException("Unable to restore fragment " + mClassName, e); |
| 83 | } |
| 84 | |
| 85 | if (mSavedFragmentState != null) { |
| 86 | mSavedFragmentState.setClassLoader(activity.getClassLoader()); |
| 87 | mInstance.mSavedFragmentState = mSavedFragmentState; |
| 88 | mInstance.mSavedViewState |
| 89 | = mSavedFragmentState.getSparseParcelableArray(VIEW_STATE_TAG); |
| 90 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 91 | mInstance.setIndex(mIndex); |
| 92 | mInstance.mFromLayout = mFromLayout; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 93 | mInstance.mFragmentId = mFragmentId; |
| 94 | mInstance.mContainerId = mContainerId; |
| 95 | mInstance.mTag = mTag; |
| 96 | mInstance.mRetainInstance = mRetainInstance; |
| 97 | |
| 98 | return mInstance; |
| 99 | } |
| 100 | |
| 101 | public int describeContents() { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | public void writeToParcel(Parcel dest, int flags) { |
| 106 | dest.writeString(mClassName); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 107 | dest.writeInt(mIndex); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 108 | dest.writeInt(mFromLayout ? 1 : 0); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 109 | dest.writeInt(mFragmentId); |
| 110 | dest.writeInt(mContainerId); |
| 111 | dest.writeString(mTag); |
| 112 | dest.writeInt(mRetainInstance ? 1 : 0); |
| 113 | dest.writeBundle(mSavedFragmentState); |
| 114 | } |
| 115 | |
| 116 | public static final Parcelable.Creator<FragmentState> CREATOR |
| 117 | = new Parcelable.Creator<FragmentState>() { |
| 118 | public FragmentState createFromParcel(Parcel in) { |
| 119 | return new FragmentState(in); |
| 120 | } |
| 121 | |
| 122 | public FragmentState[] newArray(int size) { |
| 123 | return new FragmentState[size]; |
| 124 | } |
| 125 | }; |
| 126 | } |
| 127 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 128 | /** |
| 129 | * A Fragment is a piece of an application's user interface or behavior |
| 130 | * that can be placed in an {@link Activity}. |
| 131 | */ |
| 132 | public class Fragment implements ComponentCallbacks { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 133 | private static final HashMap<String, Class> sClassMap = |
| 134 | new HashMap<String, Class>(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 135 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 136 | static final int INITIALIZING = 0; // Not yet created. |
| 137 | static final int CREATED = 1; // Created. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 138 | static final int CONTENT = 2; // View hierarchy content available. |
| 139 | static final int STARTED = 3; // Created and started, not resumed. |
| 140 | static final int RESUMED = 4; // Created started and resumed. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 141 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 142 | int mState = INITIALIZING; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 143 | |
| 144 | // When instantiated from saved state, this is the saved state. |
| 145 | Bundle mSavedFragmentState; |
| 146 | SparseArray<Parcelable> mSavedViewState; |
| 147 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 148 | // Index into active fragment array. |
| 149 | int mIndex = -1; |
| 150 | |
| 151 | // Internal unique name for this fragment; |
| 152 | String mWho; |
| 153 | |
| 154 | // True if the fragment is in the list of added fragments. |
| 155 | boolean mAdded; |
| 156 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 157 | // Set to true if this fragment was instantiated from a layout file. |
| 158 | boolean mFromLayout; |
| 159 | |
| 160 | // Number of active back stack entries this fragment is in. |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 161 | int mBackStackNesting; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 162 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 163 | // Set as soon as a fragment is added to a transaction (or removed), |
| 164 | // to be able to do validation. |
| 165 | Activity mImmediateActivity; |
| 166 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 167 | // Activity this fragment is attached to. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 168 | Activity mActivity; |
| 169 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 170 | // The optional identifier for this fragment -- either the container ID if it |
| 171 | // was dynamically added to the view hierarchy, or the ID supplied in |
| 172 | // layout. |
| 173 | int mFragmentId; |
| 174 | |
| 175 | // When a fragment is being dynamically added to the view hierarchy, this |
| 176 | // is the identifier of the parent container it is being added to. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 177 | int mContainerId; |
| 178 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 179 | // The optional named tag for this fragment -- usually used to find |
| 180 | // fragments that are not part of the layout. |
| 181 | String mTag; |
| 182 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 183 | // Set to true when the app has requested that this fragment be hidden |
| 184 | // from the user. |
| 185 | boolean mHidden; |
| 186 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 187 | // If set this fragment would like its instance retained across |
| 188 | // configuration changes. |
| 189 | boolean mRetainInstance; |
| 190 | |
| 191 | // If set this fragment is being retained across the current config change. |
| 192 | boolean mRetaining; |
| 193 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 194 | // If set this fragment has menu items to contribute. |
| 195 | boolean mHasMenu; |
| 196 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 197 | // Used to verify that subclasses call through to super class. |
| 198 | boolean mCalled; |
| 199 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 200 | // If app has requested a specific animation, this is the one to use. |
| 201 | int mNextAnim; |
| 202 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 203 | // The parent container of the fragment after dynamically added to UI. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 204 | ViewGroup mContainer; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 205 | |
| 206 | // The View generated for this fragment. |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 207 | View mView; |
| 208 | |
| 209 | public Fragment() { |
| 210 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 211 | |
| 212 | static Fragment instantiate(Activity activity, String fname) |
| 213 | throws NoSuchMethodException, ClassNotFoundException, |
| 214 | IllegalArgumentException, InstantiationException, |
| 215 | IllegalAccessException, InvocationTargetException { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 216 | Class clazz = sClassMap.get(fname); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 217 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 218 | if (clazz == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 219 | // Class not found in the cache, see if it's real, and try to add it |
| 220 | clazz = activity.getClassLoader().loadClass(fname); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 221 | sClassMap.put(fname, clazz); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 222 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 223 | return (Fragment)clazz.newInstance(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 224 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 225 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 226 | void restoreViewState() { |
| 227 | if (mSavedViewState != null) { |
| 228 | mView.restoreHierarchyState(mSavedViewState); |
| 229 | mSavedViewState = null; |
| 230 | } |
| 231 | } |
| 232 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 233 | void setIndex(int index) { |
| 234 | mIndex = index; |
| 235 | mWho = "android:fragment:" + mIndex; |
| 236 | } |
| 237 | |
| 238 | void clearIndex() { |
| 239 | mIndex = -1; |
| 240 | mWho = null; |
| 241 | } |
| 242 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 243 | /** |
| 244 | * Subclasses can not override equals(). |
| 245 | */ |
| 246 | @Override final public boolean equals(Object o) { |
| 247 | return super.equals(o); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Subclasses can not override hashCode(). |
| 252 | */ |
| 253 | @Override final public int hashCode() { |
| 254 | return super.hashCode(); |
| 255 | } |
| 256 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 257 | @Override |
| 258 | public String toString() { |
| 259 | StringBuilder sb = new StringBuilder(128); |
| 260 | sb.append("Fragment{"); |
| 261 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 262 | if (mIndex >= 0) { |
| 263 | sb.append(" #"); |
| 264 | sb.append(mIndex); |
| 265 | } |
| 266 | if (mFragmentId != 0) { |
| 267 | sb.append(" id=0x"); |
| 268 | sb.append(Integer.toHexString(mFragmentId)); |
| 269 | } |
| 270 | if (mTag != null) { |
| 271 | sb.append(" "); |
| 272 | sb.append(mTag); |
| 273 | } |
| 274 | sb.append('}'); |
| 275 | return sb.toString(); |
| 276 | } |
| 277 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 278 | /** |
| 279 | * Return the identifier this fragment is known by. This is either |
| 280 | * the android:id value supplied in a layout or the container view ID |
| 281 | * supplied when adding the fragment. |
| 282 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 283 | final public int getId() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 284 | return mFragmentId; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get the tag name of the fragment, if specified. |
| 289 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 290 | final public String getTag() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 291 | return mTag; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Return the Activity this fragment is currently associated with. |
| 296 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 297 | final public Activity getActivity() { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 298 | return mActivity; |
| 299 | } |
| 300 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 301 | /** |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 302 | * Return true if the fragment is currently added to its activity. |
| 303 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 304 | final public boolean isAdded() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 305 | return mActivity != null && mActivity.mFragments.mAdded.contains(this); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Return true if the fragment is currently visible to the user. This means |
| 310 | * it: (1) has been added, (2) has its view attached to the window, and |
| 311 | * (3) is not hidden. |
| 312 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 313 | final public boolean isVisible() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 314 | return isAdded() && !isHidden() && mView != null |
| 315 | && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Return true if the fragment has been hidden. By default fragments |
| 320 | * are shown. You can find out about changes to this state with |
Dianne Hackborn | cddfa6d | 2010-05-19 22:56:37 -0700 | [diff] [blame] | 321 | * {@link #onHiddenChanged}. Note that the hidden state is orthogonal |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 322 | * to other states -- that is, to be visible to the user, a fragment |
| 323 | * must be both started and not hidden. |
| 324 | */ |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 325 | final public boolean isHidden() { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 326 | return mHidden; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Called when the hidden state (as returned by {@link #isHidden()} of |
| 331 | * the fragment has changed. Fragments start out not hidden; this will |
| 332 | * be called whenever the fragment changes state from that. |
| 333 | * @param hidden True if the fragment is now hidden, false if it is not |
| 334 | * visible. |
| 335 | */ |
| 336 | public void onHiddenChanged(boolean hidden) { |
| 337 | } |
| 338 | |
| 339 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 340 | * Control whether a fragment instance is retained across Activity |
| 341 | * re-creation (such as from a configuration change). This can only |
| 342 | * be used with fragments not in the back stack. If set, the fragment |
| 343 | * lifecycle will be slightly different when an activity is recreated: |
| 344 | * <ul> |
| 345 | * <li> {@link #onDestroy()} will not be called (but {@link #onDetach()} still |
| 346 | * will be, because the fragment is being detached from its current activity). |
| 347 | * <li> {@link #onCreate(Bundle)} will not be called since the fragment |
| 348 | * is not being re-created. |
| 349 | * <li> {@link #onAttach(Activity)} and {@link #onReady(Bundle)} <b>will</b> |
| 350 | * still be called. |
| 351 | * </ul> |
| 352 | */ |
| 353 | public void setRetainInstance(boolean retain) { |
| 354 | mRetainInstance = retain; |
| 355 | } |
| 356 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 357 | final public boolean getRetainInstance() { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 358 | return mRetainInstance; |
| 359 | } |
| 360 | |
| 361 | /** |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 362 | * Report that this fragment would like to participate in populating |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame^] | 363 | * the options menu by receiving a call to {@link #onCreateOptionsMenu} |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 364 | * and related methods. |
| 365 | * |
| 366 | * @param hasMenu If true, the fragment has menu items to contribute. |
| 367 | */ |
| 368 | public void setHasOptionsMenu(boolean hasMenu) { |
| 369 | if (mHasMenu != hasMenu) { |
| 370 | mHasMenu = hasMenu; |
| 371 | if (isAdded() && !isHidden()) { |
| 372 | mActivity.invalidateOptionsMenu(); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 378 | * Call {@link Activity#startActivity(Intent)} on the fragment's |
| 379 | * containing Activity. |
| 380 | */ |
| 381 | public void startActivity(Intent intent) { |
| 382 | mActivity.startActivityFromFragment(this, intent, -1); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Call {@link Activity#startActivityForResult(Intent, int)} on the fragment's |
| 387 | * containing Activity. |
| 388 | */ |
| 389 | public void startActivityForResult(Intent intent, int requestCode) { |
| 390 | mActivity.startActivityFromFragment(this, intent, requestCode); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Receive the result from a previous call to |
| 395 | * {@link #startActivityForResult(Intent, int)}. This follows the |
| 396 | * related Activity API as described there in |
| 397 | * {@link Activity#onActivityResult(int, int, Intent)}. |
| 398 | * |
| 399 | * @param requestCode The integer request code originally supplied to |
| 400 | * startActivityForResult(), allowing you to identify who this |
| 401 | * result came from. |
| 402 | * @param resultCode The integer result code returned by the child activity |
| 403 | * through its setResult(). |
| 404 | * @param data An Intent, which can return result data to the caller |
| 405 | * (various data can be attached to Intent "extras"). |
| 406 | */ |
| 407 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 408 | } |
| 409 | |
| 410 | /** |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 411 | * 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] | 412 | * inflation, typically from setting the content view of an activity. This |
| 413 | * will be called both the first time the fragment is created, as well |
| 414 | * later when it is being re-created from its saved state (which is also |
| 415 | * given here). |
| 416 | * |
| 417 | * XXX This is kind-of yucky... maybe we could just supply the |
| 418 | * AttributeSet to onCreate()? |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 419 | * |
| 420 | * @param activity The Activity that is inflating the fragment. |
| 421 | * @param attrs The attributes at the tag where the fragment is |
| 422 | * being created. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 423 | * @param savedInstanceState If the fragment is being re-created from |
| 424 | * a previous saved state, this is the state. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 425 | */ |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 426 | public void onInflate(Activity activity, AttributeSet attrs, |
| 427 | Bundle savedInstanceState) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 428 | mCalled = true; |
| 429 | } |
| 430 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 431 | /** |
| 432 | * Called when a fragment is first attached to its activity. |
| 433 | * {@link #onCreate(Bundle)} will be called after this. |
| 434 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 435 | public void onAttach(Activity activity) { |
| 436 | mCalled = true; |
| 437 | } |
| 438 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 439 | public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 440 | return null; |
| 441 | } |
| 442 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 443 | /** |
| 444 | * Called to do initial creation of a fragment. This is called after |
| 445 | * {@link #onAttach(Activity)} and before {@link #onReady(Bundle)}. |
| 446 | * @param savedInstanceState If the fragment is being re-created from |
| 447 | * a previous saved state, this is the state. |
| 448 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 449 | public void onCreate(Bundle savedInstanceState) { |
| 450 | mCalled = true; |
| 451 | } |
| 452 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 453 | /** |
| 454 | * Called to have the fragment instantiate its user interface view. |
| 455 | * This is optional, and non-graphical fragments can return null (which |
| 456 | * is the default implementation). This will be called between |
| 457 | * {@link #onCreate(Bundle)} and {@link #onReady(Bundle)}. |
| 458 | * |
| 459 | * @param inflater The LayoutInflater object that can be used to inflate |
| 460 | * any views in the fragment, |
| 461 | * @param container If non-null, this is the parent view that the fragment's |
| 462 | * UI should be attached to. The fragment should not add the view itself, |
| 463 | * but this can be used to generate the LayoutParams of the view. |
| 464 | * @param savedInstanceState If non-null, this fragment is being re-constructed |
| 465 | * from a previous saved state as given here. |
| 466 | * |
| 467 | * @return Return the View for the fragment's UI, or null. |
| 468 | */ |
| 469 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 470 | Bundle savedInstanceState) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 471 | return null; |
| 472 | } |
| 473 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 474 | public View getView() { |
| 475 | return mView; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Called when the activity is ready for the fragment to run. This is |
| 480 | * most useful for fragments that use {@link #setRetainInstance(boolean)} |
| 481 | * instance, as this tells the fragment when it is fully associated with |
| 482 | * the new activity instance. This is called after {@link #onCreate(Bundle)} |
| 483 | * and before {@link #onStart()}. |
| 484 | * |
| 485 | * @param savedInstanceState If the fragment is being re-created from |
| 486 | * a previous saved state, this is the state. |
| 487 | */ |
| 488 | public void onReady(Bundle savedInstanceState) { |
| 489 | mCalled = true; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Called when the Fragment is visible to the user. This is generally |
| 494 | * tied to {@link Activity#onStart() Activity.onStart} of the containing |
| 495 | * Activity's lifecycle. |
| 496 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 497 | public void onStart() { |
| 498 | mCalled = true; |
| 499 | } |
| 500 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 501 | /** |
| 502 | * Called when the fragment is visible to the user and actively running. |
| 503 | * This is generally |
| 504 | * tied to {@link Activity#onResume() Activity.onResume} of the containing |
| 505 | * Activity's lifecycle. |
| 506 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 507 | public void onResume() { |
| 508 | mCalled = true; |
| 509 | } |
| 510 | |
| 511 | public void onSaveInstanceState(Bundle outState) { |
| 512 | } |
| 513 | |
| 514 | public void onConfigurationChanged(Configuration newConfig) { |
| 515 | mCalled = true; |
| 516 | } |
| 517 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 518 | /** |
| 519 | * Called when the Fragment is no longer resumed. This is generally |
| 520 | * tied to {@link Activity#onPause() Activity.onPause} of the containing |
| 521 | * Activity's lifecycle. |
| 522 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 523 | public void onPause() { |
| 524 | mCalled = true; |
| 525 | } |
| 526 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 527 | /** |
| 528 | * Called when the Fragment is no longer started. This is generally |
| 529 | * tied to {@link Activity#onStop() Activity.onStop} of the containing |
| 530 | * Activity's lifecycle. |
| 531 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 532 | public void onStop() { |
| 533 | mCalled = true; |
| 534 | } |
| 535 | |
| 536 | public void onLowMemory() { |
| 537 | mCalled = true; |
| 538 | } |
| 539 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 540 | /** |
| 541 | * Called when the fragment is no longer in use. This is called |
| 542 | * after {@link #onStop()} and before {@link #onDetach()}. |
| 543 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 544 | public void onDestroy() { |
| 545 | mCalled = true; |
| 546 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 547 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 548 | /** |
| 549 | * Called when the fragment is no longer attached to its activity. This |
| 550 | * is called after {@link #onDestroy()}. |
| 551 | */ |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 552 | public void onDetach() { |
| 553 | mCalled = true; |
| 554 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 555 | |
| 556 | /** |
| 557 | * Initialize the contents of the Activity's standard options menu. You |
| 558 | * should place your menu items in to <var>menu</var>. For this method |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame^] | 559 | * to be called, you must have first called {@link #setHasOptionsMenu}. |
| 560 | * See {@link Activity#onCreateOptionsMenu} for more information. |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 561 | * |
| 562 | * @param menu The options menu in which you place your items. |
| 563 | * |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame^] | 564 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 565 | * @see #onPrepareOptionsMenu |
| 566 | * @see #onOptionsItemSelected |
| 567 | */ |
| 568 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Prepare the Screen's standard options menu to be displayed. This is |
| 573 | * called right before the menu is shown, every time it is shown. You can |
| 574 | * use this method to efficiently enable/disable items or otherwise |
| 575 | * dynamically modify the contents. See |
| 576 | * {@link Activity#onPrepareOptionsMenu(Menu) Activity.onPrepareOptionsMenu} |
| 577 | * for more information. |
| 578 | * |
| 579 | * @param menu The options menu as last shown or first initialized by |
| 580 | * onCreateOptionsMenu(). |
| 581 | * |
Wink Saville | 4dc643e | 2010-06-12 22:16:41 -0700 | [diff] [blame^] | 582 | * @see #setHasOptionsMenu |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 583 | * @see #onCreateOptionsMenu |
| 584 | */ |
| 585 | public void onPrepareOptionsMenu(Menu menu) { |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * This hook is called whenever an item in your options menu is selected. |
| 590 | * The default implementation simply returns false to have the normal |
| 591 | * processing happen (calling the item's Runnable or sending a message to |
| 592 | * its Handler as appropriate). You can use this method for any items |
| 593 | * for which you would like to do processing without those other |
| 594 | * facilities. |
| 595 | * |
| 596 | * <p>Derived classes should call through to the base class for it to |
| 597 | * perform the default menu handling. |
| 598 | * |
| 599 | * @param item The menu item that was selected. |
| 600 | * |
| 601 | * @return boolean Return false to allow normal menu processing to |
| 602 | * proceed, true to consume it here. |
| 603 | * |
| 604 | * @see #onCreateOptionsMenu |
| 605 | */ |
| 606 | public boolean onOptionsItemSelected(MenuItem item) { |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 612 | * the menu with the back/menu button, or when an item is selected). |
| 613 | * |
| 614 | * @param menu The options menu as last shown or first initialized by |
| 615 | * onCreateOptionsMenu(). |
| 616 | */ |
| 617 | public void onOptionsMenuClosed(Menu menu) { |
| 618 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 619 | } |