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

julioribeiro
  • 973
  • 1
  • 10
  • 20
  • 2
    I don't see a problem. https://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String, int) and https://stackoverflow.com/questions/987072/using-application-context-everywhere?rq=1 – Raghunandan Sep 19 '17 at 12:27
  • 1
    "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. – CommonsWare Sep 19 '17 at 12:29
  • 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 @CommonsWare , btw, loved your books, I have all 3 for android. – julioribeiro Sep 19 '17 at 13:03
  • You might be interested in [Dave Smith's classic blog post on the role of various `Context` objects](https://possiblemobile.com/2013/06/context/). And I'm happy to have been useful for you! – CommonsWare Sep 19 '17 at 13:08
  • Thank you, this will be very helpful! – julioribeiro Sep 19 '17 at 19:11

1 Answers1

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.

Bamerza
  • 1,294
  • 1
  • 17
  • 32