[EventBus]使用帮助
1.添加依赖:compile 'org.greenrobot:eventbus:3.1.1'
2.创建一个消息类
3.
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_event_bus) EventBus.getDefault().register(this) btn_eventbus.onClick { startActivity<MainActivity>() } } override fun onDestroy() { super.onDestroy() EventBus.getDefault().unregister(this) } @Subscribe(threadMode = ThreadMode.MAIN) fun onMessageEvent(event: MessageEvent) { Toast.makeText(this,"test received event message", Toast.LENGTH_LONG) btn_eventbus.text = event.message Log.d("test", "test") }
流程是注册--->接受----->注销
注意,哪里注册就在那里需要@Subscribe要不会报错
4.发送信息:
class EventPresent { fun sendMessage(){ println("================") EventBus.getDefault().post(MessageEvent("test event send")) } }
这样EventBus的Activity就能接收到post的信息
参考:
相关推荐
kururunga 2020-05-07
kururunga 2020-05-01
kururunga 2020-11-16
pengruiyu 2020-08-01
kururunga 2020-07-16
kururunga 2020-04-23
一个来自吉尔尼斯 2020-04-21
一个来自吉尔尼斯 2020-04-10
xiaorulou 2020-03-01
pengruiyu 2020-02-19
kururunga 2019-12-08
一个来自吉尔尼斯 2019-12-05
Androidtalent 2014-03-23
kuangren 2019-11-01
pengruiyu 2019-10-20
huangf 2015-04-17
kururunga 2015-05-17
huangf 2015-05-17