[[["容易理解","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,["# Integrate Lifecycle with Compose\n\nThe Lifecycle library offers built-in APIs that let you integrate with Jetpack\n[Compose](/jetpack/compose). Key APIs include the following:\n\n- Flows for the current `Lifecycle.State`.\n- `LifecycleEffects` that lets you run a block based on a specific `Lifecycle.Event`.\n\nThese integrations provide convenient hooks to manage Lifecycles within the\nCompose hierarchy. This document outlines how you can use them in your app.\n| **Note:** The APIs described in this document were introduced in Lifecycle version 2.7.0.\n\nCollect lifecycle state with flows\n----------------------------------\n\nLifecycle exposes a `currentStateFlow` property that provides the current\n`Lifecycle.State` as a Kotlin `StateFlow`. You can collect this `Flow` as\n`State`. This allows your app to read changes in the Lifecycle during\ncomposition. \n\n val lifecycleOwner = LocalLifecycleOwner.current\n val stateFlow = lifecycleOwner.lifecycle.currentStateFlow\n ...\n val currentLifecycleState by stateFlow.collectAsState()\n\nThe preceding example is accessible using the `lifecycle-common` module. The\n`currentStateAsState()` method is available in the `lifecycle-runtime-compose`\nmodule, which lets you conveniently read the current Lifecycle state with a\nsingle line. The following example demonstrates this: \n\n val lifecycleOwner = LocalLifecycleOwner.current\n val currentLifecycleState = lifecycleOwner.lifecycle.currentStateAsState()\n\nRun code on lifecycle events\n----------------------------\n\nThere are also `LifecycleEffects` that let you run a block when a particular\n`Lifecycle.Event` occurs. \n\n LifecycleEventEffect(Lifecycle.Event.ON_START) {\n // do something here\n }\n\n| **Warning:** You cannot use this to listen for `Lifecycle.Event.ON_DESTROY` since composition ends before this signal is sent.\n\nIn addition to the `LifecycleEventEffect`, you can also use\n`LifecycleStartEffect` and `LifecycleResumeEffect`. These APIs are tied to\nspecific events. They also offer an additional block within their primary block\nthat helps clean up any code that the event might have kicked off.\n\n### LifecycleStartEffect\n\nThe `LifecycleStartEffect` is similar to the `LifecycleEffect`, but it runs only\non `Lifecycle.Event.ON_START` events. It also accepts keys that work like other\nCompose keys. When the key changes, it triggers the block to run again.\n\nWhen there is a `Lifecycle.Event.ON_STOP` event or the effect exits composition,\nit executes a `onStopOrDispose` block. This allows for the clean up of any work\nthat was part of the starting block. \n\n LifecycleStartEffect {\n // ON_START code is executed here\n\n onStopOrDispose {\n // do any needed clean up here\n }\n }\n\n| **Note:** The `onStopOrDispose` block is always required and if it is not needed, you should use the `LifecycleEffect`, passing in the `Lifecycle.Event.ON_START` event instead.\n\n### LifecycleResumeEffect\n\nThe `LifecycleResumeEffect` works in the same way as the\n`LifecycleStartedEffect`, but it executes on the `Lifecycle.Event.ON_RESUME`\nevent instead. It also provides an `onPauseOrDispose` block that performs the\nclean up. \n\n LifecycleResumeEffect {\n // ON_RESUME code is executed here\n\n onPauseOrDispose {\n // do any needed clean up here\n }\n }"]]