Cancel all Volley requests

Currently I am using mRequestQueue.cancelAll (getActivity ()) with the stop method in the fragment, but apparently when I move the phone from landscape to portrait, it still returns the data made in the request, but it fails because the holders for dosimeter data already exist. any sample code how to do it right?

+18
android android-volley
May 27 '13 at 13:48
source share
8 answers

Instead of using the tag for cancelAll, create an All-Pass RequestFilter.

mRequestQueue.cancelAll(new RequestQueue.RequestFilter() { @Override public boolean apply(Request<?> request) { return true; } }); 

EDIT: this cancels all requests from all actions / fragments and does not work favorably with the activity lifecycle. The best way to handle this is to add a String tag unique to your snippet.

+52
Oct 03 '13 at 1:11
source share

You must set the tag for the object, not for the method.

By setting the tag to getActivity() , you will ask Volley to use a dynamic method call in the main thread as a reference to the request that occurs in the background thread.

Therefore, when the background thread tries to cancel requests, the activity may already be dead.




Instead of using getActivity() use this or some other object or string.

This is good practice for any tag, and you should also beware of activity leaks.

Solutions:




You can use the current object:

 request.setTag(this); 



or, an object of a static class

 request.setTag(MyFragment.class); 



or, as a constant in a separate class:

 request.setTag(CustomTags.LIST_REQUESTS); 



CustomTags.LIST_REQUESTS is the best in my opinion (less likely to leak activity)

Something like that:

 public class CustomTags { public static final String LIST_REQUESTS="CustomTags:LIST_REQUESTS"; } 

Update

I just noticed that I was mistaken in tagging my queries in Volley (although the solutions that I posted above are fine).

I still thought that I would be updating an important thing to keep in mind. Signs of volleyball identity are not meaningful .

Thus, it is important to keep in mind that a tag that is simply the same string value and not the same object will not be recognized as the same tag .

It looks like the difference between

 String a1 = "A"; String a2 = "A"; a1 == a2; //evaluates to false String a1 = "A"; String a2 = "A"; a1.equals(a2); // evaluates to true 
+17
Aug 29 '13 at 1:24
source share

I know this answer has come recently, but in case someone else comes across this problem:

In my implementation, the tag was installed (and rewritten) in the place where the request was added to the queue.

Thus, even though I canceled the request using my tag, the tag in the request queue was not the same (as it was previously rewritten), and it was not canceled.

Registration of executed requests and printing tags led me to a solution:

 mRequestQueue.cancelAll(new RequestQueue.RequestFilter() { @Override public boolean apply(Request<?> request) { Log.d("DEBUG","request running: "+request.getTag().toString()); return true; } }); 
+4
Nov 22 '13 at 12:58
source share

What tag did you use when making requests? If you have not set a tag for each of your requests, it may never work. As far as I can see, Volley does NOT automatically set a tag for your requests

+3
Jun 14. '13 at 8:28
source share

If you are adding a request to the queue from a frame, you must undo the following: mRequestQueue.cancelAll(this) . And sorry if this did not work, I did not test this solution. But I hope this help helps you.

+1
Jun 06 '13 at 6:38
source share

Are you setting an activity request tag? This is the only way the code you provide will work. The cancelAll method searches for all requests with the tag of any tag that you provided, and cancels them.

0
03 Oct '13 at 18:45
source share

Check out this article. It uses Oto as a single event bus. In this way, you can notify the burst queue when your activity or fragments are recreated. Of course, you can use the simple old interface and listen to the changes. Otto's BUt looks much less detailed and elegant, like a unified solution.

http://andraskindler.com/blog/2013/eventbus-in-android-an-otto-example/

0
Aug 20 '14 at 8:41
source share

In the case of a fragment; Use only one RequestQueue rQueue; Initialize it in the OnCreate method; And use it for all volleyball requests; and in the end

@Override

 public void onStop () { super.onStop(); if (rQueue != null) { rQueue.cancelAll(this); } } 
0
Oct 23 '17 at 6:23
source share



All Articles