Android data binding inject ViewModel in custom view
The missing piece is to set a Lifecycle Owner.
binding.setLifecycleOwner(parent) //parent should be a fragment or an activity
The answered helped me and want to throw a util method you can add to get the LifeCycleOwner
fun getLifeCycleOwner(view: View): LifecycleOwner? {
var context = view.context
while (context is ContextWrapper) {
if (context is LifecycleOwner) {
return context
}
context = context.baseContext
}
return null
}
In your view:
getLifeCycleOwner(this)?.let{
binding.setLifecycleOwner(it)
}