Cancel All Volley Requests Android

前端 未结 9 1295
我在风中等你
我在风中等你 2020-12-05 14: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 re

相关标签:
9条回答
  • 2020-12-05 14:36

    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.

    0 讨论(0)
  • 2020-12-05 14:42

    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.

    0 讨论(0)
  • 2020-12-05 14:48

    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.

    0 讨论(0)
  • 2020-12-05 14:56

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 14:57

    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;
            }
    });
    
    0 讨论(0)
  • 2020-12-05 14:57

    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

    0 讨论(0)
提交回复
热议问题