1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
| 和manager相关的客户端回调接口 */ public interface LoaderCallbacks<D> { public Loader<D> onCreateLoader(int id, Bundle args);
* FragmentManager.openTransaction()} for further discussion on this. 当之前创建的一个loader已经完成它的加载时调用。注意:正常情况下应用程序不会允许在这个方法提交fragment transactions,因为它有可能在activity状态保存之后发生 * At this point * you should remove all use of the old data (since it will be released * soon), but should not do your own release of the data since its Loader * owns it and will take care of that. The Loader will take care of * management of its data so you don't have to. *这个方法确保在这个Loader提供的最后的数据释放之前调用。基于这点你应该删除所有使用的旧数据(因为它会马上被释 放) 但是需要注意 特别的: Loader会监视数据改变,通过新的调用提交给你。你不应该自己监视数据。比如:如果数据是个Cursor而且把它放到 CursorAdapter中,使用CursorAdapter#CursorAdapter(android.content.Context, * android.database.Cursor, int)构造,没有传入CursorAdapter#FLAG_AUTO_REQUERY或 CursorAdapter#FLAG_REGISTER_CONTENT_OBSERVER(也就是这个参数传入的是0)。这会防止CursorAdapter监 视Cursor,这是不必要的因为当一个修改发生你会得到一个新的Cursor * <li> 一旦Loader知道应用程序不再使用数据它就会释放数据。比如,如果数据是CursorLoader的Cursor,你不能调用 close().如果Cursor在CursorLoader中被替换,你应该使用swapCursor(Cursor)来使old Cursor不关闭。 * </ul> */ public void onLoadFinished(Loader<D> loader, D data);
*在前一个创建的loader被重置调用,并且使它的数据不可用。应用程序应该在这时候删除它对Loader数据的所有引用 */ public void onLoaderReset(Loader<D> loader); }
如果loader已经不再存在,一个新的会被创建而且(如果activity/fragment当前已经开始)启动loader。否则最后创建的loader会被重用。 在任何情况下,给定的回调是和loader相关的,而且会在loader状态改变的时候调用。如果在调用这点上,调用者在开始状态,被requested的loader已经存在而且生成了数据,然后onLoadFinished会被立刻调用(而不是这个方法),所以必须注意这种情况。 */ public abstract <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback);
在这个manager里启动一个新的或重启一个已经存在的Loader,注册回调,并且(如果activity/fragment当前已经开始)开始启动。 如果之前有相同id的loader已经启动,它会在新的loader完成工作之后自动销毁。 The callback will be delivered before the old loader * is destroyed. */ public abstract <D> Loader<D> restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback);
停止并移除给定id的loader. 如果loader之前通过#onLoadFinished(Loader, Object)给客户端提交过数据,a call * will be made to {@link LoaderCallbacks#onLoaderReset(Loader)}. */ public abstract void destroyLoader(int id);
public abstract <D> Loader<D> getLoader(int id);
class LoaderManagerImpl extends LoaderManager { static final String TAG = "LoaderManager"; static boolean DEBUG = false;
final SparseArray<LoaderInfo> mLoaders = new SparseArray<LoaderInfo>(0);
final SparseArray<LoaderInfo> mInactiveLoaders = new SparseArray<LoaderInfo>(0);
final String mWho;
Activity mActivity; boolean mStarted; boolean mRetaining; boolean mRetainingStarted;
boolean mCreatingLoader;
final class LoaderInfo implements Loader.OnLoadCompleteListener<Object>, Loader.OnLoadCanceledListener<Object> { final int mId; final Bundle mArgs; LoaderManager.LoaderCallbacks<Object> mCallbacks; Loader<Object> mLoader; boolean mHaveData; boolean mDeliveredData; Object mData; boolean mStarted; boolean mRetaining; boolean mRetainingStarted; boolean mReportNextStart; boolean mDestroyed; boolean mListenerRegistered;
LoaderInfo mPendingLoader;
public LoaderInfo(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callbacks) { mId = id; mArgs = args; mCallbacks = callbacks; }
void start() { if (mRetaining && mRetainingStarted) { mStarted = true; return; }
if (mStarted) { return; }
mStarted = true;
if (mLoader == null && mCallbacks != null) { mLoader = mCallbacks.onCreateLoader(mId, mArgs); } if (mLoader != null) { if (mLoader.getClass().isMemberClass() && !Modifier.isStatic(mLoader.getClass().getModifiers())) { throw new IllegalArgumentException( "Object returned from onCreateLoader must not be a non-static inner member class: " + mLoader); } if (!mListenerRegistered) { mLoader.registerListener(mId, this); mLoader.registerOnLoadCanceledListener(this); mListenerRegistered = true; } mLoader.startLoading(); } }
void retain() { mRetaining = true; mRetainingStarted = mStarted; mStarted = false; mCallbacks = null; }
void finishRetain() { if (mRetaining) { mRetaining = false; if (mStarted != mRetainingStarted) { if (!mStarted) { stop(); } } }
if (mStarted && mHaveData && !mReportNextStart) { callOnLoadFinished(mLoader, mData); } }
void reportStart() { if (mStarted) { if (mReportNextStart) { mReportNextStart = false; if (mHaveData) { callOnLoadFinished(mLoader, mData); } } } }
void stop() { if (DEBUG) Log.v(TAG, " Stopping: " + this); mStarted = false; if (!mRetaining) { if (mLoader != null && mListenerRegistered) { mListenerRegistered = false; mLoader.unregisterListener(this); mLoader.unregisterOnLoadCanceledListener(this); mLoader.stopLoading(); } } }
void cancel() { if (DEBUG) Log.v(TAG, " Canceling: " + this); if (mStarted && mLoader != null && mListenerRegistered) { if (!mLoader.cancelLoad()) { onLoadCanceled(mLoader); } } }
void destroy() { if (DEBUG) Log.v(TAG, " Destroying: " + this); mDestroyed = true; boolean needReset = mDeliveredData; mDeliveredData = false; if (mCallbacks != null && mLoader != null && mHaveData && needReset) { if (DEBUG) Log.v(TAG, " Reseting: " + this); String lastBecause = null; if (mActivity != null) { lastBecause = mActivity.mFragments.mNoTransactionsBecause; mActivity.mFragments.mNoTransactionsBecause = "onLoaderReset"; } try { mCallbacks.onLoaderReset(mLoader); } finally { if (mActivity != null) { mActivity.mFragments.mNoTransactionsBecause = lastBecause; } } } mCallbacks = null; mData = null; mHaveData = false; if (mLoader != null) { if (mListenerRegistered) { mListenerRegistered = false; mLoader.unregisterListener(this); mLoader.unregisterOnLoadCanceledListener(this); } mLoader.reset(); } if (mPendingLoader != null) { mPendingLoader.destroy(); } }
@Override public void onLoadCanceled(Loader<Object> loader) { if (DEBUG) Log.v(TAG, "onLoadCanceled: " + this);
if (mDestroyed) { if (DEBUG) Log.v(TAG, " Ignoring load canceled -- destroyed"); return; }
if (mLoaders.get(mId) != this) { if (DEBUG) Log.v(TAG, " Ignoring load canceled -- not active"); return; }
LoaderInfo pending = mPendingLoader; if (pending != null) { if (DEBUG) Log.v(TAG, " Switching to pending loader: " + pending); mPendingLoader = null; mLoaders.put(mId, null); destroy(); installLoader(pending); } }
@Override public void onLoadComplete(Loader<Object> loader, Object data) { if (DEBUG) Log.v(TAG, "onLoadComplete: " + this);
if (mDestroyed) { if (DEBUG) Log.v(TAG, " Ignoring load complete -- destroyed"); return; }
if (mLoaders.get(mId) != this) { if (DEBUG) Log.v(TAG, " Ignoring load complete -- not active"); return; }
LoaderInfo pending = mPendingLoader; if (pending != null) { if (DEBUG) Log.v(TAG, " Switching to pending loader: " + pending); mPendingLoader = null; mLoaders.put(mId, null); destroy(); installLoader(pending); return; }
if (mData != data || !mHaveData) { mData = data; mHaveData = true; if (mStarted) { callOnLoadFinished(loader, data); } }
LoaderInfo info = mInactiveLoaders.get(mId); if (info != null && info != this) { info.mDeliveredData = false; info.destroy(); mInactiveLoaders.remove(mId); }
if (mActivity != null && !hasRunningLoaders()) { mActivity.mFragments.startPendingDeferredFragments(); } }
void callOnLoadFinished(Loader<Object> loader, Object data) { if (mCallbacks != null) { String lastBecause = null; if (mActivity != null) { lastBecause = mActivity.mFragments.mNoTransactionsBecause; mActivity.mFragments.mNoTransactionsBecause = "onLoadFinished"; } try { if (DEBUG) Log.v(TAG, " onLoadFinished in " + loader + ": " + loader.dataToString(data)); mCallbacks.onLoadFinished(loader, data); } finally { if (mActivity != null) { mActivity.mFragments.mNoTransactionsBecause = lastBecause; } } mDeliveredData = true; } } }
LoaderManagerImpl(String who, Activity activity, boolean started) { mWho = who; mActivity = activity; mStarted = started; }
void updateActivity(Activity activity) { mActivity = activity; }
private LoaderInfo createLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callback) { LoaderInfo info = new LoaderInfo(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); Loader<Object> loader = callback.onCreateLoader(id, args); info.mLoader = (Loader<Object>)loader; return info; }
private LoaderInfo createAndInstallLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<Object> callback) { try { mCreatingLoader = true; LoaderInfo info = createLoader(id, args, callback); installLoader(info); return info; } finally { mCreatingLoader = false; } }
void installLoader(LoaderInfo info) { mLoaders.put(info.mId, info); if (mStarted) { info.start(); } }
用一个特殊的ID初始化一个Loader,如果这个ID已经有Loader和它相关联,那么它保持不变而且任何之前的回调都被新的替换。如果没有和ID相关联的Loader,会产生一个新的并且启动。 这个方法通常在一个组件初始化的时候使用来确保它依赖的Loader被创建。这样如果已经有一个,会使它重用一个已经存在的Loader的数据。比如当一个Activity在configuration改变之后re-created,它就不必再re-create它的loaders. 注意:在这种情况下如果一个存在的Loader已经被使用,那么给定的args会被忽略,因为你会继续使用之前的Loader */ @SuppressWarnings("unchecked") public <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); }
LoaderInfo info = mLoaders.get(id);
if (info == null) { info = createAndInstallLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); } else { info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback; }
if (info.mHaveData && mStarted) { info.callOnLoadFinished(info.mLoader, info.mData); }
return (Loader<D>)info.mLoader; }
调用来重建有特殊ID的Loader.如果当前有一个和这个Id相关联的Loader,它会在适当的时候canceled/stopped/destroyed。一个新的带有给定参数的Loader会被创建,然后它的数据会在可用的时候传给你。 这个方法减少了一些Loaders的生成。如果太多的相同ID的Loaders被创建,但是没有生成他们的数据,那么这个方法会被重新调用而且返回一个新的Loader,但是直到之前的Loader全部完成才会启动。 调用这个方法之后,任何之前相同ID的Loaders都变无效的,而且不应该再接受他们的更新数据。 */ @SuppressWarnings("unchecked") public <D> Loader<D> restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); }
LoaderInfo info = mLoaders.get(id); if (info != null) { LoaderInfo inactive = mInactiveLoaders.get(id); if (inactive != null) { if (info.mHaveData) { inactive.mDeliveredData = false; inactive.destroy(); info.mLoader.abandon(); mInactiveLoaders.put(id, info); } else { if (!info.mStarted) { if (DEBUG) Log.v(TAG, " Current loader is stopped; replacing"); mLoaders.put(id, null); info.destroy(); } else { if (DEBUG) Log.v(TAG, " Current loader is running; attempting to cancel"); info.cancel(); if (info.mPendingLoader != null) { if (DEBUG) Log.v(TAG, " Removing pending loader: " + info.mPendingLoader); info.mPendingLoader.destroy(); info.mPendingLoader = null; } if (DEBUG) Log.v(TAG, " Enqueuing as new pending loader"); info.mPendingLoader = createLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); return (Loader<D>)info.mPendingLoader.mLoader; } } } else { if (DEBUG) Log.v(TAG, " Making last loader inactive: " + info); info.mLoader.abandon(); mInactiveLoaders.put(id, info); } }
info = createAndInstallLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback); return (Loader<D>)info.mLoader; }
销毁给定ID的loader,在这个方法返回之后,任何和这个ID相关联的Loader对象都会被销毁。任何和他们相关联的数据都会被销毁。当你这样做时,你最好不要使用它。 */ public void destroyLoader(int id) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); }
int idx = mLoaders.indexOfKey(id); if (idx >= 0) { LoaderInfo info = mLoaders.valueAt(idx); mLoaders.removeAt(idx); info.destroy(); } idx = mInactiveLoaders.indexOfKey(id); if (idx >= 0) { LoaderInfo info = mInactiveLoaders.valueAt(idx); mInactiveLoaders.removeAt(idx); info.destroy(); } if (mActivity != null && !hasRunningLoaders()) { mActivity.mFragments.startPendingDeferredFragments(); } }
返回最近和给定ID相关联的Loader对象。 */ @SuppressWarnings("unchecked") public <D> Loader<D> getLoader(int id) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); }
LoaderInfo loaderInfo = mLoaders.get(id); if (loaderInfo != null) { if (loaderInfo.mPendingLoader != null) { return (Loader<D>)loaderInfo.mPendingLoader.mLoader; } return (Loader<D>)loaderInfo.mLoader; } return null; }
void doStart() { if (mStarted) { RuntimeException e = new RuntimeException("here"); e.fillInStackTrace(); return; }
mStarted = true;
for (int i = mLoaders.size()-1; i >= 0; i--) { mLoaders.valueAt(i).start(); } }
void doStop() { if (!mStarted) { RuntimeException e = new RuntimeException("here"); e.fillInStackTrace(); return; }
for (int i = mLoaders.size()-1; i >= 0; i--) { mLoaders.valueAt(i).stop(); } mStarted = false; }
void doRetain() { if (!mStarted) { RuntimeException e = new RuntimeException("here"); e.fillInStackTrace(); return; }
mRetaining = true; mStarted = false; for (int i = mLoaders.size()-1; i >= 0; i--) { mLoaders.valueAt(i).retain(); } }
void finishRetain() { if (mRetaining) {
mRetaining = false; for (int i = mLoaders.size()-1; i >= 0; i--) { mLoaders.valueAt(i).finishRetain(); } } }
void doReportNextStart() { for (int i = mLoaders.size()-1; i >= 0; i--) { mLoaders.valueAt(i).mReportNextStart = true; } }
void doReportStart() { for (int i = mLoaders.size()-1; i >= 0; i--) { mLoaders.valueAt(i).reportStart(); } }
void doDestroy() { if (!mRetaining) { for (int i = mLoaders.size()-1; i >= 0; i--) { mLoaders.valueAt(i).destroy(); } mLoaders.clear(); }
for (int i = mInactiveLoaders.size()-1; i >= 0; i--) { mInactiveLoaders.valueAt(i).destroy(); } mInactiveLoaders.clear(); }
public boolean hasRunningLoaders() { boolean loadersRunning = false; final int count = mLoaders.size(); for (int i = 0; i < count; i++) { final LoaderInfo li = mLoaders.valueAt(i); loadersRunning |= li.mStarted && !li.mDeliveredData; } return loadersRunning; } }
|