EventBus is an Android-optimized publish/subscribe message bus, which simplifies the communication between components in the application and between components and background threads. For example, request the network, notify the UI through Handler or Broadcast when the network returns, and communicate between two Fragments through Listener. These requirements can be achieved through EventBus
- Pros
Simplify the communication between components, can dynamically set event processing threads and priorities - Cons
Each event must customize an event class, lead to too many event classes, which virtually increases the maintenance cost

EventBus components
- Event object passed by Event
- Subscriber event subscriber
- Publisher event publisher
- ThreadMode defines in which thread the function executes
Steps to use EventBus
- 1.Add remote dependencies
implementation 'org.greenrobot:eventbus:3.0.0'
Code language: JavaScript (javascript)
- 2.Set up obfuscation rules
-keepattributes *Annotation*
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
Code language: PHP (php)
- 3.Register when Activity starts to receive messages
EventBus.getDefault().register(MainActivity.this);
Code language: CSS (css)
- 4.Logout when the Activity is destroyed, so as not to cause memory leaks
EventBus.getDefault().unregister(MainActivity.this);
Create event type
public class MessageEvent {
public String name;
public MessageEvent(String name) {
this.name = name;
}
}
Code language: JavaScript (javascript)
- 5.receive events
@Subscribe(threadMode = ThreadMode.MAIN)
public void MesssageEventBus(MessageEvent event){
// 显示接收的消息
textView.setText(event.name);
}
Code language: JavaScript (javascript)
- 6.Publish events in other classes
EventBus.getDefault().post(new MessageEvent("HJQ"));
Code language: JavaScript (javascript)
sticky event
The usage methods mentioned earlier all need to register first and then post to receive events; if you use postSticky to send events, you can receive events without first registering
- 1.Create event type
public class StickyEvent {
public String msg;
public StickyEvent(String msg) {
this.msg = msg;
}
}
Code language: JavaScript (javascript)
- 2.Post sticky events
EventBus.getDefault().postSticky(new StickyEvent("sticky_event"));
Receive sticky events in other classes
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onEvent(StickyEvent event){
textView.setText(event.msg);
}
Code language: CSS (css)
- 3.Manual registration is required, and sticky events will be automatically received and triggered after registration
EventBus.getDefault().register(MainActivity.this);
Code language: CSS (css)
- 4.Log out when the Activity is destroyed and remove the sticky event so that it will not cause memory leaks
EventBus.getDefault().removeAllStickyEvents();
EventBus.getDefault().removeStickyEvent(new StickyEvent("sticky_event"));
EventBus.getDefault().unregister(MainActivity.this);
Code language: CSS (css)
thread scheduling
@Subscribe(threadMode = ThreadMode.MAIN)
ThreadMode.MAIN:Indicates that this method is executed in the main thread
ThreadMode.BACKGROUND:Indicates that the method is executed in the background and cannot be processed at the same time
ThreadMode.ASYNC:Indicates that the method is executed in the background and can be processed at the same time
ThreadMode.POSTING:Indicates that the method and the message sender are executed in the same thread
Code language: JavaScript (javascript)
set priority
@Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
EventBus.getDefault().cancelEventDelivery(event);
Code language: CSS (css)