ViewModel和ViewModelProvider
依赖
//ViewModel |
创建ViewModel
class GameViewModel : ViewModel() { |
关联并初始化
声明
private lateinit var viewModel: GameViewModel |
初始化
viewModel = ViewModelProvider(this).get(GameViewModel::class.java) |
Important: Always use ViewModelProvider to create ViewModel objects rather than directly instantiating an instance of ViewModel. |
使用ViewModelFactory
class ScoreViewModel(finalScore: Int) : ViewModel() { |
class ScoreViewModelFactory(private val finalScore: Int) : ViewModelProvider.Factory { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 码行!
评论
ValineDisqus