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

ปลายทางและเส้นทาง Composable

ลงทะเบียนแต่ละหน้าจอเป็นเส้นทาง

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

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

Screens as Destinations

Every screen in your graph is a destination. You register one with the composable builder inside the NavHost, naming it with a route.

composable("home") { HomeScreen() }

What a Route Really Is

A route is a unique string key for a destination. Compose matches the string you navigate to against the routes you declared.

Keep Routes in Constants

Typos in raw strings cause silent failures. Store each route as a constant so the compiler catches mistakes for you.

object Routes {
    const val HOME = "home"
    const val SETTINGS = "settings"
}

A Sealed Class of Screens

For larger apps, a sealed class groups all routes in one place. It makes adding screens safe and easy to discover.

sealed class Screen(val route: String) {
    object Home : Screen("home")
    object Settings : Screen("settings")
}

Register Each Destination

List every screen with its own composable block. The lambda body is the UI shown when that route becomes active.

NavHost(navController, startDestination = Screen.Home.route) {
    composable(Screen.Home.route) { HomeScreen() }
    composable(Screen.Settings.route) { SettingsScreen() }
}

Navigate to a Route

Use the same route string to move there. The NavController finds the matching destination and the NavHost renders it.

navController.navigate(Screen.Settings.route)

Routes Must Be Unique

Two destinations cannot share a route. Each route is an address, and duplicate addresses make navigation ambiguous and unreliable.

Pass the Controller Down

Screens often trigger navigation, so hand each destination a way to navigate. A common pattern passes lambdas instead of the whole controller.

composable("home") {
    HomeScreen(onOpenSettings = { navController.navigate("settings") })
}

Keep Screens Decoupled

Passing lambdas instead of the NavController keeps screens testable and reusable. The screen says what happened, not how to navigate.

Routes Look Like URLs

Treat routes like little URLs. This style pays off later when you add arguments and deep links that open routes from outside the app.

Group Related Flows

As graphs grow, you can split screens into nested graphs with their own routes. This keeps a feature's destinations tidy and self-contained.

Quick Check

Why prefer constants or a sealed class for routes?

Recap

You learned that each screen is a destination with a unique route, and that constants or a sealed class keep those routes safe and organized. 🗂️

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

บทเรียน “ปลายทางและเส้นทาง Composable” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ปลายทางและเส้นทาง Composable”

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

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

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

บทเรียน “ปลายทางและเส้นทาง Composable” ใช้เวลานานแค่ไหน

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

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

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

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

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