Jetpack Compose Academy · Lektion

Composable-Ziele und Routen

Registrieren Sie jeden Bildschirm als Route.

Lektion 2 von 413 Schritte

Composable-Ziele und Routen ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

Screens as Destinations

Every screen in your graph is a destination. You register one with the composable builder inside the NavHost, naming it with a route.

composable("home") { HomeScreen() }

What a Route Really Is

A route is a unique string key for a destination. Compose matches the string you navigate to against the routes you declared.

Keep Routes in Constants

Typos in raw strings cause silent failures. Store each route as a constant so the compiler catches mistakes for you.

object Routes {
    const val HOME = "home"
    const val SETTINGS = "settings"
}

A Sealed Class of Screens

For larger apps, a sealed class groups all routes in one place. It makes adding screens safe and easy to discover.

sealed class Screen(val route: String) {
    object Home : Screen("home")
    object Settings : Screen("settings")
}

Register Each Destination

List every screen with its own composable block. The lambda body is the UI shown when that route becomes active.

NavHost(navController, startDestination = Screen.Home.route) {
    composable(Screen.Home.route) { HomeScreen() }
    composable(Screen.Settings.route) { SettingsScreen() }
}

Navigate to a Route

Use the same route string to move there. The NavController finds the matching destination and the NavHost renders it.

navController.navigate(Screen.Settings.route)

Routes Must Be Unique

Two destinations cannot share a route. Each route is an address, and duplicate addresses make navigation ambiguous and unreliable.

Pass the Controller Down

Screens often trigger navigation, so hand each destination a way to navigate. A common pattern passes lambdas instead of the whole controller.

composable("home") {
    HomeScreen(onOpenSettings = { navController.navigate("settings") })
}

Keep Screens Decoupled

Passing lambdas instead of the NavController keeps screens testable and reusable. The screen says what happened, not how to navigate.

Routes Look Like URLs

Treat routes like little URLs. This style pays off later when you add arguments and deep links that open routes from outside the app.

Group Related Flows

As graphs grow, you can split screens into nested graphs with their own routes. This keeps a feature's destinations tidy and self-contained.

Quick Check

Why prefer constants or a sealed class for routes?

Recap

You learned that each screen is a destination with a unique route, and that constants or a sealed class keep those routes safe and organized. 🗂️

Kostenlos starten

Lerne Kotlin mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
30
Lektionen
120

Häufig gestellte Fragen

Ist die Lektion „Composable-Ziele und Routen“ kostenlos?

Ja — der vollständige Text von „Composable-Ziele und Routen“ 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 „Composable-Ziele und Routen“?

Registrieren Sie jeden Bildschirm als Route. 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 2 von 4.

Wie lange dauert die Lektion „Composable-Ziele und Routen“?

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

  1. NavController und NavHost
  2. Composable-Ziele und Routen
  3. Argumente zwischen Bildschirmen übergeben
  4. Back Stack und Deep Links
← Zurück zu Jetpack Compose Academy