Logo

0x5a.live

for different kinds of informations and explorations.

GitHub - adrielcafe/KBus: Dead simple EventBus for Android made with Kotlin and RxJava 2

Dead simple EventBus for Android made with Kotlin and RxJava 2 - adrielcafe/KBus

Visit SiteGitHub - adrielcafe/KBus: Dead simple EventBus for Android made with Kotlin and RxJava 2

GitHub - adrielcafe/KBus: Dead simple EventBus for Android made with Kotlin and RxJava 2

Dead simple EventBus for Android made with Kotlin and RxJava 2 - adrielcafe/KBus

Powered by 0x5a.live ๐Ÿ’—

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:

  • ShowMessageEvent - the event you're subscribing
  • this - the subscriber (by default the current Activity/Fragment)
  • { showMessage() } - the subscription observer
  • it - the event instance

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'
}

Kotlin Resources

are all listed below.

Resources

listed to get explored on!!

Made with โค๏ธ

to provide different kinds of informations and resources.