0

Here is my MineSweeperView class:

class MineSweeperView : View {

constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    private var userViewModel: UserViewModel


    init {
        ....
        userViewModel = ViewModelProvider(???).get(UserViewModel::class.java)
    }

ViewModelProvider(this) and (context) is not working in View class.

Dominik
  • 49
  • 7

1 Answers1

1

You can't do this. It would be improper for a View to have a ViewModel class. ViewModels are only for LifecycleOwners, because they are scoped to the life of a LifecycleOwner. That's the whole reason for the existence of ViewModel, to have it's life controlled by the life of some other lifecycle object.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154