Data Binding Flashcards

1
Q

What is the syntax for assigning the current Activity as the binding’s lifecycleOwner?

A

binding.lifecycleOwner = this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why does a binding need a lifecycleOwner?

A

So that it knows when to start / stop looking for updates to LiveData properties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which 2 parameters are passed to the LiveData.observe function?

A

lifecycleOwner

Observer function - called whenever the LiveData property is updated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the “fully qualified” name of a class?

A

com.example.appname.packagename.classname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What kind of class is usually passed into the tag when data binding via the xml layout?

A

A data class representing an entity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the shortcut for setting a layout xml up for data binding?

A

Quick fix menu (intention menu) on the current root layout (usually ConstraintLayout)
– Convert to data binding layout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Which class is automatically built when data binding has been added to a layout xml?
What class does this extend?
A

MyLayoutXmlBinding is generated

It extends ViewDataBinding.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does (Generated)ViewDataBinding.root return?

A

The parent layout of the data-bound layout xml (usually a ConstraintLayout)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does Transformations.map() do?

A

Takes a LiveData and a lambda
Uses the lambda on the LiveData
Returns a new LiveData (of any type)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the role of @BindingAdapters?

A

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 (?)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the syntax for defining an @BindingAdapter function?

A

@BindingAdapter(“functionIdentifier”) // for use in xml
View.manipulateValuesForView(item: Entity) {
viewProperty = manipulationOfEntity(item)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of a binding object?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is data binding enabled? (3 different places)

A
  1. In build.gradle(Module)

buildFeatures {
dataBinding true
}

  1. Wrap your xml root layout in a ^layout^ tag
  2. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. How do you refer to a data bound object and its attributes in the xml?
  2. What is the syntax for accessing the object to assign to an xml attribute?
A
  1. add a ^data^ tag with a ^variable
    name = “objectName”
    type=”fully_qualified_class_name”
  2. access the attribute using “@={objectName.attribute}”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the function that can be used to force a refresh of all binding expressions?

A

binding.invalidateAll( )

17
Q

When using a binding object, what must always be done in Fragment.onCreateView( ) after the binding object has been instantiated?

A

binding. lifecycleOwner = this

binding. viewModelXmlName = viewModel