Ahmed
11/20/2024, 10:10 AMGame
is in a separate module that is introduced by
fun NavController.navigateToGame() {
navigate(route = Game)
}
fun NavGraphBuilder.gameGraph() {
navigation<Game>(startDestination = Match) {
composable<Match> { … }
composable<InGame> { … }
…
}
}
How would you handle the internal navigation of Game
?Stylianos Gakis
11/20/2024, 11:15 AMStylianos Gakis
11/20/2024, 11:16 AMAhmed
11/20/2024, 12:11 PM:featureA
to :featureB
we use the approach inspired from here.
From the repo that you have shared, I can see that you have also used the same approach
nestedGraphs: NavGraphBuilder.() -> Unit
As you also mentioned
Your feature modules should optimally just see aThat is the point of thelambda, without having to know anything about that module at allnavigateToX: () -> Unit
navigateToGame()
.
But I am unsure about how to navigate to InGame
from Match
if the destinations are internal to the Game
module.
How would you handle a case if the [internal] navigation of Game
module is.
Match > InGame > Result
For your loginGraph
I can see that you have passed a Navigator
object. That make it seems like the solution for internal navigation handling is to pass along a NavController
.
Is my assumption correct?Ahmed
11/20/2024, 12:13 PMfun NavGraphBuilder.gameGraph(navigator: NavController) {
…
}
Stylianos Gakis
11/20/2024, 12:52 PMfun NavGraphBuilder.gameGraph(navigateToOtherModule: () -> Unit) {
composable { Screen(onNavigateToX = navigateToOtherModule) }
}
And your :app
module will call the gameGraph
like:
NavHost() {
gameGraph(navigateToOtherModule = { navController.navigate(WhateverSinceAppModuleKnowsAboutEverything) })
}
"if the destinations are internal to theAll sub-modules will need to expose at least one destination which is in fact not internal, or at least some public extension function on NavController which does know how to navigate there. What we do is keep one type public as an "entry point" to that feature module: https://github.com/HedvigInsurance/android/blob/1f6e80cbcdea079155a8b83593b4815971[…]kotlin/com/hedvig/android/feature/movingflow/MovingFlowGraph.kt So the graph itself is accessible from themodule."Game
:app
module, and if you want to navigate into this feature, you just navigate to the graphAhmed
11/20/2024, 2:12 PMLocationModule
. Within that module there are 3 screens.
Screen 1 -> Countries [onTapOfCountryItem navigate to Screen 2]
Screen 2 -> Cities [onTapOfCityItem navigate to Screen 3]
Screen 3 -> Counties [onTapOfCounty selection is complete. Finish this navigation flow]
Another example can be a quiz app. Within the QuizModule
Screen 1 > Question [onTapOfOption navigate to correct or wrong (Screen 2)]
Screen 2 > Correct or Wrong [onTapOfNext give a new question]
I understand that the entry point for
LocationModule -> locationGraph()
QuizModule -> quizGraph()
But I am not sure on how will I navigate within the same module for the screens that are local to that module.Stylianos Gakis
11/20/2024, 2:20 PMfun NavGraphBuilder.someGraph(
navController: NavController, // used for internal navigation
navigateToFeatureA: () -> Unit,
navigateToFeatureA: () -> Unit,
)
That should cover all of your use cases.Ahmed
11/20/2024, 3:47 PMWarning: Don't pass yourto your composables. Expose an event as described here.NavController
Stylianos Gakis
11/20/2024, 3:49 PMAhmed
11/20/2024, 3:50 PM