Setting Up NavHost
Define a navigation graph in Compose.
Setting Up NavHost is a free Android Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.
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.NavHostshows destinations and needs astartDestination.- Declare screens with
composable("route"); routes are unique strings.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 36
- Lessons
- 152
Frequently Asked Questions
Is the “Setting Up NavHost” lesson free?
Yes — any 3 lessons of the Android Academy learning path, including “Setting Up NavHost”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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
- Setting Up NavHost
- Navigating Between Screens
- Passing Arguments
- Bottom Navigation