Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun AppCompatActivity.subscribeToBroadcastsOnLifecycle(action: String, fn: (Intent) -> Unit) {
observeBroadcasts(action).subscribeOnLifecycle(lifecycle, fn)
}
fun <T> Observable<T>.subscribeOnLifecycle(lifecycle: Lifecycle, fn: (T) -> Unit) {
val lifecycleObserver: LifecycleObserver = object : LifecycleObserver {
private var subscription: Disposable? = null
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
subscription = subscribe(fn)
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
subscription?.dispose()
}
}
lifecycle.addObserver(lifecycleObserver)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.