theactiveactor
theactiveactor

Reputation: 7554

Android: How to receive callback from Content Provider when Data Changes?

Say I have 3 active Activities that can display and add data to a ContentProvider. What's the best way of synchronizing state among the Activities? For example, if ActivityA adds a new record to the ContentProvider, I would like ActivityB and ActivityC to receive notification so they may update their view.

Upvotes: 5

Views: 3374

Answers (1)

escape-llc
escape-llc

Reputation: 1331

Use the ContentResolver.registerContentObserver() method from each activity, and supply appropriate URI(s).

You can pass the base Content URI for the provider, and pass true for notifyForDescendents parameter, and your callback will get hit every time there's a change.

The Content Provider you are accessing must support this (most do). If you are writing your own, you must call ContentResolver.notifyChange() with the URI of modified row(s).

Upvotes: 4

Related Questions