I want to use mvvm in Dialog but I don't know how to pass LifecycleOwner to observe
class CommonDialog(context: Context) : Dialog(context, R.style.AppMaskStatusTheme) {
private val viewBinding: DialogSportOrderBinding by lazy { DialogSportOrderBinding.inflate(LayoutInflater.from(context)) }
private val viewModel by lazy { ViewModelProvider(context as ViewModelStoreOwner)[SportOrderViewModel::class.java] }
init {
setContentView(viewBinding.root)
viewModel.sportOrderList.observe(***what to pass here?***, androidx.lifecycle.Observer {
})
}
}
I have tried context as LifecycleOwner, context as AppCompatActivity but all fail
please help me, thanks!
You can create your own LifecycleOwner like this
Then your Dialog class will be like this
Read more about Lifecycle-Aware Components at here