Before I use a navigation graph in my Android Studio project, just like the article says.
I use an xml file located in res\\navigation
folder to include all my different destinations.
At present, I'm learning Jetpack Compose Navigation by the article .
Code A is from the official sample project mentioned in the above article.
It seems that Jetpack Compose Navigation use Code A and other codes to navigate, and I can't find any XML file in res\\navigation
folder.
1: Is the navigation graph obsoleted when I use Jetpack Compose Navigation ?
2: Don't I need to use the navigation graph located in res\\navigation
folder again when I use Jetpack Compose Navigation?
Code A
@Composable
fun RallyNavHost(navController: NavHostController, modifier: Modifier = Modifier) {
NavHost(
navController = navController,
startDestination = Overview.name,
modifier = modifier
) {
composable(Overview.name) {
OverviewBody(
onClickSeeAllAccounts = { navController.navigate(Accounts.name) },
onClickSeeAllBills = { navController.navigate(Bills.name) },
onAccountClick = { name ->
navigateToSingleAccount(navController, name)
},
)
}
composable(Accounts.name) {
AccountsBody(accounts = UserData.accounts) { name ->
navigateToSingleAccount(navController = navController, accountName = name)
}
}
composable(Bills.name) {
BillsBody(bills = UserData.bills)
}
...
}
}
Navigation has always had three ways of building a NavGraph
object:
Manually, by using the NavGraph
constructor itself. While this serves as the basis for all other methods listed here, you aren't meant to use these APIs directly.
Building the graph via Navigation XML . This is a way of building a graph at compile time using the Navigator Editor tooling and the Safe Args plugin . This method only supports Navigation with Fragments.
Using the Navigation Kotlin DSL . This provides a type-safe way of building a navigation graph programmatically, at runtime by using a Kotlin DSL . This method supports both Navigation with Fragments and Navigation Compose.
As Compose is a way of programmatically building your UI, Navigation only supports the programmatic version of building the graph - via that Kotlin DSL that is exactly what that trailing lambda of NavHost
provides you: it is that same NavGraphBuilder
scope that allows you to call navigation(...) {}
to build a nested graph or composable
to add a new destination to your graph.
All of the concepts are the same because the underlying NavGraph
you construct ends up being the exact same set of objects at runtime, no matter how you actually build the graph.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.