Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does FusedLocationProviderClient fit in MVVM on Android?

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.

like image 340
Abdul Mateen Avatar asked Sep 19 '25 22:09

Abdul Mateen


1 Answers

you can get Context by extending your ViewModel from AndroidViewModel and then you can do SharedPreferences related stuff there without violating MVVM pattern.

like image 133
Zafer Celaloglu Avatar answered Sep 22 '25 12:09

Zafer Celaloglu