2

Edit: "Duplicate question" does not provide an applicable answer to this question. The approach provided there does NOT get rid of both warnings, only of one. Also my question contains a 2nd part which is not answered there either.

If I build a Volley Singleton according to the official tutorial,

https://developer.android.com/training/volley/requestqueue.html#singleton

I get a warning for

private static VolleySingleton mInstance 

and

private static Context mContext 

saying "do not place android context classes in static fields". I have read the other Stackoverflow questions about this, but I found no solution that gets rid of this warning or an answer that actually explains what is going on here. Yes I have read this post: Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run) But I could not extract an applicable answer. What do I actually have to change to NOT break instant run or leak something here?

Another thing I dont understand is, why I have to use <T>at

public <T> void addToRequestQueue(Request <T> request)

I know that this stands for Generics, but if I remove them, it still works. What is the reason it is there then?

This is my Volley Singleton, please someone advice me to build it properly without warnings and redundancy:

public class VolleySingleton {
private static VolleySingleton mInstance;
private static Context mContext;
private RequestQueue mRequestQueue;

private VolleySingleton(Context context) {
    mContext = context;
    mRequestQueue = getRequestQueue();
}

public static synchronized VolleySingleton getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new VolleySingleton(context);
    }

    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request <T> request) {
    getRequestQueue().add(request);
}
}
Florian Walther
  • 6,237
  • 5
  • 46
  • 104
  • Possible duplicate of [Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)](https://stackoverflow.com/questions/37709918/warning-do-not-place-android-context-classes-in-static-fields-this-is-a-memory) – Zoe Dec 22 '17 at 12:20
  • This post does only get rid of 1 of the 2 warnings. The 2nd one remains. Also my question has a part 2 which is not solved there either. – Florian Walther Dec 22 '17 at 12:27
  • Since you have the singleton in a static context, and the singleton has a context field (doesn't matter if it's static or not) the context field is essentially statically accessible. – Zoe Dec 22 '17 at 12:32

1 Answers1

3

Just to be clear, this is a duplicate. I followed the suggestions made in the question that was linked in the comments and managed to write the class you need without any lint warnings on Android Studio. Here is a snippet:

public class VolleySingleton {

  private static VolleySingleton instance;
  private RequestQueue requestQueue;

  private VolleySingleton(Context context) {
    requestQueue = Volley.newRequestQueue(context.getApplicationContext());
  }

  public static VolleySingleton getInstance(Context context) {
    if (instance == null) {
      instance = new VolleySingleton(context);
    }
    return  instance;
  }

  public RequestQueue getRequestQueue(){
    return requestQueue;
  }
}
  • Thank you. I actually built a very similar approach but it is so different from the official tutorial that I wasn't sure if thats correct. Outside of this I simply call `VolleySingleton.getRequestQueue.add` right? I still don't understand whats the point of the in the `addToRequestQueue` method, because it works without them. – Florian Walther Dec 22 '17 at 15:06
  • First time I call getIntance I will create a new VolleySingleton based on the context I send in. After that instance will never be null(?). Why do I have to pass a context for every other call to get the request queue, like getInstance(context).getRequestQueue() ? – Viktor Eriksson Feb 13 '20 at 11:48