Closed merlinJeyakumar closed 1 year ago
It's not possible. To create an instance the delegate needs to now ViewBinding class.
The delegate few creation variants and one of them has ViewModel class as parameter or even you can pass function where you will create an instance of ViewBinding
@LayoutRes private val layoutRes: Int,
) {
private var binding: T? = null
operator fun getValue(activity: R, property: KProperty<*>): T {
if (binding == null) {
binding = DataBindingUtil.setContentView<T>(activity, layoutRes).apply {
lifecycleOwner = activity
}
}
return binding!!
}
}
fun <R : AppCompatActivity, T : ViewDataBinding> contentView(
@LayoutRes layoutRes: Int,
): ContentViewBindingDelegate<R, T> = ContentViewBindingDelegate(layoutRes)
I have created and using this extension function, while using it with navigation view it causing crash NavHost.context() null pointer
its working fine for my baseclass,
you can close this ticket, Problem is I called lifecycle binding before activity creation
How to use generic type
abstract class BaseActivity<B : ViewDataBinding, VM : BaseViewModel>( private val bindingFactory: Int, private val viewModelClass: Class,
) : AppCompatActivity() {
protected val binding: B by viewBinding()
}