Vincent Roye
Vincent Roye

Reputation: 2851

Start Android activity when receiving the last response from multiple Volley requests

In the OnCreate method of my splashscreen, I make 2 different Volley requests :

RequestQueue queue = AppController.getInstance().getRequestQueue();
GsonRequest<WPPosts> myReq = new GsonRequest<WPPosts>(urlJson, WPPosts.class, null,createMyReqSuccessListener(),createMyReqErrorListener());
queue.add(myReq);

and another one to get the categories.

I would like to start my MainActivity when I receive the last response from these 2 resquests :

private Response.Listener<WPPosts> createMyReqSuccessListener() {
    return new Response.Listener<WPPosts>() {
        @Override
        public void onResponse(WPPosts response) {...}

Regardless response arrives first or last.

Would it be a semaphorical approach ?

Upvotes: 0

Views: 269

Answers (2)

kalin
kalin

Reputation: 3576

You don't need to extend listeners or anything like that.

You can just set in your splashscreen a static int, which you increment in onResponse of these requests. onResponse is delivered in the main thread so you don't need to worry about threading issues here.

Note that you probably want to have this value incemented onError as well as if an error occurs you will be never able to go to the main activity :)

Upvotes: 0

Arpit Ratan
Arpit Ratan

Reputation: 3026

Just create a class which extends Response.Listener. This class should also contain a static variable count. When you receive a callback onResponse() increment the count by 1.

When count is 2 launch the MainActivity.

Please use the same instance of the class for both the volley requests your are making.

Upvotes: 1

Related Questions