Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created September 13, 2020 17:12
Show Gist options
  • Save Audhil/80be859710f2a77009bbc5bad4eb0aba to your computer and use it in GitHub Desktop.
Save Audhil/80be859710f2a77009bbc5bad4eb0aba to your computer and use it in GitHub Desktop.
handle livedata only once!
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment