EventBus:

An Android event publish/subscribe lightweight framework features: Simplifying Android event delivery by decoupling publishers and subscribers EventBus can replace Android’s traditional Intent,Handler,Broadcast, or interface functions In fragments, the Activity, Service to transfer data between threads, the execution method. Features: Simple code, is a publish and subscribe design pattern (observer design pattern).

EventBus usage scenario

  • Used for communication between threads instead of handlers or for communication between components instead of intents
  • It is widely used in group purchase, shopping mall, social networking and other applications
  • EventBus has proven to be integrated into more than 100 million apps

EventBus advantage

  • Simplifies communication between components.
  • Separates the sender and receiver of the event.
  • Works well with activities, fragments, and threads.
  • Complex and error-prone dependencies and lifecycle issues are avoided.
  • It makes the code cleaner and the performance better.
  • Faster and smaller (about 50K JAR packages).

The principle diagram of the EventBus

  • EventBus uses annotations and reflection to retrieve subscription method information (annotation fetch first, reflection if annotation not available).
  • The current subscriber is added to the subscriptionByEventType collection of total Eventbus subscribers
  • All subscriber event types are added to typeBySubscriber so that events can be removed when unregistered

Tip: Reflection is more expensive and inefficient than annotations

Basic use of EventBus

Select Modules and click “Dependencies”, click “+”, select Library Dependency, enter EventBus in the input box, select GreenRobot to associate with EventBus. Register an Activity with OnCreate and unregister it with OnDestroy.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1. Register EventBus with context. Greenrobot org.greenRobot. // Unregister the EventBus by OnDestroy. Eventbus.getdefault ().register(mainActivity.this); eventbus.getDefault ().register(MainActivity. } Override protected void onDestroy() {eventbus.getDefault ().unregister(mainactivity.this); super.onDestroy(); }Copy the code

(3) Create the EventBus message class and set its properties.

public class MessageEvent { private String message; public MessageEvent(String message) { this.message = message; } public String getMessage() { return message; }}Copy the code

(4) Use EventBus Post to send events

Eventbus.getdefault ().post(new) eventBus.getDefault ().post(new EventBusMessage(" This is the message from the second Activity ");Copy the code

EventBus (EventBus, EventBus, EventBus, EventBus, EventBus, EventBus, EventBus, EventBus) Threadmode. MAIN: Threadmode. MAIN indicates that the method is executed in the MAIN thread (suitable for asynchronous loading, you can load data from child threads directly into the UI).

@Subscribe(threadMode = ThreadMode.MAIN) public void MessageEventBus(EventBusMessage eventBusMessage){ Tv_title.settext (eventBusMessage.message); // Display the received Message in TextView. Log.d("eventBusThread","ThreadMode.MAIN "+Thread.currentThread().getName()); Subscribe(ThreadMode = threadmode.posting) public void Subscribe(ThreadMode = threadmode.posting) public void Subscribe(ThreadMode = threadmode.posting) public void Subscribe() MessageEventBus1(EventBusMessage eventBusMessage){ Log.d("eventBusThread","ThreadMode.POSTING "+Thread.currentThread().getName()); } /* Threadmode. ASYNC is also used for background execution (i.e. sub-thread execution), which can be performed asynchronously and concurrently. */ @subscribe (threadMode = threadmode.async) public void */ Subscribe(threadMode = threadmode.async) public void MessageEventBus2(EventBusMessage eventBusMessage){ Log.d("eventBusThread","ThreadMode.ASYNC "+Thread.currentThread().getName()); } // threadmode. BACKGROUND indicates that the method runs in the BACKGROUND (i.e. in child threads) and cannot be processed concurrently. // If the publisher is in the child thread, the method is executed in the child thread. @Subscribe(threadMode = ThreadMode.BACKGROUND) public void MessageEventBus3(EventBusMessage eventBusMessage){ Log.d("eventBusThread","ThreadMode.BACKGROUND "+Thread.currentThread().getName()); }Copy the code

Posting Main thread Posting main thread Posting main thread background Async new child thread async new child thread

EventBus Used for sticky events

Concept: Sticky events are those cached within EventBus. [2] Principle :EventBus saves the last sent event — sticky — for each class type. Subsequent sticky events of the same type that are sent will automatically replace the previously cached events. When a listener registers with EventBus, it requests cached events. In this case, cached events are immediately and automatically sent to the listener with a certain amount of latency. Usage scenarios: Complex issues of passing data across the Activity and Fragment lifecycle on Android, asynchronous calls, etc

Sticky use of EventBus :(the environment setup of EventBus is consistent with the basic use of EventBus)

Share with you

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day