Skip to content

Instantly share code, notes, and snippets.

@CostaFot
Created November 20, 2023 14:00
Show Gist options
  • Save CostaFot/6eadc6bcd841e2e984fa45088efc7249 to your computer and use it in GitHub Desktop.
Save CostaFot/6eadc6bcd841e2e984fa45088efc7249 to your computer and use it in GitHub Desktop.
data class SingleLiveEvent<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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment