How important is Viewbinding to Activity? Since i ...
# android
p
How important is Viewbinding to Activity? Since i can make reference to my widgets just by importing the layout. I think Binding works better for Fragments. Any opinions ?
o
No difference. It also gives you null and types safe both for activities and fragments.
z
ViewBinding is always superior to Kotlin Synthetics
2
burn databinding, burn synthetics
😂 2
p
@zhuinden How ?
I think ViewBinding is cool for working with Fragments. BBut for Activity, its just an alternative of having a collection of all widget in one place. Where you can access easily. Just like Java and Kotlin.
m
its just an alternative of having a collection of all widget in one place
If by "alternative" you mean "alternative to Kotlin synthetic accessors", I'm in agreement with Gabor: use view binding. Quoting myself from a book:
The larger the project, though, the greater the risk is that you will use the wrong imports. Once you start getting dozens or hundreds of layout resources, making sure that you use the right imports starts to become a challenge. The compiler will be very happy to let you use any import you want. However, if you try referencing a widget from Layout X and you are really using Layout Y, it is likely that you will wind up crashing with a
NullPointerException
somewhere along the line.
For a minor amount of setup overhead (a few lines in a Gradle script and a couple of lines per activity, fragment, etc.), you get the code-generated guarantee that you are referencing widgets that exist.
❤️ 2
p
Thats the use case.
a
And in the concept of single activity all you have is fragments 🤷