Search code examples
androidkotlinandroid-lifecyclekotlin-coroutines

What's the point of using LiveData in ViewModel if I can use Flow and StateFlow with lifecycleScope \ viewLifecycleOwner.lifecycleScope


Flow has a lot of operators, LiveData has only 3 (Transformations). Is there any reason to keep using LiveData except StateFlow is still experimental?

UPD. StateFlow, SharedFlow and corresponding operators are promoted to stable API in kotlinx.coroutines 1.4.0


Solution

  • There is not much reason to use LiveData nowadays. (State)Flow/Coroutines also brings new possibilites via lifecycleScope.launchWhenCreated/Started/Resumed, hard to do with LiveData.

    But there is one reason when LiveData is needed - DataBinding. It currently doesn't support observing Flow.

    EDIT: there is going to be support for StateFlow in DataBinding in Android Studio 4.3: https://twitter.com/manuelvicnt/status/1314621067831521282