navigation-compose
Gradle:
dependencies {
implementation("ro.dragossusi:navigation:" )
}
Features
- Route navigation
- Key-Value arguments
- Backstack
- ViewModel
Usage
I made this similar to android's navigation component, you declare routes and navigate using the route String.
There is also a sample app in sample
directory.
Create a NavController using
val navController = rememberNavController()
Create the graph and declare the composable methods like this:
NavHost(
navController,
startRoute = "home"
) {
composable("home") {
HomeScreen(
onGoToItem = {
navController.navigate("item")
},
onGoToHelp = {
navController.navigate("help")
}
)
}
composable("item") {
ItemScreen(
onClick = navController::navigateUp
)
}
composable("help") {
HelpScreen(
onClick = navController::navigateUp
)
}
}
You can extend ViewModel
and use the viewModelScope
to launch coroutines whenever you want, they will be canceled when the screen is removed from backstack.
To obtain a ViewModel
instance use the rememberViewModel
method.
val viewModel: ItemViewModel = rememberViewModel(factory = <factory>)