当前位置:网站首页>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{}
边栏推荐
- About the handling of variable parameters in the Retrofit network request URL
- 字符串转换整数 (atoi)
- Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
- Intra-group reverse order adjustment of K nodes
- The new features of ABP 6.0.0 - rc. 1
- 8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
- 【微服务~远程调用】整合RestTemplate、WebClient、Feign
- 关于Retrofit网络请求URL中含有可变参数的处理
- The grep command Shell regular expressions, the three musketeers
- Go Affair, How to Become a Gopher and Find a Go Job in 7 Days, Part 1
猜你喜欢
随机推荐
ansible-cmdb friendly display ansible collects host information
#物联网征文#小熊派设备开发实战
FFmpeg在win10上编译安装(配置libx264)
Scala Advanced (7): Collection Content Summary (Part 1)
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
Simple encapsulation of glide tool class
基于STM32+铂电阻设计的测温仪
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
Shell正则表达式,三剑客之grep命令
win10编译x264库(也有生成好的lib文件)
微信支付开发流程
MongoDB-查询中$all的用法介绍
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
链表噩梦之一?5000多字带你弄清它的来龙去脉
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
Fragment中嵌套ViewPager数据空白页异常问题分析
Resolved IndentationError: unindent does not match any oute r indentation Level