Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created May 2, 2019 03:03
Show Gist options
  • Save cdmunoz/3d20720b145bfedc13bd2a76f7f2bf70 to your computer and use it in GitHub Desktop.
Save cdmunoz/3d20720b145bfedc13bd2a76f7f2bf70 to your computer and use it in GitHub Desktop.
class OneTimeObserver<T>(private val handler: (T) -> Unit) : Observer<T>, LifecycleOwner {
private val lifecycle = LifecycleRegistry(this)
init {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
}
override fun getLifecycle(): Lifecycle = lifecycle
override fun onChanged(t: T) {
handler(t)
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment