Android third party framework EventBus

EventBus is a kind of event publish-subscribe bus for Android. It simplifies the communication complexity between the various components in the application, especially the communication problem between the fragments. It can avoid the inconvenience caused by the use of broadcast communication. EventBus can replace traditional Android Intent, Handler, Broadcast or interface functions, in fragments, the Activity, Service transfer data between threads, the execution method. Add a dependency under build.gradle in your app:

Dependencies {/ / eventbus add depend on implementation 'org. Greenrobot: eventbus: 3.2.0' / / the first step: add dependent}

2. Know the EventBus

Three roles:

  • Event: Event, which can be of any type, is globally notified by EventBus based on the Event type.
  • Publisher: Publisher of events that can be published from any thread. In general, you can use EventBus.getDefault() to get an EventBus Object, and then call the POST (Object) method.
  • Subscriber: Event Subscriber. Before EventBus 3.0 we had to define the methods that begin with onEvent, OnEvent, OnEventMainThread, OnEventBackgroundThread, and OnEventAsync. After 3.0, you can use whatever name you want for the event handling method, but you need to add the annotation @subscribe and specify the thread model. The default is Posting. Subscriber to register the event: eventbus.getDefault ().register(this); EventBus.getDefault().unregister(this); This is passed to the Subscriber of the event and the event is published. If we need to, we need to register to subscribe to it.)
  • Posting: By default, the thread for an event handler is on the same thread as the thread that published the event
  • Main: Indicates that the thread of the event handler is on the MAIN thread (the UI thread), so time consuming operations cannot take place here. This is the main threading model used
  • Background: The thread that represents the event handler is a BACKGROUND thread, so it cannot perform UI operations. If the thread that publishes the event is the main thread (UI thread), then the event handler will start a background thread, and if the thread that publishes the event is in the background thread, then the event handler will use that thread.
  • ASYNC: No matter which thread the event is published on, the event handler will always create a new child thread to run. Again, it cannot perform UI operations.

Encapsulate a JavaBean object and define the event object

Public class ChooseTabEvent {private int tabId; private int tabId; public ChooseTabEvent(@IdRes int tabId) { this.tabId = tabId; } public int getTabId() { return tabId; } public void setTabId(int tabId) { this.tabId = tabId; }}

Publish an event Publish an event in the UI thread

EventBus.getDefault().postSticky(new ChooseTabEvent(0)); // Select Tab and publish the adhesive event

4. Processing an event requires registration of the event in the processing event, cancel the event registration

public class RedActivity extends BaseActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) {  EventBus.getDefault().register(this); } @Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void chooseTabEvent(ChooseTabEvent If (event. GetTabid ()==0){if (event. GetTabid ()==0){if (event. GetTabid ()==0){if (event. " + BOTTOM_TAB_ID_ARRAY.keyAt(BOTTOM_TAB_ID_ARRAY.indexOfValue(; bottomTab.setSelectedItemId(; } } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }}

Sticky event: sticky event refers to the event that the subscriber can still receive after the event has been sent.

5. The priority

We use two of the three parameters in the Subscribe annotation, and here we use the third parameter below, priority. It is used to specify the priority of the subscription method. It is an integer value. The default value is 0. The higher the value, the higher the priority. When an event is published, the higher-priority subscription method receives the event first.

ProGuard obfuscation rules

Here’s one on GitHub

- Annotation keepattributes * * / / keep reflection - keepclassmembers class * {@ org. Greenrobot. Eventbus. Subscribe < the methods >; If you are using the EventBus Processor for acceleration, you must add this, as long as the class and method are not mixed up and the decompile is convenient... If not used to accelerate, this is no} - keep enum org. Greenrobot. Eventbus. ThreadMode {*; } # And if you use AsyncExecutor:// Async asynchronous thread - KeepClassMembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { <init>(java.lang.Throwable); }

1. If an exception occurs when using EventBus: Subscriber class * and its super classes have no public methods with the @subscribe annotation The methods that use EventBus are not public and lack the @Subscribe annotation. Public and add @subscribe to it. EventBus.getDefault().register(this); Not in the public method

8.EventBus memory leak

EventBus is registered with a reference to the activity, which we de-registered in OnDestroy. OnDestroy will not be used if an exception occurs to our activity or if the system is out of memory and the activity is forced to recover. EventBus holds a reference to the activity, so a memory leak occurs. And EventBus registers again when we start the activity again.

Solution: Check if the activity has been registered before registering

if (! EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);

End: Be cautious about things you can control, and optimistic about things you can’t control. You can only do what you can, accept that, and deal with it with optimism