Home

Awesome

Release Android Arsenal

KBus

Super lightweight (13 LOC) and minimalistic (post(), subscribe(), unsubscribe()) EventBus written with idiomatic Kotlin and RxJava 2

KBus in 3 steps

1. Define your events

// With data
data class ShowMessageEvent(val message: String)

// Without data
class OtherEvent

2. Add subscribers

override fun onStart() {
    super.onStart()
    KBus.subscribe<ShowMessageEvent>(this) {
        showMessage(it.message)
    }
    KBus.subscribe<OtherEvent>(this) {
        doSomething()
    }
}

override fun onStop() {
    super.onStop()
    KBus.unsubscribe(this)
}

Where:

Sticky events

If you want to post events between Activities/Fragments just subscribe in onCreate() (or onPostCreate()) and unsubscribe in onDestroy()

override fun onPostCreate(savedInstanceState: Bundle?) {
    super.onPostCreate(savedInstanceState)
    KBus.subscribe<ShowMessageEvent>(this) {
        showMessage(it.message)
    }
}

override fun onDestroy() {
    super.onDestroy()
    KBus.unsubscribe(this)
}

3. Post events

KBus.post(ShowMessageEvent("Hello KBus!"))

Add KBus to your project

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    implementation 'com.github.adrielcafe:KBus:1.1'
}