Skip to content

Instantly share code, notes, and snippets.

@alifhasnain
Created September 29, 2020 21:02
Show Gist options
  • Save alifhasnain/b0a6e71a52bef31e1ba6f829683db303 to your computer and use it in GitHub Desktop.
Save alifhasnain/b0a6e71a52bef31e1ba6f829683db303 to your computer and use it in GitHub Desktop.
A Kotlin class which can help as being a wrapper around any other class to trigger the LiveData once.
class Event<out R>(private val data: R) {
var hasEventBeenHandled = false
private set
val content: R?
get() = if (!hasEventBeenHandled) {
hasEventBeenHandled = true
data
} else {
null
}
val oldContent: R = data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment