- Including the ViewModel Dependency
- Adding a ViewModel
- Saving Data Across Process Death
- ViewModel vs Saved Instance State
- For the More Curious: Jetpack, AndroidX, and Architecture Components
- For the More Curious: Avoiding a Half-Baked Solution
For the More Curious: Jetpack, AndroidX, and Architecture Components
The lifecycle-extensions library containing ViewModel is part of Android Jetpack Components. Android Jetpack Components, called Jetpack for short, is a set of libraries created by Google to make various aspects of Android development easier. You can see a listing of all the Jetpack libraries at developer.android.com/jetpack. You can include any of these libraries in your project by adding the corresponding dependency to your build.gradle file, as you did in this chapter.
Each of the Jetpack libraries is located in a package that starts with androidx. For this reason, you will sometimes hear the terms “AndroidX” and “Jetpack” used interchangeably.
You may recall that the New Project wizard, as shown in Figure 4.10, has a checkbox for Use AndroidX artifacts. In almost every case, you should enable this option, as you did for GeoQuiz. This will add some initial Jetpack libraries to your project and set your app up to use them by default.
FIGURE 4.10 Adding Jetpack libraries
The Jetpack libraries are broken into four categories: foundation, architecture, behavior, and UI. Libraries in Jetpack’s architecture category are often referred to as architecture components. ViewModel is one such architecture component. You will learn about other major architecture components later in this book, including Room (Chapter 11), Data Binding (Chapter 19), and WorkManager (Chapter 27).
You will also learn about some foundation Jetpack libraries, including AppCompat (Chapter 14), Test (Chapter 20), and Android KTX (Chapter 26). You will use Notifications, a behavior Jetpack library, in Chapter 27. And you will also use some UI Jetpack libraries, including Fragment (Chapter 8) and Layouts (Chapter 9 and Chapter 10).
Some of the Jetpack components are entirely new. Others have been around for a while but were previously lumped into a handful of much larger libraries collectively called the Support Library. If you hear or see anything about the Support Library, know that you should now use the Jetpack (AndroidX) version of that library instead.