EventBus 大部分人都用過(guò)了,就不再介紹了,本篇主要講一下實(shí)際項(xiàng)目中EventBus的封裝過(guò)程。
1、正常用法
根據(jù)官網(wǎng)的介紹和網(wǎng)上博客的講解,一般用法如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
/**
* 2.x 方法必須以onEvent開(kāi)頭
* @param event
*/
public void onEventMainThread(MessageEvent event) {
}
/**
* 3.0 方法名可以自定義
* @param event
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(MessageEvent event) {
}
}
可以看到,2.x版本使用時(shí)需要手動(dòng)寫(xiě)onEvent開(kāi)頭的方法來(lái)接收消息,這種用法明顯有很大的隱患,如果不小心寫(xiě)錯(cuò)、改錯(cuò)或被誤刪就會(huì)出bug,也不好維護(hù)。所以3.0針對(duì)這塊做了優(yōu)化,使用注解更符合Java風(fēng)格,而且方法名可以自定義,這樣就消除了2.x的弊端。
以上是EventBus的正常用法,一般項(xiàng)目中很多Activity和Fragment都需要重復(fù)上面EventBus的操作,onCreate和onDestroy中重復(fù)的操作也是很頭疼的,所以考慮把register和unregister這兩個(gè)操作封裝到BaseActivity和BaseFragment中,接下來(lái)主要看BaseActivity中的封裝。
2、初次封裝
public abstract class BaseAppCompatActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isBindEventBus()) {
EventBus.getDefault().register(this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isBindEventBus()) {
EventBus.getDefault().unregister(this);
}
}
/**
* 當(dāng)前Activity是否需要綁定EventBus
* @return
*/
protected abstract boolean isBindEventBus();
}
public class MainActivity extends BaseAppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected boolean isBindEventBus() {
return true;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(MessageEvent event) {
}
}
初次封裝完成,成功將register和unregister這兩個(gè)操作封裝到了BaseAppCompatActivity中,這下MainActivity解脫了。但是隨之而來(lái)的是增加了新的抽象方法isBindEventBus,所有繼承BaseAppCompatActivity的Activity都得重寫(xiě)這個(gè)方法,這樣帶來(lái)了新的問(wèn)題,有的Activity并不需要綁定EventBus,但是因?yàn)槔^承關(guān)系導(dǎo)致必須重寫(xiě)這個(gè)方法并返回false。不管從架構(gòu)還是維護(hù)角度看這都不是最好的解決方案,所以還得摸索新的思路,這時(shí)候想到了注解。
3、再次封裝
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BindEventBus {
}
public abstract class BaseAppCompatActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
EventBus.getDefault().register(this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
EventBus.getDefault().unregister(this);
}
}
}
@BindEventBus
public class MainActivity extends BaseAppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(MessageEvent event) {
}
}
這個(gè)注解的思路是從ButterKnife來(lái)的,給需要綁定EventBus的Activity加上類注解BindEventBus,這樣在BaseAppCompatActivity中判斷當(dāng)前子類上是否有該注解,有則register和unregister。這時(shí)候再看,代碼明顯干凈了,風(fēng)格也更Java了。經(jīng)過(guò)測(cè)試,EventBus收發(fā)消息沒(méi)問(wèn)題,相關(guān)代碼都在 android-blog-samples。
關(guān)于Java注解的原理和用法網(wǎng)上的學(xué)習(xí)資料很多,這里留一個(gè)慕課網(wǎng)的視頻:全面解析Java注解。
~ the end ~