Android Event Bus (Androideventbus) framework released

Source: Internet
Author: User

Androideventbus

Androideventbus is an Android platform event bus framework that simplifies interaction between components such as activity, Fragment, service, and so on, greatly reducing the coupling between them, making our code more concise and less coupled, Improve the quality of our code.

Before looking down, you can consider a scenario in which the communication between the two fragment will be achieved.
According to the official Android recommendations, the solution is as follows: Communicating with the activity, the idea is that activity implements an interface, Then the activity is strongly converted to the interface type after the activity on the FRAGMENT-A Association, then the interface is recalled at some point fragment, and then the method in Fragment-b is called from the activity. Is the process a little complicated? If you feel the same way, that's why you keep looking.

Basic structure


Androideventbus is similar to the observer pattern, through the Register function registers the object that needs to subscribe to the event bus, and then finds the subscription method in the object based on the @subcriber annotation, and stores these subscription methods and subscription objects in the map. When a user publishes an event somewhere, the event bus finds the corresponding subscriber object based on the event's parameter type and tag, and finally executes the method in the Subscriber object. These subscription methods are executed in the user-specified threading model, such as Mode=threadmode.async, which indicates that the subscription method is executing on the child thread, and see the instructions below for more details.

Different from the Eventbus of Greenrobot
    1. Greenrobot's Eventbus is a very popular open source library, but it is not friendly to the experience, for example, its subscription function must start with onevent, and if you need to specify the thread that the function is running, add the function name to the execution thread's schema name according to the rules. So it's hard to understand, for example, if I want to execute an event's receive function on the main thread, the function name must be oneventmainthread. What if I have two parameter names in one subscriber and all of them execute the receive function on the main thread? At this time it seemed that it could not be dealt with. Moreover, the function of the function is not well embodied, that is, the function's self-documentation. Androideventbus uses annotations to identify the receive function so that the function name is not restricted, such as I can write the receive function name into updateuserinfo (person info), which makes it much more flexible.
    2. Another difference is that Androideventbus adds an additional tag to identify the tag for each event that the receive function can receive, similar to the action in broadcast, such as one or more actions per broadcast. When you send the broadcast you have to specify the action of the broadcast, and then the corresponding broadcast receiver to receive. Greenrobot's Eventbus simply identifies whether the function can receive an event based on the function parameter type, which causes any event to be received as long as the parameter type is the same. , such a delivery principle is very limited. For example, I have two events, a user-added event, a delete user event, and their parameter types are user, then Greenrobot's eventbus is probably the case:
privatevoidonEventMainThread(User aUser) {    // code }

If you have two receive functions of the same parameter type and are going to be executed in the main thread, then how to name it? Even if you have two functions that meet the requirements, I am actually adding the user's event, but since Eventbus only judges the receive function based on the event parameter type, it will result in two functions being executed. Androideventbus's strategy is to add a tag for each event, and the parameter type and tag collectively identify the uniqueness of an event, ensuring accurate delivery of the event.
This is Androideventbus and Greenrobot eventbus difference, but because I greenrobot eventbus not very understanding, it is possible that I said the wrong, if so, you are welcome to point out.

Androideventbus at first just to learn, but after learning the realization of eventbus, found that it in the use of some inconvenience, I think since I have these feelings, should also feel the same people, in the development group after the communication, found that there is indeed such a situation. As a result, the formal Androideventbus is launched in the form of open source libraries, hoping to help people in need. Of course, the growth of this library needs everyone's support and testing, we welcome you to pull request. If you need a relatively stable library, Greenrobot's Eventbus and square Otto are a great choice.

Using Androideventbus

You can follow these steps to use Androideventbus.

    • 1 Registering an event receive object
 Public  class youractivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.main_activity);//Registered objectEventbus.getdefault (). Register ( This); }@Override    protected void OnDestroy() {//Do not forget to LOGOUT!!!! Eventbus.getdefault (). Unregister ( This);Super. OnDestroy (); }}
    • 2 using subscriber annotations to identify the Receive method in an event receive object
 Public  class youractivity extends Activity {    //code ...    //Receive method, default tag, execution on UI thread    @Subcriber    Private void UpdateTime(String time) {LOG.E ("","# # # Update time ="+ time); }//contains My_tag, when the user post event, only events that have the "My_tag" specified will trigger the function, executing in the UI thread    @Subcriber(tag ="My_tag")Private void Updatetimewithtag(String time) {LOG.E ("","# # Update time with My_tag, time ="+ time); }//contains My_tag, when the user post event, only events that have the "My_tag" specified will trigger the function .    On which thread the//Post function executes, which thread the function executes on    @Subcriber(tag ="My_tag", Mode=threadmode.post)Private void Updatetimewithmode(String time) {LOG.E ("","# # Update time with My_tag, time ="+ time); }//contains My_tag, when the user post event, only events that have the "My_tag" specified will trigger the function, executed on a separate thread    @Subcriber(tag ="My_tag", mode = Threadmode.async)Private void Updatetimeasync(String time) {LOG.E ("","# # # Update Time async, time ="+ Time +", Thread name ="+ Thread.CurrentThread (). GetName ()); }}

The receive function uses tag to identify the type of event that can be received, the same as the action specified in Broadcastreceiver, so that the message is delivered accurately. mode can specify which thread the target function executes on, and by default it executes on the UI thread, allowing the user to update the UI. When the target method executes a time-consuming operation, you can set mode to async so that it executes in a child thread.

    • 3 Publishing events in other components, such as activity, Fragment,service
    EventBus.getDefault().post("what‘s the time now ?");    // post a event with tag, the tag is like broadcast‘s action    EventBus.getDefault().post(new"my_tag");

After the event is published, an object that is registered for the event type receives the response event.

GitHub links

Welcome to star and fork, Androideventbus frame.

Android Event Bus (Androideventbus) framework released

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.