Destacando a Rota Selecionada
Sincronize a seleção com a pilha de retorno.
Destacando a Rota Selecionada é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Jetpack Compose Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Jetpack Compose Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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?.routeCompare 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.routeHierarchy 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
} == trueNavigate 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 = falseOne 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. 🎯
Perguntas Frequentes
A aula “Destacando a Rota Selecionada” é grátis?
Sim — o texto completo de “Destacando a Rota Selecionada” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Jetpack Compose Academy, atualize para CoddyKit PRO. O curso de Jetpack Compose Academy inclui 4 aulas no total.
O que vou aprender em “Destacando a Rota Selecionada”?
Sincronize a seleção com a pilha de retorno. Você pratica Jetpack Compose Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Jetpack Compose Academy?
Nenhuma experiência prévia é necessária. Jetpack Compose Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Destacando a Rota Selecionada”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Jetpack Compose Academy?
Sim. Cada aula de Jetpack Compose Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- NavigationBar com Várias Abas
- Destacando a Rota Selecionada
- Preservando o Estado das Abas ao Alternar
- TabRow e Páginas Deslizáveis