Skip to content

Commit 15b648b

Browse files
authored
Merge pull request #131 from android/dt/fix-multiplestacks
[Multiple stacks recipe] Refactor NavigationState. Fixes #130.
2 parents a60bdd8 + 6a047eb commit 15b648b

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

app/src/main/java/com/example/nav3recipes/multiplestacks/NavigationState.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.runtime.Composable
2020
import androidx.compose.runtime.MutableState
2121
import androidx.compose.runtime.getValue
2222
import androidx.compose.runtime.mutableStateOf
23+
import androidx.compose.runtime.remember
2324
import androidx.compose.runtime.saveable.rememberSerializable
2425
import androidx.compose.runtime.setValue
2526
import androidx.compose.runtime.snapshots.SnapshotStateList
@@ -40,35 +41,40 @@ import androidx.savedstate.compose.serialization.serializers.MutableStateSeriali
4041
fun rememberNavigationState(
4142
startRoute: NavKey,
4243
topLevelRoutes: Set<NavKey>
43-
) : NavigationState {
44+
): NavigationState {
4445

4546
val topLevelRoute = rememberSerializable(
47+
startRoute, topLevelRoutes,
4648
serializer = MutableStateSerializer(NavKeySerializer())
47-
){
49+
) {
4850
mutableStateOf(startRoute)
4951
}
5052

51-
return NavigationState(
52-
topLevelRoute = topLevelRoute,
53-
backStacks = topLevelRoutes.associateWith { key ->
54-
rememberNavBackStack(key)
55-
}
56-
)
53+
val backStacks = topLevelRoutes.associateWith { key -> rememberNavBackStack(key) }
54+
55+
return remember(startRoute, topLevelRoutes) {
56+
NavigationState(
57+
startRoute = startRoute,
58+
topLevelRoute = topLevelRoute,
59+
backStacks = backStacks
60+
)
61+
}
5762
}
5863

5964
/**
6065
* State holder for navigation state.
6166
*
67+
* @param startRoute - the start route. The user will exit the app through this route.
6268
* @param topLevelRoute - the current top level route
6369
* @param backStacks - the back stacks for each top level route
6470
*/
6571
class NavigationState(
72+
val startRoute: NavKey,
6673
topLevelRoute: MutableState<NavKey>,
6774
val backStacks: Map<NavKey, NavBackStack<NavKey>>
6875
) {
69-
val startRoute = topLevelRoute.value
70-
var topLevelRoute : NavKey by topLevelRoute
71-
val stacksInUse : List<NavKey>
76+
var topLevelRoute: NavKey by topLevelRoute
77+
val stacksInUse: List<NavKey>
7278
get(){
7379
val stacksInUse = mutableListOf(startRoute)
7480
if (topLevelRoute != startRoute) stacksInUse += topLevelRoute

app/src/main/java/com/example/nav3recipes/multiplestacks/Navigator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Navigator(val state: NavigationState){
4141
if (currentRoute == state.topLevelRoute){
4242
state.topLevelRoute = state.startRoute
4343
} else {
44-
currentStack.removeLast()
44+
currentStack.removeLastOrNull()
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)