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"
使用方式和平常使用沒有任何區別
省去了手動注冊與反注冊代碼
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);
}
}
}