2

I am new to android development and MVVM and try to understand Databinding and LiveData.

  • With one-way-DataBinding you can get Data from the viewmodel to the view.
  • With two-way-DataBinding you can get Data from the viewmodel to the view and from the view to the viewmodel.

Also LiveData updates the view, when data changes in the viewmodel.

Why should I use LiveData, when DataBinding already does that in both one-way and two-way databinding?

What are the perks of using Databinding together with LiveData?

Marco
  • 8,958
  • 1
  • 36
  • 56

1 Answers1

1

Why should I use LiveData, when DataBinding already does that in both one-way and two-way databinding?

LiveData is lifecycle aware. This means that the updates will be delivered to your UI when the Activity/Fragment is an state where those updates are meaningful.

What are the perks of using Databinding together with LiveData?

You get not worry about the lifecycle of your Activity/Fragment (livedata) and let Google generate the code to update your views for you (databinding).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Databinding library is not lifecycle aware? will databinding deliver updates to UI even when Activity/Fragment are not in Started State? – Siddarth G Sep 04 '19 at 08:06
  • 1
    Not really. Databinding generates the code one is suppose to write to bind the data to the UI, but afaik it is not lifecycle aware. That's up to the developer @SiddarthG – Blackbelt Sep 04 '19 at 08:13
  • Thanks, also would you use databinding for recyclerview viewholder items or would you make viewmodel with livedata for each viewholder? – Siddarth G Sep 04 '19 at 08:15
  • I used both approaches and in my case BaseOservable instead of LiveData proved to be more effective – Blackbelt Sep 04 '19 at 08:20