Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active August 6, 2019 15:52
Show Gist options
  • Save SeongUgJung/406e3270e1b556426c43e2cdcf224a42 to your computer and use it in GitHub Desktop.
Save SeongUgJung/406e3270e1b556426c43e2cdcf224a42 to your computer and use it in GitHub Desktop.
lifecycle callback managers
// lifecycle observers manager
class LifecycleController {
private val observers = mutableListOf<LifecycleObserver>()
private var state: LifecycleState = Unknown
fun onInit() {
observers.forEach { it.onInit() }
state = Init
}
fun onVisible() {
observers.forEach { it.onVisible() }
state = Visible
}
fun onInvisible() {
observers.forEach { it.onInvisible() }
state = Invisible
}
fun onDeinit() {
observers.forEach { it.onDeinit() }
state = Deinit
}
}
// lifecycle observer
interface LifecycleObserver {
fun onInit() = Unit
fun onVisible() = Unit
fun onInvisible() = Unit
fun onDeinit() = Unit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment