Android Academy · Lesson

Passing Arguments

Send data to a destination.

Lesson 3 of 413 steps

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

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.
Free to start

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 “Passing Arguments” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Passing Arguments”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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