I am using Android Volley lib in my project to execute network requests, all works very well but I have some troubles with the \"cancel\" feature of this lib. I explain my i
My guess would be that in the first case, the request has only been added to the RequestQueue
, which is why calling cancelAll()
works. In the second case, there is a slight delay between starting the request and pausing/destroying the Activity
: in that delay, the HTTP request has begun. You may not be aware that calling cancelAll()
works only for those requests that are still in the RequestQueue
. It doesn't work for an HTTP request that has already begun, there is no way to stop that.
Having said that, the documentation here implies that once a request is cancelled (i.e. by calling cancel()
), it can effectively be treated as though the web service had never been called in the first place. The callbacks associated with the particular request would not be invoked (although the response received would likely be held in the local cache).
I'd suggest you to cancel the added requests in onPause() method. Considering:
onPause() The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back). source: Android Developers
You can cancel a Request
attached to a TAG
. So basically you have to add a tag with every Request
.
yourRequestObject.setTag(TAG);
RequestQueue mRequestQueue;
mRequestQueue.cancelAll(TAG);
This way you can cancel all the Request
with a particular TAG
.
https://developer.android.com/training/volley/simple.html#cancel
This basically says that
Once cancelled, Volley guarantees that your response handler will never be called.
So even if there is no way to undo/cancel an http request that's already begun (which makes sense), the callbacks associated with the request won't be called, which to me effectively means I can treat is a cancelled request in most scenarios even though the response thus received silently might be available in the cache.