It might be something that will start making more sense once you write some code down and see the use cases.
Your basic Flow is cold, the code that produces values is contained within it and it will produce said values only when a collector is called. This is great because it does not waste resources and it doesn’t produce values when you don’t need to consume them. However a cold Flow implements a particular behaviour in that every time you call upon .collect you will create a new Flow.
Imagine you need to have a function which produces random numbers in a range from 0 to 100, so you create a Flow which does that exact thing and you want all of your collectors to receive
the same value (be in sync), meaning if your random sequence is 0,4,2,6 and now you’re at number 6 you want all of your collectors to receive the number 6. The problem you run into is that every time a collector is called it will trigger the build of a new flow, which will trigger a new random generator which will give a different value.
Sometimes you need to be able to represent the latest value in a stream or represent a state. You need a Flow which has an active instance regardless of it’s collectors and can broadcast values, here you can use a hot Flow. Hot Flows will produce values regardless of whether there is currently a consumer consuming said values.
You can easily turn our cold Flow into a hot SharedFlow by using the .shareIn{} operator.
It is important to note that a SharedFlow can be started lazily or eagerly by providing the correct “started” parameter to the operator. Meaning either SharingStarted.Lazily or SharingStarted.Eagerly. Both options will never stop sharing.
Addotionally there is a third option SharingStarted.WhileSubscribed() which will start sharing from the moment the first subscriber appears and stop when the last disappears.
Hot and cold streams are concepts that are represented generally in the programming world, they are not confined to Channels and Flows and you can also reference some Rx literature on the topic as well, the general concept should translate well.
Important note: I am by no means an experienced dev and whenever I write things, especially complex things like these they should be taken with a grain of salt. In case I make a mistake I urge the more experienced among us to correct me as I am only trying to give back to the dev community while working within my limitations.
Cheers!
https://elizarov.medium.com/cold-flows-hot-channels-d74769805f9
https://www.raywenderlich.com/9799571-kotlin-flow-for-android-getting-started
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-shared-flow/index.html
https://developer.android.com/kotlin/flow/stateflow-and-sharedflow