Tower Guidev2
10/20/2022, 11:04 AMTower Guidev2
10/20/2022, 11:05 AMsuper.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) }
)
}
}Tower Guidev2
10/20/2022, 11:07 AMonNavigateTo#, is there another "approach"
where i can pass multiple lamdas more "succinctly"?Stylianos Gakis
10/20/2022, 11:14 AMScreenNavigator 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.Tower Guidev2
10/20/2022, 11:37 AMNavigate 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.
Stylianos Gakis
10/20/2022, 11:39 AMor state holder that hoists the NavController instance Yes.Stylianos Gakis
10/20/2022, 11:46 AM