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; |
| 20 | import android.animation.AnimatorInflater; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorListenerAdapter; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 23 | import android.content.res.Configuration; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 25 | import android.os.Bundle; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 26 | import android.os.Debug; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 28 | import android.os.Looper; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -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 | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 34 | import android.util.LogWriter; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 35 | import android.util.SparseArray; |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 36 | import android.util.SuperNotCalledException; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 37 | import android.view.LayoutInflater; |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 38 | import android.view.Menu; |
| 39 | import android.view.MenuInflater; |
| 40 | import android.view.MenuItem; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 41 | import android.view.View; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 42 | import android.view.ViewGroup; |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 43 | import com.android.internal.util.FastPrintWriter; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 44 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 45 | import java.io.FileDescriptor; |
| 46 | import java.io.PrintWriter; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 47 | import java.util.ArrayList; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 48 | import java.util.Arrays; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 49 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Interface for interacting with {@link Fragment} objects inside of an |
| 52 | * {@link Activity} |
Joe Fernandez | b54e7a3 | 2011-10-03 15:09:50 -0700 | [diff] [blame] | 53 | * |
| 54 | * <div class="special reference"> |
| 55 | * <h3>Developer Guides</h3> |
| 56 | * <p>For more information about using fragments, read the |
| 57 | * <a href="{@docRoot}guide/topics/fundamentals/fragments.html">Fragments</a> developer guide.</p> |
| 58 | * </div> |
Dianne Hackborn | 7871bad | 2011-12-12 15:19:26 -0800 | [diff] [blame] | 59 | * |
| 60 | * While the FragmentManager API was introduced in |
| 61 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, a version of the API |
| 62 | * at is also available for use on older platforms through |
| 63 | * {@link android.support.v4.app.FragmentActivity}. See the blog post |
| 64 | * <a href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html"> |
| 65 | * Fragments For All</a> for more details. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 66 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 67 | public abstract class FragmentManager { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 68 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 69 | * Representation of an entry on the fragment back stack, as created |
| 70 | * with {@link FragmentTransaction#addToBackStack(String) |
| 71 | * FragmentTransaction.addToBackStack()}. Entries can later be |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 72 | * retrieved with {@link FragmentManager#getBackStackEntryAt(int) |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 73 | * FragmentManager.getBackStackEntry()}. |
| 74 | * |
| 75 | * <p>Note that you should never hold on to a BackStackEntry object; |
| 76 | * the identifier as returned by {@link #getId} is the only thing that |
| 77 | * will be persisted across activity instances. |
| 78 | */ |
| 79 | public interface BackStackEntry { |
| 80 | /** |
| 81 | * Return the unique identifier for the entry. This is the only |
| 82 | * representation of the entry that will persist across activity |
| 83 | * instances. |
| 84 | */ |
| 85 | public int getId(); |
| 86 | |
| 87 | /** |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 88 | * Get the name that was supplied to |
| 89 | * {@link FragmentTransaction#addToBackStack(String) |
| 90 | * FragmentTransaction.addToBackStack(String)} when creating this entry. |
| 91 | */ |
| 92 | public String getName(); |
| 93 | |
| 94 | /** |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 95 | * Return the full bread crumb title resource identifier for the entry, |
| 96 | * or 0 if it does not have one. |
| 97 | */ |
| 98 | public int getBreadCrumbTitleRes(); |
| 99 | |
| 100 | /** |
| 101 | * Return the short bread crumb title resource identifier for the entry, |
| 102 | * or 0 if it does not have one. |
| 103 | */ |
| 104 | public int getBreadCrumbShortTitleRes(); |
| 105 | |
| 106 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 107 | * Return the full bread crumb title for the entry, or null if it |
| 108 | * does not have one. |
| 109 | */ |
| 110 | public CharSequence getBreadCrumbTitle(); |
| 111 | |
| 112 | /** |
| 113 | * Return the short bread crumb title for the entry, or null if it |
| 114 | * does not have one. |
| 115 | */ |
| 116 | public CharSequence getBreadCrumbShortTitle(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Interface to watch for changes to the back stack. |
| 121 | */ |
| 122 | public interface OnBackStackChangedListener { |
| 123 | /** |
| 124 | * Called whenever the contents of the back stack change. |
| 125 | */ |
| 126 | public void onBackStackChanged(); |
| 127 | } |
| 128 | |
| 129 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 130 | * Start a series of edit operations on the Fragments associated with |
| 131 | * this FragmentManager. |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 132 | * |
| 133 | * <p>Note: A fragment transaction can only be created/committed prior |
| 134 | * to an activity saving its state. If you try to commit a transaction |
| 135 | * after {@link Activity#onSaveInstanceState Activity.onSaveInstanceState()} |
| 136 | * (and prior to a following {@link Activity#onStart Activity.onStart} |
| 137 | * or {@link Activity#onResume Activity.onResume()}, you will get an error. |
| 138 | * This is because the framework takes care of saving your current fragments |
| 139 | * in the state, and if changes are made after the state is saved then they |
| 140 | * will be lost.</p> |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 141 | */ |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 142 | public abstract FragmentTransaction beginTransaction(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 143 | |
Dianne Hackborn | 17b9b81 | 2011-01-17 17:16:02 -0800 | [diff] [blame] | 144 | /** @hide -- remove once prebuilts are in. */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 145 | @Deprecated |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 146 | public FragmentTransaction openTransaction() { |
| 147 | return beginTransaction(); |
| 148 | } |
| 149 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 150 | /** |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 151 | * After a {@link FragmentTransaction} is committed with |
| 152 | * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it |
| 153 | * is scheduled to be executed asynchronously on the process's main thread. |
| 154 | * If you want to immediately executing any such pending operations, you |
| 155 | * can call this function (only from the main thread) to do so. Note that |
| 156 | * all callbacks and other related behavior will be done from within this |
| 157 | * call, so be careful about where this is called from. |
| 158 | * |
| 159 | * @return Returns true if there were any pending transactions to be |
| 160 | * executed. |
| 161 | */ |
| 162 | public abstract boolean executePendingTransactions(); |
| 163 | |
| 164 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 165 | * Finds a fragment that was identified by the given id either when inflated |
| 166 | * from XML or as the container ID when added in a transaction. This first |
| 167 | * searches through fragments that are currently added to the manager's |
| 168 | * activity; if no such fragment is found, then all fragments currently |
| 169 | * on the back stack associated with this ID are searched. |
| 170 | * @return The fragment if found or null otherwise. |
| 171 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 172 | public abstract Fragment findFragmentById(int id); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * Finds a fragment that was identified by the given tag either when inflated |
| 176 | * from XML or as supplied when added in a transaction. This first |
| 177 | * searches through fragments that are currently added to the manager's |
| 178 | * activity; if no such fragment is found, then all fragments currently |
| 179 | * on the back stack are searched. |
| 180 | * @return The fragment if found or null otherwise. |
| 181 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 182 | public abstract Fragment findFragmentByTag(String tag); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Flag for {@link #popBackStack(String, int)} |
| 186 | * and {@link #popBackStack(int, int)}: If set, and the name or ID of |
| 187 | * a back stack entry has been supplied, then all matching entries will |
| 188 | * be consumed until one that doesn't match is found or the bottom of |
| 189 | * the stack is reached. Otherwise, all entries up to but not including that entry |
| 190 | * will be removed. |
| 191 | */ |
| 192 | public static final int POP_BACK_STACK_INCLUSIVE = 1<<0; |
| 193 | |
| 194 | /** |
Ben Komalo | 87ffa20 | 2011-02-28 12:41:42 -0800 | [diff] [blame] | 195 | * Pop the top state off the back stack. This function is asynchronous -- it |
| 196 | * enqueues the request to pop, but the action will not be performed until the |
| 197 | * application returns to its event loop. |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 198 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 199 | public abstract void popBackStack(); |
| 200 | |
| 201 | /** |
| 202 | * Like {@link #popBackStack()}, but performs the operation immediately |
| 203 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 204 | * afterwards. |
| 205 | * @return Returns true if there was something popped, else false. |
| 206 | */ |
| 207 | public abstract boolean popBackStackImmediate(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Pop the last fragment transition from the manager's fragment |
| 211 | * back stack. If there is nothing to pop, false is returned. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 212 | * This function is asynchronous -- it enqueues the |
| 213 | * request to pop, but the action will not be performed until the application |
| 214 | * returns to its event loop. |
| 215 | * |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 216 | * @param name If non-null, this is the name of a previous back state |
| 217 | * to look for; if found, all states up to that state will be popped. The |
| 218 | * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether |
| 219 | * the named state itself is popped. If null, only the top state is popped. |
| 220 | * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. |
| 221 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 222 | public abstract void popBackStack(String name, int flags); |
| 223 | |
| 224 | /** |
| 225 | * Like {@link #popBackStack(String, int)}, but performs the operation immediately |
| 226 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 227 | * afterwards. |
| 228 | * @return Returns true if there was something popped, else false. |
| 229 | */ |
| 230 | public abstract boolean popBackStackImmediate(String name, int flags); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Pop all back stack states up to the one with the given identifier. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 234 | * This function is asynchronous -- it enqueues the |
| 235 | * request to pop, but the action will not be performed until the application |
| 236 | * returns to its event loop. |
| 237 | * |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 238 | * @param id Identifier of the stated to be popped. If no identifier exists, |
| 239 | * false is returned. |
| 240 | * The identifier is the number returned by |
| 241 | * {@link FragmentTransaction#commit() FragmentTransaction.commit()}. The |
| 242 | * {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether |
| 243 | * the named state itself is popped. |
| 244 | * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. |
| 245 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 246 | public abstract void popBackStack(int id, int flags); |
| 247 | |
| 248 | /** |
| 249 | * Like {@link #popBackStack(int, int)}, but performs the operation immediately |
| 250 | * inside of the call. This is like calling {@link #executePendingTransactions()} |
| 251 | * afterwards. |
| 252 | * @return Returns true if there was something popped, else false. |
| 253 | */ |
| 254 | public abstract boolean popBackStackImmediate(int id, int flags); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 255 | |
| 256 | /** |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 257 | * Return the number of entries currently in the back stack. |
| 258 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 259 | public abstract int getBackStackEntryCount(); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Return the BackStackEntry at index <var>index</var> in the back stack; |
| 263 | * entries start index 0 being the bottom of the stack. |
| 264 | */ |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 265 | public abstract BackStackEntry getBackStackEntryAt(int index); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * Add a new listener for changes to the fragment back stack. |
| 269 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 270 | public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Remove a listener that was previously added with |
| 274 | * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}. |
| 275 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 276 | public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 277 | |
| 278 | /** |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 279 | * Put a reference to a fragment in a Bundle. This Bundle can be |
| 280 | * persisted as saved state, and when later restoring |
| 281 | * {@link #getFragment(Bundle, String)} will return the current |
| 282 | * instance of the same fragment. |
| 283 | * |
| 284 | * @param bundle The bundle in which to put the fragment reference. |
| 285 | * @param key The name of the entry in the bundle. |
| 286 | * @param fragment The Fragment whose reference is to be stored. |
| 287 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 288 | public abstract void putFragment(Bundle bundle, String key, Fragment fragment); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * Retrieve the current Fragment instance for a reference previously |
| 292 | * placed with {@link #putFragment(Bundle, String, Fragment)}. |
| 293 | * |
| 294 | * @param bundle The bundle from which to retrieve the fragment reference. |
| 295 | * @param key The name of the entry in the bundle. |
| 296 | * @return Returns the current Fragment instance that is associated with |
| 297 | * the given reference. |
| 298 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 299 | public abstract Fragment getFragment(Bundle bundle, String key); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 300 | |
| 301 | /** |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 302 | * Save the current instance state of the given Fragment. This can be |
| 303 | * used later when creating a new instance of the Fragment and adding |
| 304 | * it to the fragment manager, to have it create itself to match the |
| 305 | * current state returned here. Note that there are limits on how |
| 306 | * this can be used: |
| 307 | * |
| 308 | * <ul> |
| 309 | * <li>The Fragment must currently be attached to the FragmentManager. |
| 310 | * <li>A new Fragment created using this saved state must be the same class |
| 311 | * type as the Fragment it was created from. |
| 312 | * <li>The saved state can not contain dependencies on other fragments -- |
| 313 | * that is it can't use {@link #putFragment(Bundle, String, Fragment)} to |
| 314 | * store a fragment reference because that reference may not be valid when |
| 315 | * this saved state is later used. Likewise the Fragment's target and |
| 316 | * result code are not included in this state. |
| 317 | * </ul> |
| 318 | * |
| 319 | * @param f The Fragment whose state is to be saved. |
| 320 | * @return The generated state. This will be null if there was no |
| 321 | * interesting state created by the fragment. |
| 322 | */ |
| 323 | public abstract Fragment.SavedState saveFragmentInstanceState(Fragment f); |
| 324 | |
| 325 | /** |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 326 | * Returns true if the final {@link Activity#onDestroy() Activity.onDestroy()} |
| 327 | * call has been made on the FragmentManager's Activity, so this instance is now dead. |
| 328 | */ |
| 329 | public abstract boolean isDestroyed(); |
| 330 | |
| 331 | /** |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 332 | * Print the FragmentManager's state into the given stream. |
| 333 | * |
| 334 | * @param prefix Text to print at the front of each line. |
| 335 | * @param fd The raw file descriptor that the dump is being sent to. |
| 336 | * @param writer A PrintWriter to which the dump is to be set. |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 337 | * @param args Additional arguments to the dump request. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 338 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 339 | public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 340 | |
| 341 | /** |
| 342 | * Control whether the framework's internal fragment manager debugging |
| 343 | * logs are turned on. If enabled, you will see output in logcat as |
| 344 | * the framework performs fragment operations. |
| 345 | */ |
| 346 | public static void enableDebugLogging(boolean enabled) { |
| 347 | FragmentManagerImpl.DEBUG = enabled; |
| 348 | } |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * Invalidate the attached activity's options menu as necessary. |
| 352 | * This may end up being deferred until we move to the resumed state. |
| 353 | */ |
| 354 | public void invalidateOptionsMenu() { } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 357 | final class FragmentManagerState implements Parcelable { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 358 | FragmentState[] mActive; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 359 | int[] mAdded; |
| 360 | BackStackState[] mBackStack; |
| 361 | |
| 362 | public FragmentManagerState() { |
| 363 | } |
| 364 | |
| 365 | public FragmentManagerState(Parcel in) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 366 | mActive = in.createTypedArray(FragmentState.CREATOR); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 367 | mAdded = in.createIntArray(); |
| 368 | mBackStack = in.createTypedArray(BackStackState.CREATOR); |
| 369 | } |
| 370 | |
| 371 | public int describeContents() { |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 376 | dest.writeTypedArray(mActive, flags); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 377 | dest.writeIntArray(mAdded); |
| 378 | dest.writeTypedArray(mBackStack, flags); |
| 379 | } |
| 380 | |
| 381 | public static final Parcelable.Creator<FragmentManagerState> CREATOR |
| 382 | = new Parcelable.Creator<FragmentManagerState>() { |
| 383 | public FragmentManagerState createFromParcel(Parcel in) { |
| 384 | return new FragmentManagerState(in); |
| 385 | } |
| 386 | |
| 387 | public FragmentManagerState[] newArray(int size) { |
| 388 | return new FragmentManagerState[size]; |
| 389 | } |
| 390 | }; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 393 | /** |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 394 | * Callbacks from FragmentManagerImpl to its container. |
| 395 | */ |
| 396 | interface FragmentContainer { |
| 397 | public View findViewById(int id); |
| 398 | } |
| 399 | |
| 400 | /** |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 401 | * Container for fragments associated with an activity. |
| 402 | */ |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 403 | final class FragmentManagerImpl extends FragmentManager implements LayoutInflater.Factory2 { |
Craig Mautner | 1c43719 | 2012-08-01 10:01:16 -0700 | [diff] [blame] | 404 | static boolean DEBUG = false; |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 405 | static final String TAG = "FragmentManager"; |
| 406 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 407 | static final String TARGET_REQUEST_CODE_STATE_TAG = "android:target_req_state"; |
| 408 | static final String TARGET_STATE_TAG = "android:target_state"; |
| 409 | static final String VIEW_STATE_TAG = "android:view_state"; |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 410 | static final String USER_VISIBLE_HINT_TAG = "android:user_visible_hint"; |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 411 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 412 | ArrayList<Runnable> mPendingActions; |
| 413 | Runnable[] mTmpActions; |
| 414 | boolean mExecutingActions; |
| 415 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 416 | ArrayList<Fragment> mActive; |
| 417 | ArrayList<Fragment> mAdded; |
| 418 | ArrayList<Integer> mAvailIndices; |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 419 | ArrayList<BackStackRecord> mBackStack; |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 420 | ArrayList<Fragment> mCreatedMenus; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 421 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 422 | // Must be accessed while locked. |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 423 | ArrayList<BackStackRecord> mBackStackIndices; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 424 | ArrayList<Integer> mAvailBackStackIndices; |
| 425 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 426 | ArrayList<OnBackStackChangedListener> mBackStackChangeListeners; |
| 427 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 428 | int mCurState = Fragment.INITIALIZING; |
| 429 | Activity mActivity; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 430 | FragmentContainer mContainer; |
| 431 | Fragment mParent; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 432 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 433 | boolean mNeedMenuInvalidate; |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 434 | boolean mStateSaved; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 435 | boolean mDestroyed; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 436 | String mNoTransactionsBecause; |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 437 | boolean mHavePendingDeferredStart; |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 438 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 439 | // Temporary vars for state save and restore. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 440 | Bundle mStateBundle = null; |
| 441 | SparseArray<Parcelable> mStateArray = null; |
| 442 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 443 | Runnable mExecCommit = new Runnable() { |
| 444 | @Override |
| 445 | public void run() { |
| 446 | execPendingActions(); |
| 447 | } |
| 448 | }; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 449 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 450 | private void throwException(RuntimeException ex) { |
| 451 | Log.e(TAG, ex.getMessage()); |
| 452 | LogWriter logw = new LogWriter(Log.ERROR, TAG); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 453 | PrintWriter pw = new FastPrintWriter(logw, false, 1024); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 454 | if (mActivity != null) { |
| 455 | Log.e(TAG, "Activity state:"); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 456 | try { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 457 | mActivity.dump(" ", null, pw, new String[] { }); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 458 | } catch (Exception e) { |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 459 | pw.flush(); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 460 | Log.e(TAG, "Failed dumping state", e); |
| 461 | } |
| 462 | } else { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 463 | Log.e(TAG, "Fragment manager state:"); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 464 | try { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 465 | dump(" ", null, pw, new String[] { }); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 466 | } catch (Exception e) { |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 467 | pw.flush(); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 468 | Log.e(TAG, "Failed dumping state", e); |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 469 | } |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 470 | } |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 471 | pw.flush(); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 472 | throw ex; |
Dianne Hackborn | 5bf6e1a | 2012-08-14 18:35:02 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 475 | @Override |
Dianne Hackborn | 48e7b45 | 2011-01-17 12:28:35 -0800 | [diff] [blame] | 476 | public FragmentTransaction beginTransaction() { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 477 | return new BackStackRecord(this); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 480 | @Override |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 481 | public boolean executePendingTransactions() { |
| 482 | return execPendingActions(); |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public void popBackStack() { |
| 487 | enqueueAction(new Runnable() { |
| 488 | @Override public void run() { |
| 489 | popBackStackState(mActivity.mHandler, null, -1, 0); |
| 490 | } |
| 491 | }, false); |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public boolean popBackStackImmediate() { |
| 496 | checkStateLoss(); |
| 497 | executePendingTransactions(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 498 | return popBackStackState(mActivity.mHandler, null, -1, 0); |
| 499 | } |
| 500 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 501 | @Override |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 502 | public void popBackStack(final String name, final int flags) { |
| 503 | enqueueAction(new Runnable() { |
| 504 | @Override public void run() { |
| 505 | popBackStackState(mActivity.mHandler, name, -1, flags); |
| 506 | } |
| 507 | }, false); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public boolean popBackStackImmediate(String name, int flags) { |
| 512 | checkStateLoss(); |
| 513 | executePendingTransactions(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 514 | return popBackStackState(mActivity.mHandler, name, -1, flags); |
| 515 | } |
| 516 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 517 | @Override |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 518 | public void popBackStack(final int id, final int flags) { |
| 519 | if (id < 0) { |
| 520 | throw new IllegalArgumentException("Bad id: " + id); |
| 521 | } |
| 522 | enqueueAction(new Runnable() { |
| 523 | @Override public void run() { |
| 524 | popBackStackState(mActivity.mHandler, null, id, flags); |
| 525 | } |
| 526 | }, false); |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public boolean popBackStackImmediate(int id, int flags) { |
| 531 | checkStateLoss(); |
| 532 | executePendingTransactions(); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 533 | if (id < 0) { |
| 534 | throw new IllegalArgumentException("Bad id: " + id); |
| 535 | } |
| 536 | return popBackStackState(mActivity.mHandler, null, id, flags); |
| 537 | } |
| 538 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 539 | @Override |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 540 | public int getBackStackEntryCount() { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 541 | return mBackStack != null ? mBackStack.size() : 0; |
| 542 | } |
| 543 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 544 | @Override |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 545 | public BackStackEntry getBackStackEntryAt(int index) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 546 | return mBackStack.get(index); |
| 547 | } |
| 548 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 549 | @Override |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 550 | public void addOnBackStackChangedListener(OnBackStackChangedListener listener) { |
| 551 | if (mBackStackChangeListeners == null) { |
| 552 | mBackStackChangeListeners = new ArrayList<OnBackStackChangedListener>(); |
| 553 | } |
| 554 | mBackStackChangeListeners.add(listener); |
| 555 | } |
| 556 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 557 | @Override |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 558 | public void removeOnBackStackChangedListener(OnBackStackChangedListener listener) { |
| 559 | if (mBackStackChangeListeners != null) { |
| 560 | mBackStackChangeListeners.remove(listener); |
| 561 | } |
| 562 | } |
| 563 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 564 | @Override |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 565 | public void putFragment(Bundle bundle, String key, Fragment fragment) { |
| 566 | if (fragment.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 567 | throwException(new IllegalStateException("Fragment " + fragment |
| 568 | + " is not currently in the FragmentManager")); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 569 | } |
| 570 | bundle.putInt(key, fragment.mIndex); |
| 571 | } |
| 572 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 573 | @Override |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 574 | public Fragment getFragment(Bundle bundle, String key) { |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 575 | int index = bundle.getInt(key, -1); |
| 576 | if (index == -1) { |
| 577 | return null; |
| 578 | } |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 579 | if (index >= mActive.size()) { |
Cyril Mottier | 2de5082 | 2013-09-30 22:42:26 +0200 | [diff] [blame] | 580 | throwException(new IllegalStateException("Fragment no longer exists for key " |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 581 | + key + ": index " + index)); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 582 | } |
| 583 | Fragment f = mActive.get(index); |
| 584 | if (f == null) { |
Cyril Mottier | 2de5082 | 2013-09-30 22:42:26 +0200 | [diff] [blame] | 585 | throwException(new IllegalStateException("Fragment no longer exists for key " |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 586 | + key + ": index " + index)); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 587 | } |
| 588 | return f; |
| 589 | } |
| 590 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 591 | @Override |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 592 | public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) { |
| 593 | if (fragment.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 594 | throwException(new IllegalStateException("Fragment " + fragment |
| 595 | + " is not currently in the FragmentManager")); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 596 | } |
| 597 | if (fragment.mState > Fragment.INITIALIZING) { |
| 598 | Bundle result = saveFragmentBasicState(fragment); |
| 599 | return result != null ? new Fragment.SavedState(result) : null; |
| 600 | } |
| 601 | return null; |
| 602 | } |
| 603 | |
| 604 | @Override |
Dianne Hackborn | 6d9dcbc | 2012-10-02 17:51:13 -0700 | [diff] [blame] | 605 | public boolean isDestroyed() { |
| 606 | return mDestroyed; |
| 607 | } |
| 608 | |
| 609 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 610 | public String toString() { |
| 611 | StringBuilder sb = new StringBuilder(128); |
| 612 | sb.append("FragmentManager{"); |
| 613 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 614 | sb.append(" in "); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 615 | if (mParent != null) { |
| 616 | DebugUtils.buildShortClassTag(mParent, sb); |
| 617 | } else { |
| 618 | DebugUtils.buildShortClassTag(mActivity, sb); |
| 619 | } |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 620 | sb.append("}}"); |
| 621 | return sb.toString(); |
| 622 | } |
| 623 | |
| 624 | @Override |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 625 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 626 | String innerPrefix = prefix + " "; |
| 627 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 628 | int N; |
| 629 | if (mActive != null) { |
| 630 | N = mActive.size(); |
| 631 | if (N > 0) { |
| 632 | writer.print(prefix); writer.print("Active Fragments in "); |
| 633 | writer.print(Integer.toHexString(System.identityHashCode(this))); |
| 634 | writer.println(":"); |
| 635 | for (int i=0; i<N; i++) { |
| 636 | Fragment f = mActive.get(i); |
| 637 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 638 | writer.print(": "); writer.println(f); |
| 639 | if (f != null) { |
| 640 | f.dump(innerPrefix, fd, writer, args); |
| 641 | } |
| 642 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | if (mAdded != null) { |
| 647 | N = mAdded.size(); |
| 648 | if (N > 0) { |
| 649 | writer.print(prefix); writer.println("Added Fragments:"); |
| 650 | for (int i=0; i<N; i++) { |
| 651 | Fragment f = mAdded.get(i); |
| 652 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 653 | writer.print(": "); writer.println(f.toString()); |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 658 | if (mCreatedMenus != null) { |
| 659 | N = mCreatedMenus.size(); |
| 660 | if (N > 0) { |
| 661 | writer.print(prefix); writer.println("Fragments Created Menus:"); |
| 662 | for (int i=0; i<N; i++) { |
| 663 | Fragment f = mCreatedMenus.get(i); |
| 664 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 665 | writer.print(": "); writer.println(f.toString()); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 670 | if (mBackStack != null) { |
| 671 | N = mBackStack.size(); |
| 672 | if (N > 0) { |
| 673 | writer.print(prefix); writer.println("Back Stack:"); |
| 674 | for (int i=0; i<N; i++) { |
| 675 | BackStackRecord bs = mBackStack.get(i); |
| 676 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 677 | writer.print(": "); writer.println(bs.toString()); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 678 | bs.dump(innerPrefix, fd, writer, args); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 682 | |
| 683 | synchronized (this) { |
| 684 | if (mBackStackIndices != null) { |
| 685 | N = mBackStackIndices.size(); |
| 686 | if (N > 0) { |
| 687 | writer.print(prefix); writer.println("Back Stack Indices:"); |
| 688 | for (int i=0; i<N; i++) { |
| 689 | BackStackRecord bs = mBackStackIndices.get(i); |
| 690 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 691 | writer.print(": "); writer.println(bs); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (mAvailBackStackIndices != null && mAvailBackStackIndices.size() > 0) { |
| 697 | writer.print(prefix); writer.print("mAvailBackStackIndices: "); |
| 698 | writer.println(Arrays.toString(mAvailBackStackIndices.toArray())); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (mPendingActions != null) { |
| 703 | N = mPendingActions.size(); |
| 704 | if (N > 0) { |
| 705 | writer.print(prefix); writer.println("Pending Actions:"); |
| 706 | for (int i=0; i<N; i++) { |
| 707 | Runnable r = mPendingActions.get(i); |
| 708 | writer.print(prefix); writer.print(" #"); writer.print(i); |
| 709 | writer.print(": "); writer.println(r); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | writer.print(prefix); writer.println("FragmentManager misc state:"); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 715 | writer.print(prefix); writer.print(" mActivity="); writer.println(mActivity); |
| 716 | writer.print(prefix); writer.print(" mContainer="); writer.println(mContainer); |
| 717 | if (mParent != null) { |
| 718 | writer.print(prefix); writer.print(" mParent="); writer.println(mParent); |
| 719 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 720 | writer.print(prefix); writer.print(" mCurState="); writer.print(mCurState); |
| 721 | writer.print(" mStateSaved="); writer.print(mStateSaved); |
| 722 | writer.print(" mDestroyed="); writer.println(mDestroyed); |
| 723 | if (mNeedMenuInvalidate) { |
| 724 | writer.print(prefix); writer.print(" mNeedMenuInvalidate="); |
| 725 | writer.println(mNeedMenuInvalidate); |
| 726 | } |
| 727 | if (mNoTransactionsBecause != null) { |
| 728 | writer.print(prefix); writer.print(" mNoTransactionsBecause="); |
| 729 | writer.println(mNoTransactionsBecause); |
| 730 | } |
| 731 | if (mAvailIndices != null && mAvailIndices.size() > 0) { |
| 732 | writer.print(prefix); writer.print(" mAvailIndices: "); |
| 733 | writer.println(Arrays.toString(mAvailIndices.toArray())); |
| 734 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 737 | Animator loadAnimator(Fragment fragment, int transit, boolean enter, |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 738 | int transitionStyle) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 739 | Animator animObj = fragment.onCreateAnimator(transit, enter, |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 740 | fragment.mNextAnim); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 741 | if (animObj != null) { |
| 742 | return animObj; |
| 743 | } |
| 744 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 745 | if (fragment.mNextAnim != 0) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 746 | Animator anim = AnimatorInflater.loadAnimator(mActivity, fragment.mNextAnim); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 747 | if (anim != null) { |
| 748 | return anim; |
| 749 | } |
| 750 | } |
| 751 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 752 | if (transit == 0) { |
| 753 | return null; |
| 754 | } |
| 755 | |
| 756 | int styleIndex = transitToStyleIndex(transit, enter); |
| 757 | if (styleIndex < 0) { |
| 758 | return null; |
| 759 | } |
| 760 | |
| 761 | if (transitionStyle == 0 && mActivity.getWindow() != null) { |
| 762 | transitionStyle = mActivity.getWindow().getAttributes().windowAnimations; |
| 763 | } |
| 764 | if (transitionStyle == 0) { |
| 765 | return null; |
| 766 | } |
| 767 | |
| 768 | TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle, |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 769 | com.android.internal.R.styleable.FragmentAnimation); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 770 | int anim = attrs.getResourceId(styleIndex, 0); |
| 771 | attrs.recycle(); |
| 772 | |
| 773 | if (anim == 0) { |
| 774 | return null; |
| 775 | } |
| 776 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 777 | return AnimatorInflater.loadAnimator(mActivity, anim); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 780 | public void performPendingDeferredStart(Fragment f) { |
| 781 | if (f.mDeferStart) { |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 782 | if (mExecutingActions) { |
| 783 | // Wait until we're done executing our pending transactions |
| 784 | mHavePendingDeferredStart = true; |
| 785 | return; |
| 786 | } |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 787 | f.mDeferStart = false; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 788 | moveToState(f, mCurState, 0, 0, false); |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 792 | void moveToState(Fragment f, int newState, int transit, int transitionStyle, |
| 793 | boolean keepActive) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 794 | if (DEBUG && false) Log.v(TAG, "moveToState: " + f |
| 795 | + " oldState=" + f.mState + " newState=" + newState |
| 796 | + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5)); |
Craig Mautner | 1c43719 | 2012-08-01 10:01:16 -0700 | [diff] [blame] | 797 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 798 | // Fragments that are not currently added will sit in the onCreate() state. |
Dianne Hackborn | e181bd9 | 2012-09-25 14:15:15 -0700 | [diff] [blame] | 799 | if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 800 | newState = Fragment.CREATED; |
| 801 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 802 | if (f.mRemoving && newState > f.mState) { |
| 803 | // While removing a fragment, we can't change it to a higher state. |
| 804 | newState = f.mState; |
| 805 | } |
Adam Powell | 2db4e4b | 2011-11-02 14:30:47 -0700 | [diff] [blame] | 806 | // Defer start if requested; don't allow it to move to STARTED or higher |
| 807 | // if it's not already started. |
| 808 | if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 809 | newState = Fragment.STOPPED; |
| 810 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 811 | if (f.mState < newState) { |
Dianne Hackborn | e3a7f62 | 2011-03-03 21:48:24 -0800 | [diff] [blame] | 812 | // For fragments that are created from a layout, when restoring from |
| 813 | // state we don't want to allow them to be created until they are |
| 814 | // being reloaded from the layout. |
| 815 | if (f.mFromLayout && !f.mInLayout) { |
| 816 | return; |
| 817 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 818 | if (f.mAnimatingAway != null) { |
| 819 | // The fragment is currently being animated... but! Now we |
| 820 | // want to move our state back up. Give up on waiting for the |
| 821 | // animation, move to whatever the final state should be once |
| 822 | // the animation is done, and then we can proceed from there. |
| 823 | f.mAnimatingAway = null; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 824 | moveToState(f, f.mStateAfterAnimating, 0, 0, true); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 825 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 826 | switch (f.mState) { |
| 827 | case Fragment.INITIALIZING: |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 828 | if (DEBUG) Log.v(TAG, "moveto CREATED: " + f); |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 829 | if (f.mSavedFragmentState != null) { |
| 830 | f.mSavedViewState = f.mSavedFragmentState.getSparseParcelableArray( |
| 831 | FragmentManagerImpl.VIEW_STATE_TAG); |
| 832 | f.mTarget = getFragment(f.mSavedFragmentState, |
| 833 | FragmentManagerImpl.TARGET_STATE_TAG); |
| 834 | if (f.mTarget != null) { |
| 835 | f.mTargetRequestCode = f.mSavedFragmentState.getInt( |
| 836 | FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0); |
| 837 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 838 | f.mUserVisibleHint = f.mSavedFragmentState.getBoolean( |
| 839 | FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true); |
| 840 | if (!f.mUserVisibleHint) { |
| 841 | f.mDeferStart = true; |
| 842 | if (newState > Fragment.STOPPED) { |
| 843 | newState = Fragment.STOPPED; |
| 844 | } |
| 845 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 846 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 847 | f.mActivity = mActivity; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 848 | f.mParentFragment = mParent; |
| 849 | f.mFragmentManager = mParent != null |
| 850 | ? mParent.mChildFragmentManager : mActivity.mFragments; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 851 | f.mCalled = false; |
| 852 | f.onAttach(mActivity); |
| 853 | if (!f.mCalled) { |
| 854 | throw new SuperNotCalledException("Fragment " + f |
| 855 | + " did not call through to super.onAttach()"); |
| 856 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 857 | if (f.mParentFragment == null) { |
| 858 | mActivity.onAttachFragment(f); |
| 859 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 860 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 861 | if (!f.mRetaining) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 862 | f.performCreate(f.mSavedFragmentState); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 863 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 864 | f.mRetaining = false; |
| 865 | if (f.mFromLayout) { |
| 866 | // For fragments that are part of the content view |
| 867 | // layout, we need to instantiate the view immediately |
| 868 | // and the inflater will take care of adding it. |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 869 | f.mView = f.performCreateView(f.getLayoutInflater( |
| 870 | f.mSavedFragmentState), null, f.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 871 | if (f.mView != null) { |
| 872 | f.mView.setSaveFromParentEnabled(false); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 873 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 874 | f.onViewCreated(f.mView, f.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 875 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 876 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 877 | case Fragment.CREATED: |
| 878 | if (newState > Fragment.CREATED) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 879 | if (DEBUG) Log.v(TAG, "moveto ACTIVITY_CREATED: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 880 | if (!f.mFromLayout) { |
| 881 | ViewGroup container = null; |
| 882 | if (f.mContainerId != 0) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 883 | container = (ViewGroup)mContainer.findViewById(f.mContainerId); |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame] | 884 | if (container == null && !f.mRestored) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 885 | throwException(new IllegalArgumentException( |
| 886 | "No view found for id 0x" |
| 887 | + Integer.toHexString(f.mContainerId) + " (" |
| 888 | + f.getResources().getResourceName(f.mContainerId) |
| 889 | + ") for fragment " + f)); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | f.mContainer = container; |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 893 | f.mView = f.performCreateView(f.getLayoutInflater( |
| 894 | f.mSavedFragmentState), container, f.mSavedFragmentState); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 895 | if (f.mView != null) { |
| 896 | f.mView.setSaveFromParentEnabled(false); |
| 897 | if (container != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 898 | Animator anim = loadAnimator(f, transit, true, |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 899 | transitionStyle); |
| 900 | if (anim != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 901 | anim.setTarget(f.mView); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 902 | anim.start(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 903 | } |
| 904 | container.addView(f.mView); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 905 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 906 | if (f.mHidden) f.mView.setVisibility(View.GONE); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 907 | f.onViewCreated(f.mView, f.mSavedFragmentState); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 910 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 911 | f.performActivityCreated(f.mSavedFragmentState); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 912 | if (f.mView != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 913 | f.restoreViewState(f.mSavedFragmentState); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 914 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 915 | f.mSavedFragmentState = null; |
| 916 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 917 | case Fragment.ACTIVITY_CREATED: |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 918 | case Fragment.STOPPED: |
| 919 | if (newState > Fragment.STOPPED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 920 | if (DEBUG) Log.v(TAG, "moveto STARTED: " + f); |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 921 | f.performStart(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 922 | } |
| 923 | case Fragment.STARTED: |
| 924 | if (newState > Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 925 | if (DEBUG) Log.v(TAG, "moveto RESUMED: " + f); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 926 | f.mResumed = true; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 927 | f.performResume(); |
Adam Powell | 9520251 | 2011-08-07 17:20:17 -0700 | [diff] [blame] | 928 | // Get rid of this in case we saved it and never needed it. |
| 929 | f.mSavedFragmentState = null; |
| 930 | f.mSavedViewState = null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | } else if (f.mState > newState) { |
| 934 | switch (f.mState) { |
| 935 | case Fragment.RESUMED: |
| 936 | if (newState < Fragment.RESUMED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 937 | if (DEBUG) Log.v(TAG, "movefrom RESUMED: " + f); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 938 | f.performPause(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 939 | f.mResumed = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 940 | } |
| 941 | case Fragment.STARTED: |
| 942 | if (newState < Fragment.STARTED) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 943 | if (DEBUG) Log.v(TAG, "movefrom STARTED: " + f); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 944 | f.performStop(); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 945 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 946 | case Fragment.STOPPED: |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 947 | case Fragment.ACTIVITY_CREATED: |
| 948 | if (newState < Fragment.ACTIVITY_CREATED) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 949 | if (DEBUG) Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 950 | if (f.mView != null) { |
| 951 | // Need to save the current view state if not |
| 952 | // done already. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 953 | if (!mActivity.isFinishing() && f.mSavedViewState == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 954 | saveFragmentViewState(f); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 955 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 956 | } |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 957 | f.performDestroyView(); |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 958 | if (f.mView != null && f.mContainer != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 959 | Animator anim = null; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 960 | if (mCurState > Fragment.INITIALIZING && !mDestroyed) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 961 | anim = loadAnimator(f, transit, false, |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 962 | transitionStyle); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 963 | } |
| 964 | if (anim != null) { |
| 965 | final ViewGroup container = f.mContainer; |
| 966 | final View view = f.mView; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 967 | final Fragment fragment = f; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 968 | container.startViewTransition(view); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 969 | f.mAnimatingAway = anim; |
| 970 | f.mStateAfterAnimating = newState; |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 971 | anim.addListener(new AnimatorListenerAdapter() { |
| 972 | @Override |
| 973 | public void onAnimationEnd(Animator anim) { |
| 974 | container.endViewTransition(view); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 975 | if (fragment.mAnimatingAway != null) { |
| 976 | fragment.mAnimatingAway = null; |
| 977 | moveToState(fragment, fragment.mStateAfterAnimating, |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 978 | 0, 0, false); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 979 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 980 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 981 | }); |
| 982 | anim.setTarget(f.mView); |
| 983 | anim.start(); |
| 984 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 985 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 986 | f.mContainer.removeView(f.mView); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 987 | } |
| 988 | f.mContainer = null; |
| 989 | f.mView = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 990 | } |
| 991 | case Fragment.CREATED: |
| 992 | if (newState < Fragment.CREATED) { |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 993 | if (mDestroyed) { |
| 994 | if (f.mAnimatingAway != null) { |
| 995 | // The fragment's containing activity is |
| 996 | // being destroyed, but this fragment is |
| 997 | // currently animating away. Stop the |
| 998 | // animation right now -- it is not needed, |
| 999 | // and we can't wait any more on destroying |
| 1000 | // the fragment. |
Dianne Hackborn | 1b39e22 | 2010-12-28 14:17:18 -0800 | [diff] [blame] | 1001 | Animator anim = f.mAnimatingAway; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1002 | f.mAnimatingAway = null; |
Dianne Hackborn | 1b39e22 | 2010-12-28 14:17:18 -0800 | [diff] [blame] | 1003 | anim.cancel(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1004 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1005 | } |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1006 | if (f.mAnimatingAway != null) { |
| 1007 | // We are waiting for the fragment's view to finish |
| 1008 | // animating away. Just make a note of the state |
| 1009 | // the fragment now should move to once the animation |
| 1010 | // is done. |
| 1011 | f.mStateAfterAnimating = newState; |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1012 | newState = Fragment.CREATED; |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1013 | } else { |
| 1014 | if (DEBUG) Log.v(TAG, "movefrom CREATED: " + f); |
| 1015 | if (!f.mRetaining) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1016 | f.performDestroy(); |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | f.mCalled = false; |
| 1020 | f.onDetach(); |
| 1021 | if (!f.mCalled) { |
| 1022 | throw new SuperNotCalledException("Fragment " + f |
| 1023 | + " did not call through to super.onDetach()"); |
| 1024 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1025 | if (!keepActive) { |
| 1026 | if (!f.mRetaining) { |
| 1027 | makeInactive(f); |
| 1028 | } else { |
| 1029 | f.mActivity = null; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1030 | f.mParentFragment = null; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1031 | f.mFragmentManager = null; |
Tim Kilbourn | 7071786 | 2014-02-28 01:00:06 -0800 | [diff] [blame] | 1032 | f.mChildFragmentManager = null; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1033 | } |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1034 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1035 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | f.mState = newState; |
| 1041 | } |
| 1042 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1043 | void moveToState(Fragment f) { |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1044 | moveToState(f, mCurState, 0, 0, false); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1047 | void moveToState(int newState, boolean always) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1048 | moveToState(newState, 0, 0, always); |
| 1049 | } |
| 1050 | |
| 1051 | void moveToState(int newState, int transit, int transitStyle, boolean always) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1052 | if (mActivity == null && newState != Fragment.INITIALIZING) { |
| 1053 | throw new IllegalStateException("No activity"); |
| 1054 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1055 | |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1056 | if (!always && mCurState == newState) { |
| 1057 | return; |
| 1058 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1059 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1060 | mCurState = newState; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1061 | if (mActive != null) { |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1062 | boolean loadersRunning = false; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1063 | for (int i=0; i<mActive.size(); i++) { |
| 1064 | Fragment f = mActive.get(i); |
| 1065 | if (f != null) { |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1066 | moveToState(f, newState, transit, transitStyle, false); |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1067 | if (f.mLoaderManager != null) { |
| 1068 | loadersRunning |= f.mLoaderManager.hasRunningLoaders(); |
| 1069 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1070 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1071 | } |
Dianne Hackborn | 5f36c96 | 2010-08-26 15:54:17 -0700 | [diff] [blame] | 1072 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1073 | if (!loadersRunning) { |
| 1074 | startPendingDeferredFragments(); |
| 1075 | } |
| 1076 | |
Adam Powell | 89b09da | 2011-07-27 11:55:29 -0700 | [diff] [blame] | 1077 | if (mNeedMenuInvalidate && mActivity != null && mCurState == Fragment.RESUMED) { |
Dianne Hackborn | 5f36c96 | 2010-08-26 15:54:17 -0700 | [diff] [blame] | 1078 | mActivity.invalidateOptionsMenu(); |
| 1079 | mNeedMenuInvalidate = false; |
| 1080 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1084 | void startPendingDeferredFragments() { |
Adam Powell | 3751090 | 2011-10-31 11:48:24 -0700 | [diff] [blame] | 1085 | if (mActive == null) return; |
| 1086 | |
Adam Powell | 635c60a | 2011-10-26 10:22:16 -0700 | [diff] [blame] | 1087 | for (int i=0; i<mActive.size(); i++) { |
| 1088 | Fragment f = mActive.get(i); |
| 1089 | if (f != null) { |
| 1090 | performPendingDeferredStart(f); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1095 | void makeActive(Fragment f) { |
| 1096 | if (f.mIndex >= 0) { |
| 1097 | return; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1098 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1099 | |
| 1100 | if (mAvailIndices == null || mAvailIndices.size() <= 0) { |
| 1101 | if (mActive == null) { |
| 1102 | mActive = new ArrayList<Fragment>(); |
| 1103 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1104 | f.setIndex(mActive.size(), mParent); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1105 | mActive.add(f); |
| 1106 | |
| 1107 | } else { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1108 | f.setIndex(mAvailIndices.remove(mAvailIndices.size()-1), mParent); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1109 | mActive.set(f.mIndex, f); |
| 1110 | } |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 1111 | if (DEBUG) Log.v(TAG, "Allocated fragment index " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | void makeInactive(Fragment f) { |
| 1115 | if (f.mIndex < 0) { |
| 1116 | return; |
| 1117 | } |
| 1118 | |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 1119 | if (DEBUG) Log.v(TAG, "Freeing fragment index " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1120 | mActive.set(f.mIndex, null); |
| 1121 | if (mAvailIndices == null) { |
| 1122 | mAvailIndices = new ArrayList<Integer>(); |
| 1123 | } |
| 1124 | mAvailIndices.add(f.mIndex); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1125 | mActivity.invalidateFragment(f.mWho); |
Dianne Hackborn | afc4b28 | 2011-06-10 17:03:42 -0700 | [diff] [blame] | 1126 | f.initState(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | public void addFragment(Fragment fragment, boolean moveToStateNow) { |
| 1130 | if (mAdded == null) { |
| 1131 | mAdded = new ArrayList<Fragment>(); |
| 1132 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1133 | if (DEBUG) Log.v(TAG, "add: " + fragment); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1134 | makeActive(fragment); |
| 1135 | if (!fragment.mDetached) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1136 | if (mAdded.contains(fragment)) { |
| 1137 | throw new IllegalStateException("Fragment already added: " + fragment); |
| 1138 | } |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1139 | mAdded.add(fragment); |
| 1140 | fragment.mAdded = true; |
| 1141 | fragment.mRemoving = false; |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1142 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1143 | mNeedMenuInvalidate = true; |
| 1144 | } |
| 1145 | if (moveToStateNow) { |
| 1146 | moveToState(fragment); |
| 1147 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1148 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1151 | public void removeFragment(Fragment fragment, int transition, int transitionStyle) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1152 | if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1153 | final boolean inactive = !fragment.isInBackStack(); |
| 1154 | if (!fragment.mDetached || inactive) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1155 | if (false) { |
| 1156 | // Would be nice to catch a bad remove here, but we need |
| 1157 | // time to test this to make sure we aren't crashes cases |
| 1158 | // where it is not a problem. |
| 1159 | if (!mAdded.contains(fragment)) { |
| 1160 | throw new IllegalStateException("Fragment not added: " + fragment); |
| 1161 | } |
| 1162 | } |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1163 | if (mAdded != null) { |
| 1164 | mAdded.remove(fragment); |
| 1165 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1166 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1167 | mNeedMenuInvalidate = true; |
| 1168 | } |
| 1169 | fragment.mAdded = false; |
| 1170 | fragment.mRemoving = true; |
| 1171 | moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1172 | transition, transitionStyle, false); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 1173 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1176 | public void hideFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1177 | if (DEBUG) Log.v(TAG, "hide: " + fragment); |
| 1178 | if (!fragment.mHidden) { |
| 1179 | fragment.mHidden = true; |
| 1180 | if (fragment.mView != null) { |
Adam Powell | 2756293 | 2013-06-07 10:16:08 -0700 | [diff] [blame] | 1181 | Animator anim = loadAnimator(fragment, transition, false, |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1182 | transitionStyle); |
| 1183 | if (anim != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1184 | anim.setTarget(fragment.mView); |
Chet Haase | 61eb40d | 2010-12-28 13:59:17 -0800 | [diff] [blame] | 1185 | // Delay the actual hide operation until the animation finishes, otherwise |
| 1186 | // the fragment will just immediately disappear |
| 1187 | final Fragment finalFragment = fragment; |
| 1188 | anim.addListener(new AnimatorListenerAdapter() { |
| 1189 | @Override |
| 1190 | public void onAnimationEnd(Animator animation) { |
Chet Haase | b29407f | 2011-01-11 14:09:34 -0800 | [diff] [blame] | 1191 | if (finalFragment.mView != null) { |
| 1192 | finalFragment.mView.setVisibility(View.GONE); |
| 1193 | } |
Chet Haase | 61eb40d | 2010-12-28 13:59:17 -0800 | [diff] [blame] | 1194 | } |
| 1195 | }); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1196 | anim.start(); |
Chet Haase | e646b28c | 2010-12-28 14:48:32 -0800 | [diff] [blame] | 1197 | } else { |
| 1198 | fragment.mView.setVisibility(View.GONE); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1199 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1200 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1201 | if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1202 | mNeedMenuInvalidate = true; |
| 1203 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1204 | fragment.onHiddenChanged(true); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | public void showFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1209 | if (DEBUG) Log.v(TAG, "show: " + fragment); |
| 1210 | if (fragment.mHidden) { |
| 1211 | fragment.mHidden = false; |
| 1212 | if (fragment.mView != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1213 | Animator anim = loadAnimator(fragment, transition, true, |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1214 | transitionStyle); |
| 1215 | if (anim != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1216 | anim.setTarget(fragment.mView); |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 1217 | anim.start(); |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1218 | } |
| 1219 | fragment.mView.setVisibility(View.VISIBLE); |
| 1220 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1221 | if (fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1222 | mNeedMenuInvalidate = true; |
| 1223 | } |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1224 | fragment.onHiddenChanged(false); |
| 1225 | } |
| 1226 | } |
| 1227 | |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1228 | public void detachFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1229 | if (DEBUG) Log.v(TAG, "detach: " + fragment); |
| 1230 | if (!fragment.mDetached) { |
| 1231 | fragment.mDetached = true; |
| 1232 | if (fragment.mAdded) { |
| 1233 | // We are not already in back stack, so need to remove the fragment. |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1234 | if (mAdded != null) { |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1235 | if (DEBUG) Log.v(TAG, "remove from detach: " + fragment); |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1236 | mAdded.remove(fragment); |
| 1237 | } |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1238 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1239 | mNeedMenuInvalidate = true; |
| 1240 | } |
| 1241 | fragment.mAdded = false; |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1242 | moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | public void attachFragment(Fragment fragment, int transition, int transitionStyle) { |
| 1248 | if (DEBUG) Log.v(TAG, "attach: " + fragment); |
| 1249 | if (fragment.mDetached) { |
| 1250 | fragment.mDetached = false; |
| 1251 | if (!fragment.mAdded) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1252 | if (mAdded == null) { |
| 1253 | mAdded = new ArrayList<Fragment>(); |
| 1254 | } |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1255 | if (mAdded.contains(fragment)) { |
| 1256 | throw new IllegalStateException("Fragment already added: " + fragment); |
| 1257 | } |
| 1258 | if (DEBUG) Log.v(TAG, "add from attach: " + fragment); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1259 | mAdded.add(fragment); |
| 1260 | fragment.mAdded = true; |
Dianne Hackborn | 6c28597 | 2011-08-29 16:53:49 -0700 | [diff] [blame] | 1261 | if (fragment.mHasMenu && fragment.mMenuVisible) { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1262 | mNeedMenuInvalidate = true; |
| 1263 | } |
Dianne Hackborn | ee76efb | 2012-06-05 10:27:40 -0700 | [diff] [blame] | 1264 | moveToState(fragment, mCurState, transition, transitionStyle, false); |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1269 | public Fragment findFragmentById(int id) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1270 | if (mAdded != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1271 | // First look through added fragments. |
| 1272 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 1273 | Fragment f = mAdded.get(i); |
| 1274 | if (f != null && f.mFragmentId == id) { |
| 1275 | return f; |
| 1276 | } |
| 1277 | } |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1278 | } |
| 1279 | if (mActive != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1280 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1281 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1282 | Fragment f = mActive.get(i); |
| 1283 | if (f != null && f.mFragmentId == id) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1284 | return f; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | return null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1291 | public Fragment findFragmentByTag(String tag) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1292 | if (mAdded != null && tag != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1293 | // First look through added fragments. |
| 1294 | for (int i=mAdded.size()-1; i>=0; i--) { |
| 1295 | Fragment f = mAdded.get(i); |
| 1296 | if (f != null && tag.equals(f.mTag)) { |
| 1297 | return f; |
| 1298 | } |
| 1299 | } |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1300 | } |
| 1301 | if (mActive != null && tag != null) { |
Dianne Hackborn | 5ae74d6 | 2010-05-19 19:14:57 -0700 | [diff] [blame] | 1302 | // Now for any known fragment. |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1303 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1304 | Fragment f = mActive.get(i); |
| 1305 | if (f != null && tag.equals(f.mTag)) { |
| 1306 | return f; |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | return null; |
| 1311 | } |
| 1312 | |
| 1313 | public Fragment findFragmentByWho(String who) { |
| 1314 | if (mActive != null && who != null) { |
| 1315 | for (int i=mActive.size()-1; i>=0; i--) { |
| 1316 | Fragment f = mActive.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1317 | if (f != null && (f=f.findFragmentByWho(who)) != null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1318 | return f; |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | return null; |
| 1323 | } |
| 1324 | |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1325 | private void checkStateLoss() { |
| 1326 | if (mStateSaved) { |
| 1327 | throw new IllegalStateException( |
| 1328 | "Can not perform this action after onSaveInstanceState"); |
| 1329 | } |
| 1330 | if (mNoTransactionsBecause != null) { |
| 1331 | throw new IllegalStateException( |
| 1332 | "Can not perform this action inside of " + mNoTransactionsBecause); |
| 1333 | } |
| 1334 | } |
| 1335 | |
Alan Viverette | 95a4609 | 2013-08-14 11:17:25 -0700 | [diff] [blame] | 1336 | /** |
| 1337 | * Adds an action to the queue of pending actions. |
| 1338 | * |
| 1339 | * @param action the action to add |
| 1340 | * @param allowStateLoss whether to allow loss of state information |
| 1341 | * @throws IllegalStateException if the activity has been destroyed |
| 1342 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 1343 | public void enqueueAction(Runnable action, boolean allowStateLoss) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 1344 | if (!allowStateLoss) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1345 | checkStateLoss(); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1346 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1347 | synchronized (this) { |
Alan Viverette | 95a4609 | 2013-08-14 11:17:25 -0700 | [diff] [blame] | 1348 | if (mDestroyed || mActivity == null) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 1349 | throw new IllegalStateException("Activity has been destroyed"); |
| 1350 | } |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1351 | if (mPendingActions == null) { |
| 1352 | mPendingActions = new ArrayList<Runnable>(); |
| 1353 | } |
| 1354 | mPendingActions.add(action); |
| 1355 | if (mPendingActions.size() == 1) { |
| 1356 | mActivity.mHandler.removeCallbacks(mExecCommit); |
| 1357 | mActivity.mHandler.post(mExecCommit); |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1362 | public int allocBackStackIndex(BackStackRecord bse) { |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1363 | synchronized (this) { |
| 1364 | if (mAvailBackStackIndices == null || mAvailBackStackIndices.size() <= 0) { |
| 1365 | if (mBackStackIndices == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1366 | mBackStackIndices = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1367 | } |
| 1368 | int index = mBackStackIndices.size(); |
| 1369 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 1370 | mBackStackIndices.add(bse); |
| 1371 | return index; |
| 1372 | |
| 1373 | } else { |
| 1374 | int index = mAvailBackStackIndices.remove(mAvailBackStackIndices.size()-1); |
| 1375 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 1376 | mBackStackIndices.set(index, bse); |
| 1377 | return index; |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1382 | public void setBackStackIndex(int index, BackStackRecord bse) { |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1383 | synchronized (this) { |
| 1384 | if (mBackStackIndices == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1385 | mBackStackIndices = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1386 | } |
| 1387 | int N = mBackStackIndices.size(); |
| 1388 | if (index < N) { |
| 1389 | if (DEBUG) Log.v(TAG, "Setting back stack index " + index + " to " + bse); |
| 1390 | mBackStackIndices.set(index, bse); |
| 1391 | } else { |
| 1392 | while (N < index) { |
| 1393 | mBackStackIndices.add(null); |
| 1394 | if (mAvailBackStackIndices == null) { |
| 1395 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 1396 | } |
| 1397 | if (DEBUG) Log.v(TAG, "Adding available back stack index " + N); |
| 1398 | mAvailBackStackIndices.add(N); |
| 1399 | N++; |
| 1400 | } |
| 1401 | if (DEBUG) Log.v(TAG, "Adding back stack index " + index + " with " + bse); |
| 1402 | mBackStackIndices.add(bse); |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | public void freeBackStackIndex(int index) { |
| 1408 | synchronized (this) { |
| 1409 | mBackStackIndices.set(index, null); |
| 1410 | if (mAvailBackStackIndices == null) { |
| 1411 | mAvailBackStackIndices = new ArrayList<Integer>(); |
| 1412 | } |
| 1413 | if (DEBUG) Log.v(TAG, "Freeing back stack index " + index); |
| 1414 | mAvailBackStackIndices.add(index); |
| 1415 | } |
| 1416 | } |
| 1417 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1418 | /** |
| 1419 | * Only call from main thread! |
| 1420 | */ |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1421 | public boolean execPendingActions() { |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1422 | if (mExecutingActions) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1423 | throw new IllegalStateException("Recursive entry to executePendingTransactions"); |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Dianne Hackborn | d9b3b7e | 2010-11-16 18:22:49 -0800 | [diff] [blame] | 1426 | if (Looper.myLooper() != mActivity.mHandler.getLooper()) { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1427 | throw new IllegalStateException("Must be called from main thread of process"); |
| 1428 | } |
| 1429 | |
| 1430 | boolean didSomething = false; |
| 1431 | |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1432 | while (true) { |
| 1433 | int numActions; |
| 1434 | |
| 1435 | synchronized (this) { |
| 1436 | if (mPendingActions == null || mPendingActions.size() == 0) { |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1437 | break; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | numActions = mPendingActions.size(); |
| 1441 | if (mTmpActions == null || mTmpActions.length < numActions) { |
| 1442 | mTmpActions = new Runnable[numActions]; |
| 1443 | } |
| 1444 | mPendingActions.toArray(mTmpActions); |
| 1445 | mPendingActions.clear(); |
| 1446 | mActivity.mHandler.removeCallbacks(mExecCommit); |
| 1447 | } |
| 1448 | |
| 1449 | mExecutingActions = true; |
| 1450 | for (int i=0; i<numActions; i++) { |
| 1451 | mTmpActions[i].run(); |
Jeff Sharkey | 0d32528 | 2011-07-13 09:36:27 -0700 | [diff] [blame] | 1452 | mTmpActions[i] = null; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1453 | } |
| 1454 | mExecutingActions = false; |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1455 | didSomething = true; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1456 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1457 | |
| 1458 | if (mHavePendingDeferredStart) { |
| 1459 | boolean loadersRunning = false; |
| 1460 | for (int i=0; i<mActive.size(); i++) { |
| 1461 | Fragment f = mActive.get(i); |
| 1462 | if (f != null && f.mLoaderManager != null) { |
| 1463 | loadersRunning |= f.mLoaderManager.hasRunningLoaders(); |
| 1464 | } |
| 1465 | } |
| 1466 | if (!loadersRunning) { |
| 1467 | mHavePendingDeferredStart = false; |
| 1468 | startPendingDeferredFragments(); |
| 1469 | } |
| 1470 | } |
| 1471 | return didSomething; |
Dianne Hackborn | 445646c | 2010-06-25 15:52:59 -0700 | [diff] [blame] | 1472 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1473 | |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1474 | void reportBackStackChanged() { |
| 1475 | if (mBackStackChangeListeners != null) { |
| 1476 | for (int i=0; i<mBackStackChangeListeners.size(); i++) { |
| 1477 | mBackStackChangeListeners.get(i).onBackStackChanged(); |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | void addBackStackState(BackStackRecord state) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1483 | if (mBackStack == null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1484 | mBackStack = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1485 | } |
| 1486 | mBackStack.add(state); |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1487 | reportBackStackChanged(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1490 | boolean popBackStackState(Handler handler, String name, int id, int flags) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1491 | if (mBackStack == null) { |
| 1492 | return false; |
| 1493 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1494 | if (name == null && id < 0 && (flags&POP_BACK_STACK_INCLUSIVE) == 0) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1495 | int last = mBackStack.size()-1; |
| 1496 | if (last < 0) { |
| 1497 | return false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1498 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1499 | final BackStackRecord bss = mBackStack.remove(last); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1500 | SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); |
| 1501 | SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); |
| 1502 | bss.calculateBackFragments(firstOutFragments, lastInFragments); |
| 1503 | bss.popFromBackStack(true, null, firstOutFragments, lastInFragments); |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1504 | reportBackStackChanged(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1505 | } else { |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1506 | int index = -1; |
| 1507 | if (name != null || id >= 0) { |
| 1508 | // If a name or ID is specified, look for that place in |
| 1509 | // the stack. |
| 1510 | index = mBackStack.size()-1; |
| 1511 | while (index >= 0) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1512 | BackStackRecord bss = mBackStack.get(index); |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1513 | if (name != null && name.equals(bss.getName())) { |
| 1514 | break; |
| 1515 | } |
| 1516 | if (id >= 0 && id == bss.mIndex) { |
| 1517 | break; |
| 1518 | } |
| 1519 | index--; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1520 | } |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1521 | if (index < 0) { |
| 1522 | return false; |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1523 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1524 | if ((flags&POP_BACK_STACK_INCLUSIVE) != 0) { |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1525 | index--; |
| 1526 | // Consume all following entries that match. |
| 1527 | while (index >= 0) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1528 | BackStackRecord bss = mBackStack.get(index); |
Dianne Hackborn | b3cf10f | 2010-08-03 13:07:11 -0700 | [diff] [blame] | 1529 | if ((name != null && name.equals(bss.getName())) |
| 1530 | || (id >= 0 && id == bss.mIndex)) { |
| 1531 | index--; |
| 1532 | continue; |
| 1533 | } |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1537 | } |
| 1538 | if (index == mBackStack.size()-1) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1539 | return false; |
| 1540 | } |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1541 | final ArrayList<BackStackRecord> states |
| 1542 | = new ArrayList<BackStackRecord>(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1543 | for (int i=mBackStack.size()-1; i>index; i--) { |
| 1544 | states.add(mBackStack.remove(i)); |
| 1545 | } |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1546 | final int LAST = states.size()-1; |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1547 | SparseArray<Fragment> firstOutFragments = new SparseArray<Fragment>(); |
| 1548 | SparseArray<Fragment> lastInFragments = new SparseArray<Fragment>(); |
| 1549 | for (int i=0; i<=LAST; i++) { |
| 1550 | states.get(i).calculateBackFragments(firstOutFragments, lastInFragments); |
| 1551 | } |
George Mount | d4c3c91 | 2014-06-09 12:31:34 -0700 | [diff] [blame] | 1552 | BackStackRecord.TransitionState state = null; |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1553 | for (int i=0; i<=LAST; i++) { |
| 1554 | if (DEBUG) Log.v(TAG, "Popping back stack state: " + states.get(i)); |
George Mount | c03da0e | 2014-08-22 17:04:02 -0700 | [diff] [blame] | 1555 | state = states.get(i).popFromBackStack(i == LAST, state, |
| 1556 | firstOutFragments, lastInFragments); |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1557 | } |
| 1558 | reportBackStackChanged(); |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1559 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1560 | return true; |
| 1561 | } |
| 1562 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1563 | ArrayList<Fragment> retainNonConfig() { |
| 1564 | ArrayList<Fragment> fragments = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1565 | if (mActive != null) { |
| 1566 | for (int i=0; i<mActive.size(); i++) { |
| 1567 | Fragment f = mActive.get(i); |
| 1568 | if (f != null && f.mRetainInstance) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1569 | if (fragments == null) { |
| 1570 | fragments = new ArrayList<Fragment>(); |
| 1571 | } |
| 1572 | fragments.add(f); |
| 1573 | f.mRetaining = true; |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1574 | f.mTargetIndex = f.mTarget != null ? f.mTarget.mIndex : -1; |
Dianne Hackborn | 03fcc33 | 2012-05-15 12:49:40 -0700 | [diff] [blame] | 1575 | if (DEBUG) Log.v(TAG, "retainNonConfig: keeping retained " + f); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | return fragments; |
| 1580 | } |
| 1581 | |
| 1582 | void saveFragmentViewState(Fragment f) { |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1583 | if (f.mView == null) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1584 | return; |
| 1585 | } |
| 1586 | if (mStateArray == null) { |
| 1587 | mStateArray = new SparseArray<Parcelable>(); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 1588 | } else { |
| 1589 | mStateArray.clear(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1590 | } |
| 1591 | f.mView.saveHierarchyState(mStateArray); |
| 1592 | if (mStateArray.size() > 0) { |
| 1593 | f.mSavedViewState = mStateArray; |
| 1594 | mStateArray = null; |
| 1595 | } |
| 1596 | } |
| 1597 | |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 1598 | Bundle saveFragmentBasicState(Fragment f) { |
| 1599 | Bundle result = null; |
| 1600 | |
| 1601 | if (mStateBundle == null) { |
| 1602 | mStateBundle = new Bundle(); |
| 1603 | } |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1604 | f.performSaveInstanceState(mStateBundle); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 1605 | if (!mStateBundle.isEmpty()) { |
| 1606 | result = mStateBundle; |
| 1607 | mStateBundle = null; |
| 1608 | } |
| 1609 | |
| 1610 | if (f.mView != null) { |
| 1611 | saveFragmentViewState(f); |
Dianne Hackborn | 1333276 | 2011-06-03 17:34:45 -0700 | [diff] [blame] | 1612 | } |
| 1613 | if (f.mSavedViewState != null) { |
| 1614 | if (result == null) { |
| 1615 | result = new Bundle(); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 1616 | } |
Dianne Hackborn | 1333276 | 2011-06-03 17:34:45 -0700 | [diff] [blame] | 1617 | result.putSparseParcelableArray( |
| 1618 | FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 1619 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1620 | if (!f.mUserVisibleHint) { |
Jake Wharton | 258029e | 2012-04-22 17:17:01 -0400 | [diff] [blame] | 1621 | if (result == null) { |
| 1622 | result = new Bundle(); |
| 1623 | } |
Adam Powell | 78fed9b | 2011-11-07 10:45:34 -0800 | [diff] [blame] | 1624 | // Only add this if it's not the default value |
| 1625 | result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); |
| 1626 | } |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 1627 | |
| 1628 | return result; |
| 1629 | } |
| 1630 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1631 | Parcelable saveAllState() { |
Dianne Hackborn | 3a57fb9 | 2010-11-15 17:58:52 -0800 | [diff] [blame] | 1632 | // Make sure all pending operations have now been executed to get |
| 1633 | // our state update-to-date. |
| 1634 | execPendingActions(); |
| 1635 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1636 | mStateSaved = true; |
| 1637 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1638 | if (mActive == null || mActive.size() <= 0) { |
| 1639 | return null; |
| 1640 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1641 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1642 | // First collect all active fragments. |
| 1643 | int N = mActive.size(); |
| 1644 | FragmentState[] active = new FragmentState[N]; |
| 1645 | boolean haveFragments = false; |
| 1646 | for (int i=0; i<N; i++) { |
| 1647 | Fragment f = mActive.get(i); |
| 1648 | if (f != null) { |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 1649 | if (f.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1650 | throwException(new IllegalStateException( |
| 1651 | "Failure saving state: active " + f |
| 1652 | + " has cleared index: " + f.mIndex)); |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1655 | haveFragments = true; |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 1656 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1657 | FragmentState fs = new FragmentState(f); |
| 1658 | active[i] = fs; |
| 1659 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1660 | if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) { |
Dianne Hackborn | b46ed76 | 2011-06-02 18:33:15 -0700 | [diff] [blame] | 1661 | fs.mSavedFragmentState = saveFragmentBasicState(f); |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1662 | |
| 1663 | if (f.mTarget != null) { |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1664 | if (f.mTarget.mIndex < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1665 | throwException(new IllegalStateException( |
| 1666 | "Failure saving state: " + f |
| 1667 | + " has target not in fragment manager: " + f.mTarget)); |
Dianne Hackborn | 5d9d03a | 2011-01-24 13:15:09 -0800 | [diff] [blame] | 1668 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1669 | if (fs.mSavedFragmentState == null) { |
| 1670 | fs.mSavedFragmentState = new Bundle(); |
| 1671 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1672 | putFragment(fs.mSavedFragmentState, |
| 1673 | FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget); |
| 1674 | if (f.mTargetRequestCode != 0) { |
| 1675 | fs.mSavedFragmentState.putInt( |
| 1676 | FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, |
| 1677 | f.mTargetRequestCode); |
| 1678 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1679 | } |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1680 | |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1681 | } else { |
| 1682 | fs.mSavedFragmentState = f.mSavedFragmentState; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1685 | if (DEBUG) Log.v(TAG, "Saved state of " + f + ": " |
| 1686 | + fs.mSavedFragmentState); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | if (!haveFragments) { |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1691 | if (DEBUG) Log.v(TAG, "saveAllState: no fragments!"); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1692 | return null; |
| 1693 | } |
| 1694 | |
| 1695 | int[] added = null; |
| 1696 | BackStackState[] backStack = null; |
| 1697 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1698 | // Build list of currently added fragments. |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1699 | if (mAdded != null) { |
| 1700 | N = mAdded.size(); |
| 1701 | if (N > 0) { |
| 1702 | added = new int[N]; |
| 1703 | for (int i=0; i<N; i++) { |
| 1704 | added[i] = mAdded.get(i).mIndex; |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 1705 | if (added[i] < 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1706 | throwException(new IllegalStateException( |
| 1707 | "Failure saving state: active " + mAdded.get(i) |
| 1708 | + " has cleared index: " + added[i])); |
Dianne Hackborn | 61af8a8 | 2012-05-30 16:38:30 -0700 | [diff] [blame] | 1709 | } |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1710 | if (DEBUG) Log.v(TAG, "saveAllState: adding fragment #" + i |
| 1711 | + ": " + mAdded.get(i)); |
| 1712 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | // Now save back stack. |
| 1717 | if (mBackStack != null) { |
| 1718 | N = mBackStack.size(); |
| 1719 | if (N > 0) { |
| 1720 | backStack = new BackStackState[N]; |
| 1721 | for (int i=0; i<N; i++) { |
| 1722 | backStack[i] = new BackStackState(this, mBackStack.get(i)); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1723 | if (DEBUG) Log.v(TAG, "saveAllState: adding back stack #" + i |
| 1724 | + ": " + mBackStack.get(i)); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1729 | FragmentManagerState fms = new FragmentManagerState(); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1730 | fms.mActive = active; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1731 | fms.mAdded = added; |
| 1732 | fms.mBackStack = backStack; |
| 1733 | return fms; |
| 1734 | } |
| 1735 | |
| 1736 | void restoreAllState(Parcelable state, ArrayList<Fragment> nonConfig) { |
| 1737 | // If there is no saved state at all, then there can not be |
| 1738 | // any nonConfig fragments either, so that is that. |
| 1739 | if (state == null) return; |
| 1740 | FragmentManagerState fms = (FragmentManagerState)state; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1741 | if (fms.mActive == null) return; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1742 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1743 | // First re-attach any non-config instances we are retaining back |
| 1744 | // 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] | 1745 | if (nonConfig != null) { |
| 1746 | for (int i=0; i<nonConfig.size(); i++) { |
| 1747 | Fragment f = nonConfig.get(i); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1748 | if (DEBUG) Log.v(TAG, "restoreAllState: re-attaching retained " + f); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1749 | FragmentState fs = fms.mActive[f.mIndex]; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1750 | fs.mInstance = f; |
| 1751 | f.mSavedViewState = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1752 | f.mBackStackNesting = 0; |
Dianne Hackborn | 625ac27 | 2010-09-17 18:29:22 -0700 | [diff] [blame] | 1753 | f.mInLayout = false; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1754 | f.mAdded = false; |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1755 | f.mTarget = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1756 | if (fs.mSavedFragmentState != null) { |
Dianne Hackborn | 5164246 | 2010-10-28 10:32:37 -0700 | [diff] [blame] | 1757 | fs.mSavedFragmentState.setClassLoader(mActivity.getClassLoader()); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1758 | f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray( |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 1759 | FragmentManagerImpl.VIEW_STATE_TAG); |
Craig Mautner | 33acfbe | 2014-06-28 14:00:53 -0700 | [diff] [blame] | 1760 | f.mSavedFragmentState = fs.mSavedFragmentState; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1761 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1765 | // Build the full list of active fragments, instantiating them from |
| 1766 | // their saved state. |
| 1767 | mActive = new ArrayList<Fragment>(fms.mActive.length); |
| 1768 | if (mAvailIndices != null) { |
| 1769 | mAvailIndices.clear(); |
| 1770 | } |
| 1771 | for (int i=0; i<fms.mActive.length; i++) { |
| 1772 | FragmentState fs = fms.mActive[i]; |
| 1773 | if (fs != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1774 | Fragment f = fs.instantiate(mActivity, mParent); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1775 | if (DEBUG) Log.v(TAG, "restoreAllState: active #" + i + ": " + f); |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1776 | mActive.add(f); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 1777 | // Now that the fragment is instantiated (or came from being |
| 1778 | // retained above), clear mInstance in case we end up re-restoring |
| 1779 | // from this FragmentState again. |
| 1780 | fs.mInstance = null; |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1781 | } else { |
| 1782 | mActive.add(null); |
| 1783 | if (mAvailIndices == null) { |
| 1784 | mAvailIndices = new ArrayList<Integer>(); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1785 | } |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1786 | if (DEBUG) Log.v(TAG, "restoreAllState: avail #" + i); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1787 | mAvailIndices.add(i); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1788 | } |
| 1789 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1790 | |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1791 | // Update the target of all retained fragments. |
| 1792 | if (nonConfig != null) { |
| 1793 | for (int i=0; i<nonConfig.size(); i++) { |
| 1794 | Fragment f = nonConfig.get(i); |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1795 | if (f.mTargetIndex >= 0) { |
| 1796 | if (f.mTargetIndex < mActive.size()) { |
| 1797 | f.mTarget = mActive.get(f.mTargetIndex); |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1798 | } else { |
| 1799 | Log.w(TAG, "Re-attaching retained fragment " + f |
Dianne Hackborn | f930232 | 2011-06-14 18:36:14 -0700 | [diff] [blame] | 1800 | + " target no longer exists: " + f.mTargetIndex); |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1801 | f.mTarget = null; |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1807 | // Build the list of currently added fragments. |
| 1808 | if (fms.mAdded != null) { |
| 1809 | mAdded = new ArrayList<Fragment>(fms.mAdded.length); |
| 1810 | for (int i=0; i<fms.mAdded.length; i++) { |
| 1811 | Fragment f = mActive.get(fms.mAdded[i]); |
| 1812 | if (f == null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1813 | throwException(new IllegalStateException( |
| 1814 | "No instantiated fragment for index #" + fms.mAdded[i])); |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1815 | } |
| 1816 | f.mAdded = true; |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1817 | if (DEBUG) Log.v(TAG, "restoreAllState: added #" + i + ": " + f); |
| 1818 | if (mAdded.contains(f)) { |
| 1819 | throw new IllegalStateException("Already added!"); |
| 1820 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1821 | mAdded.add(f); |
| 1822 | } |
| 1823 | } else { |
| 1824 | mAdded = null; |
| 1825 | } |
| 1826 | |
| 1827 | // Build the back stack. |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1828 | if (fms.mBackStack != null) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1829 | mBackStack = new ArrayList<BackStackRecord>(fms.mBackStack.length); |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1830 | for (int i=0; i<fms.mBackStack.length; i++) { |
Dianne Hackborn | c6669ca | 2010-09-16 01:33:24 -0700 | [diff] [blame] | 1831 | BackStackRecord bse = fms.mBackStack[i].instantiate(this); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1832 | if (DEBUG) { |
| 1833 | Log.v(TAG, "restoreAllState: back stack #" + i |
Dianne Hackborn | b7a2e47 | 2010-08-12 16:20:42 -0700 | [diff] [blame] | 1834 | + " (index " + bse.mIndex + "): " + bse); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1835 | LogWriter logw = new LogWriter(Log.VERBOSE, TAG); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 1836 | PrintWriter pw = new FastPrintWriter(logw, false, 1024); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1837 | bse.dump(" ", pw, false); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 1838 | pw.flush(); |
Dianne Hackborn | f43a33c | 2012-09-27 00:48:11 -0700 | [diff] [blame] | 1839 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1840 | mBackStack.add(bse); |
Dianne Hackborn | dd913a5 | 2010-07-22 12:17:04 -0700 | [diff] [blame] | 1841 | if (bse.mIndex >= 0) { |
| 1842 | setBackStackIndex(bse.mIndex, bse); |
| 1843 | } |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1844 | } |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1845 | } else { |
| 1846 | mBackStack = null; |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1850 | public void attachActivity(Activity activity, FragmentContainer container, Fragment parent) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1851 | if (mActivity != null) throw new IllegalStateException("Already attached"); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1852 | mActivity = activity; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1853 | mContainer = container; |
| 1854 | mParent = parent; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 1857 | public void noteStateNotSaved() { |
| 1858 | mStateSaved = false; |
| 1859 | } |
| 1860 | |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1861 | public void dispatchCreate() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1862 | mStateSaved = false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1863 | moveToState(Fragment.CREATED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1866 | public void dispatchActivityCreated() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1867 | mStateSaved = false; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1868 | moveToState(Fragment.ACTIVITY_CREATED, false); |
| 1869 | } |
| 1870 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1871 | public void dispatchStart() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1872 | mStateSaved = false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1873 | moveToState(Fragment.STARTED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | public void dispatchResume() { |
Dianne Hackborn | 3e449ce | 2010-09-11 20:52:31 -0700 | [diff] [blame] | 1877 | mStateSaved = false; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1878 | moveToState(Fragment.RESUMED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | public void dispatchPause() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1882 | moveToState(Fragment.STARTED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | public void dispatchStop() { |
Dianne Hackborn | 16f6e89 | 2011-04-15 19:00:20 -0700 | [diff] [blame] | 1886 | moveToState(Fragment.STOPPED, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1889 | public void dispatchDestroyView() { |
| 1890 | moveToState(Fragment.CREATED, false); |
| 1891 | } |
Dianne Hackborn | 1b8ecc5 | 2012-09-08 17:03:52 -0700 | [diff] [blame] | 1892 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1893 | public void dispatchDestroy() { |
Dianne Hackborn | d173fa3 | 2010-12-23 13:58:22 -0800 | [diff] [blame] | 1894 | mDestroyed = true; |
Dianne Hackborn | c693823 | 2011-07-21 16:25:26 -0700 | [diff] [blame] | 1895 | execPendingActions(); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 1896 | moveToState(Fragment.INITIALIZING, false); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1897 | mActivity = null; |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1898 | mContainer = null; |
| 1899 | mParent = null; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1900 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1901 | |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1902 | public void dispatchConfigurationChanged(Configuration newConfig) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1903 | if (mAdded != null) { |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1904 | for (int i=0; i<mAdded.size(); i++) { |
| 1905 | Fragment f = mAdded.get(i); |
| 1906 | if (f != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1907 | f.performConfigurationChanged(newConfig); |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | public void dispatchLowMemory() { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1914 | if (mAdded != null) { |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1915 | for (int i=0; i<mAdded.size(); i++) { |
| 1916 | Fragment f = mAdded.get(i); |
| 1917 | if (f != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1918 | f.performLowMemory(); |
Dianne Hackborn | 9d07180 | 2010-12-08 14:49:15 -0800 | [diff] [blame] | 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1924 | public void dispatchTrimMemory(int level) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1925 | if (mAdded != null) { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1926 | for (int i=0; i<mAdded.size(); i++) { |
| 1927 | Fragment f = mAdded.get(i); |
| 1928 | if (f != null) { |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1929 | f.performTrimMemory(level); |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1935 | public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 1936 | boolean show = false; |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1937 | ArrayList<Fragment> newMenus = null; |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1938 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1939 | for (int i=0; i<mAdded.size(); i++) { |
| 1940 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1941 | if (f != null) { |
| 1942 | if (f.performCreateOptionsMenu(menu, inflater)) { |
| 1943 | show = true; |
| 1944 | if (newMenus == null) { |
| 1945 | newMenus = new ArrayList<Fragment>(); |
| 1946 | } |
| 1947 | newMenus.add(f); |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1948 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1949 | } |
| 1950 | } |
| 1951 | } |
Dianne Hackborn | 8eb2e24 | 2010-11-01 12:31:24 -0700 | [diff] [blame] | 1952 | |
| 1953 | if (mCreatedMenus != null) { |
| 1954 | for (int i=0; i<mCreatedMenus.size(); i++) { |
| 1955 | Fragment f = mCreatedMenus.get(i); |
| 1956 | if (newMenus == null || !newMenus.contains(f)) { |
| 1957 | f.onDestroyOptionsMenu(); |
| 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | mCreatedMenus = newMenus; |
| 1963 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1964 | return show; |
| 1965 | } |
| 1966 | |
| 1967 | public boolean dispatchPrepareOptionsMenu(Menu menu) { |
| 1968 | boolean show = false; |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1969 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1970 | for (int i=0; i<mAdded.size(); i++) { |
| 1971 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1972 | if (f != null) { |
| 1973 | if (f.performPrepareOptionsMenu(menu)) { |
| 1974 | show = true; |
| 1975 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | return show; |
| 1980 | } |
| 1981 | |
| 1982 | public boolean dispatchOptionsItemSelected(MenuItem item) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1983 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1984 | for (int i=0; i<mAdded.size(); i++) { |
| 1985 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 1986 | if (f != null) { |
| 1987 | if (f.performOptionsItemSelected(item)) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 1988 | return true; |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | } |
| 1993 | return false; |
| 1994 | } |
| 1995 | |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1996 | public boolean dispatchContextItemSelected(MenuItem item) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 1997 | if (mAdded != null) { |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 1998 | for (int i=0; i<mAdded.size(); i++) { |
| 1999 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2000 | if (f != null) { |
| 2001 | if (f.performContextItemSelected(item)) { |
Dianne Hackborn | 5ddd127 | 2010-06-12 10:15:28 -0700 | [diff] [blame] | 2002 | return true; |
| 2003 | } |
| 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2010 | public void dispatchOptionsMenuClosed(Menu menu) { |
Dianne Hackborn | acdfbcc | 2012-06-19 15:07:05 -0700 | [diff] [blame] | 2011 | if (mAdded != null) { |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2012 | for (int i=0; i<mAdded.size(); i++) { |
| 2013 | Fragment f = mAdded.get(i); |
Dianne Hackborn | 62bea2f | 2012-09-04 18:48:15 -0700 | [diff] [blame] | 2014 | if (f != null) { |
| 2015 | f.performOptionsMenuClosed(menu); |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | } |
Adam Powell | f0f5fff | 2011-08-01 13:42:50 -0700 | [diff] [blame] | 2020 | |
| 2021 | @Override |
| 2022 | public void invalidateOptionsMenu() { |
| 2023 | if (mActivity != null && mCurState == Fragment.RESUMED) { |
| 2024 | mActivity.invalidateOptionsMenu(); |
| 2025 | } else { |
| 2026 | mNeedMenuInvalidate = true; |
| 2027 | } |
| 2028 | } |
| 2029 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2030 | public static int reverseTransit(int transit) { |
| 2031 | int rev = 0; |
| 2032 | switch (transit) { |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2033 | case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: |
| 2034 | rev = FragmentTransaction.TRANSIT_FRAGMENT_CLOSE; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2035 | break; |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2036 | case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: |
| 2037 | rev = FragmentTransaction.TRANSIT_FRAGMENT_OPEN; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2038 | break; |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 2039 | case FragmentTransaction.TRANSIT_FRAGMENT_FADE: |
| 2040 | rev = FragmentTransaction.TRANSIT_FRAGMENT_FADE; |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 2041 | break; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2042 | } |
| 2043 | return rev; |
| 2044 | |
| 2045 | } |
| 2046 | |
| 2047 | public static int transitToStyleIndex(int transit, boolean enter) { |
| 2048 | int animAttr = -1; |
| 2049 | switch (transit) { |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2050 | case FragmentTransaction.TRANSIT_FRAGMENT_OPEN: |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2051 | animAttr = enter |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2052 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentOpenEnterAnimation |
| 2053 | : com.android.internal.R.styleable.FragmentAnimation_fragmentOpenExitAnimation; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2054 | break; |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2055 | case FragmentTransaction.TRANSIT_FRAGMENT_CLOSE: |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2056 | animAttr = enter |
Chet Haase | 811ed106 | 2010-08-06 10:38:15 -0700 | [diff] [blame] | 2057 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentCloseEnterAnimation |
| 2058 | : com.android.internal.R.styleable.FragmentAnimation_fragmentCloseExitAnimation; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2059 | break; |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 2060 | case FragmentTransaction.TRANSIT_FRAGMENT_FADE: |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 2061 | animAttr = enter |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 2062 | ? com.android.internal.R.styleable.FragmentAnimation_fragmentFadeEnterAnimation |
| 2063 | : com.android.internal.R.styleable.FragmentAnimation_fragmentFadeExitAnimation; |
Chet Haase | 9ff82bf | 2010-10-05 14:30:51 -0700 | [diff] [blame] | 2064 | break; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2065 | } |
| 2066 | return animAttr; |
| 2067 | } |
Adam Powell | 371a809 | 2014-06-20 12:51:12 -0700 | [diff] [blame] | 2068 | |
| 2069 | @Override |
| 2070 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
| 2071 | if (!"fragment".equals(name)) { |
| 2072 | return null; |
| 2073 | } |
| 2074 | |
| 2075 | String fname = attrs.getAttributeValue(null, "class"); |
| 2076 | TypedArray a = |
| 2077 | context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment); |
| 2078 | if (fname == null) { |
| 2079 | fname = a.getString(com.android.internal.R.styleable.Fragment_name); |
| 2080 | } |
| 2081 | int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID); |
| 2082 | String tag = a.getString(com.android.internal.R.styleable.Fragment_tag); |
| 2083 | a.recycle(); |
| 2084 | |
| 2085 | int containerId = parent != null ? parent.getId() : 0; |
| 2086 | if (containerId == View.NO_ID && id == View.NO_ID && tag == null) { |
| 2087 | throw new IllegalArgumentException(attrs.getPositionDescription() |
| 2088 | + ": Must specify unique android:id, android:tag, or have a parent with" |
| 2089 | + " an id for " + fname); |
| 2090 | } |
| 2091 | |
| 2092 | // If we restored from a previous state, we may already have |
| 2093 | // instantiated this fragment from the state and should use |
| 2094 | // that instance instead of making a new one. |
| 2095 | Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null; |
| 2096 | if (fragment == null && tag != null) { |
| 2097 | fragment = findFragmentByTag(tag); |
| 2098 | } |
| 2099 | if (fragment == null && containerId != View.NO_ID) { |
| 2100 | fragment = findFragmentById(containerId); |
| 2101 | } |
| 2102 | |
| 2103 | if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x" |
| 2104 | + Integer.toHexString(id) + " fname=" + fname |
| 2105 | + " existing=" + fragment); |
| 2106 | if (fragment == null) { |
| 2107 | fragment = Fragment.instantiate(context, fname); |
| 2108 | fragment.mFromLayout = true; |
| 2109 | fragment.mFragmentId = id != 0 ? id : containerId; |
| 2110 | fragment.mContainerId = containerId; |
| 2111 | fragment.mTag = tag; |
| 2112 | fragment.mInLayout = true; |
| 2113 | fragment.mFragmentManager = this; |
| 2114 | fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState); |
| 2115 | addFragment(fragment, true); |
| 2116 | } else if (fragment.mInLayout) { |
| 2117 | // A fragment already exists and it is not one we restored from |
| 2118 | // previous state. |
| 2119 | throw new IllegalArgumentException(attrs.getPositionDescription() |
| 2120 | + ": Duplicate id 0x" + Integer.toHexString(id) |
| 2121 | + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId) |
| 2122 | + " with another fragment for " + fname); |
| 2123 | } else { |
| 2124 | // This fragment was retained from a previous instance; get it |
| 2125 | // going now. |
| 2126 | fragment.mInLayout = true; |
| 2127 | // If this fragment is newly instantiated (either right now, or |
| 2128 | // from last saved state), then give it the attributes to |
| 2129 | // initialize itself. |
| 2130 | if (!fragment.mRetaining) { |
| 2131 | fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState); |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | // If we haven't finished entering the CREATED state ourselves yet, |
| 2136 | // push the inflated child fragment along. |
| 2137 | if (mCurState < Fragment.CREATED && fragment.mFromLayout) { |
| 2138 | moveToState(fragment, Fragment.CREATED, 0, 0, false); |
| 2139 | } else { |
| 2140 | moveToState(fragment); |
| 2141 | } |
| 2142 | |
| 2143 | if (fragment.mView == null) { |
| 2144 | throw new IllegalStateException("Fragment " + fname |
| 2145 | + " did not create a view."); |
| 2146 | } |
| 2147 | if (id != 0) { |
| 2148 | fragment.mView.setId(id); |
| 2149 | } |
| 2150 | if (fragment.mView.getTag() == null) { |
| 2151 | fragment.mView.setTag(tag); |
| 2152 | } |
| 2153 | return fragment.mView; |
| 2154 | } |
| 2155 | |
| 2156 | @Override |
| 2157 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 2158 | return null; |
| 2159 | } |
| 2160 | |
| 2161 | LayoutInflater.Factory2 getLayoutInflaterFactory() { |
| 2162 | return this; |
| 2163 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 2164 | } |