? (^∇^*) 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)