Skip to content

Instantly share code, notes, and snippets.

@AfzalivE
Last active Sep 7, 2021
Embed
What would you like to do?
jetpack compose navigation
dependencies {
// other dependencies
implementation "androidx.compose.navigation:navigation:1.0.0-SNAPSHOT"
}
@Composable
fun VeryBasicNav() {
NavHost(startDestination = "Profile") { // this: NavGraphBuilder
composable("Profile") {
Profile()
}
composable("Dashboard") {
Dashboard()
}
composable("Scrollable") {
Scrollable()
}
}
}
allprojects {
repositories {
// .. existing repositories
maven { url = 'https://androidx.dev/snapshots/builds/[buildId]/artifacts/ui/repository' }
}
}
@Composable
private fun ComposeNavApp() {
val navController = rememberNavController()
val currentScreen by navController.currentBackStackEntryAsState()
Scaffold(
topBar = {
TopAppBar(
title = {
Text(currentScreen?.destination?.id.toString())
}
)
},
bodyContent = {
NavHost(
navController = navController,
startDestination = "Profile"
) { // this: NavGraphBuilder
composable("Profile") {
Profile()
}
composable("Dashboard") {
Dashboard()
}
composable("Scrollable") {
Scrollable()
}
}
}
)
}
@Composable
fun Profile() {
val navController = AmbientNavController.current
Column(modifier = Modifier.fillMaxSize().then(Modifier.padding(8.dp))) {
Text(text = Screen.Profile.title)
Button(
onClick = { navController.navigate("Dashboard") },
) {
Text("Open Dashboard")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment