Sharing Flows & Lifecycle-Safe Collection

KOTLIN › Flow

One upstream for many collectors, and collection that stops when the screen does.

Two problems meet at the seam between a cold flow and a screen: a cold flow re-runs its producer per collector, and a collector runs until its coroutine is cancelled. stateIn and shareIn fix the upstream half, repeatOnLifecycle and collectAsStateWithLifecycle fix the downstream half, and interviewers care most about whether you understand they are two halves of one handshake. Strong answers explain where the five second timeout comes from, why launchWhenStarted was deprecated, and why collectAsState is the same mistake in Compose form.

What this covers

Study this topic

Further reading