I'm trying to observe the result of the View Collection and upstream flows stopped.
But viewModel.testFlow
is still collecting while the App is in the background.
Why can't I observe the collection is stopped? Am I observing something wrong?
ViewModel:
val testFlow = flow<Int> {
for (i in 1..100) {
delay(1000)
Timber.e("testFlow: EMIT = $i")
emit(i)
}
}
Activity:
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
lifecycle
variable for your flowWithLifecycle? – Prana