
Question:
<a href="https://stackoverflow.com/questions/44169270/do-not-place-android-context-classes-in-static-fields-this-is-a-memory-leak" rel="nofollow">Here</a> is a similar question but I think it is not identical
And I read Setting up request queue tutorial <a href="https://developer.android.com/training/volley/requestqueue.html" rel="nofollow">here</a>
In this page they write the following code
public class MySingleton {
private static Context mCtx;
And I wrote same code in my project
public class VolleySingleton {
private static Context mContext;
Android studio say “Do not place Android context classes in static fields; this is a memory leak”.<br /> What does it mean? And why dos the official android developer website use such kind of code?
Answer1:<blockquote>
Android studio say static field will leak contexts
</blockquote>Let say You are referencing your class <strong>MySingleton
</strong> from another class and your <strong>MySingleton
</strong> Class has been destroyed by the OS but the static Context(mContext)
is still in use by some activity so this will hold on the context
from garbage collection unless you set your MySingleton
reference inside your activity to null
and this will leak all the application's resources
try this? `
class VolleySingleton {
@SuppressLint("StaticFieldLeak")
private static Context mContext;
}`