GitHub:https://github.com/greenrobot/EventBus
①定義事件
public static class MessageEvent { /* Additional fields if needed */ }
②在activity或者fragment的合適生命周期注冊或者注銷
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? EventBus.getDefault().register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
? ? EventBus.getDefault().unregister(this);
}
③準(zhǔn)備訂閱者:聲明并注釋您的訂閱方法,可以選擇指定線程模式:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
}
④發(fā)送事件
EventBus.getDefault().post(new MessageEvent());