# ViewModel. How it works.

DevFeed: [ViewModel. How it works.](<https://devfeed.tech/articles/viewmodel-how-it-works-26015.md>)

Original publisher: [Read original article](<https://unbreakable-titan.medium.com/viewmodel-how-it-works-434282649286?source=rss-e32583ce94d4------2>)

Author: Vladislav Puryev

Published: 2020-01-18T15:40:09Z

Content type: tutorial

Language: en

Sources: [Stories by Vladislav Puryev on Medium](<https://devfeed.tech/sources/stories-by-vladislav-puryev-on-medium.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [Code](<https://devfeed.tech/topics/code.md>), [Framework](<https://devfeed.tech/topics/framework.md>), [configuration](<https://devfeed.tech/topics/configuration.md>), [Google](<https://devfeed.tech/topics/google.md>), [Java](<https://devfeed.tech/topics/java.md>)

Tags: [abstract-class](<https://devfeed.tech/tags/abstract-class.md>), [android](<https://devfeed.tech/tags/android.md>), [android-framework](<https://devfeed.tech/tags/android-framework.md>), [code](<https://devfeed.tech/tags/code.md>), [configuration](<https://devfeed.tech/tags/configuration.md>), [constructor](<https://devfeed.tech/tags/constructor.md>), [evolution](<https://devfeed.tech/tags/evolution.md>), [java](<https://devfeed.tech/tags/java.md>), [screen](<https://devfeed.tech/tags/screen.md>), [state](<https://devfeed.tech/tags/state.md>), [technical](<https://devfeed.tech/tags/technical.md>), [viewmodel](<https://devfeed.tech/tags/viewmodel.md>)

## AI overview

This technical tutorial examines how Android ViewModel works internally and how it helps preserve screen state across configuration changes. It explains that ViewModel itself is a simple abstract class and that state retention results from processes within the Android framework, with correct initialization and factories required for dependencies.

## Source excerpt

Android ViewModel. How it works. Hello everybody. This post will be about popular solution from Google for Android developers -- ViewModel (if you are not familiar with it yet -- get started from official documentation). The main goal of article -- show you how viewModel works, how this tool helps us to save state of our screens. First of all it is better to note that viewModel -- is just java code inside Android framework and there is no any magic... almost. This mechanism was improved something about three times from its origin (2017) till the present day. Let's take a look at "Evolution of anatomy". WARNING! This article contains a lot of technical content and internals of Android framework. And please pay attention -- all demonstrated code you can find in your Android Framework using middle-click or ctrl + left-click. There is no any of self-made code -- all is inside Android framework. Attention again -- most of code will be provided in shortened form. Only necessary details. Full versions you can find in Android Framework source codes straight inside your IDE. ViewModel 2017 Honestly, this part (I mean exactly ViewModel's version of 2017) can be unnecessary, because Medium has several articles, that describes viewModel saving state mechanism in first version. I like this one. But I suggest you more code than there. First of all let's take a look at ViewModel class itself. According to official documentation: The ViewModel class allows data to survive configuration changes such as screen rotations. It can lead to research this class. https://medium.com/media/3966f6a014283670bd0d0430100902cd/href It is just simple abstract class. Without any secrets. With one empty method. Can documentation be wrong (or obsolete) so long time? This question is not so obvious. From one side, if you will create your child viewModel and initialize it using constructor, in this case saving states mechanism will not work. But if you are doing everything in the right way (using special initial