‫Hello developers.
‫In this video we'll learn about the view models.
‫A view model is a part of the Android architecture component that it is designed to store and manage
‫UI related data in a way that survives configuration changes, for example, screen rotation and allows
‫the separation of concerns between UI components and data processing logic.
‫It helps to keep data separate from the UI components such as activities and fragments and allows for
‫better organization and maintainability of the code in Android.
‫Let's start by adding the dependency for our view model.
‫Go to build.gradle app level and here you should add the lifecycle version, which is 2.6.
‫Point two.
‫This is the latest version and the implementation of view model.
‫You can get this dependency from the Developer.android.com official website and search for Android X
‫lifecycle.
‫Select it and you can see and the life cycle contains this view model dependency.
‫Copy this and paste it in the ground.
‫Click sync now and here we go.
‫Now we're ready to use the View model in our application for View Model, I'll create a new Java class
‫and I'll name it as my view model and I'll extend from view model.
‫And you can see it's from the Android X dot lifecycle package.
‫So this class will contain the data you want to share between different UI components, for example
‫the counter.
‫So I'll move this counter to here.
‫Then I need to initialize the view model in the main activity in order to use it.
‫So you need to initialize the view model in your activity or fragment.
‫You can do this by using the view model provider.
‫So my view model.
‫View model.
‫Remove this view model equal to new my view model this dot get and the here view model provider.
‫Don't worry, I'll explain everything dot get and here my view model dot class view model provider.
‫This part of the code creates the view model provider instance.
‫A view model provider is a class provided by Android's architecture component that helps manage the
‫lifecycle of a view models.
‫It requires a reference to a view model store owner, which is typically an activity or fragment.
‫So for that I used this, and this parameter you provided refers to the current activity, which is
‫the main activity as the owner of the view model.
‫So this refers to the main activity and referring that this activity owns this view model dot get method.
‫And here I passed my view model dot class.
‫This part of code retrieves or creates a view model instance of the specified class, which in this
‫case my view model instance.
‫If you have a view model of the specified class already exists and is associated with the provided view
‫model store owner, in this case the activity main activity, it will return the existing view model
‫instance.
‫So if the view model already exists, it reused ensuring that you maintain a single instance of view
‫model associated with the activity or fragment even during configuration changes like screen rotations.
‫If the view model doesn't exist and new instance of my view model is created and it will be associated
‫with the lifecycle of the provided view model store owner.
‫So in summary, this line of code creates or retrieve my view model instance and that it is tied to
‫the lifecycle of the current activity, which is the main activity.
‫This allows you to access and manipulate the views, models, data throughout the lifecycle of the activity
‫or fragment, ensuring data, persistence, access, configuration changes and other lifecycle events.
‫Then I'll create a method public void increase counter and it will increase counter by one.
‫And here I'll create a getter method just to get the counter.
‫So one for increasing the counter and one for getting the current counter.
‫This is our view model.
‫In this case I am separating the valuables from the UI using the view model.
‫Let's move to the main activity main binding dot button that's set on Click Listener New View.
‫I can use the data binding for making this and referring to the view model to call and trigger this
‫increase counter method increase view model dot.
‫Rees counter method.
‫Also I need to display the the result or the current counter in the text view.
‫So for that main binding dot text view two dot set text view model dot get current counter.
‫Also, when running the application I need to display the counter here.
‫Let's run the application and oops, we have an error because I think it is from here.
‫I need to set an empty string and you can set the text directly for integers.
‫This returns an int so you can see we have an int here, so I can't set directly an int for a text view.
‫I need to use an empty string at first.
‫Let's run and here we go.
‫This is our application.
‫Increase the counter and it's working fine.
‫Now let's start making some configuration changes like screen rotation and you can see that the view
‫model keeps the counter, increase the counter to 17, then rotate the screen and here we go.
‫This is our view model app persisting and keeping the counter to its last state.
‫So congratulations guys.
‫We succeeded in saving and keeping the data and separating the data from the UI using the view model.