EventBus components communication tool [entry], eventbus entry

Source: Internet
Author: User

EventBus components communication tool [entry], eventbus entry
I. Overview

EventBus is a release/subscription event bus optimized for Android. The main function is to replace Intent, Handler, and BroadCast to transmit messages between Fragment, Activity, Service, and threads. The advantage is that the overhead is small and the code is more elegant. And decouple the sender and receiver.

For example, communication between multiple layers of Fragment is troublesome. If custom broadcast is used repeatedly, the performance of the software may decrease, the emergence of EventBus can solve this problem very well. You can use it to facilitate communication between components without occupying too much memory, only a few lines of code can be easily solved.
1. Download The EventBus class library
Source code: https://github.com/greenrobot/EventBus

Use the source code or jar package in eclipse

Do not hesitate to Gradle android studio

2. Basic use

(1) define a class, which can be an empty class, for example:

[Java]View plaincopy
  1. Public class AnyEventType {
  2. Public AnyEventType (){}
  3. }

(2) register on the page where you want to receive messages:

[Java]View plaincopy
  1. EventBus. register (this );

(3) send messages

[Java]View plaincopy
  1. EventBus. post (new AnyEventType event );

(4) page implementation for receiving messages (there are four functions with different functions. This is one of them and can be implemented selectively. Here we first implement ONE ):

[Java]View plaincopy
  1. Public void onEvent (AnyEventType event ){}
(5) cancel registration
[Java]View plaincopy
  1. EventBus. unregister (this );
The order is such an order that can be written by yourself. It is estimated that the order is still in the fog. The following is an example.

First, in EventBus, the method for getting an instance is generally EventBus. getInstance () is used to obtain the default EventBus instance. Of course, you can create one instance after another. I personally think it is better to use the default instance to prevent other problems.


Demo


First, describe the Demo. There is a button in the first interface. Click to enter the second interface. The second interface also has a button, click the button on the second interface to send the information to the first interface (intent is not used to pass the value here, intent can not jump), and process the information sent on the first interface.


The layout is very simple and will not be pasted out.


MAinACtivity

<Span style = "color: #333333;"> package com. example. zhuoyou. eventbus; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. toast; import com.example.zhuoyou.eventbus.com. example. zhuoyou. eventbus. domain. stringEvent; import de. greenrobot. event. eventBus; public class MainActivity extends Activity implements View. onClickListener {private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // register EventBus. getDefault (). register (this); button = (Button) findViewById (R. id. go); button. setOnClickListener (this) ;}@ Override public void onClick (View v) {Intent intent = new Intent (MainActivity. this, SecondActivity. class); startActivity (intent) ;}@ Override protected void onDestroy () {EventBus. getDefault (). unregister (this); super. onDestroy (); // cancel EventBus registration} // process events from the second interface </span> <span style = "color: # ff6666; "> public void onEventMainThread (StringEvent event) {String msg = event. getMsg (); Toast. makeText (MainActivity. this, msg + "onEventMainThread", Toast. LENGTH_LONG ). show (); Log. I ("onEventMainThread", msg) ;}</span> <span style = "color: #333333;" >}</span>

SecondActivity


Package com. example. zhuoyou. eventbus; import android. app. activity; import android. support. v7.app. actionBarActivity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. button; import android. widget. toast; import com.example.zhuoyou.eventbus.com. example. zhuoyou. eventbus. domain. stringEvent; import java.net. httpURLConnection; import de. greenrobot. event. eventBus; public class SecondActivity extends Activity implements View. onClickListener {private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_second); button = (Button) findViewById (R. id. return); button. setOnClickListener (this) ;}@ Override public void onClick (View v) {<span style = "white-space: pre"> </span> // send event EventBus. getDefault (). post (new StringEvent ("I Am a parameter from SecondACtivity"); }}< strong> </strong>

Custom Event subscription class (as needed)

Package com.example.zhuoyou.eventbus.com. example. zhuoyou. eventbus. domain;/*** Project name: My Application * package name: com.example.zhuoyou.eventbus.com. example. zhuoyou. eventbus. domain * Author: flyou * Creation Time: 15/7/24 * Description: Event subscription class */public class StringEvent {private String Msg; public String getMsg () {return Msg ;} public void setMsg (String msg) {Msg = msg;} public StringEvent (String msg) {Msg = msg ;}}


In the code, you need to register in oncreat, then cancel the listener in the onDestory method, and use onEventMainThread to process the incoming events.







Today, I will briefly introduce how to use EventBus. Next time I will go back to the details of EventBus for further research.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.