Android event bus and android Bus

Source: Internet
Author: User
Tags eventbus

Android event bus and android Bus

In Android, communication between Activity, Service, and Fragment is troublesome. The main methods are as follows:

(1) When broadcast is used, the sender sends a broadcast, and the receiver receives the broadcast for processing;

(2) Use Handler and Message. For example, after the download thread completes the download task, it sends a Message to the UI. After the UI receives the Message, it updates the interface.

However, these methods are cumbersome. A simple and effective method is to use the event bus. The basic principle of the event bus is that the sender sends the message to the event bus, and then the event bus finds in the receiver which method registers the event. If a method registers the event, this method is triggered. In this way, the communication between components is much easier than other methods, and the code is more concise.

1. Download simple_eventbus.jar, import the Android project, and import the following two classes in the component that needs to send or receive messages:

import org.simple.eventbus.EventBus;import org.simple.eventbus.Subcriber;

2. Create two new activities: MainActivity and Activity2. MainActivity is the message receiver and Activity2 is the message sender. Because both activities use the event bus, you must register EventBus in onCreate (Bundle savedInstanceState:

EventBus.getDefault().register(this);

You need to log out EventBus in onDestroy:

EventBus.getDefault().unregister(this);

3. Create a New Button in Activity2. The onClick () method is as follows:

@ Override public void onClick (View v) {switch (v. getId () {case R. id. btnRun2: num ++; txtStatus. setText ("click" + num + "times! "); EventBus. getDefault (). post (new Object ()," my_tag "); break; default: break ;}}

Each time you click a Button, the EventBus. getDefault (). post () method is used to send a message to EventBus. EventBus. getDefault (). post () has two parameters: the first parameter is the Object passed to the event receiver, because we do not need to pass the Object to the sender, so only one Object is new; the second parameter is the tag, which is equivalent to the action in the broadcast. Only the method registered with the tag in the receiver will be triggered, and the method without registering the tag will not be triggered.

4. Create a method to receive messages in MainActivity:

@ Subcriber (tag = "my_tag") private void updateUI (Object o) {num ++; txtStatus. setText ("Activity 2 clicked" + num + "times! "); Log. e (" num = ", String. valueOf (num ));}

The @ Subcriber (tag = "my_tag") annotation indicates that this method is a message receiving method. tag = "my_tag" indicates that this method registers the message "my_tag, this method is triggered when the message is received.

5. Running result. Go to Activity 2, 6 times and click Button:

Return to MainActivity, and you can see that updateUI () is triggered, showing the number of clicks of Activity 2:

 

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.