NavController und NavHost
Definieren Sie den Navigationsgraphen Ihrer App.
NavController und NavHost ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Moving Between Screens
Real apps have many screens. Navigation Compose is the official library that lets you move between them without juggling fragments or activities. 🧭
Add the Dependency
Navigation lives in its own artifact. Add androidx.navigation:navigation-compose to your module's build file before you can use any of it.
dependencies {
implementation("androidx.navigation:navigation-compose:2.7.7")
}Meet the NavController
The NavController is the brain of navigation. It tracks where you are, remembers history, and carries out every move you ask for.
Remember the Controller
You create one with rememberNavController() so it survives recomposition. Make it once near the top of your app and pass it down.
val navController = rememberNavController()Meet the NavHost
The NavHost is the container that swaps screens in and out. It shows exactly one destination at a time, based on the controller.
Wire Host to Controller
A NavHost needs two things: the navController that drives it and a startDestination that says which screen to show first.
NavHost(
navController = navController,
startDestination = "home"
) { }The startDestination
The startDestination is the route shown when the app opens. It is also the screen users land on after the back stack empties.
Declaring Destinations
Inside the NavHost lambda you list your screens with composable("route"). Each one ties a route name to the UI it should show.
NavHost(navController, startDestination = "home") {
composable("home") { HomeScreen() }
composable("profile") { ProfileScreen() }
}Navigate by Route
To change screens, call navController.navigate("route"). The host sees the request and swaps in the matching destination instantly.
Button(onClick = { navController.navigate("profile") }) {
Text("Open profile")
}One Controller, One Host
Keep a single NavController paired with a single NavHost for an app's main flow. Sharing one source of truth avoids confusing, out-of-sync history.
Why Routes Are Strings
Routes are just String identifiers, like tiny URLs inside your app. This keeps destinations decoupled and easy to link later.
Quick Check
Which piece actually performs the screen change?
Recap
You met the duo behind navigation: the NavController drives moves and history, while the NavHost shows one destination at a time from your route list. 🎉
Häufig gestellte Fragen
Ist die Lektion „NavController und NavHost“ kostenlos?
Ja — der vollständige Text von „NavController und NavHost“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „NavController und NavHost“?
Definieren Sie den Navigationsgraphen Ihrer App. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „NavController und NavHost“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- NavController und NavHost
- Composable-Ziele und Routen
- Argumente zwischen Bildschirmen übergeben
- Back Stack und Deep Links