DataBindingUtil vs DataBinding-ktx

Takumi WADA
DataBinding-ktx
Published in
3 min readMar 25, 2019

I explain how DataBinding-ktx is something better than DataBindingUtil.

TL;DR

DataBinding-ktx is better than DataBindingUtil because of the follow reasons.
・ You can be declared the same in Activity and Fragment
・ It is necessary to declare as property even if it is good with local variable
・ You can prevent forgetting of call of setLifecycleOwner

How to declare binding variables in Activity and Fragment

DataBindingUtil
Activity and Fragment have different methods. If it is a property, Activity can be val by lazy , but Fragment can not be val by lazy .

DataBinding-ktx
Activity and Fragment both are declared with val by bind .

Variable scope

DataBindingUtil
Declare local variables or properties as needed.

DataBinding-ktx
Because property declaration is required, the scope may become wider than necessary.

LiveData compatible

Remember to call setLifecycleOwner on DataBindingUtil yourself .

DataBinding-ktx
val by bind
automatically calls setLifecycleOwner, so you do not need to call it yourself.

Detailed writing comparison

DataBindingUtil in Activity

onCreate in DataBindingUtil#setContentView calls.

override fun onCreate(savedInstanceState: Bundle?) {
val binding = DataBindingUtil.setContentView<MainActivityBinding>(this, R.layout.main_activity)
// use binding
}

If you need to to property,
lateinit var in onCreate in DataBindingUtil#setContentView calls.

private lateinit var binding: MainActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
binding = DataBindingUtil.setContentView(this, R.layout.main_activity)
// use binding
}

Or,
val by lazy in DataBindingUtil#setContentView calls.
※ Please note that it will not be setContentView if you do not use binding with onCreate .

private val binding: MainActivityBinding by lazy {
DataBindingUtil.setContentView(this, R.layout.main_activity)
}
override fun onCreate(savedInstanceState: Bundle?) {
// use binding
}

DataBinding-ktx in Activity

Declare as a property with val by bind .
※ Please note that it will not be setContentView unless you use binding in onCreate as well as val by lazy .

private val binding: MainActivityBinding by bind(R.layout.main_activity)
override fun onCreate(savedInstanceState: Bundle?) {
// use binding
}

DataBindingUtil in Fragment

onCreateView in DataBindingUtil#inflate calls.

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = DataBindingUtil.inflate<MainFragmentBinding>(this, R.layout.main_fragment, container, false)
return binding.root
}

If you need to make it a property, call DataBindingUtil#inflate in onCreateView in lateinit var .
※ Because Fragment has regeneration of View, val by lazy can not be used.

private lateinit var binding: MainFragmentBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(this, R.layout.main_fragment, container, false)
return binding.root
}

DataBinding-ktx in Fragment

Declare as a property with val by bind .

private val binding: MainFragmentBinding by bind(R.layout.main_fragment)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return binding.root
}

Summary

DataBinding-ktx is required to be declared in the property, but it can be declared in val and can prevent omission of description of setLifecycleOwner for LiveData.

Please refer to the following article for usage.

--

--