EventBus的一些记录

之前项目中用到过EventBus,之前只是单纯的用,最近在项目中再一次拿出来,就记录一下吧,当复习了。看下面的GIF,大致的表达了EventBus的工作形式。

简介

借Github上面一张图来看一下EventBus是怎么工作的吧,

Eventbus是一个事件优化管理平台,可以将它想像成Android BroadCast&BroadcastReceiver的轻量版,以事件驱动的方式来简化事件处理逻辑,平时开发是以callback或handler来达到类似效果。如果物件之间传递经过太多其他物件;或是一个信息经过太多物件,EventBus将会是一个不错的选择。优点是开销小,代码更优雅。以及将发送者和接收者解耦。

使用场景

我们试想一个简单的场景,在Profile页面,我们想修改我们的个人资料,进入到修改资料页面,修改成功返回到Profile页面,我们要将修改成功的资料显示到当前页面,一种是在Profile页面onResume方法中重新进行一次网络请求,另一种就是用事件传递,我们今天讲的,是第一种用户体验好,还是第二种体验好,可想而知,这只是一个简单的使用场景,我们还可以跨页面进行事件传递等等更深奥的用法。

如何使用

1. gradle中 compile 'org.greenrobot:eventbus:3.0.0'

2. 定义自己的事件

1
2
3
4
5
6
7
8
9
public class WhateverEvent { 
/*事件可任意String, int, Object...*/
private String message
public WhateverEvent(String message){
this.message = message;
}
public String getMessage(){
return message;
}

3. 注册,反注册EventBus

getDefault()为EventBus中的单例模式

1
2
3
4
5
6
7
8
9
10
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}

也可以建立自定义的EventBus。
注册,反注册的代码是这样的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MainActivity extends AppCompatActivity {
...
@Override
protected void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
@Override
protected void onPause() {
super.onPause();
EventBus.getDefault().unregister(this);
}
...
}

记得写unregister(),不然会内存泄露(memory leak)。

4.订阅事件(接收消息)

共有四个函数,各功能不同,这里先写其中的一个。

1
2
3
4
5
6
7
8
9
public class MainActivity extends AppCompatActivity {
...
@Subscribe
public void onEvent(WhateverEvent event) {
/*处理事件*/
Log.i("TAG", event.getMessage());
};
...
}

onEvent() 的paremeter为订阅的事件,也就我们的第二步中我们写的。

5.传送事件(发送消息)

这个就很简单了eventBus.post(new AnyEventType event);
大致的介绍了一遍,不知道你们晕了吗,那么上面的GIF图用代码实现,应该就很简单了吧。就是两个Activity,每一个Activity里面又一个Button,然后进行事件的传递。由于我们是在第一个页面点击按钮进入到第二个页面,那么我们在第一个页面注册以及反注册,然后在第二个页面post消息就好了。完整的代码奉上

MainActivity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MainActivity extends AppCompatActivity {
TextView tv_event_info;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册eventbus
EventBus.getDefault().register(this);
tv_event_info = (TextView) findViewById(R.id.text);
}

public void startTo(View v) {
startActivity(new Intent(this, SecondActivity.class));
}

@Override
protected void onDestroy() {
super.onDestroy();
//反注册EventBus
EventBus.getDefault().unregister(this);
}

@Subscribe
public void onEventMainThread(EventInfo event) {
String msg = "onEventMainThread收到了消息:" + event.getInfo();
tv_event_info.setText(msg);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}

SecondActivity:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}

public void gotoFirst(View v) {
EventBus.getDefault().post(new EventInfo("这是EventBus给你的"));
finish();
}
}

EventInfo:

1
2
3
4
5
6
7
8
9
10
11
public class EventInfo {
private String mInfo;

public EventInfo(String info) {
mInfo = info;
}

public String getInfo() {
return mInfo;
}
}

其中的onEvent()我写的是onEventMainThread(),这里onEvent()可替换成 onEventMainThread(), onEventBackgroundThread(), onEventAsync()来指定要在哪个Thread执行。
Sticky event﹔事件发送完成不会马上消失 ,任何注册sticky event的class可以马上收到最后一则sticky event。
如果你要进行代码混淆,记得在你的proguard.cfg中加入

1
2
3
 -keepclassmembers class ** {
public void onEvent*(**);
}

相关文章推荐:

Android解耦库EventBus的使用和源码分析
EventBus老家Github
最近EventBus3更新了,来看看腾讯的老司机是怎么飙EventBus3的吧

版权声明:



除非注明,本博文章均为原创,转载请以链接形式标明本文地址。

坚持原创技术分享,您的支持将鼓励我继续创作!