Event Bus 最佳实践
2021-09-18
1 min read
https://greenrobot.org/eventbus/
https://github.com/greenrobot/EventBus#add-eventbus-to-your-project
- Step 1: Add Dep
implementation 'org.greenrobot:eventbus:3.2.0'
- Step2: Def Event
- Just To Create a Beam Class or Data Class (in Kotlin)
- Step3: Prepare Sub
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
- Step4: Setup And Remove Eventbus
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
- Step5: Post Event
EventBus.getDefault().post(new MessageEvent());