The observer mode is widely used in Android, especially in listener. This article uses button as an example to introduce the observer mode.
First, let's review the observer mode.
The observer mode is also called the publish/subscribe mode, Model-View Mode, source-listener mode, or dependents mode) mode.
The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves. Its structure is as follows:
We can see that the observer registers ATT with subject.ACH and detach are deregistered. When the subject status changes, the observer's method update is called to update the status of the observer.
In Android, the activity interacts with contentprovider through contentresolver. We can register an observer with contentresolver to listen for data updates. Call the contentresolver method registercontentobserver () to complete registration. In this way, when the corresponding contentprovider data is updated, the object method we register with contentresolver will be called. This is also an observer application instance in the Android system.
Of course there are still many observer applications in the Android system. Pay attention to this when learning the Android system.