需要实现 ViewModelProvider.Factory
class MyModel(val id: String) : ViewModel() { class ViewModeFactory(private val id: String) : ViewModelProvider.Factory { override fun <T : ViewModel?> create(modelClass: Class<T>): T { return MyModel(id) as T } } }
使用我们创建的model
val myModel = ViewModelProvider(this, MyModel.ViewModeFactory(id) )[MyModel::class.java]