当前位置:网站首页>Extract EventBus encapsulation to base class using annotations
Extract EventBus encapsulation to base class using annotations
2022-08-09 13:16:00 【Xie Dong_】
Introduction: Today's blog mainly analyzes the encapsulation and extraction of eventbus of version 3.0 and above. The usage of eventbus will not be introduced here. If you don't know that eventbus is aWhat, it is recommended to learn how to use EventBus first. The introductory blog about EventBus on the Internet is bad, so I will not write related introductory blog posts.
As your project gets bigger and bigger, you will always consider that there will be a unified entry for both network requests and Intent jumps, and even message passing will think about extracting an event bus to clear the entireThe architecture of the project, needless to say the benefits of EventBus, it greatly simplifies the process of Android messaging, EventBus makes messaging more concise and more flexible, so we can start today's topic, today's main analysis is to extract EventBus into the base class, about EventBusIt is recommended that readers understand the basic usage first.
Before EventBus3.0, we had to define the methods starting with onEvent, namely onEvent, onEventMainThread, onEventBackgroundThread and onEventAsync. After 3.0, the method name of event processing can be chosen at will, but it needs to be specified by annotation.The working thread of EventBus is convenient for us to customize the event name according to the business scenario, and it is also convenient for the encapsulation and extraction of the event. However, if the unified encapsulation registration and de-registration are not carried out in the process of EventBus, the Activity and Fragement in the project will be unprovoked.There is a lot of redundant code, so my first thought is to manage the registration and anti-registration of EventBus in the base class, and make relevant bindings where EventBus needs to be used.Coders who have done similar business may find that If you directly put the binding of EventBus in the base class and do not judge whether the subclass uses EventBus, then it is useless to use EventBus.Subclasses don't work properly.
Based on this scenario, my first thought is to use annotations to solve, first define an empty classAnnotation, and then bind the annotation in the Activity or Fragment that needs to use EventBus. When the base class is registered, first determine whether the current subclass has bound the annotation of BindEventBus, and then decide whether to register EventBus.
Annotation class BindEventBus.java
/*** desc: Both activit and Fragment that need to use eventbus need to be bound to this by annotation* author: xiedong* date: 2017/10/17*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface BindEventBus {}
Then first determine whether the current subclass is bound with the BindEventBus annotation in the base class of Activity and the class of Fragment, and then decide whether to register and de-register
Register EventBus in BaseActivity.java:
private void initData() {activity = this;initBaseMVP();/ / Determine whether you need to register EventBusif (this.getClass().isAnnotationPresent(BindEventBus.class)) {EventBus.getDefault().register(this);}}
Anti-registration:
@Overrideprotected void onDestroy() {super.onDestroy();if (this.getClass().isAnnotationPresent(BindEventBus.class)) {EventBus.getDefault().unregister(this);}}
The same is true in 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);}// Determine if registration is requiredif (this.getClass().isAnnotationPresent(BindEventBus.class)) {EventBus.getDefault().register(this);}}@Overridepublic void onDestroy() {super.onDestroy();if (this.getClass().isAnnotationPresent(BindEventBus.class)) {EventBus.getDefault().unregister(this);}}
Just add the annotation of BindEventBus to the subclass that needs to use EventBus. The rest of the usage process is the same as using EventBus normally, no need to use the subclass of EventBus without any processing
Use in subclass Activity:
@BindEventBuspublic class ReleaseSettingActivity extends BaseActivity {
Use in Fragment subclasses:
@BindEventBuspublic class ThemeStudycircleFragment extends MListFragment{}
边栏推荐
- 微信小程序支付及退款整体流程
- 2022 Niu Ke Duo School (6) M. Z-Game on grid
- 曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
- 罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
- Apexsqlrecover无法连接数据库
- Scala Advanced (7): Collection Content Summary (Part 1)
- 又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
- AQS同步组件-FutureTask解析和用例
- 用皮肤“听”音乐,网友戴上这款装备听音乐会:仿佛住在钢琴里
- Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
猜你喜欢
Flutter入门进阶之旅(五)Image Widget
AQS Synchronization Component - FutureTask Analysis and Use Cases
数据挖掘-05
MySQL principle and optimization of Group By optimization techniques
在北极都可以穿短袖了,温度飙升至32.5℃
Resolved IndentationError: unindent does not match any oute r indentation Level
已解决IndentationError: unindent does not match any oute r indentation Level
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
Flutter入门进阶之旅(六)Layout Widget
Win10 compiles the x264 library (there are also generated lib files)
随机推荐
二叉树的序列化和反序列化
About the handling of variable parameters in the Retrofit network request URL
Ten minutes to teach you how to use VitePress to build and deploy a personal blog site
随机快排时间复杂度是N平方?
世界第4疯狂的科学家,在103岁生日那天去世了
API调用,API传参,面向对接开发,你真的会写接口文档吗?
一甲子,正青春,CCF创建六十周年庆典在苏州举行
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
使用RecyclerView实现三级折叠列表
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
K个结点的组内逆序调整
#Internet of Things essay#Xiaoxiong pie equipment development actual combat
数字化转型之支撑保障单元
数据挖掘-05
太卷了... 腾讯一面被问到内存满了,会发生什么?
报告:想学AI的学生数量已涨200%,老师都不够用了
Flutter Getting Started and Advanced Tour (2) Hello Flutter
【HCIP持续更新】IS-IS协议原理与配置
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
基于CAP组件实现补偿事务与幂等性保障