安卓eventbus信息傳遞

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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章