From the course: Android Development Essential Training: App Architecture with Kotlin

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Add a ViewModel to an activity

Add a ViewModel to an activity - Android Tutorial

From the course: Android Development Essential Training: App Architecture with Kotlin

Start my 1-month free trial

Add a ViewModel to an activity

- [Instructor] I've previously described how to use one of Google's android architecture components, the LifeCycleObserver. There are other incredibly valuable architecture components and now we're going to talk about one of them, the ViewModel. A ViewModel is designed to manage your data at runtime. Each activity or fragment in an app, can have its own ViewModel. The ViewModel stores data persistently in memory and it sticks around even during configuration changes. So while an activity or a fragment are destroyed and recreated, say when the device changes orientation, the ViewModel stays in memory, and it can then publish its data to the activity or fragment and the activity or fragment can subscribe to handle that data when it changes. The first step in using a ViewModel is to add a new dependency to your Gradle Build file. Open the Gradle file for your module and then go down to the dependency section. Add a new dependency after the existing implementations, add a new…

Contents