Reputation: 81
I am still learning the Android Programming and trying to understand the concept of ViewModel. Major motivations behind the View Models (in almost all the ViewModel tutorials) is to keep the data agnostic to LifeCycle Events. I find that Adapters used under ListView or Recyclers also offer this feature as same adapter is used by the view created in newly created Activity. I am wondering what is the exact difference between both?
As per my current understanding, ViewModel provide the ability to have lifecycle agnostic data for all the views within Activity however Adapter (though they could offer the same things) can work only on certain views. Is my understanding correct?
Upvotes: 3
Views: 1083
Reputation: 24917
As per my current understanding, ViewModel provide the ability to have lifecycle agnostic data for all the views within Activity however Adapter (though they could offer the same things) can work only on certain views. Is my understanding correct?
Yes.
To put it in a simple way:
RecyclerViewAdapter:
Provide a binding from an app-specific data set to views that are displayed within a RecyclerView
ListAdapter:
An extended Adapter
that is the bridge between a ListView
and the data that backs the list
ViewModel:
Designed to store and manage UI-related data in a lifecycle conscious way. It also allows data to survive configuration changes such as screen rotations.
Upvotes: 1