Android eventbus3.x Use Details (a)

Source: Internet
Author: User

? (^∇^*) 51 holiday at home nothing, the new project is Rxjava2+eventbus feel good, while this leisure summary under Eventbus

I. Brief description

Eventbus is an observer-based Android event publish/Subscribe framework that simplifies the delivery of Android events by decoupling publishers and subscribers, where events can be understood as messages. Event delivery can be used to communicate between four components of Android, as well as asynchronous threads and main thread communication.
The emergence of Eventbus is to solve the traditional problem of callback hell through interface events, in contrast to the eventbus is a bit of code concise, simple to use, and to fully decouple event publishing and subscription.

Eventbus is made up of three parts: event events, subscriber subscribers, Publisher Publishers.

Eventbus website Address: http://greenrobot.org/eventbus/

Eventbus Github:https://github.com/greenrobot/eventbus

Second, basic use of preparatory work

Add dependencies (two ways):

Via Gradle
Compile ' org.greenrobot:eventbus:3.1.1 '
<!--Via maven--><dependency>    <groupId>org.greenrobot</groupId>    <artifactid >eventbus</artifactId>    <version>3.1.1</version></dependency>
The first step defines the event

The event is Pojo (a normal old Java object) and does not have any specific requirements.

 Public class messageevent {     publicfinal  String message;       Public messageevent (String message) {        this. Message = message;    }} 
Step two prepare Subscribers-subscriber
//  This method would be called if a messageevent is posted (in the UI thread for Toast)@Subscribe (Threadmode =  Threadmode.main)publicvoid  onmessageevent (messageevent event) {    // This method would be called if a someotherevent is posted @Subscribe  Public void Handlesomethingelse (Someotherevent event) {    Dosomethingwith (event);}

Subscribers need Registe and unregist

Cond.. April 29, 2018 21:00:47

    

Android eventbus3.x Use Details (a)

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.