สถานะในเขียน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ดูวิธีสร้างและจัดการสถานะในแอปที่ใช้ Compose และวิธีกำหนดค่า UI ให้ตอบสนองต่อการเปลี่ยนแปลงสถานะ ดูวิธีสร้างสถานะที่สังเกตได้ วิธีเก็บสถานะไว้ระหว่างการจัดเรียงใหม่หรือการเปลี่ยนแปลงการกําหนดค่า และวิธีจัดโครงสร้างคอมโพสิเบิลเพื่อให้การไหลของข้อมูลมีประสิทธิภาพสูงสุด
ข้อมูลสำคัญ
- หากสถานะของแอปอยู่ในคอมโพสิเบิลภายใน ให้ใช้
remember
เพื่อเก็บสถานะไว้ระหว่างการคอมโพสิชันใหม่
- ใช้
rememberSaveable
เพื่อคงสถานะไว้เมื่อมีการทําการเปลี่ยนแปลงการกําหนดค่า
- การย้ายสถานะเป็นรูปแบบการเขียนโปรแกรมที่คุณย้ายสถานะไปยังผู้เรียกใช้คอมโพสิเบิล ใช้การยกระดับสถานะเมื่อเป็นไปได้เพื่อให้คอมโพสิชันนิ่งนํากลับมาใช้ซ้ำและทดสอบได้มากขึ้น
- ใช้คลาส
ViewModel
เพื่อเก็บสถานะที่แสดงในตัวแฮนเดิลสถานะที่สังเกตได้ ซึ่งจะรวมสถานะไว้ได้ดียิ่งขึ้นและสร้างแหล่งที่มาของข้อมูลเดียวสําหรับ UI
แหล่งข้อมูล
คอลเล็กชันที่มีคู่มือนี้
คู่มือนี้เป็นส่วนหนึ่งของคอลเล็กชันคู่มือฉบับย่อที่มีการดูแลจัดการ ซึ่งครอบคลุมเป้าหมายการพัฒนา Android ที่กว้างขึ้น ดังนี้
องค์ประกอบพื้นฐาน (คอลเล็กชันวิดีโอ)
ชุดวิดีโอนี้จะแนะนำ Compose API ต่างๆ โดยแสดงสิ่งที่มีให้ใช้งานและวิธีใช้อย่างรวดเร็ว
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# State in Compose\n\n\u003cbr /\u003e\n\nLearn how to establish and manage state in your Compose-based app and how\nto configure the UI to react to changes in state. See how to create observable\nstates, how to retain state across recompositions or configuration changes, and\nhow to structure your composables for optimal data flow. \n\nKey points\n----------\n\n- If your app's state is internal to a composable, use [`remember`](/reference/kotlin/androidx/compose/runtime/package-summary#remember(kotlin.Function0)) to persist the state across re-composition.\n- Use [`rememberSaveable`](/reference/kotlin/androidx/compose/runtime/saveable/package-summary#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)) to persist the state across configuration changes.\n- *State hoisting* is a programming pattern where you move the state to the caller of a composable. Where possible, use state hoisting to make the composable more reusable and testable.\n- Use the [`ViewModel`](/reference/androidx/lifecycle/ViewModel) class to hold an exposed state in an observable state holder, better encapsulating the state and creating a single source of truth for the UI.\n\nResources\n---------\n\n- [Codelab: State in Jetpack Compose](/codelabs/jetpack-compose-state#0)\n\nCollections that contain this guide\n-----------------------------------\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\n### Compose basics (video collection)\n\nThis series of videos introduces various Compose APIs, quickly showing you what's available and how to use them. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/compose-basics) \n\nHave questions or feedback\n--------------------------\n\nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]