Android Architecture Components ViewModel Context
Asked Answered
M

1

11

I'm studying google's architecture components to implement ViewModel and LiveData to my app, and the official documentation says that:

Note: Since the ViewModel outlives specific activity and fragment instantiations, it should never reference a View, or any class that may hold a reference to the activity context. If the ViewModel needs the Application context (for example, to find a system service), it can extend the AndroidViewModel class and have a constructor that receives the Application in the constructor (since Application class extends Context)

Following that, I ended up with a code like that:

public class ViewModelTest extends AndroidViewModel {

public ViewModelTest(Application application) {
    super(application);
}

public void test(){
    Prefs.getCurrentCode(getApplication());
}

And should I instantiante it normally on the activity?

  val viewModel2 = ViewModelProviders.of(this).get(ViewModelTest::class.java)
    viewModel2.test()

Isn't it bad? To use this application variable when need to access SharedPreferences or anything that need a context? And if it is, should I avoid using it on the ViewModel and use it only on the view? Specially if I want to update a UI component with a value that needs a context. I kinda don't know how to approach this issue, and I'm open for any suggestions.

Thanks in advance

Marymarya answered 19/9, 2017 at 12:24 Comment(5)
I don't see a problem. developer.android.com/reference/android/content/…, int) and #987572Chelsiechelsy
"Isn't it bad?" -- why? "To use this application variable when need to access SharedPreferences or anything that need a context?" -- what else would you propose? Pretty much all local data storage is centered around a Context. For non-UI work (e.g., working with local data storage), Application is a fairly safe Context to use.Engrail
That's exactly my question,if using Application class is a safe context approach, since I always read that using "local" context or activity contexts were a better practice. It looked ok to me, but I wanted more opinions before jump in, thanks @Engrail , btw, loved your books, I have all 3 for android.Marymarya
You might be interested in Dave Smith's classic blog post on the role of various Context objects. And I'm happy to have been useful for you!Engrail
Thank you, this will be very helpful!Marymarya
C
6

AndroidViewModel class is provided as part of the android.arch.lifecycle package which is part of Android's architecture components. It itself calls for the Application Context passed into the constructor. The Application Context lives across the Activity lifecycle.

An Application context in a ViewModel is okay because the Application context is tied to the whole Application lifecycle as opposed to an Activity context, which is tied to the Activity lifecycle.

ViewModel documentation specifically is referring to not use the Activity Context, but the Application Context if fine.

Cowshed answered 5/4, 2018 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.