This article learns how to use EventBus 3.0 together.
EventBus is used to publish/subscribe event bus, which simplifies communication between components, components and background threads in the application. The advantage is that the expenses are small, the code is more elegant, and the deed and the receiver are decoupled.
implementation 'org.greenrobot:eventbus:3.1.1'
EventBus.getDefault()
public class MessageEvent {
public int event;
public String message;
public Object object;
public MessageEvent(int event) {
this.event = event;
}
public MessageEvent(int event, String message) {
this.event = event;
this.message = message;
}
public MessageEvent(int event, String message, Object object) {
this.event = event;
this.message = message;
this.object = object;
}
}
EventBus.getDefault().register(subscriber); // Subscriber type is Object, that is, the category of subscribed
EventBus.getDefault().post(messageEvent);
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onEventTest(MessageEvent messageEvent) {
int event = messageEvent.event;
// Event processing ...
}
EventBus.getDefault().unregister(subscriber);
public class MyEventBus {
public static final String TAG = "MyEventBus";
public static MyEventBus instance;
public static final int EVENT_UPDATE = 1;
public static MyEventBus getInstance() {
if (instance == null) {
synchronized(MyEventBus.class) {
if (instance == null) {
instance = new MyEventBus();
}
}
}
return instance;
}
// Register event
public void register() {
EventBus.getDefault().register(this);
}
// Cancel the registration event
public void unregister() {
EventBus.getDefault().unregister(this);
}
/**
* Event processing method, according to the specified ThreadMode, will be executed in the background thread, so UI operation cannot be performed
*/
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onEventBackground(MessageEvent messageEvent) {
int event = messageEvent.event;
switch (event) {
case EVENT_UPDATE:
...
break;
}
}
}
@Override
protected void onCreate() {
MyEventBus.getInstance().register(this);
}
public void clickPostEvent(View view) {
MessageEvent event = new MessageEvent(EmmEventBus.EVENT_UPDATE);
EventBus.getDefault().post(event);
}
@Override
protected void onDestroy() {
MyEventBus.getInstance().unregister(this);
}
In addition to the ordinary events mentioned above, EventBus also supports sending sticky events, namely: Send an event-> subscription event-> handling event.
There are only the following differences in the writing of sticky events:
// The processing method of viscosity event, increase the attribute sticky = true
@Subscribe(threadMode = ThreadMode.BACKGROUND,sticky = true)
public void onEventBackground(MessageEvent messageEvent) {
...
}
// Send sticky event
EventBus.getDefault().postSticky(new MessageEvent(2));
2019 Unicorn Enterprise Heavy Glour Recruitment Python Engineer Standard >>> 1, download Eventbus class library Source code:https://github.com/greenrobot/EventBus 2, basically use (1) Customi...
Vuex to use 1. Introduction and Installation 2. Configure 3. Registration 4. In the assembly 1. Introduction and Installation EventBus: Also known as the event bus. EventBus can be used in the Vue as ...
1. Guide package 2. Add confusion The obfuscation rules are as follows 3. The first activity 4. The second activity 5. Simple functions are achieved...
Vue components are very common with parent-child component communication and brother component communication. The communication between parent and child components is very simple. The parent component...
1. Overview EventBus definition: it is a publish/subscribe event bus. In this way, it should contain 4 components: publisher, subscriber, event, and bus. So what is the relationship between these four...
purpose of usage Communication within the component is also transmitted data Scope of application The brothers component passes the value, the grandchildren component pass the value, and the component...
1 Introduction Once, layer after layer of business logic made me feel overwhelmed, one after another callback makes you dizzy, one parameter after another makes you confused. EventBus, a framework tha...
Introduction GitHub:https://github.com/greenrobot/EventBus Let's talk about what the EventBus thread bus does, the environment, its advantages and disadvantages. What are you doing? In a word, simple ...
Android development framework EventBus Series event bus Introduction GitHub:https://github.com/greenrobot/EventBus EventBus to talk threads bus is doing, environment, advantages, disadvantages. Doing?...
In order to simplify and more high-quality communication between Activity, Fragment, Thread and Service, etc., while solving the high coupling between components and still continue to communicate effi...