0

My ContentProvider contains a ContentObserver. It's easy to register the observer during onCreate(). However, I see no way to unregister it.

From some digging around, it appears the Android cleans up some things when destroying a ContentProvider. Will it also clean up ContentObserver registrations?

public class MyProvider extends ContentProvider
{
  MyObserver observer = null;

  @Override
  public boolean onCreate ()
  {
    observer = new MyObserver ();
    getContext ().getContentResolver().registerContentObserver (uri, true, myObserver);
    return true;
  }
  ... other methods ...
}
Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

1 Answers1

0

There's no need to unregister anything in a content provider because content providers are around as long as your app process is. See this similar question, not exactly a duplicate, but will explain more.

Closing the database in a ContentProvider

Greg Moens
  • 1,705
  • 8
  • 15
  • I had seen that post. Diane says "A content provider is created when its hosting process is created, and remains around for as long as the process does." The hosting process could die at any time for various reasons. She only makes a claim about the db being cleaned up but infers nothing about other resources. – Peri Hartman Dec 20 '18 at 15:31
  • Well, if your app process goes away, your resources will go along with it. This includes any content observers that you registered against your own app context. Even if you're observing a Uri from another app, I'm sure IPC cleans that up. – Greg Moens Dec 20 '18 at 22:09