Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrielcafe/KBus
Dead simple EventBus for Android made with Kotlin and RxJava 2
https://github.com/adrielcafe/KBus
android android-library eventbus kotlin kotlin-android kotlin-library rxjava rxjava-android rxjava2
Last synced: 13 days ago
JSON representation
Dead simple EventBus for Android made with Kotlin and RxJava 2
- Host: GitHub
- URL: https://github.com/adrielcafe/KBus
- Owner: adrielcafe
- License: mit
- Created: 2017-12-21T00:01:10.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-25T12:03:02.000Z (almost 5 years ago)
- Last Synced: 2024-08-01T01:30:55.196Z (3 months ago)
- Topics: android, android-library, eventbus, kotlin, kotlin-android, kotlin-library, rxjava, rxjava-android, rxjava2
- Language: Kotlin
- Size: 129 KB
- Stars: 46
- Watchers: 4
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-kotlin - KBus - Dead simple EventBus for Android made with Kotlin and RxJava 2. (Libraries)
README
[![Release](https://jitpack.io/v/adrielcafe/KBus.svg)](https://jitpack.io/#adrielcafe/KBus) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-KBus-green.svg?style=flat)]( https://android-arsenal.com/details/1/6583)
# 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
```kotlin
// With data
data class ShowMessageEvent(val message: String)// Without data
class OtherEvent
```### 2. Add subscribers
```kotlin
override fun onStart() {
super.onStart()
KBus.subscribe(this) {
showMessage(it.message)
}
KBus.subscribe(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()`
```kotlin
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
KBus.subscribe(this) {
showMessage(it.message)
}
}override fun onDestroy() {
super.onDestroy()
KBus.unsubscribe(this)
}
```### 3. Post events
```kotlin
KBus.post(ShowMessageEvent("Hello KBus!"))
```## Add KBus to your project
```gradle
repositories {
maven { url "https://jitpack.io" }
}dependencies {
implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
implementation 'com.github.adrielcafe:KBus:1.1'
}
```