I have a class which has a static variable which holds Context
reference.
public static Context context;
Android studio gives a warning saying that static references to the Context class leads to memory leaks
If I remove the static
keyword, it does not give any warning.
Similar scenario with ContextWrapper
class as well.
My understanding is, if we hold a reference to the classes which are related to Context
will lead to memory leak. But, Why Android studio does not give any warning for non-static Context
references?
I have seen some code examples where they have extended the ContextWrapper
class. Whenever they needed the Context
, they are accessing through the class which was extended `ContextWrapper'. Will this approach does not lead to Memory leak?
Also, Will memory leaks happens for non-static Context
references at runtime? Did I understand it in wrong way? Am I missing something here?
Can anyone give explanation for this?
Thanks in advance.