Trạng thái trong Compose
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tìm hiểu cách thiết lập và quản lý trạng thái trong ứng dụng dựa trên Compose cũng như cách định cấu hình giao diện người dùng để phản ứng với các thay đổi về trạng thái. Xem cách tạo trạng thái có thể quan sát, cách giữ lại trạng thái trong các lần kết hợp lại hoặc thay đổi cấu hình, cũng như cách định cấu trúc các thành phần kết hợp để có luồng dữ liệu tối ưu.
Điểm chính
- Nếu trạng thái của ứng dụng nằm trong thành phần kết hợp, hãy sử dụng
remember
để duy trì trạng thái trong quá trình kết hợp lại.
- Sử dụng
rememberSaveable
để duy trì trạng thái trên các thay đổi về cấu hình.
- Chuyển trạng thái lên trên là một mẫu lập trình mà bạn di chuyển trạng thái đến phương thức gọi của một thành phần kết hợp. Nếu có thể, hãy sử dụng tính năng chuyển trạng thái lên trên để giúp thành phần kết hợp dễ sử dụng lại và kiểm thử hơn.
- Sử dụng lớp
ViewModel
để giữ trạng thái hiển thị trong một phần tử giữ trạng thái có thể quan sát, đóng gói trạng thái tốt hơn và tạo một nguồn giá trị duy nhất cho giao diện người dùng.
Tài nguyên
Các bộ sưu tập chứa hướng dẫn này
Hướng dẫn này là một phần của các bộ sưu tập Hướng dẫn nhanh được tuyển chọn này, bao gồm các mục tiêu phát triển Android rộng hơn:
Kiến thức cơ bản về Compose (bộ sưu tập video)
Loạt video này giới thiệu nhiều API Compose, nhanh chóng cho bạn biết những API có sẵn và cách sử dụng các API đó.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 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)"]]