Compose 中的狀態
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
瞭解如何在以 Compose 為基礎的應用程式中建立及管理狀態,以及如何設定 UI 以回應狀態變更。瞭解如何建立可觀察的狀態、如何在重組或設定變更時保留狀態,以及如何為可組合項建立結構,以便取得最佳資料流。
重點
- 如果應用程式的狀態是可組合項的內部狀態,請使用
remember
在重新組合時保留狀態。
- 使用
rememberSaveable
在設定變更期間保留狀態。
- 狀態提升是一種程式設計模式,可將狀態移至可組合函式的呼叫端。盡可能使用狀態提升功能,讓可組合函式更易重複使用及測試。
- 使用
ViewModel
類別,在可觀察的狀態容器中保留公開的狀態,以便更妥善地封裝狀態,並為 UI 建立單一真實來源。
資源
包含此指南的集合
本指南是精選的快速指南系列之一,涵蓋更廣泛的 Android 開發目標:
Compose 基本概念 (影片集合)
本系列影片將介紹各種 Compose API,快速向您展示可用的 API 和使用方式。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[],[],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)"]]