Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling an AsyncTask from another AsyncTask

At a certain point of my AsyncTask, after some validations have been done, I need to spawn off another thread to do some other work. So I'd like two background threads at this point, each doing it's own thing (approx 2-3 seconds to execute each). The idea is to maximize performance on dual core processors like Atrix.

Is it acceptable to create another asynctask & execute it from the first one? Can anyone suggest a better way of doing this?

Thanks!

EDIT: I'm wondering what publishProgress() from the second task would even do... since it was not started from an Activity?

like image 838
OceanBlue Avatar asked Apr 25 '11 15:04

OceanBlue


People also ask

What is the alternative to AsyncTask?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

Is it possible to start AsyncTask from background thread?

Methods of AsyncTaskwe can directly comminicate background operation using on doInBackground() but for the best practice, we should call all asyncTask methods . doInBackground(Params) − In this method we have to do background operation on background thread.

Can an AsyncTask be executed in a worker thread?

According to Android doco you must run an AsyncTask from a UI thread, but in reality it depends on who runs this line in AsyncTask.


1 Answers

Is it acceptable to create another asynctask & execute it from the first one?

Yes, but only inside onProgressUpdate() or onPostExecute() since these methods runs on the UI thread. Therefore, start the second AsyncTask on the UI thread by choosing one of the two methods listed above.

I'm wondering what publishProgress() from the second task would even do... since it was not started from an Activity?

It does exactly the same thing, since you are starting it from the UI thread.

like image 193
Wroclai Avatar answered Oct 02 '22 13:10

Wroclai