Kotlin Flow Patterns Every Senior Android Developer Should Know
Cold streams, hot streams, sharing, retries, combining state, and the interview-level reasoning behind Flow choices.
Cold Versus Hot
A cold Flow starts work for each collector. A hot stream such as StateFlow or SharedFlow continues independently of a single collector.
Sharing State
Use stateIn when the UI needs the latest value.
val uiState = repository.observeProfile()
.map { profile -> ProfileUiState.Content(profile) }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = ProfileUiState.Loading
)Retry With Care
Retries should respect idempotency, user expectations, and battery usage. Do not hide repeated backend failures behind infinite retry loops.
Combine Inputs
Search, filters, and auth state often need combine, debounce, and distinctUntilChanged to prevent noisy work.