Getting started with EventBus and its use

EventBus introduction:

     EventBus comes from greenrobot, which comes from the same company as the famous GreenDao before. I have been using version 2.4 before. Today we will learn to analyze the latest Event 3.0. The latest feature of EventBus 3.0 is to add annotations to inform which thread the subscription function runs in.

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

     Official documentation: http://greenrobot.org/eventbus/documentation

EventBus main roles:

  •  Event object passed by Event
  •  Subscriber event subscriber 
  •  Publisher event publisher
  •  ThreadMode defines in which thread the function executes

  The collaboration diagram of various roles given by the official website

  • The bottom layer of EventBus uses annotations and reflections to obtain subscription method information (the first is to obtain annotations, if the annotations cannot be obtained, then reflection is used)
  • The current subscriber is added to the subscriptionByEventType collection of Eventbus total event subscribers
  • All subscribed event types of subscribers are added to typeBySubscriber to facilitate the removal of events when unregistering

EventBus function: By decoupling publishers and subscribers to simplify Android event delivery, EventBus can replace Android 's traditional Intent, Handler, Broadcast or interface functions, pass data between Fragment, Activity, and Service threads, and execute methods. 

EventBus features The code is concise, and it is a publish-subscribe design pattern (observer design pattern).

EventBus example:

  1. Associated EventBus framework
Add the following dependencies in Android Studio:

compile 'org.greenrobot:eventbus:3.0.0'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772889&siteId=291194637