Design and implementation of the Android event bus (Eventbus)

Source: Internet
Author: User
Tags eventbus

1. Function Introduction

Androideventbus is an Android platform event Bus library that simplifies the interaction between activity, Fragment, service and other components or objects, greatly reducing the coupling between them, making our code more concise and less coupled, Improve the quality of our code.

Androideventbus absorbed the advantages of Greenrobot's Eventbus and square Otto, and made corresponding improvements on this basis, making the event bus framework more suitable for users ' usage habits, and making the delivery of events more precise and flexible.

The difference between Eventbus and Otto
name is the subscription function executable in other threads features
greenrobot eventbus is Use the name pattern mode, which is highly efficient but inconvenient to use.
square Otto no use annotations, easy to use, but less efficient than eventbus.
androideventbus is use annotations, easy to use, but less efficient than eventbus. The subscription function supports tag (like the action of the broadcast receiver) to make the event delivery more accurate and adaptable to more usage scenarios.

For more details, please refer to the Androideventbus, Android event bus (androideventbus) Open Source Library release.

2. Overall design

Androideventbus simple overall design as shown, mainly divided into three parts, namely Eventbus, Subscribermethodhunter, EventHandler.

Eventbus is responsible for managing subscription objects and events, such as registering, Unregistering, and publishing events. When an object is initially registered in Eventbus, Eventbus iterates through all the methods in the object class, manages the number of parameters 1 and uses the function subscriber annotation identification. Build a EventType as an event type with the tag of the event type and subscription function subscriber, and an event type pair should have a list of recipients. When an event is published, Eventbus builds eventtype with the tag based on the event type published, then finds the corresponding list of subscribers and posts the events to all subscribers. Subscribermethodhunter is responsible for locating the appropriate eventtype, and EventHandler is responsible for executing these subscription functions into the appropriate thread. At this point, the entire event bus operation process is complete, of course, when the activity, fragment and other components registered to Eventbus, do not forget to remove them from the Eventbus when these objects are destroyed, that is, call the unregister method.

3. Flowchart Registration Receiver Simple flowchart

Publish Event Simple flowchart

4. Detailed design 4.1 Core class detailed introduction
  • Eventbus: The core class of event bus, which encapsulates the registration, logout, and the release and delivery of the events, is the core class of the events buses.
  • EventType: Event type Object, consisting of the event class type and the receive function tag, used to identify an event type;
  • Subscriber:subscriber annotations, which are used on functions, are used to identify this function as an event subscription function;
  • Threadmode: The execution threading model of the receive function, which is executed by default on the UI thread;
  • Subscription: A wrapper class for a subscription object that contains the receiving object, the target method, and the threading model;
  • Targetmethod: Target subscription method;
  • Subscibermethodhunter: The subscription method finds the auxiliary class, finds the EventType list according to the event object, and Eventbus posts the event to subscribers based on the returned list;
  • EventHandler: Event handler;
  • Defaulteventhandler: The default event handler, which is the thread in which the event is posted, the processing event is also on that thread;
  • Uithreadeventhandler: Event handling in the UI thread of the handler;
  • Asynceventhandler: Event handling on a separate thread of the handler;
  • Matchpolicy: Event matching policy interface;
  • Defaultmatchpolicy: The default matching policy, when the event is published, Eventbus looks for the function that the parameter is the parent of the event, and constructs the corresponding eventtype;
  • Strictmatchpolicy: Strict matching policy, when publishing an event, only the subscription function that exactly matches the parameter type is found;
Class 4.2 diagram

5. Talk

The basic structure and design of the event bus are similar, but everyone has made a choice between various feature or enhanced some functions, so that it can meet the specific needs of users. The basic features of several event bus libraries have been given roughly in Chapter 1. You can choose according to your own needs.

In this article we have made a brief introduction to Androideventbus, in the following article we will carry out a detailed introduction, please look forward to.



From for notes (Wiz)

Design and implementation of the Android event bus (Eventbus)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.