0PricingLogin
Jetpack Compose Academy · Lesson

stateIn & WhileSubscribed

Cache flows and stop work when nobody listens.

From Cold Flow to Hot State

UI state should be hot and cached, but your transformed flows are cold. The stateIn operator converts a cold flow into a StateFlow.

What stateIn Does

stateIn launches the upstream flow in a scope, shares one collection across all collectors, and caches the latest value for new ones.

val ui = source.stateIn(
    viewModelScope, started, initial)

All lessons in this course

  1. StateFlow vs SharedFlow
  2. Mapping & Combining Flows
  3. stateIn & WhileSubscribed
  4. One-Off Events with Channels
← Back to Jetpack Compose Academy