Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel All Volley Requests Android

Tags:

At the moment i´m using mRequestQueue.cancelAll(getActivity()) at on stop method in a fragment but apparently when i move the phone from landscape to portrait it is still returning the data made in the request but causing crash because the holders for the data dosent exist anymore. any sample code of how to do it properly?

like image 933
geekkoz Avatar asked May 27 '13 13:05

geekkoz


People also ask

How do I cancel a volley request?

getMyData("urltocall", getDataListener, requestTag); MySingleton. getRequestQueue(). cancelAll(requestTag);

How to make get request using Volley in Android?

Use newRequestQueue RequestQueue queue = Volley. newRequestQueue(this); String url = "https://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request. Method.


2 Answers

Instead of using a tag for cancelAll, make an all-pass RequestFilter.

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

EDIT: This cancels all Requests from all activities/fragments, and doesn't work favorably with the Activity Lifecycle. The best way to manage this is to add a String tag unique to your fragment.

like image 122
gorjan5sk Avatar answered Oct 04 '22 21:10

gorjan5sk


You should set the tag to an object, not a method.

By setting the tag to getActivity(), you are asking Volley to use a dynamic method call on the main thread as a reference to the request which is happening on a background thread.

So when the background thread is trying to cancel the requests, the activity could already be dead.


Rather than using getActivity(), use this or some other object or string.

This is good practice for any Tag, and you should also beware of leaking your activity.

Solutions:


You could use the current object:

request.setTag(this); 

or, the static class object

request.setTag(MyFragment.class); 

or, as a constant in a separate class:

request.setTag(CustomTags.LIST_REQUESTS); 

CustomTags.LIST_REQUESTS being the best in my opinion (less chance of leaking activity)

Something like this:

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

Update

I just noticed I was making a mistake in tagging my requests in Volley (though the solutions I posted above are fine).

I still thought I would update here an important thing to keep in mind. Volley tags by identity not value.

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

It's similar to 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 
like image 27
pjco Avatar answered Oct 04 '22 22:10

pjco