4

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

}
}
6
  • As the doc states "The task can be executed only once (an exception will be thrown if a second execution is attempted.)" Mar 1, 2012 at 1:12
  • So I can NEVER execute it twice? The user has to exit the entire app and run it again later? What good is "oh, you already used that code, you can NEVER use it again"?)
    – Carol
    Mar 1, 2012 at 1:14
  • I'm not seeing any "exception thrown".
    – Carol
    Mar 1, 2012 at 1:16
  • No Android app can EVER go and grab 2 web pages?
    – Carol
    Mar 1, 2012 at 1:25
  • 1
    An instance of the task can only be executed once. OP creates a second instance of the AsyncTask. Mar 1, 2012 at 1:49

2 Answers 2

5

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.

1
  • If you use new DownloadWebPageTask().execute(new String[] { "http://yahoo.com" });, you don't keep track of that instance in a variable, isn't that a problem? For example, you cannot cancel the AsyncTask if the Activity is destroyed ...
    – caw
    Jan 5, 2013 at 21:38
0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.