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 - Android Tutorial
From the course: Android Development Essential Training: App Architecture with Kotlin
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
-
-
-
-
(Locked)
Work with synthetic view bindings5m 10s
-
(Locked)
Get view references with findViewById()4m 53s
-
(Locked)
Handle view events with lambdas5m 26s
-
(Locked)
Create companion object functions6m 16s
-
(Locked)
Update display from new data4m 47s
-
(Locked)
Restore state after orientation change7m 10s
-
(Locked)
Add a ViewModel to an activity4m 43s
-
(Locked)
Subscribe to changes in LiveData values7m 57s
-
(Locked)
-
-
-