Carol
Carol

Reputation: 407

Android app: Calling AsyncTask twice?

I'm using AsyncTask and some pretty common Android code to get the contents of a remote webpage. Based on that returned content, I can then call another page.

http://developer.android.com/reference/android/os/AsyncTask.html

My debugging lines should print like this:

 1> StartA() 
 2> onPreExecute
 3> doInBackground 
 4> onPostExecute    Note: Code here will call EndA()
 5> EndA()
 6> 
 7> StartB() 
 8> onPreExecute 
 9> doInBackground 
10> onPostExecute     Note:  Code here will call EndB() 
11> EndB()

Is that impossible to do? I get all of the above to work... EXCEPT I get one addtional call to EndB() appearing between lines 8 and 9.

I can't for the life of me figure out why. Nothing looks like it should call EndB() twice. And it definitely shouldn't get called BEFORE 9 and 10.

private void StartA()
{
    Debug("StartA()");

    g_GetWhat = 1;
    DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://google.com" });

}

private void EndA()
{
    Debug("EndA()");

    StartB();
}

private void StartB()
{
    Debug("StartB()");

    g_GetWhat = 2;
    DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://yahoo.com" });

}

private void EndB()
{
    Debug("EndB()");
}

///////////////////////////////////////////////////

private class DownloadWebPageTask extends AsyncTask<String, Void, String> 
{
protected void onPreExecute()   
    {
        Debug("onPreExecute()");

    }

protected String doInBackground(String... urls) 
     {
    Debug("doInBackground()");
}

protected void onPostExecute(String result) 
{

    Debug("onPostExecute()"); 
    if(g_GetWhat == 1)  { EndA(); }
    if(g_GetWhat == 2)  { EndB(); }

}
}

Upvotes: 4

Views: 6512

Answers (2)

Caumons
Caumons

Reputation: 9585

If you need to do multiple simultaneous works in background, then you should use the Service class in Android (not IntentService as the calls are enqueued and do no run at the same time).

Upvotes: 0

dmck
dmck

Reputation: 7861

You can only execute an AsyncTask instance once. You are actually creating two instances, but you should call it like this anyways so that it can never be recalled:

new DownloadWebPageTask().execute(new String[] { "http://yahoo.com" });
new DownloadWebPageTask().execute(new String[] { "http://google.com" });

instead of like this:

DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://google.com" });

I think your running into the problem here:

private void EndA()
{
    Debug("EndA()");

    StartB();
}

Your value for g_GetWhat is getting changed as soon as StartB begins. So when execution returns from EndA() the next if statement evaluates to true since g_GetWhat's value has changed.

if(g_GetWhat == 1)  { EndA(); }
if(g_GetWhat == 2)  { EndB(); }

The value for g_GetWhat is actually 2, which is why you see the result you are seeing. You should pass g_GetWhat into your AsyncTask when you call it and make it an instance variable of the task.

Upvotes: 5

Related Questions