background

In small projects, EventBus is a good way to decouple and avoid unnecessary interface code. The sender just sends the message, regardless of what the recipient of the corresponding message type does.

However, in the process of use, it is hard to avoid some bad features.

EventBus can declare the recipient method via the @SUBSCRIBE annotation. The correspondence between sender and receiver is done through the classes of the sent data.

The sender sends a String message, and all methods annotated with @SUBSCRIBE’s String and Object arguments receive the message in registered subscribers.

On the one hand, this makes the relationship between sender and receiver less intuitive. On the other hand, if you really want to pass a String, you can’t do it without being received by some of the subscriber’s String argument methods. So, in addition to parameter types to assign receivers, I think we need to add another dimension to distinguish messages.

To improve the

Based on the open source 3.0.0 code for EventBus, I modified the @SUBSCRIBE annotation to add an ID attribute of type int, and overloaded the EventBus class with the POST (int,Object) method so that recipients can annotate their own methods:

@Subscribe(id = 1)
public void print(String message){
    Log.i("tag",message);
}

@Subscribe(id = 2)
public void setMessage(String message){
    this.mMessage = message;
}Copy the code

The sender can then send its own message as follows:

EventBus.getDefault().post(1,"hello world!");Copy the code

Thus, although there are two methods that receive String, only the print method receives the message and executes because the message id is 1.

Of course, EventBus’s original no-ID approach is still preserved. You can either not specify an ID for the receiver or use the ID parameter when sending the message, so that all recipients of the same parameter type will still execute it. As follows.

. @Override protected void onCreate(Bundle savedInstanceState) { EventBus.getDefault().post("hello world");
}

@Subscribe
public void print(String message){
    Log.i("tag",message);
}Copy the code

use

Put the GitHub source link first.

1. Add the JitPack repository link to the root project’s build.gradle:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io'}}}Copy the code

2. Add dependencies to build.gradle in module

dependencies {
    implementation 'com. Making. Lizhengxian1991. Eventbusplus: eventbus: 1.0.0'
    annotationProcessor 'com. Making. Lizhengxian1991. Eventbusplus: eventbus -- the annotation processor: 1.0.0'
}Copy the code

3. Add the Processor parameter to module build.gradle to specify the index class name for Eventbus (instead of

).

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : '<Your index class name>']}}}}Copy the code

4. Initialize index in Application onCreate method (replace ‘

‘ with class defined in 3)

EventBus.builder().addIndex(new <Your index class name>()).installDefaultEventBus();Copy the code

Use eventBus.getDefault ().register(subscriber) to register the message receiver object, and then you can post and subscribe as much as you like!