call restartLoader every time you need fresh data, or you want to change your arguments for the cursor.
If you use initLoader to load data
lm = fragment.getLoaderManager(); lm.initLoader(LOADER_ID_LIST, null, this);
Each initLoader call returns the same cursor to onLoadFinished . The onCreateLoader method will only be called the first time initLoader is called. Therefore, you cannot change the request. You get the same data for the onLoadFinished method.
@Override public void onLoadFinished( android.support.v4.content.Loader<Cursor> loader, Cursor cursor) { switch (loader.getId()) { case LOADER_ID_LIST:
So, to get the new cursor data on onLoadFinished , use restartLoader and you can pass the package information if you want. Below I pass null because there is a global variable.
lm = fragment.getLoaderManager(); lm.restartLoader(LOADER_ID_LIST, null, this);
The loaderManager manager then calls onCreateLoaderMethod .
@Override public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) { android.support.v4.content.Loader<Cursor> ret = null; // Create a new CursorLoader with the following query parameters. switch (id) { // load the entire list case LOADER_ID_LIST: String sortOrder = null; switch (mSortOrder) { case 0: sortOrder = RidesDatabaseHandler.KEY_DATE_UPDATE + " desc"; break; case 1: sortOrder = RidesDatabaseHandler.KEY_ID + " desc"; break; case 2: sortOrder = RidesDatabaseHandler.KEY_NAME; } return new CursorLoader(context, RidesDatabaseProvider.CONTENT_URI, PROJECTION, null, null, sortOrder); // load a single item case LOADER_ID_ENTRY: sortOrder = null; String where = RidesDatabaseHandler.KEY_ID + "=?"; String[] whereArgs = new String[] { Integer.toString(lastshownitem) }; return new CursorLoader(context, RidesDatabaseProvider.CONTENT_URI, PROJECTION, where, whereArgs, null); } return ret; }
Notes. You do not need to call initLoader , you can call restartLoader every time if you really want the same data to be returned from a previous request. You don't need to call onContentChanged() , but the docs say Loader.ForceLoadContentObserver is done for you.