How to get Context in Android MVVM ViewModel
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Android's architecture components provide a robust framework for implementing the MVVM (Model-View-ViewModel) pattern in applications. This pattern promotes a clean separation of concerns, making applications more modular, testable, and maintainable. One fundamental aspect that often challenges developers is obtaining context within the ViewModel. Since ViewModel is designed to outlive `Activity` rotations or fragment recreations, directly referencing a context can be problematic. Let's explore the solutions to address this in an Android application.
Why Context in ViewModel?
Obtaining a context in ViewModel can be necessary for various reasons, such as accessing resources, system services, or initiating certain operations like starting an `Activity`. However, due to the lifecycle of ViewModel, which is detached from the view lifecycle, directly keeping a context reference introduces potential memory leaks or unwanted behavior.
Best Practices for Accessing Context in ViewModel
1. Using `AndroidViewModel`
One of the simplest approaches to access context in a ViewModel is to extend `AndroidViewModel` instead of the base `ViewModel`. `AndroidViewModel` is sub-classed from `ViewModel` and provides an application context designed to suit these needs.
- Provides easy access to application context.
- Safe as it uses the lifecycle-aware application context.
- Less suited for unit testing due to its reliance on the Android framework (`Application`).
- Pass resource values/data to ViewModel from the `Activity`/`Fragment`.
- Use data binding or View Binding to minimize direct context interaction within ViewModel.
- Provides more scalable and testable code.
- Encourages clean separation of components.
- Resource Wrappers: Consider creating wrapper classes for resources or services that require context, which you can inject or pass to ViewModel.
- Utility Classes: Define utility or helper classes that take context as parameters only during specific operations and do not retain it.
- Single Responsibility Principle: Keep context-bound operations at the UI or a specific service level.
Related reading
- How to get Context in Jetpack Compose
- How to get Core Data object from specific Object ID?
- How to get current foreground activity context in android?
- How to get current language code with Swift?
- How to get current time and date in Android
- How to get current time and date in Android
- How to get device make and model on iOS?
- How to get enum from raw value in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.