diff --git a/voyager-core/src/androidMain/kotlin/cafe/adriel/voyager/androidx/AndroidScreenLifecycleOwner.kt b/voyager-core/src/androidMain/kotlin/cafe/adriel/voyager/androidx/AndroidScreenLifecycleOwner.kt index e4e5c1b2..c25a5ff0 100644 --- a/voyager-core/src/androidMain/kotlin/cafe/adriel/voyager/androidx/AndroidScreenLifecycleOwner.kt +++ b/voyager-core/src/androidMain/kotlin/cafe/adriel/voyager/androidx/AndroidScreenLifecycleOwner.kt @@ -58,6 +58,7 @@ public class AndroidScreenLifecycleOwner private constructor() : private val controller = SavedStateRegistryController.create(this) private var isCreated: Boolean by mutableStateOf(false) + private var isContextResetCallbackRegistered = false override val savedStateRegistry: SavedStateRegistry get() = controller.savedStateRegistry @@ -140,7 +141,10 @@ public class AndroidScreenLifecycleOwner private constructor() : @Composable private fun getHooks(): List> { - atomicContext.compareAndSet(null, LocalContext.current) + val context = LocalContext.current + atomicContext.compareAndSet(null, context) + registerContextResetCallback(context) + atomicParentLifecycleOwner.compareAndSet(null, LocalLifecycleOwner.current) return remember(this) { @@ -187,6 +191,20 @@ public class AndroidScreenLifecycleOwner private constructor() : } } + private fun registerContextResetCallback(context: Context) { + if (!isContextResetCallbackRegistered) { + val lifecycleOwner = (context as? LifecycleOwner) ?: return + lifecycleOwner.lifecycle.addObserver( + object : DefaultLifecycleObserver { + override fun onDestroy(owner: LifecycleOwner) { + atomicContext.set(null) + isContextResetCallbackRegistered = false + } + } + ) + } + } + @Composable private fun LifecycleDisposableEffect() { val savedState = rememberSaveable { Bundle() } @@ -248,6 +266,7 @@ public class AndroidScreenLifecycleOwner private constructor() : private val disposeEvents = arrayOf( Lifecycle.Event.ON_DESTROY ) + public fun get(screen: Screen): ScreenLifecycleOwner { return ScreenLifecycleStore.get(screen) { AndroidScreenLifecycleOwner() } }