Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
flowWithLifecycle(lifecycle, Lifecycle.State.STARTED) doesn't stop flows while App is in background

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")
}

Logs Lifecycle Diagram

https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are using Lifecycle.State.STARTED state to start observing Flow, the corresponding method of the Activity when emission stops is onStop(). If onStop() method of Activity is called the emission and collecting will stop.

If you want to stop emitting and collection data when onPause method is called, you can use Lifecycle.State.RESUMED state.

When app goes to background onStop() method of Activity is called and when using Lifecycle.State.STARTED state to observe the Flow you should see the emission and collecting stop.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!