views:

346

answers:

1

Hi all, I am running into an issue with the way my asynctasks are executed. Here's the problem code:

firstTask = new background().new FirstTask(context);
    if(firstTask.execute().get().toString().equals("1"))
      secondTask = new background().new SecondTask(context);

What I'm doing here is creating a new asynctask object, assigning it to firstTask and then executing it. I then want to fire off a separate asynctask when the first one is done and making sure it returns a success value (1 in this case). This works perfectly on Android 2.0 and up. However, I am testing with Android 1.5 and problems start popping up. The code above will run the first asynctask but doInBackground() is never called despite onPreExecute() being called. If I am to execute the first task without the get() method, doInBackground() is called and everything works as expected. Except now I do not have a way to determine if the first task completed successfully so that I can tell the second task to execute. Is it safe to assume that this is a bug with asynctask on Android 1.5? Especially since the API (http://developer.android.com/intl/de/reference/android/os/AsyncTask.html#get%28%29) says that the get method has been implemented since API 3. Is there any way to fix this? Or another way to determine that the first task has finished?

A: 

If you are going to block (via get()), why are you bothering with AsyncTask in the first place? The whole point of AsyncTask is to not block.

If you want to have SecondTask execute when FirstTask is done, have FirstTask execute SecondTask in FirstTask's onPostExecute().

CommonsWare
Hi again CommonsWare, you see, firstTask is a login task and secondTask is one of various tasks that download certain data from a website once logged in. Rather than pass parameters through the login task to determine what task to execute when it is done, I'd rather have a master task that controls what tasks are running and knows when to start new ones. Thus, I am searching for a way to tell when a task is done.Also, this is not on the UI thread so the UI is not being slowed down by these threads.
Matt
You tell when a task is done by `onPostExecute()` being called on that task. Have each task tell the coordinator "yo! I'm done!", so the coordinator can move the next leg down the state machine.
CommonsWare