我正在尝试观察View Collection 和上游流停止的结果。但viewModel.testFlow
在应用程序处于后台时仍在收集。为什么我无法观察到收集已停止?我观察到什么不对吗?
视图模型:
val testFlow = flow<Int> {
for (i in 1..100) {
delay(1000)
Timber.e("testFlow: EMIT = $i")
emit(i)
}
}
活动:
override fun onViewCreated() {
lifecycleScope.launch {
viewModel.testFlow
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.collect {
Timber.d("testFlow: $it Collected")
}
}
}
override fun onActivityPaused(activity: Activity) {
super.onActivityPaused(activity)
Timber.e("testFlow: onActivityPaused")
}
https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda