Ansh Sachdeva
Oct 24, 2021

its okay to access application context to start some service or database which requires context for initialisation, but for that case too you must destroy/stop those instances/listeners in on cleared.

however its not advised to use application context to access drawables/strings because application context does not know of the specific resource to access.

for eg if your app supports english and spanish language , and you want to access a string that is both in english and spanish in your resources( strings.xml and sp/strings.xml) , then with view context, you get the user preferred language string(spanish or english) , and with application context you get the default language string( i.e from the english version)

thus on a general level, you can use viewmodels in almost all the cases, except when you wanna user db/service. and with dagger/hilt, you can move those things out too

Ansh Sachdeva
Ansh Sachdeva

Written by Ansh Sachdeva

curiously fiddling around with tech | https://root-ansh.github.io/curioustools/ also @anshsachdevaprofessional

Responses (1)