# StateFlow and SharedFlow in ViewModels

DevFeed: [StateFlow and SharedFlow in ViewModels](<https://devfeed.tech/articles/stateflow-and-sharedflow-in-viewmodels-22664.md>)

Original publisher: [Read original article](<https://www.valueof.io/blog/stateflow-sharedflow-flow-viewmodel-lifecycle>)

Author: James Shvarts

Published: 2022-12-28T19:21:19Z

Content type: tutorial

Language: en

Sources: [Android Blog - Mobile Dev Notes](<https://devfeed.tech/sources/android-blog-mobile-dev-notes.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Android](<https://devfeed.tech/topics/android.md>), [Code](<https://devfeed.tech/topics/code.md>), [Unit testing](<https://devfeed.tech/topics/unit-testing.md>), [Compose](<https://devfeed.tech/topics/compose.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [compose](<https://devfeed.tech/tags/compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [sharedflow](<https://devfeed.tech/tags/sharedflow.md>), [stateflow](<https://devfeed.tech/tags/stateflow.md>), [unit-testing](<https://devfeed.tech/tags/unit-testing.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

A practical guide to choosing between StateFlow and SharedFlow in Android ViewModels. It covers lifecycle-aware collection, exposing flows as UI state in view-based apps and Compose, and unit testing ViewModels that convert flows into state or shared events.

## Source excerpt

Collecting State and Shared Flows from ViewModels while respecting lifecycle of the collector views