0Pricing
Android Academy · Lesson

Passing Arguments

Send data to a destination.

Passing Arguments is a free Android Academy lesson on CoddyKit — lesson 3 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.

Passing Data Between Screens

Often a destination needs data — like the id of the item to display. In Navigation Compose you encode arguments directly in the route, similar to a URL.

Route with a Placeholder

Declare a placeholder in the route using curly braces. Here {id} is an argument named id.

composable("details/{id}") { backStackEntry ->
    // read id here
}

Declaring the Argument Type

Tell the navigator the argument's type with arguments and navArgument. This validates and converts the value for you.

composable(
    "details/{id}",
    arguments = listOf(navArgument("id") { type = NavType.IntType })
) { /* ... */ }

NavType

NavType declares the argument's data type — IntType, StringType, BoolType, and more. The navigator parses the route value accordingly.

Reading the Argument

The destination lambda gives you a backStackEntry. Read arguments from backStackEntry.arguments.

composable(
    "details/{id}",
    arguments = listOf(navArgument("id") { type = NavType.IntType })
) { backStackEntry ->
    val id = backStackEntry.arguments?.getInt("id") ?: 0
    DetailsScreen(id = id)
}

Navigating with a Value

To open the screen, build the route string with the real value substituted for the placeholder.

navController.navigate("details/42")

Building the Route Safely

Use string templates to insert the value. Keep the path segment exactly matching the declared route.

val itemId = 42
navController.navigate("details/$itemId")

Optional Arguments

Optional arguments use a query-string style with ?name=value and a defaultValue. They can be omitted when navigating.

composable(
    "search?query={query}",
    arguments = listOf(navArgument("query") {
        defaultValue = ""
    })
) { /* ... */ }

String Arguments Need Care

String arguments must be URL-safe. Avoid passing long text or slashes through routes; for those, prefer a shared ViewModel or repository keyed by an id.

Pass IDs, Not Objects

Best practice: pass a small identifier (like an item id) through the route, then look up the full object on the destination screen. Do not serialize big objects into routes.

Putting It Together

A list navigates to a detail screen, passing the selected id.

composable("list") {
    ListScreen(onItemClick = { id ->
        navController.navigate("details/$id")
    })
}
composable(
    "details/{id}",
    arguments = listOf(navArgument("id") { type = NavType.IntType })
) { entry ->
    val id = entry.arguments?.getInt("id") ?: 0
    DetailsScreen(id = id)
}

Quick Check

How do you declare a required integer argument named id in a route?

Recap

Passing arguments:

  • Put a {placeholder} in the route.
  • Declare the type with navArgument + NavType.
  • Read it from backStackEntry.arguments.
  • Navigate with the value substituted; pass small ids, not big objects.

Frequently asked questions

Is the “Passing Arguments” lesson free?

Yes — the full text of “Passing Arguments” 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 “Passing Arguments”?

Send data to a destination. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Passing Arguments” 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