Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In this blog, we will learn ViewModels In Android and check how it works under the hood & how it can be used to store and manage UI-related data in a LifeCycle-aware manner.
ViewModel work in a LifeCycle-aware manner, what does that mean? ViewModel stores data only while the app is running and will destroy data when the app is killed or destroyed. The ViewModel scope stays alive throughout the activity & fragment lifecycle and only destroys when a component is associated with it destroy.Let’s understand how we can implement ViewModels in our Applications for optimizing the code and storing UI-related data which can survive the orientation changes.
First of all, we will create MyViewModel class which extends ViewModel. In this class, we will add our business logic for performing any actions. In this dummy code, we are fetching user data from the repository(Server or Room DB) and will return to the main component which calls ViewModel.
You may use any of the repository systems, you can use the Room database for fetching the data and REST API calls for fetching the data or performing any operation
For accessing the ViewModel, we need the “ViewModelProvider” class object which returns the ViewModel if already created. And if it is not created then create the same and then return to the calling Activity or Fragment class.ViewModelProider constructor takes two arguments for creating its object which is as follows1. ViewModelStoreOwner => Which stores the ViewModels, our activity or fragments works as ViewModelStore owner. As ComponentActivity(Parent class of Activity) implements ViewModelStoreOwner interface which has only one method as mentioned below
2. FactoryWhen you don’t provide a factor class then ViewModelProvider auto creates the same. But if you want to send some additional parameters in the ViewModel class then you need to create your own factory Class. The main purpose of this class is to send additional arguments or data to the ViewModel constructor.
After creating the ViewModel object, we will register an observer. So that, whenever data changes in the ViewModel it will reflect the main class.
We talked about ViewModels In Android in this blog.
I hope it will aid in your comprehension and provide you with a basic understanding of it.
You may also read the Developer Console for additional information.
I appreciate your reading.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
The results provided were above our expectations both in deliverability timeline as well as operation. They are a top-notch company with many resources and a company we feel honoured to work with again!
Lee Seward
Founder, Gro-Connect
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.