Skip to content

Commit

Permalink
fix: event emit on destroy state #280 (#297)
Browse files Browse the repository at this point in the history
* fix: event emit on destroy state #280

* fix lint
  • Loading branch information
DevSrSouza authored Jan 9, 2024
1 parent fdc4b99 commit d7dbda9
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ public class AndroidScreenLifecycleOwner private constructor() :
isCreated = true
controller.performRestore(savedState)
initEvents.forEach {
lifecycle.handleLifecycleEvent(it)
lifecycle.safeHandleLifecycleEvent(it)
}
}

private fun emitOnStartEvents() {
startEvents.forEach {
lifecycle.handleLifecycleEvent(it)
lifecycle.safeHandleLifecycleEvent(it)
}
}

private fun emitOnStopEvents() {
stopEvents.forEach {
lifecycle.handleLifecycleEvent(it)
lifecycle.safeHandleLifecycleEvent(it)
}
}

Expand All @@ -130,7 +130,7 @@ public class AndroidScreenLifecycleOwner private constructor() :
if (activity != null && activity.isChangingConfigurations) return
viewModelStore.clear()
disposeEvents.forEach { event ->
lifecycle.handleLifecycleEvent(event)
lifecycle.safeHandleLifecycleEvent(event)
}
}

Expand Down Expand Up @@ -160,19 +160,19 @@ public class AndroidScreenLifecycleOwner private constructor() :
if (lifecycleOwner != null) {
val observer = object : DefaultLifecycleObserver {
override fun onPause(owner: LifecycleOwner) {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
lifecycle.safeHandleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
}

override fun onResume(owner: LifecycleOwner) {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
lifecycle.safeHandleLifecycleEvent(Lifecycle.Event.ON_RESUME)
}

override fun onStart(owner: LifecycleOwner) {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START)
lifecycle.safeHandleLifecycleEvent(Lifecycle.Event.ON_START)
}

override fun onStop(owner: LifecycleOwner) {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
lifecycle.safeHandleLifecycleEvent(Lifecycle.Event.ON_STOP)

// when the Application goes to background, perform save
performSave(outState)
Expand Down Expand Up @@ -222,6 +222,13 @@ public class AndroidScreenLifecycleOwner private constructor() :
else -> null
}

private fun LifecycleRegistry.safeHandleLifecycleEvent(event: Lifecycle.Event) {
val currentState = currentState
if (!currentState.isAtLeast(Lifecycle.State.INITIALIZED)) return

handleLifecycleEvent(event)
}

public companion object {

private val initEvents = arrayOf(
Expand Down

0 comments on commit d7dbda9

Please sign in to comment.