bob
bob

Reputation: 155

Volley cancel request

i am fetching data from volley my problem is some time when user press back button request continues and app crash as activity is finish and then response is coming so i am calling on back press

RequestQueue.cancelAll(requesttag)
 i am seting tag as 
 AppRequest.settag(requesttag)

but its good to call cancel every time user press back button ? i haven't found what where i can check if that request is in progress then only cancel . any good way to avoid it

Upvotes: 1

Views: 1567

Answers (3)

Awadesh
Awadesh

Reputation: 4068

If you are using AsynTask for fetching data from the server, then you can define an instance of that Asynctask. And when user presses back button you can cancel that task specifically like this

 FetchDataListTask fetchDataListAsyncTask;
 fetchDataListAsyncTask = new FetchDataListTask(true);
 fetchDataListAsyncTask.execute();

And put this in your Onbackpressed() method

if (fetchLeadListAsyncTask != null) {
                fetchLeadListAsyncTask.cancel(true);
            }

Upvotes: 0

Ashkan
Ashkan

Reputation: 1378

duplicate question you should stop volley in your onPause and onStop. Same Question

Upvotes: 1

Vikas Nokhwal
Vikas Nokhwal

Reputation: 113

Instead of using tag to cancel all request. you can use filter,

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

Hope this help.

Upvotes: 1

Related Questions