diff --git a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/androidx/WorkflowLifecycleOwner.kt b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/androidx/WorkflowLifecycleOwner.kt index 3f3f29cc7..728b432e0 100644 --- a/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/androidx/WorkflowLifecycleOwner.kt +++ b/workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/androidx/WorkflowLifecycleOwner.kt @@ -8,6 +8,7 @@ import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting.Companion.PRIVATE import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle.Event +import androidx.lifecycle.Lifecycle.State import androidx.lifecycle.Lifecycle.State.DESTROYED import androidx.lifecycle.Lifecycle.State.INITIALIZED import androidx.lifecycle.Lifecycle.State.RESUMED @@ -190,12 +191,18 @@ internal class RealWorkflowLifecycleOwner( oldLifecycle?.removeObserver(this) parentLifecycle?.addObserver(this) } - updateLifecycle() + + // if (parentLifecycle?.currentState == INITIALIZED) { + // // updateLifecycle(Event.ON_RESUME.targetState) + // } else { + // + // } + updateLifecycle(Event.ON_RESUME.targetState) } override fun onViewDetachedFromWindow(v: View) { viewIsAttachedToWindow = false - updateLifecycle() + updateLifecycle(Event.ON_PAUSE.targetState) } /** Called when the [parentLifecycle] changes state. */ @@ -203,19 +210,19 @@ internal class RealWorkflowLifecycleOwner( source: LifecycleOwner, event: Event ) { - updateLifecycle() + updateLifecycle(event.targetState) } override fun destroyOnDetach() { if (!destroyOnDetach) { destroyOnDetach = true - updateLifecycle() + updateLifecycle(Event.ON_DESTROY.targetState) } } @VisibleForTesting(otherwise = PRIVATE) - internal fun updateLifecycle() { - val parentState = parentLifecycle?.currentState + internal fun updateLifecycle(parentTargetState: State) { + // val parentState = parentLifecycle?.currentState val localState = localLifecycle.currentState if (localState == DESTROYED || hasBeenDestroyed) { @@ -224,32 +231,7 @@ internal class RealWorkflowLifecycleOwner( return } - localLifecycle.currentState = when { - destroyOnDetach && !viewIsAttachedToWindow -> { - // We've been enqueued for destruction. - // Stay attached to the parent's lifecycle until we re-attach, since the parent could be - // destroyed while we're detached. - DESTROYED - } - parentState != null -> { - // We may or may not be attached, but we have a parent lifecycle so we just blindly follow - // it. - parentState - } - localState == INITIALIZED -> { - // We have no parent and we're not destroyed, which means we have never been attached, so - // the only valid state we can be in is INITIALIZED. - INITIALIZED - } - else -> { - // We don't have a parent and we're neither in DESTROYED or INITIALIZED: this is an invalid - // state. Throw an AssertionError instead of IllegalStateException because there's no API to - // get into this state, so this means the library has a bug. - throw AssertionError( - "Must have a parent lifecycle after attaching and until being destroyed." - ) - } - }.let { newState -> + localLifecycle.currentState = parentTargetState.let { newState -> if (newState == DESTROYED) { hasBeenDestroyed = true