Hi is there a "Better" way to handle multiple comp...
# compose
t
Hi is there a "Better" way to handle multiple compose navigation destinations when passing "Down to Lowever levels"?
My application has a backdrop with multiple menu options that allow the user to navigate to screens within the app currently i have the following code using navigationHost
super.onCreate(savedInstanceState)
setContent {
AppTheme {
val navigator = rememberNavController()
val windowSize = calculateWindowSizeClass(this@HomeActivity)
val devicePosture by devicePostureFlow.collectAsState()
MyApp(
windowSize = windowSize,
devicePosture = devicePosture,
onNavigateToHome = {
navigator.navigateSingleTopTo(HomeScreenRoute)
},
onNavigateToA = {
navigator.navigateSingleTopTo(AScreenRoute)
},
onNavigateToB = {
navigator.navigateSingleTopTo(BScreenRoute)
},
onNavigateToC = {
navigator.navigateSingleTopTo(CScreenRoute)
},
onNavigateToD = {
navigator.navigateSingleTopTo(DScreenRoute)
},
onNavigateToE = {
navigator.navigateSingleTopTo(EScreenRoute)
},
onNavigateToF = {
navigator.navigateSingleTopTo(FScreenRoute)
},
onNavigateToG = {
navigator.navigateSingleTopTo(GScreenRoute)
},
onNavigateToH = {
navigator.navigateSingleTopTo(HScreenRoute)
},
onNavigateToI = {
navigator.navigateSingleTopTo(IScreenRoute)
},
onNavigateToJ = {
navigator.navigateSingleTopTo(JScreenRoute)
},
onNavigateToK = {
navigator.navigateSingleTopTo(KScreenRoute)
},
frontLayerContent = { MyNavigationHost(navController = navigator) }
)
}
}
i do not like the repeated
onNavigateTo#
, is there another "approach" where i can pass multiple lamdas more "succinctly"?
s
An option is to create a class
ScreenNavigator
or whatever, and as a constructor have it accept the
NavConroller
and have that define all the onNavigateToX functions. Then simply pass that class to MyApp. Then on the call site inside
MyApp
, it will be
screenNavigator.onX
instead of just
onX
.
t
does that approach keep with the following "Rule"?
Navigate calls triggered by other composable functions
The NavController's navigate function modifies the NavController's internal state. To comply with the single source of truth principle, *only the composable function or state holder that hoists the NavController instance should make navigation calls*. Navigation events triggered from other composable functions lower in the UI hierarchy need to expose those events to the caller appropriately using functions.
s
or state holder that hoists the NavController instance
Yes.
Here, in NiA, that class takes in the NavHost, and uses it to navigate. It’s created here and in their case they pass a function to allow child composables to navigate. You can adjust it to your case though and have a class which does only the navigation parts instead of what they are doing there, so that it is passed by itself down the composable tree, and you can call the functions yourself, instead of having to smth like this. In general, it’s up to you and your requirements and what makes sense, you know 😊