DataBinding-ktx
make easy declaring DataBinding.
ViewBinding-ktx is here.
- Forgetting to call
setLifecycleOwner
in DataBinding. - Differences in the way of declaring a
binding
variable in Activity and Fragment.- In Activity, you can declare the
binding
variable usingby lazy
. - In Fragment, you can't declare the
binding
variable usingby lazy
because of recreating Fragment's view.
- In Activity, you can declare the
- Wasted memory unless you set the
binding
variable to null after onDestroyView- If you use Navigation component, BackStack or detach in Fragment, Fragment is still alive but Fragment's view is dead after onDestroyView.
Because the
binding
variable has view tree, your app wasted memory. For saving memory, you should set thebinding
variable to null after onDestroyView.
- If you use Navigation component, BackStack or detach in Fragment, Fragment is still alive but Fragment's view is dead after onDestroyView.
Because the
DataBinding-ktx
is automatically callingsetLifecycleOwner
.DataBinding-ktx
provide the unified way of declaring thebinding
variable in Activity and Fragment.DataBinding-ktx
is saving memory because of cleaning up thebinding
variable having the view tree after onDestroyView.DataBinding-ktx
needs one of the following- calling
setContentView
in Activity and callinginflater.inflate
inonCreateView
of Fragment. - calling Activity/Fragment's secondary constructor passing layout res id.
- calling
private val binding: DataBindingActivityBinding by dataBinding()
- Use
setContentView
before using thebinding
variable.
class DataBindingActivity : FragmentActivity() {
// Declare the `binding` variable using `by dataBinding()`.
private val binding: DataBindingActivityBinding by dataBinding()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.data_binding_activity)
// You can use binding
}
}
- Use Activity's secondary constructor passing layout res id.
class DataBindingActivity : AppCompatActivity(R.layout.data_binding_activity) {
// Declare the `binding` variable using `by dataBinding()`.
private val binding: DataBindingActivityBinding by dataBinding()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// You can use binding
}
}
- Use
inflater.inflate
inonCreateView
.
class DataBindingFragment : Fragment() {
// Declare the `binding` variable using `by dataBinding()`.
private val binding: DataBindingFragmentBinding by dataBinding()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.data_binding_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// You can use binding
}
}
- Use Fragment's secondary constructor passing layout res id.
class DataBindingFragment : Fragment(R.layout.data_binding_fragment) {
// Declare the `binding` variable using `by dataBinding()`.
private val binding: DataBindingFragmentBinding by dataBinding()
// DO NOT override onCreateView
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// You can use binding
}
}
- If you forget to use Activity's secondary constructor passing layout res id, your app crash.
// You can define and use DataBindingAppCompatActivity for not forgetting.
open class DataBindingAppCompatActivity<T : ViewDataBinding>(@LayoutRes contentLayoutId : Int) : AppCompatActivity(contentLayoutId) {
protected val binding: T by dataBinding()
}
class YourActivity : DataBindingAppCompatActivity<YourActivityBinding>(R.layout.your_activity) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// You can use binding
}
}
- If you forget to use Fragment's secondary constructor passing layout res id, Fragment is not shown.
// You can define and use DataBindingFragment for not forgetting.
open class DataBindingFragment<T : ViewDataBinding>(@LayoutRes contentLayoutId : Int) : Fragment(contentLayoutId) {
protected val binding: T by dataBinding()
}
class YourFragment : DataBindingFragment<YourFragmentBinding>(R.layout.your_fragment) {
// DO NOT override onCreateView
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// You can use binding
}
}
android {
buildFeatures {
dataBinding = true
}
}
repositories {
maven { url "https://www.jitpack.io" }
}
dependencies {
implementation 'com.github.wada811:DataBinding-ktx:x.y.z'
}
Copyright (C) 2020 wada811
Licensed under the Apache License, Version 2.0