Abdul Mateen
Abdul Mateen

Reputation: 1684

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.

Upvotes: 4

Views: 1169

Answers (1)

Zafer Celaloglu
Zafer Celaloglu

Reputation: 1418

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

Upvotes: 3

Related Questions