Created
May 2, 2019 03:03
-
-
Save cdmunoz/3d20720b145bfedc13bd2a76f7f2bf70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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