0Pricing
Jetpack Compose Academy · Aula

NavController e NavHost

Defina o grafo de navegação do aplicativo.

NavController e NavHost é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 1 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.

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. 🎉

Perguntas Frequentes

A aula “NavController e NavHost” é grátis?

Sim — o texto completo de “NavController e NavHost” é 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 “NavController e NavHost”?

Defina o grafo de navegação do aplicativo. 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 1 de 4.

Quanto tempo leva a aula “NavController e NavHost”?

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

  1. NavController e NavHost
  2. Destinos e Rotas Composable
  3. Passando Argumentos entre Telas
  4. Pilha de Retorno e Links Profundos
← Voltar para Jetpack Compose Academy