Proper way to build a Volley Singleton

。_饼干妹妹 提交于 2021-01-25 03:32:32

问题


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);
}
}

回答1:


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;
  }
}


来源:https://stackoverflow.com/questions/47941438/proper-way-to-build-a-volley-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!