🐦 Kotlin
Advanced
What is the difference between hot and cold flows in Kotlin?
Answer
A cold flow (regular Flow) does not produce values until a collector subscribes to it — each new collector gets its own independent execution of the flow producer. This is like a music track: each listener plays it from the beginning. A hot flow (StateFlow, SharedFlow, Channel) is active independently of collectors — it produces values regardless of whether anyone is listening. Late subscribers may miss past emissions (configurable via replay buffer). StateFlow and SharedFlow are the recommended hot abstractions for sharing state and events in ViewModels, respectively.