EventBus (1) Basic use

 What is EventBus?



EventBus is a component similar to Broadcast that can communicate and transfer data between different components and different threads.


 Why use EventBus instead of Broadcast?



Compared with the Broadcast thread, EventBus has much smaller overhead, and the operation is simple and fast.


EventBus specific usage scenarios



1. Data transfer between Activity and Fragment.


    When we use Activity, if we need to pass data, we can use Intent or sharepreference persistence , but if we pass data in Fragment, we generally use interface callback to handle it. In fact, we will use EventBus for better results here.


2. Pass information across multiple activities.


    Suppose there is a scene with three activities A, B, and C. When I go from A to B to C, I need to close A. At this time, we can send a message to A through EventBus. After A receives the message, it executes finish ()operate.
    

EventBus usage steps



Since the registration and deregistration of EventBus appear in pairs, the steps are as follows


Add  dependency


compile 'org.greenrobot:eventbus:3.0.0'


Step 1: Register data in onCreate()


EventBus.getDefault.register(this)


Step 2:
Unregister the data in onDestroy()


EventBus.getDefault.unregister(this)


Step 3:
In the registered Activity, write a method, which is decorated with @Subscribe. Then, in this method, judge whether the passed information meets the requirements you need, and then take corresponding processing.


@Subscribe
    public void onMainEvent(Event event) {
        
        finish();
    }

It should be noted here that we can name the method modified by subscribe after EventBus 3.0, but when using EventBus 2.0, this method cannot be named arbitrarily.


The fourth step is


to write an Event class and use this class to send messages uniformly

public class EventBus<T> {
    private Integer code;
    private String message;
    private T data;


    public Integer getCode() {
        return code;
    }


    public void setCode(Integer code) {
        this.code = code;
    }


    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }


    public T getData() {
        return data;
    }


    public void setData(T data) {
        this.data = data;
    }
}

the fifth step


Send the message using EventBus.getDefault.post(Object obect) in the method that needs to send the message.


From the above five steps, we can complete the operation and use of Eventbus.




The little thing about Subscribe

We know that after using EventBus to send a message, we will use a method written by ourselves to receive this message with the subscribe modification. But what if the operation after receiving this message is a very time-consuming operation? Generally, the operation we think of is to open a thread to handle this time-consuming operation alone. In fact, there is a thread model for subscribe in EventBus, and you can choose which thread to execute the received message processing according to business needs.


EventBus has four threading models ( threadMode = ThreadMode.XX )

  • POSTING (default): The feature of this threading model is that in which thread the event is issued, the event handler will run in which thread. That is, emitting events and receiving events always occur in one thread.
  • MAIN: This threading model, as the name suggests, no matter which threads the event is issued in, the event processing and receiving always operate in the main thread. Therefore, it must be noted here that in this mode, we do not perform time-consuming operations in the event handler to avoid ANR.
  • BACKGROUND: If the event is emitted in the UI thread, the event handler will run in a new sub-thread, and if the event is emitted in the sub-thread, the event-handling function will be received directly in the sub-thread. In this event, it is forbidden to do update UI operation
  • ASYNC: No matter which thread the event is posted on, the event handler will be executed in the newly created child thread. In this event, it is forbidden to do update UI operation




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686016&siteId=291194637