Skip to main content

Posts

Showing posts with the label View Model

How to use View Model Architecture Components to preserve the data of the Activity and Fragment

In this tutorial, we will learn how to implement the View Model Architecture Components to preserve the data of the Activity and Fragment.  You know, Activity or Fragment is get created and destroyed very frequently whenever any configuration change has at the Android system level.  For example whenever you rotate the screen activity and fragment will go through the full lifecycle and it gets recreated again. In this scenario, all data of your UI element will get lost. So ViewModel is the solution to this problem. Introduction To clear the concept of View Model and implementation of it, we are making this tutorial very simple. In this, we will add two component a TextView and Button at layout layer. When the user clicks on the Button TextView will show the number of clicks. What happens in this case at the screen rotation TextView click count get reset.  As the solution of this, we will implement the ViewModel so that number of click count stored into the variable of main activity shou...