Search code examples
androidandroid-contentproviderandroid-contentresolvercontentobserver

Prevent ContentObserver from getting a "specific" change on URI


I have an app that allow users to edit the ContractsContacts DB.

When a user edits the ContractsContacts DB using the functions of my app, I don't want that my ContentObserver is aware of that changes.

I want my ContentObserver to be aware only of changes generated from other apps. For example when users edit ContractsContacts DB by using the mobile apps.

So, my question is: is there a way to tell the ContentObserver "hey, don't listen to this change, because I'm aware of it, dont call your onChange() method":

The only solution I found is:

  1. unregister the ContentObserver before starting the "edit function" of my app
  2. register again the ContentObserver after my function did all its work.

Thank you


Solution

  • This is not your ContentProvider, and so you cannot prevent Android from updating registered observers. Your observer is nothing special to Android.

    So, in addition to your unregister-modify-register flow, you could tell your observer to ignore the next update (have it track that in a boolean or something), then modify the data. The observer would skip whatever work it normally does when that boolean is set, just flipping it to false to pick up future changes.

    Both of these suffer from race conditions (you and another app modifying the provider at the same time).

    Ideally IMHO, you modify whatever logic is being triggered by the observer to live with triggers coming from your own updates, so that all changes of the data are treated equally, whether coming from your app or not.