Android eventbus 3.0.0 Usage Summary (required)

preface

Eventbus framework

Eventbus is a common name, such as guava produced by Google. Guava is a huge library. Eventbus is only a small function attached to it, so it is not used much in actual projects. Greenrobot / eventbus is used most. The advantage of this library is simple interface and convenient integration, but it limits the method name and does not support annotation. Another library, square / Otto, is modified from guava and used by many people. So the goal of our research today is the eventbus of greenrobot

Introduction to eventbus

1. Eventbus 3.0.0 is the latest version. 2. Eventbus is an Android publish / subscribe event bus, which can simplify the messaging between activities, fragments, threads, services and other components. 3. It can replace traditional solutions such as intent, handler, broadcast and interface. It is faster and smaller. The jar package of about 50K makes the code more elegant and completely decoupled.

GitHub address: https://github.com/greenrobot/EventBus

Eventbus schematic

How to add dependencies

Add in the dependencies tab in the build.gredle file of the module

compile 'org.greenrobot:eventbus:3.0.0'

for example

How to use

Registration event

EventBus.getDefault().register( this );

Unregister

EventBus.getDefault().unregister( this );

send data

Eventbus. Getdefault(). Post ("I launched");

Simple example: passing a simple string using eventbus

Thread model

In the method of receiving event messages, you can set the thread model by annotation. Eventbus has built-in four thread models, namely threadmode.posting, threadmode.main, threadmode.backgroup and threadmode.async

For example:

Postthread: if the thread model is specified as postthread using the event handler function, the event handler function will run in the thread in which the event is published, that is, the event is published and received in the same thread. In the event handling function with the thread model of postthread, try to avoid time-consuming operations, because it will block the transmission of events and even cause ANR.

Mainthread: if the thread model is specified as mainthread using the event handler function, the event handler function will be executed in the UI thread no matter which thread the event is published in. This method can be used to update the UI, but it cannot handle time-consuming operations.

Backgroundthread: if the thread model is specified as backgroundthread using the event handler function, if the event is published in the UI thread, the event handler function will run in the new thread. If the event is originally published in the child thread, the event handler function will be executed directly in the thread that publishes the event. UI update operation is prohibited in this event handler function.

Async: if the thread model is specified as async using the event handler function, the event handler function will be executed in the new child thread no matter which thread the event is published in. Similarly, UI update is prohibited in this event handler function.

Small example 1: sending data in a child thread

Operation results:

D / event launch data thread:: thread-109 E / event backgroundthread: Message: I launched thread: thread-109 E / event postthread: Message: I launched thread: thread-109 E / event async: Message: I launched thread: pool-1-thread-2 E / event mainthread: Message: I launched thread: Main

Small example 2: send data in the main thread

Operation results:

D / event launch data thread:: Main E / event mainthread: Message: I launched thread: Main E / event postthread: Message: I launched thread: Main E / event async: Message: I launched thread: pool-1-thread-3 E / event backgroundthread: Message: I launched thread: pool-1-thread-4

Viscous event

In addition to the common events mentioned above, eventbus also supports sending sticky events. What is a sticky event? In short, you can receive the event by subscribing to the event after sending the event, which is similar to sticky broadcasting. The specific usage is as follows:

register

EventBus.getDefault().register( this );

Event reception

Unregister

EventBus.getDefault().unregister( this ) ;

Send event

Eventbus. Getdefault(). Poststicky ("I launched");

Small example: send events in mainactivity, register and receive events in activity2

Mainactivity source code

Activity2 source code

This is the sticky event, which can receive the message sent before the subscription. However, it can only receive the latest message once. For example, it has sent multiple sticky messages before subscribing, and then subscribing can only receive the latest message.

Eventbus source code analysis

Subscribe interface source code

It can be seen that the default thread model is threadmode. Posting; The default sticky event is false, that is, the sticky event is not enabled by default; The default preference level is 0.

Eventbus class part of the source code

Getdefault () is a singleton pattern with only one instance object.

Threadmode class source code

This class is an enumeration class that defines several types in the thread model.

The above summary of the use of Android eventbus 3.0.0 (must see article) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>