当前位置:网站首页>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{}

原网站

版权声明
本文为[Xie Dong_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091203557739.html