I am having a couple of problems with my AsyncTaskLoader, not sure if they are related, as they both occur when trying to restart the bootloader. In my application, I have 3 instances of a custom CursorAdapter supported by three instances of a custom AsyncTaskLoader, managed by 1 singleton LoaderManager. The problems are related to two different adapter / bootloader pairs, but the code used is the same in each case:
getLoaderManager().restartLoader(loaderId, bundle, loaderManager);
Problem 1: I call restartLoader () and the LoaderManager registers a call to onCreateLoader, but not one for onLoaderReset (). The loader receives Result () for delivery, but onLoadFinished () is never called. The bootloader does not have the 'reset' or 'start' flags set (see code below).
Problem 2: I call restartLoader (), and the LoaderManager registers a call to onLoaderReset (). The loader gets onReset () but doesn't get any more. The cursor is set to null, but the new cursor is not loaded.
Any ideas what could be the problem? Here are some of the code for Loader and Loader Manager:
CustomCursorLoader.java
@Override protected void onStartLoading() { Log.v(TAG, "Starting Loader"); if (lastCursor != null) { deliverResult(lastCursor); } if (takeContentChanged() || lastCursor == null) { forceLoad(); } } @Override public void deliverResult(Cursor cursor) { Log.v(TAG, "Delivering result"); if (isReset()) { Log.v(TAG, "reset"); if (cursor != null) { cursor.close(); } return; } Cursor oldCursor = lastCursor; lastCursor = cursor; if (isStarted()) { Log.v(TAG, "started"); super.deliverResult(cursor); } if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) { oldCursor.close(); } } @Override protected void onReset() { Log.v(TAG, "Reset"); super.onReset(); onStopLoading(); if (lastCursor != null && !lastCursor.isClosed()) { lastCursor.close(); } lastCursor = null; }
CustomCursorLoaderManager.java:
@Override public Loader<Cursor> onCreateLoader(int loaderId, Bundle bundle) { return new CustomCursorLoader(); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { cursorAdapter.changeCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { cursorAdapter.changeCursor(null); }