当前位置:网站首页>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{}
边栏推荐
- FFmpeg在win10上编译安装(配置libx264)
- FFmpeg库在win10上配置使用(不配置libx264)
- Intra-group reverse order adjustment of K nodes
- Flutter入门进阶之旅(五)Image Widget
- 链表噩梦之一?5000多字带你弄清它的来龙去脉
- 数据挖掘-06
- The grep command Shell regular expressions, the three musketeers
- The core key points of microservice architecture
- 8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
- Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
猜你喜欢
【无标题】
h264 protocol
API调用,API传参,面向对接开发,你真的会写接口文档吗?
Flutter入门进阶之旅(四)文本输入Widget TextField
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
How to upload local file trial version in binary mode in ABAP report
十分钟教会你如何使用VitePress搭建及部署个人博客站点
随机推荐
注释、关键字、标识符的区别你知道吗?
WebView注入Js代码实现大图自适应屏幕点击图片预览详情
【Untitled】
微服务架构的核心关键点
Common gadgets of Shell (sort, uniq, tr, cut)
Simple understanding of ThreadLocal
Reading and writing after separation, performance were up 100%
基于CAP组件实现补偿事务与幂等性保障
我们真的需要DApp吗?App真的不能满足我们的幻想吗?
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
How should the acceptance criteria for R&D requirements be written?| Agile Practices
MySQL 原理与优化,Group By 优化 技巧
WebView injects Js code to realize large image adaptive screen click image preview details
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
Rust从入门到精通04-数据类型
1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
Fragment中嵌套ViewPager数据空白页异常问题分析
About the handling of variable parameters in the Retrofit network request URL
十分钟教会你如何使用VitePress搭建及部署个人博客站点
无重复字符的最长子串