Cancel All Volley Requests Android

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?

  • Android design library CoordinatorLayout, AppBarLayout and DrawerLayout
  • ActionBarActivity cannot resolve a symbol
  • Does android:scaleType=“fitCenter” only work with fix Layout_width and Layout_height attributes?
  • SearchView focused when hiding the ActionBar menu
  • Padding lost using <merge> tag with Custom Compound Component
  • How to pass intent with extras to an already running activity
  • How bad is Android SoundPool? What alternative to use?
  • ionic build android failure - Execution failed for task processDebugResources
  • Android In-app Purchase V3 Error: Authentication is required
  • How can I create an android menu item using android setting icon
  • Download IPA / APK from Crashlytics
  • Is there any way to reduce the boot up time of Android Emulator provided with android SDK
  • 7 Solutions collect form web for “Cancel All Volley Requests Android”

    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.

    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
    

    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

    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;
            }
    });
    

    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.

    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.

    Check this article. It uses Oto as the singleton Event bus. This way you can notify the volley queue when your Activity or Fragments get recreated. You can of course use a plain old Interface instead and listen on the changes. BUt Otto looks much less verbose and elegant as a unified solution.

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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.