Skip to content

Commit 8015304

Browse files
committed
Refactor NavigationState. Fixes #130.
This commit refactors the `NavigationState` to ensure its properties are stable across recompositions. The `NavigationState` instance is now wrapped in `remember`, and its `startRoute` is a `val`. Additionally, the `popBackStack` function in the `Navigator` now uses `removeLastOrNull()` to prevent crashes when the back stack is empty.
1 parent 4e268ac commit 8015304

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

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

Lines changed: 16 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,39 @@ 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(
4647
serializer = MutableStateSerializer(NavKeySerializer())
47-
){
48+
) {
4849
mutableStateOf(startRoute)
4950
}
5051

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

5963
/**
6064
* State holder for navigation state.
6165
*
66+
* @param startRoute - the start route. The user will exit the app through this route.
6267
* @param topLevelRoute - the current top level route
6368
* @param backStacks - the back stacks for each top level route
6469
*/
6570
class NavigationState(
71+
val startRoute: NavKey,
6672
topLevelRoute: MutableState<NavKey>,
6773
val backStacks: Map<NavKey, NavBackStack<NavKey>>
6874
) {
69-
val startRoute = topLevelRoute.value
70-
var topLevelRoute : NavKey by topLevelRoute
71-
val stacksInUse : List<NavKey>
75+
var topLevelRoute: NavKey by topLevelRoute
76+
val stacksInUse: List<NavKey>
7277
get(){
7378
val stacksInUse = mutableListOf(startRoute)
7479
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)