Sign in Go Pro

Android Architecture Components - A Deep Dive

What is a ViewModel?

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson

Up next



The ViewModel class is designed to store and manage UI-related data. They are responsible to prepare & provide data for UI and automatically allow data to survive configuration change.

In this lesson, we will look at the problems of managing data in the Activity or Fragment and then use ViewModel to solve those problems. We will also dive deep into how ViewModels internally work. You'll learn to solve the "God Activity" problem and start to keep only UI logic in the Activity or Fragment


cloning the repository the Observer does not get updated, it indicates even not zero from the beginning but the LiveData object in the screen. If I breakpoint the increment method in the View Model, is updated without problems with successive int values (1,2 etc)
counter.value = counter.value?.inc()

EDIT=Actually the activity in the GIT is different by the one in the text, there is any observe method subscription of LiveData in the original

I do not understand how come you call directly the xml attribute id as an object ex count.
how is possible that?

There is a little difference between the Activity on Git and in the video to keep it simple to explain in the video.
The magic of directly calling the xml attribute id is done using Kotlin Android Extension plugin. Check out this lesson for more information:

Nice, thanks now it works

You need to go PRO to post comments.

Lessons in Android Architecture Components - A Deep Dive