I am working on an Android application where I have to display weather data to users when they start the app. I'm following MVVM architecture and have successfully followed MVVM in all the use cases I have implemented including displaying data to users and manipulating SharedPreferences for Settings screen. I am using FusedLocationProviderClient
for getting user's location, but it needs Context
for instantiation. What should be the ideal way of dealing with this problem using while abiding by the MVVM architecture?
I cannot instantiate it from ViewModel
since it will result in Context
being passed to ViewModel
hence causing potential memory leaks.
Currently, I am using FusedLocationProviderClient
from my Fragment
and updating data from there, but this is a very naive approach and clearly violates MVVM.
you can get Context by extending your ViewModel from AndroidViewModel and then you can do SharedPreferences related stuff there without violating MVVM pattern.