Skip to content

Instantly share code, notes, and snippets.

@odbol
Created October 2, 2025 17:44
Show Gist options
  • Save odbol/dc2bcf67533d2fb5b41429808185813d to your computer and use it in GitHub Desktop.
Save odbol/dc2bcf67533d2fb5b41429808185813d to your computer and use it in GitHub Desktop.
Shares events across activities, services, etc. So much simpler than the Java version! But still useful.
package com.odbol.utils
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
/** Shares events across activities, services, etc */
object EventBus {
val events = MutableStateFlow<Event>(Event.None)
}
sealed class Event {
data object None : Event()
data class OnError(val error: String) : Event()
// Add your own events here...
}
// Example of how to use it in an Activity or wherever
fun onCreate() {
lifecycleScope.launch(Dispatchers.IO) {
EventBus.events.collect { event ->
when (event) {
is Event.None -> Log.v(TAG, "Subscribing to event bus")
is Event.OnError -> Log.e(TAG, "Error: ${event.error}")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment