Cancel All Volley Requests Android
Asked Answered
G

9

23

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?

Globoid answered 27/5, 2013 at 13:48 Comment(1)
Did you check tour activity didn't change ? I believed activity is destroyed and then rebuild when rotating. And as Activity seems to be your tag, it's not the same tag anymore.Circosta
P
59

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.

Pomeranian answered 3/10, 2013 at 1:11 Comment(3)
In Kotlin: mRequestQueue?.cancelAll({ true })Bugle
requestQueue?.cancelAll { true } is more Kotln ;)Fe
Java8+ : mRequestQueue.cancelAll((req) -> { return true; }); not pretty but it works.Valverde
U
19

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
Unbeliever answered 29/8, 2013 at 1:24 Comment(4)
I would like to thank you for pointing out the String compare concept which I should be aware of. I stuck in a problem that I believed I called RequestQueue.cancelAll(final Object tag) to cancel all my requests that tagged with Activity.toString(), but didn't work. The tag comparison in cancelAll(final Object tag) method is actually using request.getTag() == tag as a RequestFilter which I didn't notice. Now wrote a request.getTag().equal(tag) as RequestFilter fixed my problem.Larson
@Unbeliever shouldn't setTag() be called on the request itself and not on the queue?Hubbub
Yes I think you are correct I will update the answer. My variable names imply it is the cue, which is not what you tag.Unbeliever
@SilviaHisham Answer updated, thank you for noticing that!Unbeliever
L
4

I know this answer comes in late, but in case anyone else is having this problem:

In my implementation the Tag was being set (and overwritten) at the point where the request was added to the queue.

So despite that I was cancelling the request with my Tag, the tag on the request queue was not the same (as it was previously overwritten) and it was not cancelled.

Logging the requests running and printing out the tags, led me to the solution:

mRequestQueue.cancelAll(new RequestQueue.RequestFilter() {
    @Override
        public boolean apply(Request<?> request) {
        Log.d("DEBUG","request running: "+request.getTag().toString());
            return true;
        }
});
Lundgren answered 22/11, 2013 at 12:58 Comment(0)
S
3

Which tag did you use when making the requests? If you didn't set a tag on each of your requests it may never work. As far as I see, Volley does NOT automatically set a tag for your requests

Studdard answered 14/6, 2013 at 8:28 Comment(2)
i setted the tag as mRequestQueue.setTag(getActivity()); , but it didnt worked.Globoid
I haven't used this functionality myself, but I think that you should put the tag on your Request object, not the Queue.Studdard
C
1

If you add request to queue from framgment, you should cancel like this: mRequestQueue.cancelAll(this) . And sorry if it didn't work - i didn't test this solution. But i hope this help to you.

Chauncey answered 6/6, 2013 at 6:38 Comment(1)
this works only if you had set tag for all the requests :( didnt work for meCentiliter
D
1

In Kotlin

  requestQueue?.cancelAll { true }
Debonair answered 14/12, 2020 at 13:8 Comment(0)
D
0

Are you setting the tag of the requests to the activity? That's the only way the code you are providing will work. The cancelAll method searches all of the requests with the tag of whatever tag you provided and cancels them.

Donal answered 3/10, 2013 at 18:45 Comment(0)
A
0

In Case Of Fragment; Use only One RequestQueue rQueue; Initialize it in OnCreate method; And use it for all volley request; and at the end

@Override

public void onStop () {

    super.onStop();
    if (rQueue != null) {
        rQueue.cancelAll(this);
    }
}
Anonym answered 23/10, 2017 at 6:23 Comment(0)
W
-1

I've struggled with the memory leak for the longest time until I found I called stop() from the class 'RequestQueue'.

//Initialize the object
RequestQueue requestQueue = 
        Volley.newRequestQueue(getActivity().getApplicationContext());

//Release the object
requestQueue.stop();
requestQueue = null;

The class says it "Stops the cache and network dispatchers." Whatever that means...

Wherefore answered 9/2, 2019 at 0:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.