当前位置:网站首页>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{}
边栏推荐
- Customize VIEW to realize in-app message reminder to rotate up and down
- Nature:猪死亡1小时后,器官再次运转
- Glory to the Blue Yonder, speeds up the strategic growth
- Intra-group reverse order adjustment of K nodes
- 两个链表相加
- 2022牛客多校(六)M. Z-Game on grid
- 微信支付开发流程
- The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
- 读写分离后,性能居然提升100%了呀
- win10编译x264库(也有生成好的lib文件)
猜你喜欢
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
26、管道参数替换命令xargs
链表噩梦之一?5000多字带你弄清它的来龙去脉
在“Extend the Omniverse”比赛中构建用于 3D 世界的工具
How to upload local file trial version in binary mode in ABAP report
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
ansible-cmdb friendly display ansible collects host information
中断系统结构及中断控制详解
随机推荐
李开复花上千万投的缝纫机器人,团队出自大疆
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
ThreadLocal的简单理解
使用RecyclerView实现三级折叠列表
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
API调用,API传参,面向对接开发,你真的会写接口文档吗?
一甲子,正青春,CCF创建六十周年庆典在苏州举行
go基础之web获取参数
数字化转型之支撑保障单元
The grep command Shell regular expressions, the three musketeers
900页数学论文证明旋转的黑洞不会爆炸,丘成桐:30多年来广义相对论首次重大突破...
MySQL principle and optimization of Group By optimization techniques
关于Retrofit网络请求URL中含有可变参数的处理
读写分离后,性能居然提升100%了呀
h264 protocol
Intranet penetration tool ngrok usage tutorial
26、管道参数替换命令xargs
Compensation transaction and idempotency guarantee based on CAP components
自定义VIEW实现应用内消息提醒上下轮播
Intra-group reverse order adjustment of K nodes