Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of Databinding in kotlin?

I would like to know the benefits of writing extra code to implement dataBinding in Kotlin when developing Android Apps. Because Kotlin extensions already allows us to access the views directly without the need of findViewById's.

like image 668
Doilio Matsinhe Avatar asked May 11 '19 16:05

Doilio Matsinhe


People also ask

What is the advantage of data binding?

Below are the advantages of using the Data Binding Library in your Android application: You can reduce findViewById calls and enhance your app's performance. Helps get rid of memory leaks or nullPointerException s. Uses declarative layout, which is more adaptable.

What is DataBinding in Kotlin?

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.

What is the advantages of data binding in Android?

Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.

Which is better ViewBinding and DataBinding?

ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.


2 Answers

I've found an answer here:

Hey! Developer Advocate for Android at Google here!

I wanted to add a bit of background here. Kotlin Extensions with synthetic views was never intentionally “recommended” though that shouldn’t be taken as a recommendation to not use them. If they're working for you please feel free to continue using them in your app!

We’ve been shifting away from them (e.g. we don’t teach them in the Udacity course) because they expose a global namespace of ids that’s unrelated to the layout that’s actually inflated with no checks against invalid lookups, are Kotlin only, and don't expose nullability when views are only present in some configuration. All together, these issues cause the API to increase number of crashes for Android apps.

On the other hand, they do offer a lightweight API that can help simplify view lookups. In this space it's also worth taking a look at Data Binding which also does automatic view lookups - as well as integrates with LiveData to automatically update your views as data changes.

Today, there's a few options in this space that work:

  • Data Binding is the recommendation for view lookup as well as binding, but it does add a bit of overhead when compared to Android Kotlin Extensions. It's worth taking a look to see if this is a good fit for your app. Data Binding also allows you to observe LiveData to bind views automatically when data changes. Compared to Kotlin Extensions, it adds compile time checking of view lookups and type safety.
  • Android Kotlin Extensions is not officially recommended (which is not the same as recommendation against). It does come with the issues mentioned above, so for our code we're not using them.
  • Butter Knife is another solution that is extremely popular and works for both Kotlin and the Java Programming Language.

Reading through the comments here there's a lot of developers that are having great luck with Kotlin Extensions. That's great - and something we'll keep in mind as we look at ways to continue improving our APIs. If you haven't taken a look at Data Binding, definitely give it a shot.

As an aside, our internal code style guide is not intended to be directly applied outside of our codebase. For example, we use mPrefixVariables, but there's no reason that every app should follow that style.

like image 119
Doilio Matsinhe Avatar answered Sep 21 '22 13:09

Doilio Matsinhe


To clarify things, Accessing view directly and dataBinding are different. Prior to kotlin, we used to have a library called butterknife which did help to access views directly.

So coming back to dataBinding, what it does exactly is setting the values to the views directly/ getting event trigger(two way binding) inside xml instead you doing it explicitly in activity/fragment class.

what's the benefit: Assume you have 20 Textviews inside your layout xml, you want set the values to those textviews from response object(with 20 corresponding fields).

1)Without databinding, you need to access each view and set the value from object(so 20 lines of code).

2) With databinding, you just bind the object to xml(one liner) and your Xml will populate respective fields using the binding code inside xml.

like image 35
rafa Avatar answered Sep 24 '22 13:09

rafa