Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.content.Loader; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 21 | import android.util.DebugUtils; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 22 | import android.util.Log; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 23 | import android.util.SparseArray; |
| 24 | |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 25 | import java.io.FileDescriptor; |
| 26 | import java.io.PrintWriter; |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 27 | import java.lang.reflect.Modifier; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 28 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 29 | /** |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 30 | * Interface associated with an {@link Activity} or {@link Fragment} for managing |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 31 | * one or more {@link android.content.Loader} instances associated with it. This |
| 32 | * helps an application manage longer-running operations in conjunction with the |
| 33 | * Activity or Fragment lifecycle; the most common use of this is with a |
| 34 | * {@link android.content.CursorLoader}, however applications are free to write |
| 35 | * their own loaders for loading other types of data. |
| 36 | * |
| 37 | * <p>As an example, here is the full implementation of a {@link Fragment} |
| 38 | * that displays a {@link android.widget.ListView} containing the results of |
| 39 | * a query against the contacts content provider. It uses a |
| 40 | * {@link android.content.CursorLoader} to manage the query on the provider. |
| 41 | * |
| 42 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentListCursorLoader.java |
| 43 | * fragment_cursor} |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 44 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 45 | public abstract class LoaderManager { |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Callback interface for a client to interact with the manager. |
| 48 | */ |
| 49 | public interface LoaderCallbacks<D> { |
| 50 | /** |
| 51 | * Instantiate and return a new Loader for the given ID. |
| 52 | * |
| 53 | * @param id The ID whose loader is to be created. |
| 54 | * @param args Any arguments supplied by the caller. |
| 55 | * @return Return a new Loader instance that is ready to start loading. |
| 56 | */ |
| 57 | public Loader<D> onCreateLoader(int id, Bundle args); |
| 58 | |
| 59 | /** |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 60 | * Called when a previously created loader has finished its load. Note |
| 61 | * that normally an application is <em>not</em> allowed to commit fragment |
| 62 | * transactions while in this call, since it can happen after an |
| 63 | * activity's state is saved. See {@link FragmentManager#openTransaction() |
| 64 | * FragmentManager.openTransaction()} for further discussion on this. |
| 65 | * |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 66 | * <p>This function is guaranteed to be called prior to the release of |
| 67 | * the last data that was supplied for this Loader. At this point |
| 68 | * you should remove all use of the old data (since it will be released |
| 69 | * soon), but should not do your own release of the data since its Loader |
| 70 | * owns it and will take care of that. The Loader will take care of |
| 71 | * management of its data so you don't have to. In particular: |
| 72 | * |
| 73 | * <ul> |
| 74 | * <li> <p>The Loader will monitor for changes to the data, and report |
| 75 | * them to you through new calls here. You should not monitor the |
| 76 | * data yourself. For example, if the data is a {@link android.database.Cursor} |
| 77 | * and you place it in a {@link android.widget.CursorAdapter}, use |
| 78 | * the {@link android.widget.CursorAdapter#CursorAdapter(android.content.Context, |
| 79 | * android.database.Cursor, int)} constructor <em>without</em> passing |
| 80 | * in either {@link android.widget.CursorAdapter#FLAG_AUTO_REQUERY} |
| 81 | * or {@link android.widget.CursorAdapter#FLAG_REGISTER_CONTENT_OBSERVER} |
| 82 | * (that is, use 0 for the flags argument). This prevents the CursorAdapter |
| 83 | * from doing its own observing of the Cursor, which is not needed since |
| 84 | * when a change happens you will get a new Cursor throw another call |
| 85 | * here. |
| 86 | * <li> The Loader will release the data once it knows the application |
| 87 | * is no longer using it. For example, if the data is |
| 88 | * a {@link android.database.Cursor} from a {@link android.content.CursorLoader}, |
| 89 | * you should not call close() on it yourself. If the Cursor is being placed in a |
| 90 | * {@link android.widget.CursorAdapter}, you should use the |
| 91 | * {@link android.widget.CursorAdapter#swapCursor(android.database.Cursor)} |
| 92 | * method so that the old Cursor is not closed. |
| 93 | * </ul> |
| 94 | * |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 95 | * @param loader The Loader that has finished. |
| 96 | * @param data The data generated by the Loader. |
| 97 | */ |
| 98 | public void onLoadFinished(Loader<D> loader, D data); |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Called when a previously created loader is being reset, and thus |
| 102 | * making its data unavailable. The application should at this point |
| 103 | * remove any references it has to the Loader's data. |
| 104 | * |
| 105 | * @param loader The Loader that is being reset. |
| 106 | */ |
| 107 | public void onLoaderReset(Loader<D> loader); |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Ensures a loader is initialized and active. If the loader doesn't |
| 112 | * already exist, one is created and (if the activity/fragment is currently |
| 113 | * started) starts the loader. Otherwise the last created |
| 114 | * loader is re-used. |
| 115 | * |
| 116 | * <p>In either case, the given callback is associated with the loader, and |
| 117 | * will be called as the loader state changes. If at the point of call |
| 118 | * the caller is in its started state, and the requested loader |
| 119 | * already exists and has generated its data, then |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 120 | * callback {@link LoaderCallbacks#onLoadFinished} will |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 121 | * be called immediately (inside of this function), so you must be prepared |
| 122 | * for this to happen. |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 123 | * |
| 124 | * @param id A unique identifier for this loader. Can be whatever you want. |
| 125 | * Identifiers are scoped to a particular LoaderManager instance. |
| 126 | * @param args Optional arguments to supply to the loader at construction. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 127 | * If a loader already exists (a new one does not need to be created), this |
| 128 | * parameter will be ignored and the last arguments continue to be used. |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 129 | * @param callback Interface the LoaderManager will call to report about |
| 130 | * changes in the state of the loader. Required. |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 131 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 132 | public abstract <D> Loader<D> initLoader(int id, Bundle args, |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 133 | LoaderManager.LoaderCallbacks<D> callback); |
| 134 | |
| 135 | /** |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 136 | * Starts a new or restarts an existing {@link android.content.Loader} in |
| 137 | * this manager, registers the callbacks to it, |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 138 | * and (if the activity/fragment is currently started) starts loading it. |
| 139 | * If a loader with the same id has previously been |
| 140 | * started it will automatically be destroyed when the new loader completes |
| 141 | * its work. The callback will be delivered before the old loader |
| 142 | * is destroyed. |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 143 | * |
| 144 | * @param id A unique identifier for this loader. Can be whatever you want. |
| 145 | * Identifiers are scoped to a particular LoaderManager instance. |
| 146 | * @param args Optional arguments to supply to the loader at construction. |
| 147 | * @param callback Interface the LoaderManager will call to report about |
| 148 | * changes in the state of the loader. Required. |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 149 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 150 | public abstract <D> Loader<D> restartLoader(int id, Bundle args, |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 151 | LoaderManager.LoaderCallbacks<D> callback); |
| 152 | |
| 153 | /** |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame] | 154 | * Stops and removes the loader with the given ID. If this loader |
| 155 | * had previously reported data to the client through |
| 156 | * {@link LoaderCallbacks#onLoadFinished(Loader, Object)}, a call |
| 157 | * will be made to {@link LoaderCallbacks#onLoaderReset(Loader)}. |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 158 | */ |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 159 | public abstract void destroyLoader(int id); |
| 160 | |
| 161 | /** |
| 162 | * @deprecated Renamed to {@link #destroyLoader}. |
| 163 | */ |
| 164 | @Deprecated |
| 165 | public void stopLoader(int id) { |
| 166 | destroyLoader(id); |
| 167 | } |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Return the Loader with the given id or null if no matching Loader |
| 171 | * is found. |
| 172 | */ |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 173 | public abstract <D> Loader<D> getLoader(int id); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Print the LoaderManager's state into the given stream. |
| 177 | * |
| 178 | * @param prefix Text to print at the front of each line. |
| 179 | * @param fd The raw file descriptor that the dump is being sent to. |
| 180 | * @param writer A PrintWriter to which the dump is to be set. |
| 181 | * @param args Additional arguments to the dump request. |
| 182 | */ |
| 183 | public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Control whether the framework's internal loader manager debugging |
| 187 | * logs are turned on. If enabled, you will see output in logcat as |
| 188 | * the framework performs loader operations. |
| 189 | */ |
| 190 | public static void enableDebugLogging(boolean enabled) { |
| 191 | LoaderManagerImpl.DEBUG = enabled; |
| 192 | } |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 195 | class LoaderManagerImpl extends LoaderManager { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 196 | static final String TAG = "LoaderManager"; |
| 197 | static boolean DEBUG = true; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 198 | |
| 199 | // These are the currently active loaders. A loader is here |
| 200 | // from the time its load is started until it has been explicitly |
| 201 | // stopped or restarted by the application. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 202 | final SparseArray<LoaderInfo> mLoaders = new SparseArray<LoaderInfo>(); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 203 | |
| 204 | // These are previously run loaders. This list is maintained internally |
| 205 | // to avoid destroying a loader while an application is still using it. |
| 206 | // It allows an application to restart a loader, but continue using its |
| 207 | // previously run loader until the new loader's data is available. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 208 | final SparseArray<LoaderInfo> mInactiveLoaders = new SparseArray<LoaderInfo>(); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 209 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 210 | Activity mActivity; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 211 | boolean mStarted; |
| 212 | boolean mRetaining; |
| 213 | boolean mRetainingStarted; |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 214 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 215 | final class LoaderInfo implements Loader.OnLoadCompleteListener<Object> { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 216 | final int mId; |
| 217 | final Bundle mArgs; |
| 218 | LoaderManager.LoaderCallbacks<Object> mCallbacks; |
| 219 | Loader<Object> mLoader; |
| 220 | Object mData; |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 221 | Object mDeliveredData; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 222 | boolean mStarted; |
| 223 | boolean mRetaining; |
| 224 | boolean mRetainingStarted; |
| 225 | boolean mDestroyed; |
| 226 | boolean mListenerRegistered; |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 227 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 228 | public LoaderInfo(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callbacks) { |
| 229 | mId = id; |
| 230 | mArgs = args; |
| 231 | mCallbacks = callbacks; |
| 232 | } |
| 233 | |
| 234 | void start() { |
| 235 | if (mRetaining && mRetainingStarted) { |
| 236 | // Our owner is started, but we were being retained from a |
| 237 | // previous instance in the started state... so there is really |
| 238 | // nothing to do here, since the loaders are still started. |
| 239 | mStarted = true; |
| 240 | return; |
| 241 | } |
| 242 | |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 243 | if (mStarted) { |
| 244 | // If loader already started, don't restart. |
| 245 | return; |
| 246 | } |
| 247 | |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 248 | mStarted = true; |
| 249 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 250 | if (DEBUG) Log.v(TAG, " Starting: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 251 | if (mLoader == null && mCallbacks != null) { |
| 252 | mLoader = mCallbacks.onCreateLoader(mId, mArgs); |
| 253 | } |
| 254 | if (mLoader != null) { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 255 | if (mLoader.getClass().isMemberClass() |
| 256 | && !Modifier.isStatic(mLoader.getClass().getModifiers())) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 257 | throw new IllegalArgumentException( |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 258 | "Object returned from onCreateLoader must not be a non-static inner member class: " |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 259 | + mLoader); |
| 260 | } |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 261 | if (!mListenerRegistered) { |
| 262 | mLoader.registerListener(mId, this); |
| 263 | mListenerRegistered = true; |
| 264 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 265 | mLoader.startLoading(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | void retain() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 270 | if (DEBUG) Log.v(TAG, " Retaining: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 271 | mRetaining = true; |
| 272 | mRetainingStarted = mStarted; |
| 273 | mStarted = false; |
| 274 | mCallbacks = null; |
| 275 | } |
| 276 | |
| 277 | void finishRetain() { |
| 278 | if (mRetaining) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 279 | if (DEBUG) Log.v(TAG, " Finished Retaining: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 280 | mRetaining = false; |
| 281 | if (mStarted != mRetainingStarted) { |
| 282 | if (!mStarted) { |
| 283 | // This loader was retained in a started state, but |
| 284 | // at the end of retaining everything our owner is |
| 285 | // no longer started... so make it stop. |
| 286 | stop(); |
| 287 | } |
| 288 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (mStarted && mData != null) { |
| 292 | // This loader has retained its data, either completely across |
| 293 | // a configuration change or just whatever the last data set |
| 294 | // was after being restarted from a stop, and now at the point of |
| 295 | // finishing the retain we find we remain started, have |
| 296 | // our data, and the owner has a new callback... so |
| 297 | // let's deliver the data now. |
| 298 | callOnLoadFinished(mLoader, mData); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | void stop() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 303 | if (DEBUG) Log.v(TAG, " Stopping: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 304 | mStarted = false; |
Dianne Hackborn | debb2e2 | 2010-08-09 16:32:52 -0700 | [diff] [blame] | 305 | if (!mRetaining) { |
| 306 | if (mLoader != null && mListenerRegistered) { |
| 307 | // Let the loader know we're done with it |
| 308 | mListenerRegistered = false; |
| 309 | mLoader.unregisterListener(this); |
| 310 | mLoader.stopLoading(); |
| 311 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | void destroy() { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 316 | if (DEBUG) Log.v(TAG, " Destroying: " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 317 | mDestroyed = true; |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 318 | boolean needReset = mDeliveredData != null; |
| 319 | mDeliveredData = null; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 320 | if (mCallbacks != null && mLoader != null && mData != null && needReset) { |
| 321 | if (DEBUG) Log.v(TAG, " Reseting: " + this); |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 322 | String lastBecause = null; |
| 323 | if (mActivity != null) { |
| 324 | lastBecause = mActivity.mFragments.mNoTransactionsBecause; |
| 325 | mActivity.mFragments.mNoTransactionsBecause = "onLoaderReset"; |
| 326 | } |
| 327 | try { |
| 328 | mCallbacks.onLoaderReset(mLoader); |
| 329 | } finally { |
| 330 | if (mActivity != null) { |
| 331 | mActivity.mFragments.mNoTransactionsBecause = lastBecause; |
| 332 | } |
| 333 | } |
| 334 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 335 | mCallbacks = null; |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 336 | mData = null; |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 337 | if (mLoader != null) { |
| 338 | if (mListenerRegistered) { |
| 339 | mListenerRegistered = false; |
| 340 | mLoader.unregisterListener(this); |
| 341 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 342 | mLoader.reset(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 345 | |
| 346 | @Override public void onLoadComplete(Loader<Object> loader, Object data) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 347 | if (DEBUG) Log.v(TAG, "onLoadComplete: " + this); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 348 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 349 | if (mDestroyed) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 350 | if (DEBUG) Log.v(TAG, " Ignoring load complete -- destroyed"); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 354 | // Notify of the new data so the app can switch out the old data before |
| 355 | // we try to destroy it. |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 356 | if (mData != data) { |
| 357 | mData = data; |
| 358 | if (mStarted) { |
| 359 | callOnLoadFinished(loader, data); |
| 360 | } |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 361 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 362 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 363 | //if (DEBUG) Log.v(TAG, " onLoadFinished returned: " + this); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 364 | |
| 365 | // We have now given the application the new loader with its |
| 366 | // loaded data, so it should have stopped using the previous |
| 367 | // loader. If there is a previous loader on the inactive list, |
| 368 | // clean it up. |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 369 | LoaderInfo info = mInactiveLoaders.get(mId); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 370 | if (info != null && info != this) { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 371 | info.mDeliveredData = null; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 372 | info.destroy(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 373 | mInactiveLoaders.remove(mId); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 376 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 377 | void callOnLoadFinished(Loader<Object> loader, Object data) { |
| 378 | if (mCallbacks != null) { |
| 379 | String lastBecause = null; |
| 380 | if (mActivity != null) { |
| 381 | lastBecause = mActivity.mFragments.mNoTransactionsBecause; |
| 382 | mActivity.mFragments.mNoTransactionsBecause = "onLoadFinished"; |
| 383 | } |
| 384 | try { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 385 | if (DEBUG) Log.v(TAG, " onLoadFinished in " + loader + ": " |
| 386 | + loader.dataToString(data)); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 387 | mCallbacks.onLoadFinished(loader, data); |
| 388 | } finally { |
| 389 | if (mActivity != null) { |
| 390 | mActivity.mFragments.mNoTransactionsBecause = lastBecause; |
| 391 | } |
| 392 | } |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 393 | mDeliveredData = data; |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 397 | @Override |
| 398 | public String toString() { |
| 399 | StringBuilder sb = new StringBuilder(64); |
| 400 | sb.append("LoaderInfo{"); |
| 401 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 402 | sb.append(" #"); |
| 403 | sb.append(mId); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 404 | sb.append(" : "); |
| 405 | DebugUtils.buildShortClassTag(mLoader, sb); |
| 406 | sb.append("}}"); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 407 | return sb.toString(); |
| 408 | } |
| 409 | |
| 410 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 411 | writer.print(prefix); writer.print("mId="); writer.print(mId); |
| 412 | writer.print(" mArgs="); writer.println(mArgs); |
| 413 | writer.print(prefix); writer.print("mCallbacks="); writer.println(mCallbacks); |
| 414 | writer.print(prefix); writer.print("mLoader="); writer.println(mLoader); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 415 | if (mLoader != null) { |
| 416 | mLoader.dump(prefix + " ", fd, writer, args); |
| 417 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 418 | writer.print(prefix); writer.print("mData="); writer.println(mData); |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 419 | writer.print(prefix); writer.print("mDeliveredData="); writer.println(mDeliveredData); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 420 | writer.print(prefix); writer.print("mStarted="); writer.print(mStarted); |
| 421 | writer.print(" mRetaining="); writer.print(mRetaining); |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame] | 422 | writer.print(" mDestroyed="); writer.println(mDestroyed); |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 423 | writer.print(prefix); writer.print("mListenerRegistered="); |
| 424 | writer.println(mListenerRegistered); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 425 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 426 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 427 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 428 | LoaderManagerImpl(Activity activity, boolean started) { |
| 429 | mActivity = activity; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 430 | mStarted = started; |
| 431 | } |
| 432 | |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 433 | void updateActivity(Activity activity) { |
| 434 | mActivity = activity; |
| 435 | } |
| 436 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 437 | private LoaderInfo createLoader(int id, Bundle args, |
| 438 | LoaderManager.LoaderCallbacks<Object> callback) { |
| 439 | LoaderInfo info = new LoaderInfo(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
| 440 | mLoaders.put(id, info); |
| 441 | Loader<Object> loader = callback.onCreateLoader(id, args); |
| 442 | info.mLoader = (Loader<Object>)loader; |
| 443 | if (mStarted) { |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 444 | // The activity will start all existing loaders in it's onStart(), |
| 445 | // so only start them here if we're past that point of the activitiy's |
| 446 | // life cycle |
| 447 | info.start(); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 448 | } |
| 449 | return info; |
| 450 | } |
| 451 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 452 | @SuppressWarnings("unchecked") |
| 453 | public <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) { |
| 454 | LoaderInfo info = mLoaders.get(id); |
| 455 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 456 | if (DEBUG) Log.v(TAG, "initLoader in " + this + ": args=" + args); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 457 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 458 | if (info == null) { |
| 459 | // Loader doesn't already exist; create. |
| 460 | info = createLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 461 | if (DEBUG) Log.v(TAG, " Created new loader " + info); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 462 | } else { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 463 | if (DEBUG) Log.v(TAG, " Re-using existing loader " + info); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 464 | info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback; |
| 465 | } |
| 466 | |
| 467 | if (info.mData != null && mStarted) { |
| 468 | // If the loader has already generated its data, report it now. |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 469 | info.callOnLoadFinished(info.mLoader, info.mData); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return (Loader<D>)info.mLoader; |
| 473 | } |
| 474 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 475 | @SuppressWarnings("unchecked") |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 476 | public <D> Loader<D> restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) { |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 477 | LoaderInfo info = mLoaders.get(id); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 478 | if (DEBUG) Log.v(TAG, "restartLoader in " + this + ": args=" + args); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 479 | if (info != null) { |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 480 | LoaderInfo inactive = mInactiveLoaders.get(id); |
| 481 | if (inactive != null) { |
| 482 | if (info.mData != null) { |
| 483 | // This loader now has data... we are probably being |
| 484 | // called from within onLoadComplete, where we haven't |
| 485 | // yet destroyed the last inactive loader. So just do |
| 486 | // that now. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 487 | if (DEBUG) Log.v(TAG, " Removing last inactive loader: " + info); |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame^] | 488 | inactive.mDeliveredData = null; |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 489 | inactive.destroy(); |
| 490 | mInactiveLoaders.put(id, info); |
| 491 | } else { |
| 492 | // We already have an inactive loader for this ID that we are |
| 493 | // waiting for! Now we have three active loaders... let's just |
| 494 | // drop the one in the middle, since we are still waiting for |
| 495 | // its result but that result is already out of date. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 496 | if (DEBUG) Log.v(TAG, " Removing intermediate loader: " + info); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 497 | info.destroy(); |
| 498 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 499 | } else { |
| 500 | // Keep track of the previous instance of this loader so we can destroy |
| 501 | // it when the new one completes. |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 502 | if (DEBUG) Log.v(TAG, " Making last loader inactive: " + info); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 503 | mInactiveLoaders.put(id, info); |
| 504 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 505 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 506 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 507 | info = createLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); |
| 508 | return (Loader<D>)info.mLoader; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 511 | public void destroyLoader(int id) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 512 | if (DEBUG) Log.v(TAG, "destroyLoader in " + this + " of " + id); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 513 | int idx = mLoaders.indexOfKey(id); |
| 514 | if (idx >= 0) { |
| 515 | LoaderInfo info = mLoaders.valueAt(idx); |
| 516 | mLoaders.removeAt(idx); |
Dianne Hackborn | 4911b78 | 2010-07-15 12:54:39 -0700 | [diff] [blame] | 517 | info.destroy(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 518 | } |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame] | 519 | idx = mInactiveLoaders.indexOfKey(id); |
| 520 | if (idx >= 0) { |
| 521 | LoaderInfo info = mInactiveLoaders.valueAt(idx); |
| 522 | mInactiveLoaders.removeAt(idx); |
| 523 | info.destroy(); |
| 524 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 527 | @SuppressWarnings("unchecked") |
| 528 | public <D> Loader<D> getLoader(int id) { |
| 529 | LoaderInfo loaderInfo = mLoaders.get(id); |
| 530 | if (loaderInfo != null) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 531 | return (Loader<D>)mLoaders.get(id).mLoader; |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 532 | } |
| 533 | return null; |
| 534 | } |
| 535 | |
| 536 | void doStart() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 537 | if (DEBUG) Log.v(TAG, "Starting in " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 538 | if (mStarted) { |
| 539 | RuntimeException e = new RuntimeException("here"); |
| 540 | e.fillInStackTrace(); |
| 541 | Log.w(TAG, "Called doStart when already started: " + this, e); |
| 542 | return; |
| 543 | } |
| 544 | |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 545 | mStarted = true; |
| 546 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 547 | // Call out to sub classes so they can start their loaders |
| 548 | // Let the existing loaders know that we want to be notified when a load is complete |
| 549 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 550 | mLoaders.valueAt(i).start(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 551 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void doStop() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 555 | if (DEBUG) Log.v(TAG, "Stopping in " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 556 | if (!mStarted) { |
| 557 | RuntimeException e = new RuntimeException("here"); |
| 558 | e.fillInStackTrace(); |
| 559 | Log.w(TAG, "Called doStop when not started: " + this, e); |
| 560 | return; |
| 561 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 562 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 563 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 564 | mLoaders.valueAt(i).stop(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 565 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 566 | mStarted = false; |
| 567 | } |
| 568 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 569 | void doRetain() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 570 | if (DEBUG) Log.v(TAG, "Retaining in " + this); |
Dianne Hackborn | fb3cffe | 2010-10-25 17:08:56 -0700 | [diff] [blame] | 571 | if (!mStarted) { |
| 572 | RuntimeException e = new RuntimeException("here"); |
| 573 | e.fillInStackTrace(); |
| 574 | Log.w(TAG, "Called doRetain when not started: " + this, e); |
| 575 | return; |
| 576 | } |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 577 | |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 578 | mRetaining = true; |
| 579 | mStarted = false; |
| 580 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 581 | mLoaders.valueAt(i).retain(); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | void finishRetain() { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 586 | if (mRetaining) { |
| 587 | if (DEBUG) Log.v(TAG, "Finished Retaining in " + this); |
Dianne Hackborn | 5e0d595 | 2010-08-05 13:45:35 -0700 | [diff] [blame] | 588 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 589 | mRetaining = false; |
| 590 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
| 591 | mLoaders.valueAt(i).finishRetain(); |
| 592 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 596 | void doDestroy() { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 597 | if (!mRetaining) { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 598 | if (DEBUG) Log.v(TAG, "Destroying Active in " + this); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 599 | for (int i = mLoaders.size()-1; i >= 0; i--) { |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 600 | mLoaders.valueAt(i).destroy(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 603 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 604 | if (DEBUG) Log.v(TAG, "Destroying Inactive in " + this); |
Dianne Hackborn | 2707d60 | 2010-07-09 18:01:20 -0700 | [diff] [blame] | 605 | for (int i = mInactiveLoaders.size()-1; i >= 0; i--) { |
| 606 | mInactiveLoaders.valueAt(i).destroy(); |
| 607 | } |
| 608 | mInactiveLoaders.clear(); |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 609 | } |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 610 | |
| 611 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 612 | public String toString() { |
| 613 | StringBuilder sb = new StringBuilder(128); |
| 614 | sb.append("LoaderManager{"); |
| 615 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 616 | sb.append(" in "); |
| 617 | DebugUtils.buildShortClassTag(mActivity, sb); |
| 618 | sb.append("}}"); |
| 619 | return sb.toString(); |
| 620 | } |
| 621 | |
| 622 | @Override |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 623 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 624 | if (mLoaders.size() > 0) { |
| 625 | writer.print(prefix); writer.println("Active Loaders:"); |
| 626 | String innerPrefix = prefix + " "; |
| 627 | for (int i=0; i < mLoaders.size(); i++) { |
| 628 | LoaderInfo li = mLoaders.valueAt(i); |
| 629 | writer.print(prefix); writer.print(" #"); writer.print(mLoaders.keyAt(i)); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 630 | writer.print(": "); writer.println(li.toString()); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 631 | li.dump(innerPrefix, fd, writer, args); |
| 632 | } |
| 633 | } |
| 634 | if (mInactiveLoaders.size() > 0) { |
| 635 | writer.print(prefix); writer.println("Inactive Loaders:"); |
| 636 | String innerPrefix = prefix + " "; |
| 637 | for (int i=0; i < mInactiveLoaders.size(); i++) { |
| 638 | LoaderInfo li = mInactiveLoaders.valueAt(i); |
| 639 | writer.print(prefix); writer.print(" #"); writer.print(mInactiveLoaders.keyAt(i)); |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 640 | writer.print(": "); writer.println(li.toString()); |
Dianne Hackborn | 30d7189 | 2010-12-11 10:37:55 -0800 | [diff] [blame] | 641 | li.dump(innerPrefix, fd, writer, args); |
| 642 | } |
| 643 | } |
| 644 | } |
Dianne Hackborn | c801768 | 2010-07-06 13:34:38 -0700 | [diff] [blame] | 645 | } |