androidandroid-contentproviderandroid-contentresolvercontentobserver

What happens to Content Observer object after unregisterContentObserver() is called?


When using a ContentProvider to manage the application's data access, it's possible to use a ContentObserver to monitor for changes in the ContentProvider.

To create a ContentObserver we need to create a new object, normally inside Activity's onResume() method. Then the ContentObserver object is registered to get callbacks when data identified by a given content URI changes.

private Google_AR_Observer googleARObserver = null;    

@Override
public void onResume() {
    super.onResume();

    googleARObserver = new Google_AR_Observer(this);
    getContentResolver().registerContentObserver(
        CONTENT_URI, 
        true, 
        googleARObserver);
}

Finally, inside the Activity's onPause() method, the previously registered ContentObserver that is no longer needed is unregistered.

@Override
public void onPause() {
    super.onPause();

    getContentResolver().unregisterContentObserver(googleARObserver);
}

My question is: What happens to ContentObserver after being unregistered? Is it destroyed?


Solution

  • If you look at the source code especially ContentObserverclass , you will find a method releaseContentObserver()which is called when you unregister any observer. This method internally holds the reference of contentObserverobject (inside other inner class Transport) which gets assigned to null value . So basically you can say its destroyed as soon as you unregister it