From the course: Android Development Essential Training: 3 Navigation
Unlock the full course today
Join today to access over 22,400 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: 3 Navigation
Add a ViewModel to an Activity
- [Instructor] Let's add a view model to our MainActivity. To get started, we'll update our build.gradle file to include the activity-ktx library. And we'll do that here on line number 45. Let's click on sync now. KTX are accustom extensions for Jetpack components which allow us to write more idiomatic Kotlin. So you'll see more of these in future videos as well. Next, we need to create a new class for our view model in the same package as our MainActivity. First let's open up the project view. We're going to click on this package and we'll right click and choose new, Kotlin File/Class. This is going to be a class and we're going to name it MainViewModel and then click on enter. This class is going to extend ViewModel. ViewModel is an abstract class so we'll needs to provide the parentheses when we create it and let's use our intention action in order to bring in that import. Perfect and that's really all that's…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.