Deep Kotlin tutorial covering cold stream, hot stream, shareIn, stateIn with examples, diagram, mistakes, and interview answers.

Cold vs Hot Streams, Sharing, and Replay

What is Cold vs Hot Streams, Sharing, and Replay?

Cold vs Hot Streams, Sharing, and Replay is an important Kotlin topic from 2.9 Cold vs Hot Streams. In this lesson we focus only on the concepts that belong together: cold stream, hot stream, shareIn, stateIn, replay, sharing data. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand Cold vs Hot Streams, Sharing, and Replay in simple language
  • Use cold stream in real Kotlin or Android code
  • Explain Cold vs Hot Streams, Sharing, and Replay in interviews with examples and trade-offs

How it works

Cold vs Hot Streams, Sharing, and Replay: cold stream, hot stream, shareIn, stateIn, replay, sharing data works by moving values through a stream over time. The important decision is whether the producer starts only when collected, whether the latest value must be stored, and whether old work should finish or be cancelled when a new value arrives.

  1. Identify what problem Cold vs Hot Streams, Sharing, and Replay: cold stream, hot stream, shareIn, stateIn, replay, sharing data solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • cold stream: core concept in this lesson
  • hot stream: core concept in this lesson
  • shareIn: core concept in this lesson
  • stateIn: core concept in this lesson
  • replay: core concept in this lesson
  • sharing data: core concept in this lesson
  • State: current value that UI can render
  • Event: one-time signal such as toast or navigation

Important differences

Concept 1Concept 2How to remember it
ColdHotCold starts per collector; hot can emit without collectors.
StateEventState is current UI data; event is a one-time signal.
collectcollectLatestcollect finishes every value; collectLatest cancels old work for new values.
bufferconflatebuffer keeps every value queued for a slow collector; conflate drops intermediate values and keeps only the latest.

Diagram

flowchart TD
A["2.9 Cold vs Hot Streams"] --> B["Cold vs Hot Streams, Sharing, and Replay"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

State and event streams

private val _state = MutableStateFlow(UiState.Loading)
val state: StateFlow<UiState> = _state
private val _events = MutableSharedFlow<UiEvent>()
val events = _events.asSharedFlow()

Update state

_state.value = UiState.Content(items)

When to use it

Use it when: Reach for this when it makes your code clearer, safer, or easier for the next developer to follow.

Avoid it when: Skip it when a simpler, more familiar approach already solves the problem without extra ceremony.

Common mistakes

  • Collecting Flow without lifecycle awareness
  • Using StateFlow for one-time events
  • Forgetting that cold Flow restarts per collector
  • Choosing replay or buffer values without understanding delivery
  • Putting heavy work in the wrong dispatcher

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is Cold vs Hot Streams, Sharing, and Replay in 2.9 Cold vs Hot Streams?

Answer: Cold vs Hot Streams, Sharing, and Replay is an important Kotlin topic from 2.9 Cold vs Hot Streams. In this lesson we focus only on the concepts that belong together: cold stream, hot stream, shareIn, stateIn, replay, sharing data. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

private val _state = MutableStateFlow(UiState.Loading)
val state: StateFlow<UiState> = _state
private val _events = MutableSharedFlow<UiEvent>()
val events = _events.asSharedFlow()
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Collecting Flow without lifecycle awareness

2. How does Cold vs Hot Streams, Sharing, and Replay work in Kotlin or Android code?

Answer: Cold vs Hot Streams, Sharing, and Replay: cold stream, hot stream, shareIn, stateIn, replay, sharing data works by moving values through a stream over time. The important decision is whether the producer starts only when collected, whether the latest value must be stored, and whether old work should finish or be cancelled when a new value arrives.

_state.value = UiState.Content(items)
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Using StateFlow for one-time events

3. What is one common mistake with Cold vs Hot Streams, Sharing, and Replay?

Answer: A common mistake is: Collecting Flow without lifecycle awareness Another is: Using StateFlow for one-time events

private val _state = MutableStateFlow(UiState.Loading)
val state: StateFlow<UiState> = _state
private val _events = MutableSharedFlow<UiEvent>()
val events = _events.asSharedFlow()
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Collecting Flow without lifecycle awareness

4. How would you explain Cold vs Hot Streams, Sharing, and Replay with a small example?

Answer: Cold vs Hot Streams, Sharing, and Replay is an important Kotlin topic from 2.9 Cold vs Hot Streams. In this lesson we focus only on the concepts that belong together: cold stream, hot stream, shareIn, stateIn, replay, sharing data. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

private val _state = MutableStateFlow(UiState.Loading)
val state: StateFlow<UiState> = _state
private val _events = MutableSharedFlow<UiEvent>()
val events = _events.asSharedFlow()
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Using StateFlow for one-time events

5. What is the trade-off of using Cold vs Hot Streams, Sharing, and Replay?

Answer: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.

_state.value = UiState.Content(items)
  • Trade-off: Cold and Hot solve related but different problems: Cold starts per collector; hot can emit without collectors.
  • Common mistake: Collecting Flow without lifecycle awareness

Practice task

Write one Kotlin example for Cold vs Hot Streams, Sharing, and Replay. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.