我們推薦以生命周期感知方式在 Android 上收集數據流。如果您正在用 Jetpack Compose 構建 Android 應用,請使用 collectAsStateWithLifecycle API 以生命周期感知方式從用戶界面收集數據流。
-
使用界面狀態
https://developer.android.google.cn/topic/architecture/ui-layer#consume-ui-state
借助 collectAsStateWithLifecycle,您可以在不需要應用資源時釋放它們,例如當應用處于后臺時。此類資源可能包括 Firebase 查詢、位置或網絡更新及數據庫連接等,在不需要它們的情況下讓其處于活躍狀態會影響用戶設備的運行健康狀況。 請繼續閱讀本文,以詳細了解此 API、以生命周期感知方式收集數據流的理由,以及此 API 與 collectAsState API 的差異。
collectAsStateWithLifecycle
collectAsStateWithLifecycle 是一個可組合函數,可從數據流中收集值,并以生命周期感知方式將最新值表示為 Compose State。每當數據流發出新值時,此 State 對象的值都會更新,從而讓組合 (Composition) 中每個使用 State.value 的對象進行重新組合。
-
State
https://developer.android.google.cn/reference/kotlin/androidx/compose/runtime/State
默認情況下,collectAsStateWithLifecycle 使用 Lifecycle.State.STARTED 從數據流中開始和結束收集值。這些動作會在生命周期 (Lifecycle) 移入和移出目標狀態時發生。您可以通過 minActiveState 參數配置此生命周期狀態。

△ 默認情況下,當應用處于后臺時 collectAsStateWithLifecycle 會取消收集數據流
-
Lifecycle.State.STARTED
https://developer.android.google.cn/reference/android/arch/lifecycle/Lifecycle.State#started
以下代碼片段展示了如何使用 collectAsStateWithLifecycle 來收集可組合函數中的 ViewModel 所公開的 StateFlow 的 uiState 字段:
/* Copyright 2022 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
fun AuthorRoute(
onBackClick: () -> Unit,
modifier: Modifier = Modifier,
viewModel: AuthorViewModel = hiltViewModel()
) {
val uiState: AuthorScreenUiState by viewModel.uiState.collectAsStateWithLifecycle()
AuthorScreen(
authorState = uiState.authorState,
newsState = uiState.newsState,
modifier = modifier,
onBackClick = onBackClick,
onFollowClick = viewModel::followAuthorToggle,
)
}
每當 AuthorViewModel 的 uiState 發出新的 AuthorScreenUiState 值時,都會重新組合 AuthorRoute。有關 collectAsStateWithLifecycle 的更多用法,請參考 "Now in Android" 應用及相關遷移 PR。
-
AuthorViewModel
https://github.com/android/nowinandroid/blob/main/feature-author/src/main/java/com/google/samples/apps/nowinandroid/feature/author/AuthorViewModel.kt -
AuthorRoute
https://github.com/android/nowinandroid/blob/main/feature-author/src/main/java/com/google/samples/apps/nowinandroid/feature/author/AuthorScreen.kt -
Now in Android
https://github.com/android/nowinandroid/search?q=collectAsStateWithLifecycle -
遷移 PR
https://github.com/android/nowinandroid/pull/166
如果您要在項目中使用 collectAsStateWithLifecycle API,請將 androidx.lifecycle.lifecycle-runtime-compose 工件添加到項目中。
/* Copyright 2022 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// app/build.gradle file
dependencies {
implementation "androidx.lifecycle2.6.0-alpha01"
}
注意: 這是一個尚處于 Alpha 版的全新 API,且該 API 還要求您使用ExperimentalLifecycleComposeApi 注釋。
-
版本 2.6.0-alpha01
https://developer.android.google.cn/jetpack/androidx/releases/lifecycle#version_26_2 -
ExperimentalLifecycleComposeApi
https://developer.android.google.cn/reference/kotlin/androidx/lifecycle/compose/ExperimentalLifecycleComposeApi
collectAsStateWithLifecycle 在實現機制上使用了 repeatOnLifecycle API,我們也推薦大家在 Android 視圖 (View) 系統中收集數據流的 API。
-
collectAsStateWithLifecycle 的實現機制
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:lifecycle/lifecycle-runtime-compose/src/main/java/androidx/lifecycle/compose/FlowExt.kt;l=168 -
repeatOnLifecycle
https://developer.android.google.cn/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)
借助 collectAsStateWithLifecycle,您無需輸入下方的樣板代碼,這些代碼同樣以生命周期感知的方式從可組合函數收集數據流:
/*Copyright2022GoogleLLC.
SPDX-License-Identifier: Apache-2.0 */
funAuthorRoute(...){
vallifecycle=LocalLifecycleOwner.current.lifecycle
valuiStatebyproduceState
( initialValue=viewModel.uiState.value
key1=lifecycle
key2=viewModel
){
lifecycle.repeatOnLifecycle(state=STARTED){
viewModel.uiState.collect{value=it}
}
}
AuthorScreen(...)
}
在架構中收集數據流
應用架構中的類型不應該知道其他類型的實現細節。界面不應該知道 ViewModel 如何產生界面狀態。如果界面在屏幕上不可見,則應停止收集數據流,以釋放應用資源 (如果可行的話)。
界面可以通過使用 collectAsStateWithLifecycle 收集界面狀態來幫助釋放資源。ViewModel 可以通過以收集器感知的方式生成界面狀態來完成相同的操作。如果沒有收集器,例如當界面在屏幕上不可見時,則停止收集來自數據層的上游數據流。您可以在生成界面狀態時使用 .stateIn(WhileSubscribed) 數據流 API 來執行此操作。如需了解更多信息,請觀看 "Kotlin Flows 實戰" 講座的這一部分。如要測試以這種方法生成界面狀態的 ViewModel,請查看測試指南。

△ 在界面層中,使用 collectAsStateWithLifecycle 收集界面狀態,并在數據層公開響應式數據流時使用 .stateIn(WhileSubscribed) 生成界面狀態。這樣一來應用的其余部分便能在不需要的時候釋放資源
-
.stateIn(WhileSubscribed)
https://github.com/android/nowinandroid/blob/main/feature-author/src/main/java/com/google/samples/apps/nowinandroid/feature/author/AuthorViewModel.kt#L104 -
Kotlin Flows 實戰
https://www.youtube.com/watch?v=fSB6_KE95bU&t=1009s -
測試 StateFlow
https://developer.android.google.cn/kotlin/flow/test#statein
數據流的使用者和生產者不需要知道彼此的實現方式。在具備多個環境、變體、代碼庫和功能的大型應用中找出實現細節是非常耗時的。更糟糕的是,依賴于實現細節的代碼維護起來非常困難。
讓資源在后臺保持活躍狀態
Android 應用可以在海量 Android 設備上運行。但遺憾的是,所有設備和用戶擁有的資源都是有限的,因此應用通常在受限環境中運行。運行 Android 應用時,有一些重要因素會影響用戶體驗和設備系統健康:-
CPU 使用: 在所有設備組件中,CPU 的耗電量最高。而電池續航時間一直是用戶關注的重點,因此如果發生 CPU 濫用的情況,用戶可能會卸載您的應用;
-
流量消耗: 在未連接 Wi-Fi 時減少應用的網絡流量,可以幫助用戶節省流量費用;
-
內存用量: 應用對內存的使用方式也會對設備的整體穩定性和性能產生非常大的影響。
-
為數十億用戶打造產品
https://developer.android.google.cn/docs/quality-guidelines/build-for-billions
與 collectAsState 的差異
開發者們經常會問道: 如果 collectAsStateWithLifecycle 是從 Android 可組合函數中收集數據流最安全的方法,那現在為什么還需要 collectAsState API?為什么不將生命周期感知功能添加到 collectAsState 中,而是創建新的 API?可組合函數的生命周期與 Compose 運行的平臺無關。正如 "可組合項的生命周期" 頁面中所述,可組合函數的實例進入組合,執行 0 次或多次重組,然后離開組合:
https://developer.android.google.cn/jetpack/compose/lifecycle
△ 組合中可組合函數實例的生命周期
collectAsState API 遵循組合的生命周期。此 API 在可組合項進入組合時開始收集數據流,并在可組合項離開組合時停止收集。collectAsState 是用于收集數據流且與平臺無關的 API。
但是,在 Android 應用中使用 Compose 時,Android 生命周期也會對資源的管理方式產生非常大的影響。即使 Compose 在 Android 應用處于后臺時停止重組,collectAsState 也會繼續收集數據流。這使得層次結構的其余部分無法釋放資源。
collectAsState 和 collectAsStateWithLifecycle 在 Compose 中各有用途。后者用于開發 Android 應用,前者用于在其他平臺進行開發。
從 collectAsState 遷移到 collectAsStateWithLifecycle 非常容易:
/* Copyright 2022 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
fun AuthorRoute(...) {
val lifecycle = LocalLifecycleOwner.current.lifecycle
val uiState by produceState
( initialValue = viewModel.uiState.value
key1 = lifecycle
key2 = viewModel
) {
lifecycle.repeatOnLifecycle(state = STARTED) {
viewModel.uiState.collect { value = it }
}
}
AuthorScreen(...)
}
推薦大家以生命周期感知方式在 Android 上收集數據流,這樣做可以使應用的其他部分在需要時釋放資源。 如果您正在使用 Jetpack Compose 構建 Android 應用,請使用 collectAsStateWithLifecycle 可組合函數來執行此操作。 另外: 感謝 Jose Alcérreca、Marton Braun、Alejandra Stamato 和 Jake Roseman 對文章內容進行審核。
-
Jose Alcérreca
https://medium.com/u/e0a4c9469bb5 -
Marton Braun
https://medium.com/u/ec2087b3c81f -
Alejandra Stamato
https://medium.com/u/92c44d274e60
審核編輯 :李倩
-
Android
+關注
關注
12文章
3959瀏覽量
129170 -
API
+關注
關注
2文章
1553瀏覽量
63254 -
數據流
+關注
關注
0文章
122瀏覽量
14629
原文標題:在 Jetpack Compose 中安全地使用數據流
文章出處:【微信號:Google_Developers,微信公眾號:谷歌開發者】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
KaihongOS操作系統:頁面的生命周期介紹
KaihongOS操作系統:UIAbility的生命周期
設備全生命周期數字孿生:從采購到報廢的智能決策閉環

一文讀懂產品生命周期管理系統(PLM)?

PLM產品生命周期管理系統需要采集哪些數據

PLM解決方案,產品生命周期管理軟件

什么是PLM產品生命周期管理系統?

鴻蒙開發組件:DataAbility的生命周期
鴻蒙開發:【PageAbility的生命周期】

鴻蒙Ability Kit(程序框架服務)【UIAbility組件生命周期】實例

Traveo II B-H中的SECURE和SECURE_WITH_DEBUG生命周期階段有何不同?
HarmonyOS開發案例:【UIAbility和自定義組件生命周期】

評論