[Android] DataBinding-ktx 2.0.0 Released

Takumi WADA
DataBinding-ktx
Published in
2 min readJun 20, 2019

DataBinding-ktx 3.0.1 has been released.

Original article is below.

What is DataBinding-ktx

A library for solving problems related to DataBinding and using it safely and easily.

DataBinding Issues

1. The method of variable declaration differs between Activity and Fragment

Activity in by lazy you can use but,
Fragment If you use a BackStack and Attach / Detach the so View is regenerated by lazy (※ 1) and lateinit Var (※ 2) can not use.
* 1: A binding instance is not created for the re-created View.
※ 2: The view leaks memory.

2. Forget to call setLifecycleOwner

If you use LiveData, LiveData will not be DataBinding unless you call setLifecycleOwner.

DataBinding-ktx resolution

1. The method of variable declaration differs between Activity and Fragment

Kotlin’s Delegated Properties allow you to declare properties in the same way in Activity and Fragment.

// Activity
val binding: MainActivityBinding by dataBinding(R.layout.main_activity)
// Fragment
val binding: MainFragmentBinding by dataBinding(R.layout.main_fragment)

2. Forget to call setLifecycleOwner

Because setLifecycleOwner is automatically called at the first access to the binding variable, it will not forget to call it.

How to use DataBinding-ktx

Activity

class MainActivity : AppCompatActivity() {
private val binding: MainActivityBinding by dataBinding(R.layout.main_activity)
}

Fragment

class MainFragment : Fragment() {
private val binding: MainFragmentBinding by dataBinding(R.layout.main_fragment)
}

build.gradle

repositories {
maven { url "https://jitpack.io" }
}
dependencies {
implementation 'com.github.wada811:DataBinding-ktx:x.y.z'
}

Please refer to the README for the version.

DataBinding-ktx 2.2.0 Released

--

--