Skip to content

Commit 47546f5

Browse files
Apply owners and the graph as part of composition
As all owners and setGraph are all idempotent operations, we can apply them to NavController as part of composition rather than via DisposableEffect/SideEffect, thus ensuring that they are set for the first composition. Relnote: "Navigation Compose's `NavHost` now sets the graph as part of the first composition." Test: existing NavHostTest tests still pass Change-Id: Ieb7be7689ab5c40cfcc52fefd70a87b406ce8c60
1 parent b0af29c commit 47546f5

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

navigation/navigation-compose/src/main/java/androidx/navigation/compose/NavHost.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,15 @@ public fun NavHost(
9797
val onBackPressedDispatcherOwner = LocalOnBackPressedDispatcherOwner.current
9898
val onBackPressedDispatcher = onBackPressedDispatcherOwner?.onBackPressedDispatcher
9999

100-
// on successful recompose we setup the navController with proper inputs
101-
// after the first time, this will only happen again if one of the inputs changes
102-
DisposableEffect(navController, lifecycleOwner, viewModelStoreOwner, onBackPressedDispatcher) {
103-
navController.setLifecycleOwner(lifecycleOwner)
104-
navController.setViewModelStore(viewModelStoreOwner.viewModelStore)
105-
if (onBackPressedDispatcher != null) {
106-
navController.setOnBackPressedDispatcher(onBackPressedDispatcher)
107-
}
108-
109-
onDispose { }
100+
// Setup the navController with proper owners
101+
navController.setLifecycleOwner(lifecycleOwner)
102+
navController.setViewModelStore(viewModelStoreOwner.viewModelStore)
103+
if (onBackPressedDispatcher != null) {
104+
navController.setOnBackPressedDispatcher(onBackPressedDispatcher)
110105
}
111106

112-
SideEffect { navController.graph = graph }
107+
// Then set the graph
108+
navController.graph = graph
113109

114110
val saveableStateHolder = rememberSaveableStateHolder()
115111

0 commit comments

Comments
 (0)