Data Binding Flashcards
What is the syntax for assigning the current Activity as the binding’s lifecycleOwner?
binding.lifecycleOwner = this
Why does a binding need a lifecycleOwner?
So that it knows when to start / stop looking for updates to LiveData properties
Which 2 parameters are passed to the LiveData.observe function?
lifecycleOwner
Observer function - called whenever the LiveData property is updated
What is the “fully qualified” name of a class?
com.example.appname.packagename.classname
What needs to be added to the layout xml to use data binding through it?
What needs to be added to the Fragment for this to work?
A parent ^layout^ tag A ^data^ tag A ^variable^ tag within the ^data^ tag Properties for the ^variable^ tag name="my_name" type="fullyqualifiedclassname"
in Fragment.onCreateView( ):
binding.my_name = viewModel / LiveData / whatever represents this binding object in the Fragment
What kind of class is usually passed into the tag when data binding via the xml layout?
A data class representing an entity
What is the shortcut for setting a layout xml up for data binding?
Quick fix menu (intention menu) on the current root layout (usually ConstraintLayout)
– Convert to data binding layout
Which class is automatically built when data binding has been added to a layout xml? What class does this extend?
MyLayoutXmlBinding is generated
It extends ViewDataBinding.java
What does (Generated)ViewDataBinding.root return?
The parent layout of the data-bound layout xml (usually a ConstraintLayout)
What does Transformations.map() do?
Takes a LiveData and a lambda
Uses the lambda on the LiveData
Returns a new LiveData (of any type)
What is the role of @BindingAdapters?
Annotation is applied to extension methods that sit between a view and bound data to provide custom behaviour when the data changes.
Can be used as an alternative to a LiveData Observer (?)
What is the syntax for defining an @BindingAdapter function?
@BindingAdapter(“functionIdentifier”) // for use in xml
View.manipulateValuesForView(item: Entity) {
viewProperty = manipulationOfEntity(item)
}
What is the purpose of a binding object?
Once a binding object has been created for your app, you can access the views, and other data, through the binding object, without having to traverse the view hierarchy or search for the data (using findViewById( )).
How is data binding enabled? (3 different places)
- In build.gradle(Module)
buildFeatures {
dataBinding true
}
- Wrap your xml root layout in a ^layout^ tag
- In the Fragment, add EITHER
private lateinit var binding: ActivityMainBinding
AND in onCreate( )
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
OR in Fragment.onCreateView( )
val binding = FragmentOverviewBinding.inflate(inflater)
Now, instead of using findViewById, all views can be referenced by using binding.viewId OR binding.apply{ viewId1 // do something viewId2 // do something }
- How do you refer to a data bound object and its attributes in the xml?
- What is the syntax for accessing the object to assign to an xml attribute?
- add a ^data^ tag with a ^variable
name = “objectName”
type=”fully_qualified_class_name” - access the attribute using “@={objectName.attribute}”