Extract the EventBus encapsulation into the base class using annotations

   As your project gets bigger and bigger, you always consider that there will be a unified entrance for both network requests and Intent jumps. Even for messaging, you will want to extract an event bus to clarify the architecture of the whole project. Needless to say, EventBus greatly simplifies the process of Android messaging. EventBus makes messaging more concise and flexible, OK, let's start today's topic. Today we mainly analyze the extraction of EventBus into the base class. Readers are advised to understand the basic usage of EventBus first.

   Before EventBus 3.0, we must define the methods starting with onEvent, namely onEvent, onEventMainThread, onEventBackgroundThread and onEventAsync. After 3.0, the method name of event processing can be taken at will, but the working thread of EventBus needs to be specified by annotation, so that we can customize the event name according to the business scenario, It is also convenient to encapsulate and extract events. However, if there is no unified encapsulation, registration and de registration in the process of EventBus, there will be a lot of redundant code in the Activity and fragment of the project for no reason. Therefore, my first idea is to manage the registration and de registration of EventBus in the base class and bind where EventBus needs to be used, There is no need to do any processing where it does not need to be used. If a coder has done similar business, it may find that if the binding of EventBus is directly placed in the base class without judging whether the subclass uses EventBus, the subclass that does not use EventBus will not work normally.

   Based on this scenario, I first think of using annotations to solve the problem. First define an empty class annotation, and then bind the annotation in the Activity or Fragment that needs to use EventBus. When registering the base class, first judge whether the current subclass has bound the BindEventBus annotation, and then decide whether to register EventBus.

Annotation class BindEventBus.java
 

/**
 * desc: Activities and fragments that need to use eventbus need to be bound to this in the form of annotations
 * author: xiedong
 * date: 2017/10/17
 */
 
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BindEventBus {
}

Then, in the base class and Fragment class of Activity, first judge whether the current subclass is bound with BindEventBus annotation, and then decide whether to register or de register

Register EventBus in BaseActivity.java:

 private void initData() {
        activity = this;
        initBaseMVP();
        //Determine whether to register EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBus.getDefault().register(this);
        }
    }

De registration:

@Override
    protected 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 whether registration is required
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBus.getDefault().register(this);
        }
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBus.getDefault().unregister(this);
        }
    }

In subclasses that need to use EventBus, you only need to add the annotation of BindEventBus. The rest of the use process is the same as that of using EventBus normally. Subclasses that do not need to use EventBus do not need to do any processing

Use in subclass Activity:

@BindEventBus
public class ReleaseSettingActivity extends BaseActivity {

Use in Fragment subclass:

@BindEventBus
public class ThemeStudycircleFragment extends MListFragment{}

Reprint: Extract the EventBus encapsulation into the base class using annotations_ Xie Dong's column - CSDN blog

Tags: Java

Posted on Tue, 21 Sep 2021 23:24:53 -0400 by abbeyvb