1

I am new to android MVVM architecture . I saw in some articles that they say:

ViewModel should be limited to Activity and Fragment. RecyclerView items should not be extends from ViewModel.

In some tutorials ViewModel of RecyclerView Items extended from ViewModel :

public class UsersItemViewModel extends ViewModel {}

In some other tutorials extended from BaseObservable :

public class UsersItemViewModel extends BaseObservable {}

in some other tutorials does not extends from anything

public class UsersItemViewModel{}

I'm a little confused and I really do not know which one is right?

1 Answers1

0

The intent of ViewModel is hold data to Activities and Fragments (view, in general), besides view's lifecycles. Then, your recyclerview should not extends the viewmodel, because the view is managed by OS (cache, creating and remove). See this image take it from Google guide to MVVMModules in app

msfreire
  • 494
  • 6
  • 10
  • Thanks a lot @msfreire . Can still use BaseObservable ? –  Jul 25 '21 at 17:59
  • Inside VM, of course. But take a look if you are using two way data binding. Here an example: https://stackoverflow.com/a/62830022/3210714 – msfreire Jul 26 '21 at 18:00