EventBus is introduced

EventBus is the publish/subscribe EventBus for Android and Java. Normal Java(non-android) projects are supported starting with EventBus3.1. The address of the lot

Why use EventBus

  • It simplifies the communication between components within the application and between components and background threads
  • Decouple event sender and receiver
  • Less overhead, faster, more elegant code, smaller size (~ 50K JAR)
  • Avoid complex and error-prone dependencies and lifecycle issues

The three elements of EventBus

  • Event: an Event that can be an object of any type.
  • Subscriber: event Subscriber. Prior to EventBus3.0, message processing methods are limited to onEvent, onEventMainThread, onEventBackgroundThread, and onEventAsync. After EventBus3.0, event-handling methods can be named anything, but you need to add a @subscribe annotation, and specify a threading model (by default, POSTING), which is covered below.
  • Publisher: Event Publisher, can send events from any thread in any location, directly call eventBus.getDefault (). Post (Object), according to the type of the post function parameter, will automatically call the function to subscribe to the corresponding type of events.

EventBus’s five threading models

  • POSTING (default) : If you use an event handler to specify the POSTING model as POSTING, the event handler will run in the POSTING thread. That is, POSTING and receiving events are in the same thread. In event handlers with a POSTING model, avoid performing time-consuming operations because they block the delivery of events and can even cause ANR.
  • MAIN: Event processing is performed in the UI thread. The event processing time should not be too long, or the event will be ANR.
  • BACKGROUND: If the event is published in the UI thread, the event handler is executed in the new thread. If the event was originally published in the child thread, the event handler is executed directly in the thread that published the event. UI update operations are disabled in this event handler.
  • ASYNC: Regardless of which thread the event is published in, the event handler is executed in the new child thread. Again, UI updates are prohibited in this event handler
  • MAIN_ORDERED: Added after EventBus3.1 and executed synchronously according to the order in which events are published

EventBus commonly used Api

Inheritance structure

java.lang.Object
    org.greenrobot.eventbus.EventBus
Copy the code

Commonly used method

methods instructions
EventBus.getDefault() Get an instance of EventBus
EventBus.getDefault().register(this) Call, subscribe to register events where needed
EventBus.getDefault().post(messageEvent) Publish events to the event bus
@Subscribe(threadMode = ThreadMode.MAIN) Specifies the threading model to handle events
EventBus.getDefault().unregister(this) Unsubscribe event

Use of EventBus to register common events

Adding a dependency library

implementation "Org. Greenrobot: eventbus: 3.1.1."
Copy the code

Customize an event class

data class MessageEvent(val message:String)
Copy the code

Prepare subscriber: Declare and comment subscription methods, optionally specifying thread patterns

@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: MessageEvent){ /* Do something */ }
Copy the code

Before EventBus3.0, message processing methods were limited to onEvent, onEventMainThread, onEventBackgroundThread, and onEventAsync. After EventBus3.0, message processing methods could be named as anything. But you need to add an annotation @subscribe and specify the threading model (default POSTING)

Register and unsubscribe events

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    EventBus.getDefault().register(this)
}

override fun onDestroy() {
    super.onDestroy()
    EventBus.getDefault().unregister(this)
}
Copy the code

Publish event

EventBus.getDefault().post(MessageEvent("Send a MessageEvent"))
Copy the code

Sticky events in EventBus

In EventBus, in addition to normal events, you can also send sticky events, which can be received by subscribing to the event after sending it

Subscribers handle sticky events

@Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
fun onMessageEvent(event: MessageEvent){ /* Do something */ }
Copy the code

Send stickiness events

EventBus.getDefault().postSticky(MessageEvent("Send a MessageEvent"))
Copy the code

Call, subscribe to register events where needed

EventBus.getDefault().register(this)
Copy the code

ProGuard 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);
}
Copy the code

The resources

  • ProGuard
  • EventBus
  • EventBus official Api
  • EventBus in the Android Development series
  • EventBus3.0 is fully resolved