Observer (observer) mode for Android applications

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.