0Pricing
Jetpack Compose Academy · Lezione

Passare argomenti tra le schermate

Invii dati lungo la route di navigazione.

Passare argomenti tra le schermate è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Screens Need Data

A detail screen needs to know which item to show. Navigation Compose lets you carry that data along the route itself.

Declare a Placeholder

Add a placeholder in curly braces to a route. It marks where a value will slot in when you navigate.

composable("profile/{userId}") { backStackEntry ->
    ProfileScreen()
}

Define the Argument

List each placeholder in arguments and give it a type. This tells Compose how to parse the value out of the route.

composable(
    "profile/{userId}",
    arguments = listOf(navArgument("userId") { type = NavType.IntType })
) { }

Send the Value

To pass data, build the route string with the real value in place of the placeholder. The receiving screen reads it back out.

navController.navigate("profile/42")

Read It from the Entry

The backStackEntry carries the arguments bundle. Pull your value out by the same name you declared in the route.

composable("profile/{userId}") { entry ->
    val id = entry.arguments?.getInt("userId")
    ProfileScreen(userId = id)
}

Choose the Right NavType

The NavType decides parsing: StringType, IntType, BoolType, and more. Match it to your data so values arrive correctly typed.

Optional Arguments

Use a query-style optional argument with a default value for data that may be missing, like a filter that is not always set.

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

Pass Small Data Only

Route arguments are for identifiers, not whole objects. Send an id, then load the full data from your repository or ViewModel.

Encode Tricky Values

Strings with slashes or spaces can break a route. URL-encode them before navigating and decode after reading, just like a real URL.

Let the ViewModel Read It

A ViewModel can grab arguments from SavedStateHandle, so your screen stays clean and survives recreation with the right data.

class ProfileViewModel(state: SavedStateHandle) {
    val userId: Int = checkNotNull(state["userId"])
}

Keep Argument Names Aligned

The placeholder, the navArgument, and the lookup key must all use the same name. A mismatch returns null instead of your value.

Quick Check

How should you send a complex item to a detail screen?

Recap

You can now pass data by adding a placeholder to a route, typing it with navArgument, and reading it back from the back stack entry. 📦

Domande Frequenti

La lezione «Passare argomenti tra le schermate» è gratuita?

Sì — il testo completo di «Passare argomenti tra le schermate» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Cosa imparerò in «Passare argomenti tra le schermate»?

Invii dati lungo la route di navigazione. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Jetpack Compose Academy?

Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Passare argomenti tra le schermate»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?

Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. NavController e NavHost
  2. Destinazioni e route Composable
  3. Passare argomenti tra le schermate
  4. Back stack e deep link
← Torna a Jetpack Compose Academy