waylonbrown
09/12/2017, 10:22 PMclass MyClass {
Context context;
MyClass(Context context) {
this.context = context.getApplicationContext();
}
}
The key point is that I want to make sure that I don't hold a reference to the Context that's passed in, but rather hold a reference to the context.getApplicationContext(). I've tried this attached picture in Kotlin, though as you can see it still refers to and holds onto the Context that was passed in (MainActivity) which is very much what I don't want. I've also done this successfully
class MyClass private constructor(val context : Context) {
companion object {
fun create(context: Context) = MyClass(context.applicationContext)
}
}
though it's not ideal since if I call it from Java I have to refer to the companion object with MyClass.Companion.create(context) where ideally I'd just call the constructor just like with the Java example