A deep dive into how Kotlin's three emission mechanisms work: cold flows, hot flows, and channels. Cold flows are stateless recipes that re-execute per collector with no storage. Hot flows (SharedFlow, StateFlow) are fire-and-forget by default, with opt-in replay caching. Channels are FIFO queues delivering each value to exactly one receiver, making them fundamentally different from flows. The post also covers when emissions can be lost in each mechanism, including dispatcher gaps during lifecycle transitions, drop policies, and the consumeAsFlow() vs receiveAsFlow() distinction. A practical example shows why wrapping a network call in a cold flow results in multiple API calls when collected by multiple screens, and how stateIn() solves this.

28m read timeFrom proandroiddev.com
Post cover image
Table of contents
Cold flow lossHot flow lossChannel lossThe pattern across all three

Sort: