How to get an Instance of ViewModel in activity in 2020/21?

后端 未结 9 1539
野的像风
野的像风 2021-02-13 04:27

I am new to the mvvm pattern. I created a ViewModel for the main activity. Now I want to get an instance of the ViewModel in the main activity.

Most Tutorials and answe

9条回答
  •  死守一世寂寞
    2021-02-13 04:44

    Duplicate question and already answered here

    Simply replace:

    This:

    boardViewModel = ViewModelProviders.of(this).get(BoardViewModel::class.java)
    

    With this:

    boardViewModel = ViewModelProvider(this).get(BoardViewModel::class.java)
    

提交回复
热议问题