How to get Context in Android MVVM ViewModel
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.