Take the 2-minute tour ×

What is difference between ContentObserver and DatasetObserver?

When one or another should be used?

I get Cursor with single row. I want to be notified about data changes - eg. when row is updated.

Which observer class should I register?

share|improve this question
add comment

2 Answers

up vote 21 down vote accepted

If you are using a ContentProvider (via ContentResolver or Activity.managedQuery()) to get your data, simply attach a ContentObserver to your Cursor. The code in onChange() will be called whenever the ContentResolver broadcasts a notification for the Uri associated with your cursor.

Cursor myCursor = managedQuery(myUri, projection, where, whereArgs, sortBy);
myCursor.registerContentObserver(new ContentObserver() {
    public void onChange(boolean selfChange) {
        // This cursor's Uri has been notified of a change
        // Call cursor.requery() or run managedQuery() again

    public boolean deliverSelfNotifications() {
        return true;

Make sure your ContentProvider is a "good citizen" and registers the Uri with the cursor after a query:

cursor.setNotificationUri(getContentResolver(), uri);

It should also notify the ContentResolver of any changes to the underlying data (for instance, during insert, delete, and update operations on your SQLite database):

getContentResolver().notifyChange(uri, null);

This approach is a good example of the Observer Pattern of object-oriented design.

share|improve this answer
Ok, and in which way DatasetObserver differs from ContentProvider? –  pixel Apr 13 '11 at 13:47
I haven't used a DataSetObserver, but it seems from the documentation (developer.android.com/reference/android/database/…) a DataSetObserver is notified of cursor lifecycle changes such as closing and requerying, whereas ContentObserver is used to watch for changes in underlying data. Hope that helps. –  ptc Apr 13 '11 at 17:10
Now I see. DatasetObserver observes Cursor state, whereas ContentProvider observes underlying content changes. Thanks! –  pixel Apr 13 '11 at 17:56
requery() is now deprecated, has the method for doing this changed? –  Andrew Wyld Mar 18 '13 at 14:45
Using managedQuery is also deprecated. The currently recommended approach is to asynchronously query your ContentProvider with CursorLoaders. They observe changes and automatically requery their cursors. See this: stackoverflow.com/questions/7182920/… –  ptc Mar 19 '13 at 15:05
add comment

From my last app developed I can say. The main difference between ContentObserver and DatasetObserver, is the ContentObserver make to Observer any change affects on Content Provider, other hand the DatasetObserver Observer any change effect on the database.

share|improve this answer
DatasetObserver can be registered with a BaseAdapter - which isn't constrained solely to a database. –  Igor Ganapolsky Jan 20 at 21:05
add comment

Your Answer


