0Pricing
Android Academy · Lesson

Setting Up NavHost

Define a navigation graph in Compose.

Setting Up NavHost is a free Android Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Navigating Between Screens

Most apps have more than one screen. The Navigation Compose library lets you move between composable screens while keeping a back stack.

The Dependency

Add the navigation library to your Gradle file, then you get NavController, NavHost, and helpers.

// build.gradle (module)
implementation("androidx.navigation:navigation-compose:2.7.0")

The NavController

The NavController is the object that performs navigation and remembers the back stack. Create it with rememberNavController().

val navController = rememberNavController()

The NavHost

The NavHost is a composable that displays the current destination. It needs the controller and a startDestination route.

NavHost(
    navController = navController,
    startDestination = "home"
) {
    // destinations go here
}

Defining Destinations

Inside the NavHost lambda, declare each screen with composable("route"). The string is the destination's route.

composable("home") {
    HomeScreen()
}

Routes Are Strings

A route is a unique string id for a destination, like a URL path. You navigate by route, and the NavHost shows the matching composable.

A Two-Screen Graph

Here is a minimal navigation graph with two destinations. "home" is shown first because it is the startDestination.

NavHost(navController, startDestination = "home") {
    composable("home") { HomeScreen() }
    composable("details") { DetailsScreen() }
}

startDestination

The startDestination is the first screen the user sees and the root of the back stack. The app cannot pop past it.

Passing the Controller Down

Screens that trigger navigation need the NavController. Pass it as a parameter, or better, pass simple lambdas like onContinue to keep screens reusable.

composable("home") {
    HomeScreen(onOpenDetails = {
        navController.navigate("details")
    })
}

One NavHost per Area

Typically you have a single NavHost at the top of your app. The controller is created once with rememberNavController() and shared across destinations.

Putting It Together

A complete setup: controller, host, and two destinations.

@Composable
fun App() {
    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = "home"
    ) {
        composable("home") { HomeScreen() }
        composable("details") { DetailsScreen() }
    }
}

Quick Check

Which object remembers the back stack and performs navigation?

Recap

Navigation setup:

  • rememberNavController() creates the controller.
  • NavHost shows destinations and needs a startDestination.
  • Declare screens with composable("route"); routes are unique strings.

Frequently asked questions

Is the “Setting Up NavHost” lesson free?

Yes — the full text of “Setting Up NavHost” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Setting Up NavHost”?

Define a navigation graph in Compose. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Setting Up NavHost” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Setting Up NavHost
  2. Navigating Between Screens
  3. Passing Arguments
  4. Bottom Navigation
← Back to Android Academy