How to safely unregister Android ContentObserver

  • February 20, 2013

To save system resources ContentObservers need to be safely unregistered when no longer in use.  The best way to do this is the following.

try {
	getContentResolver().unregisterContentObserver(myContentObserver);
} catch (IllegalStateException ise) {
	// Do Nothing.  Observer has already been unregistered.
}

As of Android 4.1.2, there is no method to check whether the observer is registered or not.  Therefore we use the try-catch method to catch situations where the observer has already been unregistered.  This sub-routine will typically be included in the onDestroy() of an activity but can be called anywhere you need it.




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *