0Pricing
Jetpack Compose Academy · Lektion

Die ausgewählte Route hervorheben

Synchronisieren Sie die Auswahl mit dem Back Stack.

Die ausgewählte Route hervorheben 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.

Selection Must Be Real

A tab should light up because of where the user actually is, not a value you set by hand. The back stack is your source of truth. 🧭

Read the Current Entry

Ask the NavController for the live currentBackStackEntryAsState. It updates automatically every time navigation changes.

val entry by navController.currentBackStackEntryAsState()

Get the Active Route

From that entry you read the active route string off its destination. That string tells you which tab is showing.

val route = entry?.destination?.route

Compare to Mark Selected

Now set each item selected by comparing the active route to that tab route. The match becomes the highlighted selected tab.

selected = route == tab.route

Hierarchy Beats Equality

For nested graphs, exact equality can miss. Checking the destination hierarchy marks a tab active even on its sub-screens.

selected = entry?.destination?.hierarchy?.any {
  it.route == tab.route
} == true

Navigate on Tap

In onClick, call navController.navigate with the tab route to move there. The selection then follows the back stack on its own.

onClick = { navController.navigate(tab.route) }

Avoid Stacking Copies

Tapping a tab repeatedly should not pile up screens. Use launchSingleTop so only one copy of a destination stays on top.

navController.navigate(tab.route) {
  launchSingleTop = true
}

Pop to the Start

Pressing back from any tab should return to your home tab, not walk every tab you visited. popUpTo the start destination fixes that.

popUpTo(navController.graph.startDestinationId) {
  saveState = true
}

Tint the Active Tab

NavigationBarItem auto-tints the selected item, but you can theme it with colors so the active tab matches your brand.

colors = NavigationBarItemDefaults.colors(
  selectedIconColor = MaterialTheme.colorScheme.primary
)

Label Visibility

Use alwaysShowLabel to decide if unselected tabs still show text. Showing only the active label is a common clean look.

alwaysShowLabel = false

One Reactive Loop

Put it together: read the route once, then in your forEach compare each tab to it. Selection now mirrors navigation perfectly.

Quick Check

You wired selection to the back stack. One quick question.

Recap

You synced highlighting to the back stack: read the current route, compare per tab, navigate with launchSingleTop and popUpTo. Selection now stays honest. 🎯

Häufig gestellte Fragen

Ist die Lektion „Die ausgewählte Route hervorheben“ kostenlos?

Ja — der vollständige Text von „Die ausgewählte Route hervorheben“ 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 „Die ausgewählte Route hervorheben“?

Synchronisieren Sie die Auswahl mit dem Back Stack. 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 „Die ausgewählte Route hervorheben“?

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. NavigationBar mit mehreren Tabs
  2. Die ausgewählte Route hervorheben
  3. Tab-State beim Wechsel bewahren
  4. TabRow und wischbare Seiten
← Zurück zu Jetpack Compose Academy