当前位置:网站首页>使用注解将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{}
边栏推荐
- win10编译x264库(也有生成好的lib文件)
- IDEA close/open reference prompt Usages
- 中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
- Shell之常用小工具(sort、uniq、tr、cut)
- The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
- 西湖大学教授怎么看AI制药革命?|量子位智库圆桌实录
- 两个链表相加
- 发明时代,「幂集创新」事关你我
- World's 4th mad scientist dies on his 103rd birthday
- 用 API Factory 产品生成 API 文档
猜你喜欢
随机推荐
Golang学习之路(五):Golang的函数
脱光衣服待着就能减肥,当真有这好事?
GET请求和POST请求区别
LeetCode #101. 对称二叉树
FFmpeg在win10上编译安装(配置libx264)
OpenSSF的开源软件风险评估工具:Scorecards
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
goalng-sync/atomic原子操作
Recommend a free 50-hour AI computing platform
1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
我们真的需要DApp吗?App真的不能满足我们的幻想吗?
Too much volume... Tencent was asked on the side that the memory was full, what would happen?
Win10 compiles the x264 library (there are also generated lib files)
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
Say goodbye to the AI era of hand looms
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
从零开始Blazor Server(9)--修改Layout
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
Shell正则表达式,三剑客之grep命令
IDEA close/open reference prompt Usages