Example of sending messages using eventbus for Android

1、 Overview

Eventbus is a publish / subscribe event bus optimized for Android. The main function is to replace intent, handler and broadcast to pass messages between fragments, activities, services and threads. The advantages are less overhead and more elegant code. And decouple the sender and receiver.

1. Download the class library of eventbus

Source code: eventbus_ jb51.rar

2. Basic use

(1) Customize a class, which can be empty, such as:

(2) Register on the page to receive the message:

(3) Send message

(4) Page implementation of receiving messages (there are four functions with different functions. This is one of them, which can be implemented selectively. Here, implement one first):

(5) Deregister

The sequence is such a sequence, but if you really let yourself write, it is estimated that it is still in the clouds. Here is an example to illustrate.

First of all, in eventbus, the method of obtaining instances is generally to use eventbus. Getinstance() to obtain the default eventbus instances. Of course, you can also new one after another. Personally, it's better to use the default ones to prevent errors.

2、 Actual combat

Let me show you an example:

When hitting BTN_ When you click the try button, you will jump to the second activity. When you click the first event button above the second activity, you will send a message to the first activity. When the first activity receives the message, it will display the message toast and textview on the one hand.

Follow the steps below to build the project:

1. Basic framework construction

You must be able to write the program for jumping from one activity to the second activity. Here, build the code for jumping from two activities. Add eventbus related gadgets later.

Mainactivity layout (activity_main. XML)

Create a new activity, secondactivity layout (activity_second. XML)

Mainactivity.java (click BTN to jump to the second activity)

At this point, the basic framework is completed. Next, start using eventbus step by step.

2. Create a new class firstevent

This class is very simple. A string is passed in during construction, and then it can be obtained through getmsg ().

3. Register the eventbus on the page to receive the message:

In the above GIF image demonstration, you can also see that we want to receive messages in mainactivity, so we register messages in mainactivity.

Through, we will register eventbus in oncreate() function and de register in ondestroy() function. Therefore, the overall registration and de registration codes are as follows:

4. Send message

The post method in eventbus is used to send messages. The past messages sent are instances of our new class!

The complete code of secondactivity.java is as follows:

5. Receive message

When receiving messages, we use oneventmainthread() function, which is the most commonly used function in eventbus, to receive messages. We will talk about why to use this function in the next chapter. Here we will give you a preliminary understanding. We should be able to use eventbus first.

Override oneventmainthread (firstevent event) in mainactivity. The parameter is the class defined by ourselves:

After receiving the event instance, we take out the messages carried in it. On the one hand, toast is sent out, and on the other hand, it is sent to textview;

The complete mainactiviy code is as follows:

Well, at this point, we have basically used eventbus. In the next chapter, we will talk about several functions of eventbus and how to identify which function to call at present.

Source address: eventbus_ jb51.rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>