StateFlow là lựa chọn ưu tiên cho code mới (2025) — tích hợp tự nhiên với coroutine, luôn có value non-null có thể đọc an toàn, và hoạt động tốt với Compose.
- LiveData lifecycle-aware và tự động unsubscribe nhưng đang ở chế độ maintenance.
- StateFlow yêu cầu initial value; LiveData thì không.
- Khi dùng StateFlow trong Fragment, observe trong
repeatOnLifecycleđể tránh memory leak.
StateFlow is the preferred choice for new code (2025) — it integrates naturally with coroutines, always has a non-null .value you can read safely at any time, and works better with Compose.
- LiveData is lifecycle-aware and auto-unsubscribes but is in maintenance mode.
- StateFlow requires an initial value; LiveData does not.
- When collecting StateFlow in Fragments, use
repeatOnLifecycleto avoid leaks.