Dominik
Dominik

Reputation: 49

In VIew how to use ViewModel with ViewModelProvider

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.

Upvotes: 0

Views: 403

Answers (1)

Tenfour04
Tenfour04

Reputation: 93561

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.

Upvotes: 1

Related Questions