Gradle插件開發(fā)-EventBus自動注冊

com.eventbus.autoregister

EventBus自動注冊與反注冊

  • 支持在Activity onCreate方法進行注冊,在onDestroy方法中反注冊
  • 支持在Fragment onCreate方法進行注冊,在onDestroy方法中反注冊
  • 支持在View onAttachedToWindow方法進行注冊,在onDetachedFromWindow方法中反注冊

gradle 依賴配置

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.liwg.eventbus.autoregister:autoRegisterPlugin:1.0.0"
    }
}

apply plugin: "com.eventbus.autoregister"

使用方式和平常使用沒有任何區(qū)別

省去了手動注冊與反注冊代碼
public class MainActivity extends AppCompatActivity {
    @Subscribe
    public void onEvent(Object object){

    }
}

這是編譯自動生成的代碼

public class MainActivity extends AppCompatActivity {
    public MainActivity() {
    }

    @Subscribe
    public void onEvent(Object object) {
    }

    public void onCreate(Bundle var1) {
        super.onCreate(var1);
        //自動在Activity的onCreate中生成注冊代碼
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    public void onDestroy() {
        super.onDestroy();
        //自動在Activity的onDestroy中生成反注冊代碼
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
倉庫地址 https://github.com/lwugang/EventBusAutoRegister
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容