Skip to content

Commit

Permalink
Fix issue with restoring dynamic containers and retaining active cont…
Browse files Browse the repository at this point in the history
…ainer state
  • Loading branch information
isaac-udy committed Apr 28, 2024
1 parent 223dcbe commit 19a5538
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,20 @@ public class ComposableNavigationContainer internal constructor(
mutableStateOf(Unit)
},
stateSaver = object : Saver<Unit, Bundle> {
override fun restore(value: Bundle) = when(registrationStrategy) {
ContainerRegistrationStrategy.DisposeWithComposition -> this@ComposableNavigationContainer.restore(value)
ContainerRegistrationStrategy.DisposeWithCompositionDoNotSave -> Unit
ContainerRegistrationStrategy.DisposeWithLifecycle -> Unit
override fun restore(value: Bundle) {
// When restoring, there are some cases where the active container is not the container that is being restored,
// and performing the restore might set that container to be active when that's not actually what we want,
// so we're going to remember the currently active container key, before performing the restore,
// and then re-set the active container afterwards.
val activeBeforeRestore = context.containerManager.activeContainer?.key
when (registrationStrategy) {
ContainerRegistrationStrategy.DisposeWithComposition -> this@ComposableNavigationContainer.restore(value)
ContainerRegistrationStrategy.DisposeWithCompositionDoNotSave -> Unit
ContainerRegistrationStrategy.DisposeWithLifecycle -> Unit
}
if (activeBeforeRestore != null) {
context.containerManager.setActiveContainerByKey(activeBeforeRestore)
}
}

override fun SaverScope.save(value: Unit): Bundle? = when(registrationStrategy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ class NavigationContainerTests {
.forward(GenericComposableKey("Five"))
expectComposableContext<GenericComposableKey> { it.navigation.key.id == "Five" }

waitFor { activity.primaryContainer.isActive }
scenario.recreate()
activity = expectActivity()
assertEquals(
Expand Down

0 comments on commit 19a5538

Please sign in to comment.