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

后端 未结 9 1559
野的像风
野的像风 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:50

    I had similar problems and in the end found out I missed off the "extends ViewModel" within the class definition, so it should look like this:

    public class ViewModelClass extends ViewModel
    {
        // Tracks the score for Team A
        public int scoreTeamA = 0;
    
        // Tracks the score for Team B
        public int scoreTeamB = 0;
    }
    

提交回复
热议问题