0Pricing
Jetpack Compose Academy · Lesson

stateIn & WhileSubscribed

Cache flows and stop work when nobody listens.

stateIn & WhileSubscribed is a free Jetpack Compose Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

It Needs a Scope

The first argument is a scope, usually viewModelScope, which ties the running flow to the ViewModel lifecycle.

flow.stateIn(viewModelScope, ...)

It Needs an Initial Value

Because a StateFlow always holds a value, stateIn requires an initial one to show before the first real emission arrives.

flow.stateIn(scope, started,
    initialValue = UiState.Loading)

The Sharing Strategy

The middle argument is the SharingStarted policy. It decides exactly when the upstream flow starts and stops running.

Eagerly and Lazily

SharingStarted.Eagerly starts the flow immediately and never stops it, while Lazily starts on the first collector and keeps going.

started = SharingStarted.Eagerly

Meet WhileSubscribed

WhileSubscribed only runs the upstream flow while there is at least one active collector, then stops it when the last one leaves.

started = SharingStarted.WhileSubscribed()

The Five Second Timeout

Passing a small timeout to WhileSubscribed keeps the flow alive briefly after the last collector, surviving quick rotations.

SharingStarted.WhileSubscribed(5000)

Why the Timeout Helps

During a screen rotation the old collector leaves and a new one arrives moments later. The timeout avoids a needless restart in between.

Stops Wasted Work

With WhileSubscribed a database or network flow pauses when the screen is off, then resumes when the user returns. Battery wins. 🔋

A Full stateIn Recipe

Put it together: transform your data, then call stateIn with the scope, WhileSubscribed, and an initial loading state.

val ui = repo.flow().map { UiState(it) }
    .stateIn(viewModelScope,
        SharingStarted.WhileSubscribed(5000),
        UiState.Loading)

Quick Check

Reason about subscriber-driven lifecycles.

Recap: stateIn & WhileSubscribed

Well done! stateIn turns a cold flow into a cached StateFlow, and WhileSubscribed runs it only while observed so you stop wasting work. 🎯

Frequently asked questions

Is the “stateIn & WhileSubscribed” lesson free?

Yes — the full text of “stateIn & WhileSubscribed” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “stateIn & WhileSubscribed”?

Cache flows and stop work when nobody listens. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “stateIn & WhileSubscribed” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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