Hi, I'm a bit confused as I have been using an `An...
# getting-started
n
Hi, I'm a bit confused as I have been using an
AndroidViewModel
so I could pass a context with no memory leak, after this conversation: https://kotlinlang.slack.com/archives/C0B8MA7FA/p1609278349460800?thread_ts=1609277107.460700&cid=C0B8MA7FA The problem since I've recently updated Kotlin is that this line is highlighted with a leak warning:
Copy code
private val context = getApplication<Application>().applicationContext
Why this change and what's the point of
AndroidViewModel
in that case? Any ideas what I could do to avoid the problem?
i
An
Application
class is a
Context,
you don't need that variable at all
Just make your constructor
val application: Application
if you need to reference a Context variable sometime after init
n
I see, brilliant, many thanks!
l
@Nat Strangerweather no need to change your constructor... just use AndroidViewModel.getApplication()
👍 1