问题
According to https://developer.android.com/training/volley/requestqueue.html#singleton it is discouraged to use the old way of implementing a singleton class by setting up the RequestQueue in Application.onCreate()
The provided "new" more modular way as seen below however doesn't contain a method for adding tags to requests and cancelling them using these tags.
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
How do I go about adding the methods similar to the ones below from the old way (in Application.onCreate()):
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
回答1:
Nothing about RequestQueue has changed in that respect. You're just accessing it from a different singleton than the Application class. Add the tags to the requests the same way you always would:
ImageRequest request = new ImageRequest(...);
request.setTag(MY_TAG);
MySingleton.getInstance(this).addToRequestQueue(request);
-- edit --
Further elaboration: it's the same as if you were to get the RequestQueue object from anywhere else. It's still just a RequestQueue.
So this:
mRequestQueue.cancelAll(tag);
Becomes:
MySingleton.getRequestQueue().cancelAll(tag);
The same goes for anything else you were previously doing with a RequestQueue. The example MySingleton class there is just acting to hold on to the request queue for you. It isn't changing it.
To cancel absolutely everything, no matter what tag:
MySingleton.getRequestQueue().cancelAll(new RequestQueue.RequestFilter() {
@Override
public boolean apply(Request<?> request) {
return true;
}
});
来源:https://stackoverflow.com/questions/38964271/android-volley-singleton-pattern-how-to-add-cancel-requests-based-on-tag