I want to send an extra parameter to my ViewModel, but this extends from AndroidViewModel.
How can I add this parameter to the ViewModelFactory class ?
ViewModel
class ProjectViewModel(application: Application) : AndroidViewModel(application) {
// need a param for project id...
}
ViewModelFactory
class ProjectViewModelFactory(val projectId: Int): ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
// need to send this...
return ProjectViewModel(projectId) as T
}
}
Note: I notice that in the documentation its says: AndroidViewModel Subclasses must have a constructor which accepts Application as the only parameter.
So I don't know if it is posible (or good) to do what I'm trying to do.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…