From the example <here>, If the `Game` is in a sep...
# compose-android
a
From the example here, If the
Game
is in a separate module that is introduced by
Copy code
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
?
a
Thanks for taking the time to provide an input @Stylianos Gakis . When we face a case where we have to navigate from a
: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 a
navigateToX: () -> Unit
lambda, without having to know anything about that module at all
That is the point of the
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.
Copy code
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?
That would make it so
Copy code
fun NavGraphBuilder.gameGraph(navigator: NavController) {
  …
}
s
The Navigator does not do anything but delegate to NavController. Don't look too much into that. It is only there for some lifecycle related stuff. The answer is that your extension on the builder should simply take in a lambda which does the navigation for them. So:
Copy code
fun NavGraphBuilder.gameGraph(navigateToOtherModule: () -> Unit) {
  composable { Screen(onNavigateToX = navigateToOtherModule) }
}
And your
:app
module will call the
gameGraph
like:
Copy code
NavHost() {
 gameGraph(navigateToOtherModule = { navController.navigate(WhateverSinceAppModuleKnowsAboutEverything) })
}
"if the destinations are internal to the
Game
module."
All 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 the
:app
module, and if you want to navigate into this feature, you just navigate to the graph
a
I think I need to rephrase my question. I am sorry for causing any confusion. I am not looking into how to navigate to another module. I am asking about how can I navigate within a same module. Let's say I have a hypothetical
LocationModule
. Within that module there are 3 screens.
Copy code
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
Copy code
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
Copy code
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.
s
Ah Ahmed sorry for the confusion, it seems I didn't understand your original question that well. Yes, just pass in your NavController to your graph and do whatever you wish with it inside there, that's is indeed the answer.
Copy code
fun NavGraphBuilder.someGraph(
  navController: NavController, // used for internal navigation 
  navigateToFeatureA: () -> Unit,
  navigateToFeatureA: () -> Unit,
)
That should cover all of your use cases.
🫡 1
a
The only sour thing is now https://developer.android.com/guide/navigation/use-graph/navigate#events
Warning: Don't pass your
NavController
to your composables. Expose an event as described here.
s
Yep, you don't pass it to your composable. You pass it to your builder extension functions. It's a very big difference
1
a
Ah! Yes, I overlooked that distinction. Thank you for your kindness and help
blob hug 1