Binding property after adding _binding

I am really confused by the last page of the chapter (pg.175) where the _binding backing property is introduced and the binding property is given a precondition check for null. Once the two binding lines in onCreateView and onDestroyView are changed to _binding, what is the point of binding? Where does it get any value? Where is it used? I am totally new to Kotlin, so if it is a Kotlin-thing, please help me understand that part. I just don’t get the relationship between _binding and binding, and how both are used in this context.
Thanks for any help!!

1 Like

The non-null binding is used in onViewCreated() and anywhere you want to really interact with the layout.

We want to have a nullable reference to the because of the stuff mentioned in the “Fragments and memory management” section in the chapter.

If you are coming from Java or Javascript or any other language, this nullable/non-null stuff might be a little new. In Java, you could have a variable with the type String and the value assigned could be a actual String (like "Angela") or it could be null. In Kotlin, you cannot have a variable of type String and have the value assigned to be null. Kotlin accounts for nullability in how you define types for variables, so you have to deal with that.

This _binding and binding workaround is all about keeping the Kotlin type system happy, while also giving you nice, clean, performant code.