In the query method of your ContentProvider attach the listener to the returned cursor:
Cursor cursor = queryBuilder.query(dbConnection, projection, selection, selectionArgs, null, null, sortOrder); cursor.setNotificationUri(getContext().getContentResolver(), uri);
Then in your insert / update / delete methods use the following code:
final long objectId = dbConnection.insertOrThrow(ObjectTable.TABLE_NAME, null, values); final Uri newObjectUri = ContentUris.withAppendedId(OBJECT_CONTENT_URI, objectId ); getContext().getContentResolver().notifyChange(newObjectUri , null);
Your CursorLoader will be notified, and OnLoadFinished(Loader, Cursor) will be called again.
If you are not using Loader , ContentObserver is a path with a few lines of code that you notify about changes to the DB (but you will need to manually request it).
private ContentObserver objectObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); restartObjectLoader(); } };
Remember to call onResume() :
getContentResolver().registerContentObserver(ObjectProvider.OBJECT_CONTENT_URI, false, objectObserver);
and in onPause() :
getContentResolver().unregisterContentObserver(objectObserver);
Update: UI Changes This is a larger topic because it depends on the Adapter that you use to populate a ListView or RecyclerView .
CursorAdapter In onLoadFinished(Loader loader, Cursor data)
mAdapter.swapCursor(data);
ArrayAdapter In onLoadFinished(Loader loader, Cursor data)
Object[] objects = transformCursorToArray(data);
RecyclerView.Adapter In onLoadFinished(Loader loader, Cursor data)
Object[] objects = transformCursorToArray(data);
Read here for a different way to alert RecyclerView.Adapter .