17

Every time I do a httpRequest, the screen will appear to be locked up for a few seconds while the code is executing. Hence I used AsyncTask to do all my httpRequest stuff in a separate thread while putting up a ProgressDialog so the user knows something is happening.

I recently encountered the following situation: the input of one of my httpRequest is dependent on the result from a previous httpRequest (+parse) action. I can't just put the two AsyncTasks sequentially cause Android will put them in two threads and start the second one without the first one being finished. And without an appropriate input (the result of the first httpRequest), my second httpRequest will crash the app.

Is there way I can put-in a wait() to force the second AsyncTask not to start until the first one finishes?

The Vee
  • 11,420
  • 5
  • 27
  • 60
adjfac
  • 635
  • 1
  • 7
  • 18

6 Answers6

11

I also had some same situation the other day. I had solved it in this way: Pass the reference of your activity to the constructor of async class and execute the do in background function. Now in post execute function call a public method of ur activity from async class to execute the task again... or try this:

            if (asynclass.getStatus() == android.os.AsyncTask.Status.PENDING) {
                asynclass.execute();
            } else if (RF.getStatus() == android.os.AsyncTask.Status.FINISHED) {
                asynclass = new asyncclass();
                asynclass.execute();
            } else {
                Toast.maketoast(this, "Plz wait", 1).show();
            }

Cheers

Awais Tariq
  • 7,724
  • 5
  • 31
  • 54
  • 2
    +1 Yes agree with @Await, we can get the status of any AsyncTask by using getStatus() method. – Paresh Mayani Sep 21 '11 at 05:29
  • I considered doing what you and @nik suggested to just call the 2nd asynctask in the postExcute function, but is it a bad practice to call an asynctask within another ansyctask? – adjfac Sep 21 '11 at 15:01
6

so i think(not sure),you need to call second asyncTask on post execution method

protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
                     new secTask().execute();
            }}
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
4

Yes, you can. But it is only possible in onProgressUpdate() or onPostExecute(). Try doing it like this:

private class Task1 extends AsyncTask<Void, Void, Void> {
...
...
    protected void onPostExecute(Void unused) { 
        dialog1.dismiss();
        new Task2().execute();
    }

}

Similarly, you'll have to put new Task3().execute(); in the onPostExecute() method of Task2's AsyncTask

Don't call them like this in your main code. If you do, then all of them will run together:

// This is wrong
new Task1().execute();
new Task2().execute();
new Task3().execute();

Reference: AsyncTask

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
3

How about using executOnExecutor method :

public void runTasks() {

        AsyncTask<Void,Void,Void> aTask = myTask();
        AsyncTask<Void,Void,Integer> aTask1 = myTask1();
        AsyncTask<Void,Void,Void> aTask2 = myTask2();

        aTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        aTask1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        aTask2.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

    }
Mickey Tin
  • 3,408
  • 10
  • 42
  • 71
  • But this way you won't be able create some sort of flow control. Oh, my bad, you still can do that in the onPreExecute callback. – Daniel F May 27 '15 at 10:44
1

Stumbled on this thread looking for a better solution than what I currently use, but it still seems the best...

What I do is pass a runnable in the constructor of each of my async tasks.

Either in the post execute or at the end of the do in the background, I launch my next task, if I have one by executing the runnable.

 if(doOnPostExec != null)
        doOnPostExec.run();

This way I can changeup the order of my asynctasks by the runnables I pass keeping the code flexible, and if I pass no runnable they complete normally. The runnables just contain one line calling the next asynctask.

I just don't like making all those runnables. Was hoping something existed like in vb.net for chaining delegates.

sean262 none
  • 212
  • 2
  • 8
0

I encountered this problem before. What I did is use a call back interface class and call its method inside the onPostExecute() method. This way you can call another AsyncTask from the callback method.

neilQ5
  • 179
  • 1
  • 12