Android Architecture Components : ViewModel
ViewModel : UI-Related Data Object One of the biggest challenges for an Android developer is keeping data across configuration changes such as screen rotation or during the recreation of Activity / Fragments after its been destroyed and recreated by the framework. Luckily the new Android Architecture Components library has ViewModel class that is intended to store and manage UI-related data so that the data survives configuration changes. Benefits Keeps data across UI recreation No need to repeat API calls and Database queries on UI recreation Keeps Activity / Fragments clean since work is now delegated to ViewModels Share ViewModels between Fragments Subclass ViewModel To create a view-model just subclass ViewModel . View-models are responsible to provide data and keep reference to it. Our TimerViewModel lazily initializes TimerLiveData which is a LiveData implementation of a timer that counts seconds, read more about TimerLiveData a...