Eventbus of the communication weapon between Android components/controls

Source: Internet
Author: User

The actual project development process, often encountered the following scenario: the different application components of the control has a certain degree of correlation, wherein the user to the latter of the operation will cause the corresponding changes. Take a specific scenario: in the case of embarrassing encyclopedia, for example, in the embarrassing things List page and Detail page, for each embarrassing thing, the layout is basically the same, on the details page click on the likes, the number of likes increased, and the icon has changed, at this time to return to the list page, this embarrassing thing on the icon and the number and the need In an example, for multiple bottom navigation tab of the information class to read the app, on the Consulting Details page Click on the collection, and then the collection succeeded, at this time back to the bottom tab in the Personal center, if the personal center has my collection, but also shows the number of collections in the back, At this point, this collection needs to change the number immediately with the collection/de-collection that the user has just made. Similar requirements scenarios are common.

Sometimes, when such requirements are relatively simple, through the interface to implement callbacks, etc. can be done, but when the relationship between different components/controls are complicated, the interface-based approach not only makes the code very cumbersome, but also the logic of the program is very confusing, based on this, Eventbus, This provides a very convenient solution for the implementation of such requirements.

There are a lot of eventbus on the Internet, in this simple introduction of the complete use of the process.

1. First define the event base class (in fact, it can not be defined, the benefit of the definition is that the same callback function directly according to the different subclass event type can directly continue the logical processing, the code and logic more concise and clear)

 Public class baseevent {}

2. Define the specific event type (example of the above collection event)

 Public classFavoreventextendsBaseEvent {Private intDid ;  Publicfavorevent () {} PublicFavorevent (intDid ) { This. did =Did ; }     Public intGetdid () {returnDid ; }     Public voidSetdid (intDid ) { This. did =Did ; }}

3. Register an event listener with Eventbus where you need to listen to this collection event

Eventbus.getdefault (). Register (this);

4. When you need to unregister the event listener

Eventbus.getdefault (). Unregister (this);

Note: In Android, when encountering registers such as Eventbus, the general, corresponding will have unregister logic. The register and unregister logic correspond to each other, in the different life cycle of components such as activity. This is because Eventbus (and others) are registered with a potential memory leak due to the use of hard references, and a potential memory leak can be eliminated by de-registering in the corresponding lifecycle, such as OnDestroy.

5. When an event occurs, you need to notify the appropriate event listener to handle the corresponding logic

// inform Eventbus that it has been collected to notify interested parties (the main is to inform the Personal Center page to change my collection number)2 Eventbus.getdefault (). Post (new favorevent ( ));

6. Other component/listener specific event logic

 Public void onEvent (BaseEvent event) {    //  receive favorite event notifications, synchronize favorite numbers    ifinstanceof favorevent) {        //  instantly update the number of favorites (get the number of favorites from SQLite)        updatefavornum (    ); Else if (..) {        ...    }}

The whole use of the process is so much, among them, about the child thread and UI thread, etc. can also be similar to the event notification, online such information many, no longer repeat the

Eventbus of the communication weapon between Android components/controls

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.