0Pricing
Jetpack Compose Academy · บทเรียน

ส่งอาร์กิวเมนต์ระหว่างหน้าจอ

ส่งข้อมูลไปตามเส้นทางการนำทาง

ส่งอาร์กิวเมนต์ระหว่างหน้าจอ เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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. 📦

คำถามที่พบบ่อย

บทเรียน “ส่งอาร์กิวเมนต์ระหว่างหน้าจอ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ส่งอาร์กิวเมนต์ระหว่างหน้าจอ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ส่งอาร์กิวเมนต์ระหว่างหน้าจอ”

ส่งข้อมูลไปตามเส้นทางการนำทาง คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ส่งอาร์กิวเมนต์ระหว่างหน้าจอ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. NavController และ NavHost
  2. ปลายทางและเส้นทาง Composable
  3. ส่งอาร์กิวเมนต์ระหว่างหน้าจอ
  4. สแตกย้อนกลับและลิงก์ลึก
← กลับไปที่ Jetpack Compose Academy