Skip to content

Instantly share code, notes, and snippets.

@AfzalivE
Last active September 7, 2021 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfzalivE/06dfcd7de37414a0ecc11254aee64a3f to your computer and use it in GitHub Desktop.
Save AfzalivE/06dfcd7de37414a0ecc11254aee64a3f to your computer and use it in GitHub Desktop.
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