--

That linked article is very poorly named and the conclusions are simply wrong. It's not a leak but poor job management.

The flow being collected doesn't care that it's in a view model or a fragment. It's within a scope that will be canceled when the lifecycle of the holding object ends.

The exact same issue could happen in a fragment's lifecycle scope. If you start observing a flow with say "onResume" you'd run into the exact same issue because you can go from resume to pause and back again without the scope being cancelled.

There's absolutely nothing wrong with collecting flows in view models. The scope will cancel the flow(s) started just fine. Not cancelling the previous observer before starting a new one is the problem.

--

--

Michael Ferguson
Michael Ferguson

Written by Michael Ferguson

Android software developer. Views and opinions expressed are my own and not that of my employer.

No responses yet