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 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 19 | import android.content.res.TypedArray; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 24 | import android.util.Log; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 25 | import android.util.SparseArray; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 26 | import android.view.Menu; |
| 27 | import android.view.MenuInflater; |
| 28 | import android.view.MenuItem; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 29 | import android.view.View; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 30 | import android.view.ViewGroup; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 31 | import android.view.animation.Animation; |
| 32 | import android.view.animation.AnimationUtils; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 36 | final class FragmentManagerState implements Parcelable { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 37 | FragmentState[] mActive; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 38 | int[] mAdded; |
| 39 | BackStackState[] mBackStack; |
| 40 | |
| 41 | public FragmentManagerState() { |
| 42 | } |
| 43 | |
| 44 | public FragmentManagerState(Parcel in) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 45 | mActive = in.createTypedArray(FragmentState.CREATOR); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 46 | mAdded = in.createIntArray(); |
| 47 | mBackStack = in.createTypedArray(BackStackState.CREATOR); |
| 48 | } |
| 49 | |
| 50 | public int describeContents() { |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 55 | dest.writeTypedArray(mActive, flags); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 56 | dest.writeIntArray(mAdded); |
| 57 | dest.writeTypedArray(mBackStack, flags); |
| 58 | } |
| 59 | |
| 60 | public static final Parcelable.Creator<FragmentManagerState> CREATOR |
| 61 | = new Parcelable.Creator<FragmentManagerState>() { |
| 62 | public FragmentManagerState createFromParcel(Parcel in) { |
| 63 | return new FragmentManagerState(in); |
| 64 | } |
| 65 | |
| 66 | public FragmentManagerState[] newArray(int size) { |
| 67 | return new FragmentManagerState[size]; |
| 68 | } |
| 69 | }; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 72 | /** |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 73 | * @hide |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 74 | * Container for fragments associated with an activity. |
| 75 | */ |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 76 | public class FragmentManager { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 77 | static final boolean DEBUG = true; |
| 78 | static final String TAG = "FragmentManager"; |
| 79 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 80 | ArrayList<Runnable> mPendingActions; |
| 81 | Runnable[] mTmpActions; |
| 82 | boolean mExecutingActions; |
| 83 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 84 | ArrayList<Fragment> mActive; |
| 85 | ArrayList<Fragment> mAdded; |
| 86 | ArrayList<Integer> mAvailIndices; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 87 | ArrayList<BackStackEntry> mBackStack; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 88 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 89 | // Must be accessed while locked. |
| 90 | ArrayList<BackStackEntry> mBackStackIndices; |
| 91 | ArrayList<Integer> mAvailBackStackIndices; |
| 92 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 93 | int mCurState = Fragment.INITIALIZING; |
| 94 | Activity mActivity; |
| 95 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 96 | boolean mNeedMenuInvalidate; |
| 97 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 98 | // Temporary vars for state save and restore. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 99 | Bundle mStateBundle = null; |
| 100 | SparseArray<Parcelable> mStateArray = null; |
| 101 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 102 | Runnable mExecCommit = new Runnable() { |
| 103 | @Override |
| 104 | public void run() { |
| 105 | execPendingActions(); |
| 106 | } |
| 107 | }; |
| 108 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 109 | Animation loadAnimation(Fragment fragment, int transit, boolean enter, |
| 110 | int transitionStyle) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 111 | Animation animObj = fragment.onCreateAnimation(transitionStyle, enter, |
| 112 | fragment.mNextAnim); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 113 | if (animObj != null) { |
| 114 | return animObj; |
| 115 | } |
| 116 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 117 | if (fragment.mNextAnim != 0) { |
| 118 | Animation anim = AnimationUtils.loadAnimation(mActivity, fragment.mNextAnim); |
| 119 | if (anim != null) { |
| 120 | return anim; |
| 121 | } |
| 122 | } |
| 123 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 124 | if (transit == 0) { |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | int styleIndex = transitToStyleIndex(transit, enter); |
| 129 | if (styleIndex < 0) { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | if (transitionStyle == 0 && mActivity.getWindow() != null) { |
| 134 | transitionStyle = mActivity.getWindow().getAttributes().windowAnimations; |
| 135 | } |
| 136 | if (transitionStyle == 0) { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle, |
| 141 | com.android.internal.R.styleable.WindowAnimation); |
| 142 | int anim = attrs.getResourceId(styleIndex, 0); |
| 143 | attrs.recycle(); |
| 144 | |
| 145 | if (anim == 0) { |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | return AnimationUtils.loadAnimation(mActivity, anim); |
| 150 | } |
| 151 | |
| 152 | void moveToState(Fragment f, int newState, int transit, int transitionStyle) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 153 | // Fragments that are not currently added will sit in the onCreate() state. |
| 154 | if (!f.mAdded && newState > Fragment.CREATED) { |
| 155 | newState = Fragment.CREATED; |
| 156 | } |
| 157 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 158 | if (f.mState < newState) { |
| 159 | switch (f.mState) { |
| 160 | case Fragment.INITIALIZING: |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 161 | if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 162 | f.mActivity = mActivity; |
| 163 | f.mCalled = false; |
| 164 | f.onAttach(mActivity); |
| 165 | if (!f.mCalled) { |
| 166 | throw new SuperNotCalledException("Fragment " + f |
| 167 | + " did not call through to super.onAttach()"); |
| 168 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 169 | mActivity.onAttachFragment(f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 170 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 171 | if (!f.mRetaining) { |
| 172 | f.mCalled = false; |
| 173 | f.onCreate(f.mSavedFragmentState); |
| 174 | if (!f.mCalled) { |
| 175 | throw new SuperNotCalledException("Fragment " + f |
| 176 | + " did not call through to super.onCreate()"); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 179 | f.mRetaining = false; |
| 180 | if (f.mFromLayout) { |
| 181 | // For fragments that are part of the content view |
| 182 | // layout, we need to instantiate the view immediately |
| 183 | // and the inflater will take care of adding it. |
| 184 | f.mView = f.onCreateView(mActivity.getLayoutInflater(), |
| 185 | null, f.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 186 | if (f.mView != null) { |
| 187 | f.mView.setSaveFromParentEnabled(false); |
| 188 | f.restoreViewState(); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 189 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 190 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 191 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 192 | case Fragment.CREATED: |
| 193 | if (newState > Fragment.CREATED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 194 | if (DEBUG) Log.v(TAG, "moveto CONTENT: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 195 | if (!f.mFromLayout) { |
| 196 | ViewGroup container = null; |
| 197 | if (f.mContainerId != 0) { |
| 198 | container = (ViewGroup)mActivity.findViewById(f.mContainerId); |
| 199 | if (container == null) { |
| 200 | throw new IllegalArgumentException("New view found for id 0x" |
| 201 | + Integer.toHexString(f.mContainerId) |
| 202 | + " for fragment " + f); |
| 203 | } |
| 204 | } |
| 205 | f.mContainer = container; |
| 206 | f.mView = f.onCreateView(mActivity.getLayoutInflater(), |
| 207 | container, f.mSavedFragmentState); |
| 208 | if (f.mView != null) { |
| 209 | f.mView.setSaveFromParentEnabled(false); |
| 210 | if (container != null) { |
| 211 | Animation anim = loadAnimation(f, transit, true, |
| 212 | transitionStyle); |
| 213 | if (anim != null) { |
| 214 | f.mView.setAnimation(anim); |
| 215 | } |
| 216 | container.addView(f.mView); |
| 217 | f.restoreViewState(); |
| 218 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 219 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | f.mCalled = false; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 224 | f.onActivityCreated(f.mSavedFragmentState); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 225 | if (!f.mCalled) { |
| 226 | throw new SuperNotCalledException("Fragment " + f |
| 227 | + " did not call through to super.onReady()"); |
| 228 | } |
| 229 | f.mSavedFragmentState = null; |
| 230 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 231 | case Fragment.ACTIVITY_CREATED: |
| 232 | if (newState > Fragment.ACTIVITY_CREATED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 233 | if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 234 | f.mCalled = false; |
| 235 | f.onStart(); |
| 236 | if (!f.mCalled) { |
| 237 | throw new SuperNotCalledException("Fragment " + f |
| 238 | + " did not call through to super.onStart()"); |
| 239 | } |
| 240 | } |
| 241 | case Fragment.STARTED: |
| 242 | if (newState > Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 243 | if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 244 | f.mCalled = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 245 | f.mResumed = true; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 246 | f.onResume(); |
| 247 | if (!f.mCalled) { |
| 248 | throw new SuperNotCalledException("Fragment " + f |
| 249 | + " did not call through to super.onResume()"); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } else if (f.mState > newState) { |
| 254 | switch (f.mState) { |
| 255 | case Fragment.RESUMED: |
| 256 | if (newState < Fragment.RESUMED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 257 | if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 258 | f.mCalled = false; |
| 259 | f.onPause(); |
| 260 | if (!f.mCalled) { |
| 261 | throw new SuperNotCalledException("Fragment " + f |
| 262 | + " did not call through to super.onPause()"); |
| 263 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 264 | f.mResumed = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 265 | } |
| 266 | case Fragment.STARTED: |
| 267 | if (newState < Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 268 | if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 269 | f.mCalled = false; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 270 | f.performStop(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 271 | if (!f.mCalled) { |
| 272 | throw new SuperNotCalledException("Fragment " + f |
| 273 | + " did not call through to super.onStop()"); |
| 274 | } |
| 275 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 276 | case Fragment.ACTIVITY_CREATED: |
| 277 | if (newState < Fragment.ACTIVITY_CREATED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 278 | if (DEBUG) Log.v(TAG, "movefrom CONTENT: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 279 | if (f.mView != null) { |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 280 | f.mCalled = false; |
| 281 | f.onDestroyView(); |
| 282 | if (!f.mCalled) { |
| 283 | throw new SuperNotCalledException("Fragment " + f |
| 284 | + " did not call through to super.onDestroyedView()"); |
| 285 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 286 | // Need to save the current view state if not |
| 287 | // done already. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 288 | if (!mActivity.isFinishing() && f.mSavedFragmentState == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 289 | saveFragmentViewState(f); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 290 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 291 | if (f.mContainer != null) { |
| 292 | if (mCurState > Fragment.INITIALIZING) { |
| 293 | Animation anim = loadAnimation(f, transit, false, |
| 294 | transitionStyle); |
| 295 | if (anim != null) { |
| 296 | f.mView.setAnimation(anim); |
| 297 | } |
| 298 | } |
| 299 | f.mContainer.removeView(f.mView); |
| 300 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 301 | } |
| 302 | f.mContainer = null; |
| 303 | f.mView = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 304 | } |
| 305 | case Fragment.CREATED: |
| 306 | if (newState < Fragment.CREATED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 307 | if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 308 | if (!f.mRetaining) { |
| 309 | f.mCalled = false; |
| 310 | f.onDestroy(); |
| 311 | if (!f.mCalled) { |
| 312 | throw new SuperNotCalledException("Fragment " + f |
| 313 | + " did not call through to super.onDestroy()"); |
| 314 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 315 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 316 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 317 | f.mCalled = false; |
| 318 | f.onDetach(); |
| 319 | if (!f.mCalled) { |
| 320 | throw new SuperNotCalledException("Fragment " + f |
| 321 | + " did not call through to super.onDetach()"); |
| 322 | } |
| 323 | f.mActivity = null; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | f.mState = newState; |
| 329 | } |
| 330 | |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 331 | void moveToState(int newState, boolean always) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 332 | moveToState(newState, 0, 0, always); |
| 333 | } |
| 334 | |
| 335 | void moveToState(int newState, int transit, int transitStyle, boolean always) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 336 | if (mActivity == null && newState != Fragment.INITIALIZING) { |
| 337 | throw new IllegalStateException("No activity"); |
| 338 | } |
| 339 | |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 340 | if (!always && mCurState == newState) { |
| 341 | return; |
| 342 | } |
| 343 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 344 | mCurState = newState; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 345 | if (mActive != null) { |
| 346 | for (int i=0; i<mActive.size(); i++) { |
| 347 | Fragment f = mActive.get(i); |
| 348 | if (f != null) { |
| 349 | moveToState(f, newState, transit, transitStyle); |
| 350 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 351 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 355 | void makeActive(Fragment f) { |
| 356 | if (f.mIndex >= 0) { |
| 357 | return; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 358 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 359 | |
| 360 | if (mAvailIndices == null || mAvailIndices.size() <= 0) { |
| 361 | if (mActive == null) { |
| 362 | mActive = new ArrayList<Fragment>(); |
| 363 | } |
| 364 | f.setIndex(mActive.size()); |
| 365 | mActive.add(f); |
| 366 | |
| 367 | } else { |
| 368 | f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1)); |
| 369 | mActive.set(f.mIndex, f); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void makeInactive(Fragment f) { |
| 374 | if (f.mIndex < 0) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | mActive.set(f.mIndex, null); |
| 379 | if (mAvailIndices == null) { |
| 380 | mAvailIndices = new ArrayList<Integer>(); |
| 381 | } |
| 382 | mAvailIndices.add(f.mIndex); |
Dianne Hackborn | 9e14e9f3 | 2010-07-14 11:07:38 -0700 | [diff] [blame] | 383 | mActivity.invalidateFragmentIndex(f.mIndex); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 384 | f.clearIndex(); |
| 385 | } |
| 386 | |
| 387 | public void addFragment(Fragment fragment, boolean moveToStateNow) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 388 | if (DEBUG) Log.v(TAG, "add: " + fragment); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 389 | if (mAdded == null) { |
| 390 | mAdded = new ArrayList<Fragment>(); |
| 391 | } |
| 392 | mAdded.add(fragment); |
| 393 | makeActive(fragment); |
| 394 | fragment.mAdded = true; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 395 | if (fragment.mHasMenu) { |
| 396 | mNeedMenuInvalidate = true; |
| 397 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 398 | if (moveToStateNow) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 399 | moveToState(fragment, mCurState, 0, 0); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 400 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 403 | public void removeFragment(Fragment fragment, int transition, int transitionStyle) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 404 | if (DEBUG) Log.v(TAG, "remove: " + fragment); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 405 | mAdded.remove(fragment); |
| 406 | final boolean inactive = fragment.mBackStackNesting <= 0; |
| 407 | if (inactive) { |
| 408 | makeInactive(fragment); |
| 409 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 410 | if (fragment.mHasMenu) { |
| 411 | mNeedMenuInvalidate = true; |
| 412 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 413 | fragment.mAdded = false; |
| 414 | moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, |
| 415 | transition, transitionStyle); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 418 | public void hideFragment(Fragment fragment, int transition, int transitionStyle) { |
| 419 | if (DEBUG) Log.v(TAG, "hide: " + fragment); |
| 420 | if (!fragment.mHidden) { |
| 421 | fragment.mHidden = true; |
| 422 | if (fragment.mView != null) { |
| 423 | Animation anim = loadAnimation(fragment, transition, false, |
| 424 | transitionStyle); |
| 425 | if (anim != null) { |
| 426 | fragment.mView.setAnimation(anim); |
| 427 | } |
| 428 | fragment.mView.setVisibility(View.GONE); |
| 429 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 430 | if (fragment.mAdded && fragment.mHasMenu) { |
| 431 | mNeedMenuInvalidate = true; |
| 432 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 433 | fragment.onHiddenChanged(true); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | public void showFragment(Fragment fragment, int transition, int transitionStyle) { |
| 438 | if (DEBUG) Log.v(TAG, "show: " + fragment); |
| 439 | if (fragment.mHidden) { |
| 440 | fragment.mHidden = false; |
| 441 | if (fragment.mView != null) { |
| 442 | Animation anim = loadAnimation(fragment, transition, true, |
| 443 | transitionStyle); |
| 444 | if (anim != null) { |
| 445 | fragment.mView.setAnimation(anim); |
| 446 | } |
| 447 | fragment.mView.setVisibility(View.VISIBLE); |
| 448 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 449 | if (fragment.mAdded && fragment.mHasMenu) { |
| 450 | mNeedMenuInvalidate = true; |
| 451 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 452 | fragment.onHiddenChanged(false); |
| 453 | } |
| 454 | } |
| 455 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 456 | public Fragment findFragmentById(int id) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 457 | if (mActive != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 458 | // First look through added fragments. |
| 459 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 460 | Fragment f = mAdded.get(i); |
| 461 | if (f != null && f.mFragmentId == id) { |
| 462 | return f; |
| 463 | } |
| 464 | } |
| 465 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 466 | for (int i=mActive.size()-1; i>=0; i--) { |
| 467 | Fragment f = mActive.get(i); |
| 468 | if (f != null && f.mFragmentId == id) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 469 | return f; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | return null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 476 | public Fragment findFragmentByTag(String tag) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 477 | if (mActive != null && tag != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 478 | // First look through added fragments. |
| 479 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 480 | Fragment f = mAdded.get(i); |
| 481 | if (f != null && tag.equals(f.mTag)) { |
| 482 | return f; |
| 483 | } |
| 484 | } |
| 485 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 486 | for (int i=mActive.size()-1; i>=0; i--) { |
| 487 | Fragment f = mActive.get(i); |
| 488 | if (f != null && tag.equals(f.mTag)) { |
| 489 | return f; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | return null; |
| 494 | } |
| 495 | |
| 496 | public Fragment findFragmentByWho(String who) { |
| 497 | if (mActive != null && who != null) { |
| 498 | for (int i=mActive.size()-1; i>=0; i--) { |
| 499 | Fragment f = mActive.get(i); |
| 500 | if (f != null && who.equals(f.mWho)) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 501 | return f; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | return null; |
| 506 | } |
| 507 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 508 | public void enqueueAction(Runnable action) { |
| 509 | synchronized (this) { |
| 510 | if (mPendingActions == null) { |
| 511 | mPendingActions = new ArrayList<Runnable>(); |
| 512 | } |
| 513 | mPendingActions.add(action); |
| 514 | if (mPendingActions.size() == 1) { |
| 515 | mActivity.mHandler.removeCallbacks(mExecCommit); |
| 516 | mActivity.mHandler.post(mExecCommit); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 521 | public int allocBackStackIndex(BackStackEntry bse) { |
| 522 | synchronized (this) { |
| 523 | if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { |
| 524 | if (mBackStackIndices == null) { |
| 525 | mBackStackIndices = new ArrayList<BackStackEntry>(); |
| 526 | } |
| 527 | int index = mBackStackIndices.size(); |
| 528 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 529 | mBackStackIndices.add(bse); |
| 530 | return index; |
| 531 | |
| 532 | } else { |
| 533 | int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); |
| 534 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 535 | mBackStackIndices.set(index, bse); |
| 536 | return index; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | public void setBackStackIndex(int index, BackStackEntry bse) { |
| 542 | synchronized (this) { |
| 543 | if (mBackStackIndices == null) { |
| 544 | mBackStackIndices = new ArrayList<BackStackEntry>(); |
| 545 | } |
| 546 | int N = mBackStackIndices.size(); |
| 547 | if (index < N) { |
| 548 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 549 | mBackStackIndices.set(index, bse); |
| 550 | } else { |
| 551 | while (N < index) { |
| 552 | mBackStackIndices.add(null); |
| 553 | if (mAvailBackStackIndices == null) { |
| 554 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 555 | } |
| 556 | if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); |
| 557 | mAvailBackStackIndices.add(N); |
| 558 | N++; |
| 559 | } |
| 560 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 561 | mBackStackIndices.add(bse); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | public void freeBackStackIndex(int index) { |
| 567 | synchronized (this) { |
| 568 | mBackStackIndices.set(index, null); |
| 569 | if (mAvailBackStackIndices == null) { |
| 570 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 571 | } |
| 572 | if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); |
| 573 | mAvailBackStackIndices.add(index); |
| 574 | } |
| 575 | } |
| 576 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 577 | /** |
| 578 | * Only call from main thread! |
| 579 | */ |
| 580 | public void execPendingActions() { |
| 581 | if (mExecutingActions) { |
| 582 | throw new IllegalStateException("Recursive entry to execPendingActions"); |
| 583 | } |
| 584 | |
| 585 | while (true) { |
| 586 | int numActions; |
| 587 | |
| 588 | synchronized (this) { |
| 589 | if (mPendingActions == null || mPendingActions.size() == 0) { |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | numActions = mPendingActions.size(); |
| 594 | if (mTmpActions == null || mTmpActions.length < numActions) { |
| 595 | mTmpActions = new Runnable[numActions]; |
| 596 | } |
| 597 | mPendingActions.toArray(mTmpActions); |
| 598 | mPendingActions.clear(); |
| 599 | mActivity.mHandler.removeCallbacks(mExecCommit); |
| 600 | } |
| 601 | |
| 602 | mExecutingActions = true; |
| 603 | for (int i=0; i<numActions; i++) { |
| 604 | mTmpActions[i].run(); |
| 605 | } |
| 606 | mExecutingActions = false; |
| 607 | } |
| 608 | } |
| 609 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 610 | public void addBackStackState(BackStackEntry state) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 611 | if (mBackStack == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 612 | mBackStack = new ArrayList<BackStackEntry>(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 613 | } |
| 614 | mBackStack.add(state); |
| 615 | } |
| 616 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 617 | public boolean popBackStackState(Handler handler, String name, int flags) { |
| 618 | return popBackStackState(handler, name, -1, flags); |
| 619 | } |
| 620 | |
| 621 | public boolean popBackStackState(Handler handler, int id, int flags) { |
| 622 | if (id < 0) { |
| 623 | return false; |
| 624 | } |
| 625 | return popBackStackState(handler, null, id, flags); |
| 626 | } |
| 627 | |
| 628 | boolean popBackStackState(Handler handler, String name, int id, int flags) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 629 | if (mBackStack == null) { |
| 630 | return false; |
| 631 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 632 | if (name == null && id < 0) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 633 | int last = mBackStack.size()-1; |
| 634 | if (last < 0) { |
| 635 | return false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 636 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 637 | final BackStackEntry bss = mBackStack.remove(last); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 638 | enqueueAction(new Runnable() { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 639 | public void run() { |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 640 | if (DEBUG) Log.v(TAG, "Popping back stack state: " + bss); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 641 | bss.popFromBackStack(); |
| 642 | moveToState(mCurState, reverseTransit(bss.getTransition()), |
| 643 | bss.getTransitionStyle(), true); |
| 644 | } |
| 645 | }); |
| 646 | } else { |
| 647 | int index = mBackStack.size()-1; |
| 648 | while (index >= 0) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 649 | BackStackEntry bss = mBackStack.get(index); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 650 | if (name != null && name.equals(bss.getName())) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 651 | break; |
| 652 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 653 | if (id >= 0 && id == bss.mIndex) { |
| 654 | break; |
| 655 | } |
| 656 | index--; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 657 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 658 | if (index < 0) { |
| 659 | return false; |
| 660 | } |
| 661 | if ((flags&Activity.POP_BACK_STACK_INCLUSIVE) != 0) { |
| 662 | index--; |
| 663 | } |
| 664 | if (index == mBackStack.size()-1) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 665 | return false; |
| 666 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 667 | final ArrayList<BackStackEntry> states |
| 668 | = new ArrayList<BackStackEntry>(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 669 | for (int i=mBackStack.size()-1; i>index; i--) { |
| 670 | states.add(mBackStack.remove(i)); |
| 671 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 672 | enqueueAction(new Runnable() { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 673 | public void run() { |
| 674 | for (int i=0; i<states.size(); i++) { |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 675 | if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i)); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 676 | states.get(i).popFromBackStack(); |
| 677 | } |
| 678 | moveToState(mCurState, true); |
| 679 | } |
| 680 | }); |
| 681 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 682 | return true; |
| 683 | } |
| 684 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 685 | ArrayList<Fragment> retainNonConfig() { |
| 686 | ArrayList<Fragment> fragments = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 687 | if (mActive != null) { |
| 688 | for (int i=0; i<mActive.size(); i++) { |
| 689 | Fragment f = mActive.get(i); |
| 690 | if (f != null && f.mRetainInstance) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 691 | if (fragments == null) { |
| 692 | fragments = new ArrayList<Fragment>(); |
| 693 | } |
| 694 | fragments.add(f); |
| 695 | f.mRetaining = true; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | return fragments; |
| 700 | } |
| 701 | |
| 702 | void saveFragmentViewState(Fragment f) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 703 | if (f.mView == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | if (mStateArray == null) { |
| 707 | mStateArray = new SparseArray<Parcelable>(); |
| 708 | } |
| 709 | f.mView.saveHierarchyState(mStateArray); |
| 710 | if (mStateArray.size() > 0) { |
| 711 | f.mSavedViewState = mStateArray; |
| 712 | mStateArray = null; |
| 713 | } |
| 714 | } |
| 715 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 716 | Parcelable saveAllState() { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 717 | if (mActive == null || mActive.size() <= 0) { |
| 718 | return null; |
| 719 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 720 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 721 | // First collect all active fragments. |
| 722 | int N = mActive.size(); |
| 723 | FragmentState[] active = new FragmentState[N]; |
| 724 | boolean haveFragments = false; |
| 725 | for (int i=0; i<N; i++) { |
| 726 | Fragment f = mActive.get(i); |
| 727 | if (f != null) { |
| 728 | haveFragments = true; |
| 729 | |
| 730 | FragmentState fs = new FragmentState(f); |
| 731 | active[i] = fs; |
| 732 | |
| 733 | if (mStateBundle == null) { |
| 734 | mStateBundle = new Bundle(); |
| 735 | } |
| 736 | f.onSaveInstanceState(mStateBundle); |
| 737 | if (!mStateBundle.isEmpty()) { |
| 738 | fs.mSavedFragmentState = mStateBundle; |
| 739 | mStateBundle = null; |
| 740 | } |
| 741 | |
| 742 | if (f.mView != null) { |
| 743 | saveFragmentViewState(f); |
| 744 | if (f.mSavedViewState != null) { |
| 745 | if (fs.mSavedFragmentState == null) { |
| 746 | fs.mSavedFragmentState = new Bundle(); |
| 747 | } |
| 748 | fs.mSavedFragmentState.putSparseParcelableArray( |
| 749 | FragmentState.VIEW_STATE_TAG, f.mSavedViewState); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | if (!haveFragments) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 757 | return null; |
| 758 | } |
| 759 | |
| 760 | int[] added = null; |
| 761 | BackStackState[] backStack = null; |
| 762 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 763 | // Build list of currently added fragments. |
| 764 | N = mAdded.size(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 765 | if (N > 0) { |
| 766 | added = new int[N]; |
| 767 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 768 | added[i] = mAdded.get(i).mIndex; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | // Now save back stack. |
| 773 | if (mBackStack != null) { |
| 774 | N = mBackStack.size(); |
| 775 | if (N > 0) { |
| 776 | backStack = new BackStackState[N]; |
| 777 | for (int i=0; i<N; i++) { |
| 778 | backStack[i] = new BackStackState(this, mBackStack.get(i)); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 783 | FragmentManagerState fms = new FragmentManagerState(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 784 | fms.mActive = active; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 785 | fms.mAdded = added; |
| 786 | fms.mBackStack = backStack; |
| 787 | return fms; |
| 788 | } |
| 789 | |
| 790 | void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) { |
| 791 | // If there is no saved state at all, then there can not be |
| 792 | // any nonConfig fragments either, so that is that. |
| 793 | if (state == null) return; |
| 794 | FragmentManagerState fms = (FragmentManagerState)state; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 795 | if (fms.mActive == null) return; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 796 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 797 | // First re-attach any non-config instances we are retaining back |
| 798 | // to their saved state, so we don't try to instantiate them again. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 799 | if (nonConfig != null) { |
| 800 | for (int i=0; i<nonConfig.size(); i++) { |
| 801 | Fragment f = nonConfig.get(i); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 802 | FragmentState fs = fms.mActive[f.mIndex]; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 803 | fs.mInstance = f; |
| 804 | f.mSavedViewState = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 805 | f.mBackStackNesting = 0; |
| 806 | f.mAdded = false; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 807 | if (fs.mSavedFragmentState != null) { |
| 808 | f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( |
| 809 | FragmentState.VIEW_STATE_TAG); |
| 810 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 814 | // Build the full list of active fragments, instantiating them from |
| 815 | // their saved state. |
| 816 | mActive = new ArrayList<Fragment>(fms.mActive.length); |
| 817 | if (mAvailIndices != null) { |
| 818 | mAvailIndices.clear(); |
| 819 | } |
| 820 | for (int i=0; i<fms.mActive.length; i++) { |
| 821 | FragmentState fs = fms.mActive[i]; |
| 822 | if (fs != null) { |
| 823 | mActive.add(fs.instantiate(mActivity)); |
| 824 | } else { |
| 825 | mActive.add(null); |
| 826 | if (mAvailIndices == null) { |
| 827 | mAvailIndices = new ArrayList<Integer>(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 828 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 829 | mAvailIndices.add(i); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 832 | |
| 833 | // Build the list of currently added fragments. |
| 834 | if (fms.mAdded != null) { |
| 835 | mAdded = new ArrayList<Fragment>(fms.mAdded.length); |
| 836 | for (int i=0; i<fms.mAdded.length; i++) { |
| 837 | Fragment f = mActive.get(fms.mAdded[i]); |
| 838 | if (f == null) { |
| 839 | throw new IllegalStateException( |
| 840 | "No instantiated fragment for index #" + fms.mAdded[i]); |
| 841 | } |
| 842 | f.mAdded = true; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 843 | f.mImmediateActivity = mActivity; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 844 | mAdded.add(f); |
| 845 | } |
| 846 | } else { |
| 847 | mAdded = null; |
| 848 | } |
| 849 | |
| 850 | // Build the back stack. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 851 | if (fms.mBackStack != null) { |
| 852 | mBackStack = new ArrayList<BackStackEntry>(fms.mBackStack.length); |
| 853 | for (int i=0; i<fms.mBackStack.length; i++) { |
| 854 | BackStackEntry bse = fms.mBackStack[i].instantiate(this); |
| 855 | mBackStack.add(bse); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame^] | 856 | if (bse.mIndex >= 0) { |
| 857 | setBackStackIndex(bse.mIndex, bse); |
| 858 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 859 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 860 | } else { |
| 861 | mBackStack = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 865 | public void attachActivity(Activity activity) { |
| 866 | if (mActivity != null) throw new IllegalStateException(); |
| 867 | mActivity = activity; |
| 868 | } |
| 869 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 870 | public void dispatchCreate() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 871 | moveToState(Fragment.CREATED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 874 | public void dispatchActivityCreated() { |
| 875 | moveToState(Fragment.ACTIVITY_CREATED, false); |
| 876 | } |
| 877 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 878 | public void dispatchStart() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 879 | moveToState(Fragment.STARTED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | public void dispatchResume() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 883 | moveToState(Fragment.RESUMED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | public void dispatchPause() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 887 | moveToState(Fragment.STARTED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | public void dispatchStop() { |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 891 | moveToState(Fragment.ACTIVITY_CREATED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | public void dispatchDestroy() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 895 | moveToState(Fragment.INITIALIZING, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 896 | mActivity = null; |
| 897 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 898 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 899 | public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 900 | boolean show = false; |
| 901 | if (mActive != null) { |
| 902 | for (int i=0; i<mAdded.size(); i++) { |
| 903 | Fragment f = mAdded.get(i); |
| 904 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 905 | show = true; |
| 906 | f.onCreateOptionsMenu(menu, inflater); |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | return show; |
| 911 | } |
| 912 | |
| 913 | public boolean dispatchPrepareOptionsMenu(Menu menu) { |
| 914 | boolean show = false; |
| 915 | if (mActive != null) { |
| 916 | for (int i=0; i<mAdded.size(); i++) { |
| 917 | Fragment f = mAdded.get(i); |
| 918 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 919 | show = true; |
| 920 | f.onPrepareOptionsMenu(menu); |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | return show; |
| 925 | } |
| 926 | |
| 927 | public boolean dispatchOptionsItemSelected(MenuItem item) { |
| 928 | if (mActive != null) { |
| 929 | for (int i=0; i<mAdded.size(); i++) { |
| 930 | Fragment f = mAdded.get(i); |
| 931 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 932 | if (f.onOptionsItemSelected(item)) { |
| 933 | return true; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | return false; |
| 939 | } |
| 940 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 941 | public boolean dispatchContextItemSelected(MenuItem item) { |
| 942 | if (mActive != null) { |
| 943 | for (int i=0; i<mAdded.size(); i++) { |
| 944 | Fragment f = mAdded.get(i); |
| 945 | if (f != null && !f.mHidden) { |
| 946 | if (f.onContextItemSelected(item)) { |
| 947 | return true; |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | return false; |
| 953 | } |
| 954 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 955 | public void dispatchOptionsMenuClosed(Menu menu) { |
| 956 | if (mActive != null) { |
| 957 | for (int i=0; i<mAdded.size(); i++) { |
| 958 | Fragment f = mAdded.get(i); |
| 959 | if (f != null && !f.mHidden && f.mHasMenu) { |
| 960 | f.onOptionsMenuClosed(menu); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 966 | public static int reverseTransit(int transit) { |
| 967 | int rev = 0; |
| 968 | switch (transit) { |
| 969 | case FragmentTransaction.TRANSIT_ENTER: |
| 970 | rev = FragmentTransaction.TRANSIT_EXIT; |
| 971 | break; |
| 972 | case FragmentTransaction.TRANSIT_EXIT: |
| 973 | rev = FragmentTransaction.TRANSIT_ENTER; |
| 974 | break; |
| 975 | case FragmentTransaction.TRANSIT_SHOW: |
| 976 | rev = FragmentTransaction.TRANSIT_HIDE; |
| 977 | break; |
| 978 | case FragmentTransaction.TRANSIT_HIDE: |
| 979 | rev = FragmentTransaction.TRANSIT_SHOW; |
| 980 | break; |
| 981 | case FragmentTransaction.TRANSIT_ACTIVITY_OPEN: |
| 982 | rev = FragmentTransaction.TRANSIT_ACTIVITY_CLOSE; |
| 983 | break; |
| 984 | case FragmentTransaction.TRANSIT_ACTIVITY_CLOSE: |
| 985 | rev = FragmentTransaction.TRANSIT_ACTIVITY_OPEN; |
| 986 | break; |
| 987 | case FragmentTransaction.TRANSIT_TASK_OPEN: |
| 988 | rev = FragmentTransaction.TRANSIT_TASK_CLOSE; |
| 989 | break; |
| 990 | case FragmentTransaction.TRANSIT_TASK_CLOSE: |
| 991 | rev = FragmentTransaction.TRANSIT_TASK_OPEN; |
| 992 | break; |
| 993 | case FragmentTransaction.TRANSIT_TASK_TO_FRONT: |
| 994 | rev = FragmentTransaction.TRANSIT_TASK_TO_BACK; |
| 995 | break; |
| 996 | case FragmentTransaction.TRANSIT_TASK_TO_BACK: |
| 997 | rev = FragmentTransaction.TRANSIT_TASK_TO_FRONT; |
| 998 | break; |
| 999 | case FragmentTransaction.TRANSIT_WALLPAPER_OPEN: |
| 1000 | rev = FragmentTransaction.TRANSIT_WALLPAPER_CLOSE; |
| 1001 | break; |
| 1002 | case FragmentTransaction.TRANSIT_WALLPAPER_CLOSE: |
| 1003 | rev = FragmentTransaction.TRANSIT_WALLPAPER_OPEN; |
| 1004 | break; |
| 1005 | case FragmentTransaction.TRANSIT_WALLPAPER_INTRA_OPEN: |
| 1006 | rev = FragmentTransaction.TRANSIT_WALLPAPER_INTRA_CLOSE; |
| 1007 | break; |
| 1008 | case FragmentTransaction.TRANSIT_WALLPAPER_INTRA_CLOSE: |
| 1009 | rev = FragmentTransaction.TRANSIT_WALLPAPER_INTRA_OPEN; |
| 1010 | break; |
| 1011 | } |
| 1012 | return rev; |
| 1013 | |
| 1014 | } |
| 1015 | |
| 1016 | public static int transitToStyleIndex(int transit, boolean enter) { |
| 1017 | int animAttr = -1; |
| 1018 | switch (transit) { |
| 1019 | case FragmentTransaction.TRANSIT_ENTER: |
| 1020 | animAttr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation; |
| 1021 | break; |
| 1022 | case FragmentTransaction.TRANSIT_EXIT: |
| 1023 | animAttr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation; |
| 1024 | break; |
| 1025 | case FragmentTransaction.TRANSIT_SHOW: |
| 1026 | animAttr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation; |
| 1027 | break; |
| 1028 | case FragmentTransaction.TRANSIT_HIDE: |
| 1029 | animAttr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation; |
| 1030 | break; |
| 1031 | case FragmentTransaction.TRANSIT_ACTIVITY_OPEN: |
| 1032 | animAttr = enter |
| 1033 | ? com.android.internal.R.styleable.WindowAnimation_activityOpenEnterAnimation |
| 1034 | : com.android.internal.R.styleable.WindowAnimation_activityOpenExitAnimation; |
| 1035 | break; |
| 1036 | case FragmentTransaction.TRANSIT_ACTIVITY_CLOSE: |
| 1037 | animAttr = enter |
| 1038 | ? com.android.internal.R.styleable.WindowAnimation_activityCloseEnterAnimation |
| 1039 | : com.android.internal.R.styleable.WindowAnimation_activityCloseExitAnimation; |
| 1040 | break; |
| 1041 | case FragmentTransaction.TRANSIT_TASK_OPEN: |
| 1042 | animAttr = enter |
| 1043 | ? com.android.internal.R.styleable.WindowAnimation_taskOpenEnterAnimation |
| 1044 | : com.android.internal.R.styleable.WindowAnimation_taskOpenExitAnimation; |
| 1045 | break; |
| 1046 | case FragmentTransaction.TRANSIT_TASK_CLOSE: |
| 1047 | animAttr = enter |
| 1048 | ? com.android.internal.R.styleable.WindowAnimation_taskCloseEnterAnimation |
| 1049 | : com.android.internal.R.styleable.WindowAnimation_taskCloseExitAnimation; |
| 1050 | break; |
| 1051 | case FragmentTransaction.TRANSIT_TASK_TO_FRONT: |
| 1052 | animAttr = enter |
| 1053 | ? com.android.internal.R.styleable.WindowAnimation_taskToFrontEnterAnimation |
| 1054 | : com.android.internal.R.styleable.WindowAnimation_taskToFrontExitAnimation; |
| 1055 | break; |
| 1056 | case FragmentTransaction.TRANSIT_TASK_TO_BACK: |
| 1057 | animAttr = enter |
| 1058 | ? com.android.internal.R.styleable.WindowAnimation_taskToBackEnterAnimation |
| 1059 | : com.android.internal.R.styleable.WindowAnimation_taskToBackExitAnimation; |
| 1060 | break; |
| 1061 | case FragmentTransaction.TRANSIT_WALLPAPER_OPEN: |
| 1062 | animAttr = enter |
| 1063 | ? com.android.internal.R.styleable.WindowAnimation_wallpaperOpenEnterAnimation |
| 1064 | : com.android.internal.R.styleable.WindowAnimation_wallpaperOpenExitAnimation; |
| 1065 | break; |
| 1066 | case FragmentTransaction.TRANSIT_WALLPAPER_CLOSE: |
| 1067 | animAttr = enter |
| 1068 | ? com.android.internal.R.styleable.WindowAnimation_wallpaperCloseEnterAnimation |
| 1069 | : com.android.internal.R.styleable.WindowAnimation_wallpaperCloseExitAnimation; |
| 1070 | break; |
| 1071 | case FragmentTransaction.TRANSIT_WALLPAPER_INTRA_OPEN: |
| 1072 | animAttr = enter |
| 1073 | ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenEnterAnimation |
| 1074 | : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraOpenExitAnimation; |
| 1075 | break; |
| 1076 | case FragmentTransaction.TRANSIT_WALLPAPER_INTRA_CLOSE: |
| 1077 | animAttr = enter |
| 1078 | ? com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseEnterAnimation |
| 1079 | : com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation; |
| 1080 | break; |
| 1081 | } |
| 1082 | return animAttr; |
| 1083 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1084 | } |