当前位置:网站首页>使用注解将EventBus封装抽取到基类
使用注解将EventBus封装抽取到基类
2022-08-09 12:04:00 【谢栋_】
引题:今天这篇博客主要分析的对3.0以上版本的eventbus进行封装抽取,关于eventbus的使用方法在此就不做介绍了,如果你还不了解eventbus是个什么东东,建议先学习下EventBus的使用方法,网上关于EventBus的入门博客烂大街了,我就不写相关的介绍性博文了。
随着你的项目越做越大,你总是会考虑着不论是网络请求还是Intent跳转都会有一个统一的入口,即便是消息传递也会想着抽取出一个事件总线来清晰整个项目的架构,对于EventBus好处自不用说,它大大简化了安卓消息传递的流程,EventBus让消息传递更加简洁更加灵活,好开始今天的话题,今天主要分析把EventBus抽取到基类中,关于EventBus的基本用法建议读者先自行去了解。
在EventBus3.0之前我们必须定义以onEvent开头的那几个方法,分别是onEvent、onEventMainThread、onEventBackgroundThread和onEventAsync,而在3.0之后事件处理的方法名可以随意取,不过需要通过注解的方式指定EventBus的工作线程,便于我们根据业务场景自定义事件名称,也便于对事件的封装抽取,但是在EventBus的过程中如果不进行统一封装注册跟反注册的话,在项目中的Activity跟Fragement中会无端多出来好多冗余代码,所以我的第一想法就是把EventBus的注册跟反注册放在基类去管理,在需要使用EventBus的地方进行相关绑定,不需要使用的地方不用做任何处理,如果做过类似业务的coder可能会发现如果直接把EventBus的绑定放在基类不去做判断子类是否用到EventBus,那么没用用到EventBus的子类是不能正常工作的。
基于该场景我首先想到的是使用注解来解决,首先定义一个空的类注解,然后在需要使用EventBus的Activity或者Fragment中绑定该注解,在基类注册的时候先判断当前子类是否已经绑定了BindEventBus的注解,然后再决定是否需要注册EventBus。
注解类BindEventBus.java
/**
* desc:需要使用eventbus的activit和Fragment都需要以注解的方式绑定到此
* author:xiedong
* date:2017/10/17
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BindEventBus {
}
然后在Activity的基类跟Fragment的类中先判断当前子类是否绑定了BindEventBus注解,然后再决定是否进行注册跟反注册
在BaseActivity.java注册EventBus:
private void initData() {
activity = this;
initBaseMVP();
//判断是否需要注册EventBus
if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
EventBus.getDefault().register(this);
}
}
反注册:
@Override
protected void onDestroy() {
super.onDestroy();
if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
EventBus.getDefault().unregister(this);
}
}
在Fragment中同理:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = (BaseActivity) getActivity();
fragment = this;
presenter = TUtil.getT(this, 0);
model = TUtil.getT(this, 1);
if (this instanceof BaseView) {
presenter.setContext(activity);
presenter.setVM(this, model);
}
//判断是否需要注册
if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
EventBus.getDefault().register(this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
EventBus.getDefault().unregister(this);
}
}
在需要使用EventBus的子类中只需加上BindEventBus的注解即可,剩下的使用过程跟正常使用EventBus一样,不需要使用EventBus的子类不用做任何处理
在子类Activity中使用:
@BindEventBus
public class ReleaseSettingActivity extends BaseActivity {
在Fragment子类中使用:
@BindEventBus
public class ThemeStudycircleFragment extends MListFragment{}
边栏推荐
- Rust从入门到精通04-数据类型
- The redis library cannot be imported
- 阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
- Simple understanding of ThreadLocal
- Intranet penetration tool ngrok usage tutorial
- 世界第4疯狂的科学家,在103岁生日那天去世了
- 2022牛客多校(六)M. Z-Game on grid
- Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers
- 张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
- 在北极都可以穿短袖了,温度飙升至32.5℃
猜你喜欢
GRPC整体学习
一甲子,正青春,CCF创建六十周年庆典在苏州举行
MySQL 原理与优化,Group By 优化 技巧
【无标题】
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
ThreadLocal的简单理解
Resolved IndentationError: unindent does not match any oute r indentation Level
00后写个暑假作业,被监控成这笔样
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
随机推荐
Flutter入门进阶之旅(十)Dialog&Toast
告别手摇织布机的AI时代
HAproxy: load balancing
智驾科技完成C1轮融资,此前2轮已融4.5亿元
微服务架构的核心关键点
The FFmpeg library is configured and used on win10 (libx264 is not configured)
#物联网征文#小熊派设备开发实战
发明时代,「幂集创新」事关你我
内网穿透工具ngrok使用教程
Intranet penetration tool ngrok usage tutorial
已解决IndentationError: unindent does not match any oute r indentation Level
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
【Untitled】
全面了解什么是TPS、QPS以及两者的区别
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
#Internet of Things essay#Xiaoxiong pie equipment development actual combat
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
goalng-sync/atomic原子操作
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
十分钟教会你如何使用VitePress搭建及部署个人博客站点