史上最简洁Kotlin版EventBus的使用教程

Posted super码王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了史上最简洁Kotlin版EventBus的使用教程相关的知识,希望对你有一定的参考价值。

EventBus简介

  • EventBus是一种用于android的事件发布-订阅总线。他简化了应用程序内各个组件之间进行通信的复杂度。

GitHub - greenrobot/EventBus: Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.https://github.com/greenrobot/EventBus

EventBus使用步骤 

        1、配置gradle,导入依赖

implementation 'org.greenrobot:eventbus:3.3.1'

        2、定义Event类型(非必须

package com.example.eventbusdemo

data class MessageEvent(val name: String) 

        3、注册、注销EventBus

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //注册订阅者
        //避免重复注册,重复注册会导致崩溃
        if (!EventBus.getDefault().isRegistered(this))  //这里的取反别忘记了
            EventBus.getDefault().register(this)
         else 
            println("请勿重复注册事件")
        
    
    override fun onDestroy() 
        super.onDestroy()
        //注销订阅者
        EventBus.getDefault().unregister(this)
    

        4、定义事件处理订阅者

    //准备接受事件的订阅者
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: MessageEvent) 
        println(event.name)
    

        5、创建并发送消息

    fun sendEvent(v: View) 
        EventBus.getDefault().post(MessageEvent("我来自第二个页面"))
    

运行效果展示

常见问题总结

1、Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.example.eventbusdemo.SecondActivity and its super classes have no public methods with the @Subscribe annotation

Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.example.eventbusdemo.SecondActivity and its super classes have no public methods with the @Subscribe annotation
        at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
        at org.greenrobot.eventbus.EventBus.register(EventBus.java:150)
        at com.example.eventbusdemo.SecondActivity.onCreate(SecondActivity.kt:20)
        at android.app.Activity.performCreate(Activity.java:7893)
        at android.app.Activity.performCreate(Activity.java:7880)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3313)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3487) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7561) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) 

问题原因:你这个类注册了 EventBus 可是这个类没有写带 @Subscribe 注解的方法

解决方法:补上该方法即可


2、D/EventBus: No subscribers registered for event class com.example.eventbusdemo.MessageEvent
      D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent

D/EventBus: No subscribers registered for event class com.example.eventbusdemo.MessageEvent
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent

问题原因:EventBus没有被注册上,检查 EventBus 的注册代码

以上是关于史上最简洁Kotlin版EventBus的使用教程的主要内容,如果未能解决你的问题,请参考以下文章

史上最详Android版kotlin协程入门进阶实战

android room史上最快速入门教程(kotlin版本)

android room史上最快速入门教程(kotlin版本)

android room史上最快速入门教程(kotlin版本)

史上最详Android版kotlin协程入门进阶实战

Intellij idea史上最简单的教程之Linux下安装与破解Intellij idea2017