EventBus is easy to use.

Source: Internet
Author: User
Tags eventbus

EventBus is easy to use.
What is EventBus contributed by the greenrobot organization (the organization also contributed greenDAO), an Android event release/subscription Lightweight Framework, function: simplify Android event transmission by decoupling publishers and subscribers, eventBus can replace the traditional Android Intent, Handler, Broadcast or interface functions, and transmit data and execute methods between Fragment, Activity, and Service threads. Features: the code is concise, and it is a publishing and subscription design mode (observer design mode ). How to Use

First, introduce

compile 'org.greenrobot:eventbus:3.0.0'

 

EventBus has three common functions.

1. register events. The registered classes can accept message events sent by EventBus.

EventBus.getDefault().register(this);

2. cancel registration

EventBus.getDefault().unregister(this);

3. Send a message and call this method in any activity or service to transmit data. All classes that have registered an event can receive the message. The parameter is of the object type and is forced conversion in the event processing function.

EventBus. getDefault (). post ("hello ");

4. event processing function, which is used to process post-transmitted data in the class that has registered EventBus. Function names can be arbitrary. You only need to declare them with annotations (only EventBus3.0 and 3.0 support annotations. The previous functions are fixed functions). The threadMode parameter is explained below.

@Subscribe(threadMode = ThreadMode.MAIN)    public void XXX(String str){        Log.d("LoginActivity",str);    }

 

Event processing functions

1. parameter description. threadMode has four parameters in total to identify where the function runs, such as the main thread or subthread.

    • POSTING (default): If the thread model specified by the event handler is POSTING, the event handler will run in the thread where the event is released, that is to say, the publishing event and the receiving event are in the same thread. In the event processing function whose thread model is POSTING, try to avoid time-consuming operations because it will block event transmission and may even cause ANR. MAIN:
    • MAIN: event processing is executed in the UI thread. The event processing time cannot be too long. It will be ANR for a long time.
    • BACKGROUND: if an event is released in the UI thread, the event processing function runs in the new thread. If the event is released in the Child thread, the event handler function is executed directly in the thread where the event is published. UI update is prohibited in this event handler function.
    • ASYNC: No matter which thread the event is released, the event handler function is executed in the newly created child thread. Similarly, the event handler function prohibits UI update. This mode should be used for time-consuming operations

2. Event priority. In the same thread mode, high-priority subscribers receive events earlier than low-priority subscribers. EventBus has a default priority of 0. After processing a high-priority event, you can use cancelEventDelivery (object) to block message propagation.
Note: The Priority does not affect the sequence of events received by subscriber in different thread modes.

@ Subscribe (threadMode = ThreadMode. MAIN, priority = 1)
Public void XXX (object o ){
// Processing logic
// Block transfer
CancelEventDelivery (o );
}

 

Sticky events

To put it simply, a sticky event is to register EventBus in a class after a sticky message is published, and declare a sticky event processing function to receive any previously released sticky events.

1. Release events. Note that postSticky is used here.

EventBus.getDefault().postSticky("Hello everyone!");

2. Register Eventbus, as before

EventBus.getDefault().register(this);

3. process the message. Note that sticky: true is declared in the annotation parameter, indicating that the event can accept sticky events.

  

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)public void onEvent(String event) {    Log.d("EventBus",event);}

 

Related Article

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.