I have an application that uses Loader to get into a database, which is also edited by IntentService . I get data from the Loader implementation through LoaderCallbacks , which works fine.
I also use ContentResolver#notifyChange(Uri, ContentObserver) to trigger a reboot. However, this works when I call Cursor#setNotificationUri(Uri) in advance.
I canβt find a link to the last method in any documentation, and it seems like this could lead to crashes: see also
IllegalStateException "trying to reopen an already closed object" in SimpleCursorAdapter from ContentProvider
However, without this call to Cursor , LoaderCallbacks#onLoadFinished(Loader<Cursor>, Cursor) gets only after the boot, and not after notification. Do I need to also implement OnLoadCompleteListener , well, exactly the same?
ContentProvider request method:
class MyContentProvider extends ContentProvider {
Typical LoaderCallbacks :
LoaderCallbacks<Cursor> mCallbacks = new LoaderCallbacks<Cursor>() { @Override public void onLoaderReset(Loader<Cursor> loader) { mArticleAdapter.swapCursor(null); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { if(cursor.isClosed()) { Log.d(TAG, "CURSOR RETURNED CLOSED"); Activity activity = getActivity(); if(activity!=null) { activity.getLoaderManager().restartLoader(mFragmentId, null, mCallbacks); } return; } mArticleAdapter.swapCursor(cursor); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { triggerArticleFeed(); CursorLoader cursorLoader = null; if(id == mFragmentId) { cursorLoader = new CursorLoader(getActivity(), MyContentProvider.ARTICLES_URI, null, ArticlesContentHelper.ARTICLES_WHERE, ArticlesContentHelper.ARTICLES_WHEREARGS, null); } return(cursorLoader); } };