Dead simple EventBus for Android made with Kotlin and RxJava 2

Overview

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'
}
You might also like...
Android Kotlin paged endpoints made easy
Android Kotlin paged endpoints made easy

A smart and simple way to work with paged endpoints. To see an example of how to use it, check out the introducing Fountain posts: part one and part t

Open source Crypto Currency Tracker Android App made fully in Kotlin
Open source Crypto Currency Tracker Android App made fully in Kotlin

CoinBit CoinBit is a beautiful CryptoCurrency app, completely open sourced and 100% in kotlin. It supports following features Track prices of over 300

Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.
Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.

Delight-Playground 🎉 Native Android application built with Kotlin and Jetpack Compose. This app also illustrates the usage of advance libraries such

A curated collection of splendid gradients made in Kotlin
A curated collection of splendid gradients made in Kotlin

Gradients A curated collection of splendid gradients made in Kotlin (port of https://webgradients.com for Android). Only linear gradients included for

SharedPreference usage made fun in Kotlin

PreferenceHolder Kotlin Android Library, that makes preference usage simple and fun. To stay up-to-date with news about library This library is younge

Filmesflix - Project made during the NTT DATA Android Developer bootcamp. Developing knowledge in MVVM and Clear Architecture
Filmesflix - Project made during the NTT DATA Android Developer bootcamp. Developing knowledge in MVVM and Clear Architecture

FilmesFlix Projeto criado para o módulo de MVVM e Clean Architecture no Bootcamp

A personal project made using Jetpack Compose, Clean-MVVM architecture and other jetpack libraries

A basic CRUD for recording your personal credit and debit transactions. Made using Jetpack Compose, Clean-MVVM and other Jetpack libraries, incorporated with Unit Tests.

An android application that made as an exercise, that does 4 different conversions.

Following android studio basic course, this is my second (and bit more complicate this time) "practice on your own" project. In few words, it is an an

:octocat: Navigation toolbar is a slide-modeled UI navigation controller made by @Ramotion
:octocat: Navigation toolbar is a slide-modeled UI navigation controller made by @Ramotion

NAVIGATION TOOLBAR Navigation toolbar is a Kotlin slide-modeled UI navigation controller. We specialize in the designing and coding of custom UI for M

Comments
  • problem implementing

    problem implementing

    hi, sorry if this is an obvious question, i'm only a sprout with android. i was trying to implement your library in my project as you suggest, adding a few lines in my build.gradle (after copying "library" folder from KBus-master to my project "lib" folder):

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

    also tried

    implementation 'com.github.adrielcafe:KBus:1.0'
    

    as this is the version github is reporting, yet to no avail. it tells me dependency cannot be resolved. what am i missing?

    -- using AndroidStudio 3.5.2

    opened by voblaunsane 0
  • KT and Java mixed code in the same project

    KT and Java mixed code in the same project

    I have kt and Java mixed in the same project. I send messages to bus from KT

    How do I receive messages in Java part of the project? Some lines of code would be useful

    question 
    opened by Duna 2
Releases(1.0)
Owner
Adriel Café
Remote Android Developer
Adriel Café
Functional Constructs for Databinding + Kotlin + RxJava

ObservableFlow Pt 1/3 Pt 2/3: Stepper Indicator Pt 3/3: SugarPreferences Functional Kotlin constructs like map(), filter() and 12 more functions built

Rakshak R.Hegde 26 Oct 3, 2022
base url not included, rxJava, Retrofit

NewsFeed NewsFeed is an android sample application built using kotlin, androidx artifacts, kotlin-extensions in MVP pattern. Libraries Used Dagger 2 D

Sagar Raval 0 Dec 29, 2021
base url not included, rxJava, Retrofit

NewsFeed NewsFeed is an android sample application built using kotlin, androidx artifacts, kotlin-extensions in MVP pattern. Libraries Used Dagger 2 D

Sagar Raval 0 Dec 29, 2021
🚟 Lightweight, and simple scheduling library made for Kotlin (JVM)

Haru ?? Lightweight, and simple scheduling library made for Kotlin (JVM) Why did you build this? I built this library as a personal usage library to h

Noel 13 Dec 16, 2022
⏰ A powerful and simple-to-use guilded wrapper made in Kotlin.

⏰ guilded-kt [WIP] A powerful yet simple-to-use guilded wrapper made entirely in Kotlin with supporting multiplatform. Take a look at an example of th

Gabriel 13 Jul 30, 2022
🎲 A powerful and simple-to-use guilded wrapper made in Kotlin.

?? deck [WIP] Deck is a powerful yet simple-to-use guilded wrapper made entirely in Kotlin with support to multiplatform. Implementating In case you'r

Gabriel 13 Jul 30, 2022
HowsTheWeather - 🌄 Your typical weather app, made really simple.

How's The Weather This is a simple Android app made with the purpose of learning how to access external APIs. It displays the data fetched from this A

Sam Garcia 1 Jan 3, 2022
Server Sent Events (SSE) client multiplatform library made with Kotlin and backed by coroutines

OkSSE OkSSE is an client for Server Sent events protocol written in Kotlin Multiplatform. The implementation is written according to W3C Recommendatio

BioWink GmbH 39 Nov 4, 2022
A discord bot made in Kotlin powered by JDA and Realms.

A discord bot made in Kotlin powered by JDA and Realms.

null 1 Jun 30, 2022
Utility for developers and QAs what helps minimize time wasting on writing the same data for testing over and over again. Made by Stfalcon

Stfalcon Fixturer A Utility for developers and QAs which helps minimize time wasting on writing the same data for testing over and over again. You can

Stfalcon LLC 31 Nov 29, 2021