Skip to content

Jetpack Compose Navigation — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jetpack Compose Navigation. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

You navigate to a screen and the back button takes you to the wrong place, or you pass arguments that arrive as null, or the screen recreates when it shouldn't.

Wrong Approach ❌

@Composable
fun BadNav() {
    val navController = rememberNavController()
    NavHost(navController, startDestination = "home") {
        composable("home") { Home(navController) }
        composable("details/{id}") { backStackEntry ->
            val id = backStackEntry.arguments?.getString("id") // Null!
            Details(id ?: "missing")
        }
    }
}
// Direct navigation without route management
navController.navigate("details/123") // Multiple back stack entries

Output: Null argument, multiple "details" entries on the back stack.

Right Approach ✅

// Define routes as sealed class
sealed class Screen(val route: String) {
    object Home : Screen("home")
    object Details : Screen("details/{id}") {
        fun create(id: String) = "details/$id"
    }
}

@Composable
fun GoodNav() {
    val navController = rememberNavController()
    NavHost(navController, startDestination = Screen.Home.route) {
        composable(
            route = Screen.Details.route,
            arguments = listOf(navArgument("id") { type = NavType.StringType })
        ) { backStackEntry ->
            val id = backStackEntry.arguments?.getString("id") ?: return@composable
            Details(id)
        }
        composable(Screen.Home.route) {
            Home(onNavigate = { navController.navigate(Screen.Details.create(it)) })
        }
    }
}
// Single top — prevents duplicate entries
navController.navigate(Screen.Details.create(id)) {
    launchSingleTop = true
    restoreState = true
}

Output: Type-safe arguments, single top stack, proper state restoration.

Prevention

  • Always use NavType for arguments — never parse them manually.
  • Use launchSingleTop = true to prevent duplicate screens.
  • Use restoreState = true to preserve state when navigating back.
  • Define routes in a sealed class for compile-time safety.

Common Mistakes with compose navigation

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How do I pass complex objects via navigation?

Use NavType.ParcelableType or NavType.SerializableType for Parcelable/Serializable objects. For large data, pass an ID and load from a Repository.

### What is the difference between navigate() and popBackStack()?

navigate() adds a new entry to the back stack. popBackStack() removes the current entry (or up to a specific route). Use navController.popBackStack() for back button behavior.

### How do I handle deep links in Compose navigation?

Define deep link patterns in navDeepLink { uriPattern = "myapp://details/{id}" } and add them to the composable() call. The system handles the rest.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro