1. 首先需要導入eventbus的jar包.
2. 註冊(在當前頁)
EventBus.getDefault().register(this);
3. 發送
EventBus.getDefault().post(sdata);
其中post(xx),xx的類型由接收函數的參數類型決定。
4. 接收(方式有四種,onEventMainThread,onEvent等,區別自行了解,此只介紹一種)
public void onEventMainThread(StoreData sdata) { tv1.setText(sdata.mMsg); }
5. 解除註冊(建議在onDestroy中)
EventBus.getDefault().unregister(this);
在onDestroy中爲:
@Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }