I am building an Android App using MVVM and DataBinding. And I have a function inside my ViewModel that starts an Activity. Is it okay to have an onClick call inside a ViewModel
The way I do it is, in your ViewModel:
val activityToStart = MutableLiveData, Bundle?>>()
This allows you to check the class of Activity started, and the data passed in the Bundle. Then, in your Activity, you can add this code:
viewModel.activityToStart.observe(this, Observer { value ->
val intent = Intent(this, value.first.java)
if(value.second != null)
intent.putExtras(value.second)
startActivity(intent)
})