Skip to content Skip to sidebar Skip to footer

Executing A Thread Inside AsyncTask's DoInBackground()

I have several AsyncTasks doing network operations. I was recently asked to add, after each of these operations, another network call that invokes a remote service sending some sta

Solution 1:

Try starting a second AsyncTask in the first AsyncTasks 'onPostExecute' Method. For me this worked without any issues.


Solution 2:

If you want to start thread in doInBackground method, you can start it in onProgressUpdate() method

Example:

protected class MyTask extends AsyncTask<Void,Integer,Void> {

        public static final int START_THREAD = -1;
        @Override
        protected void onProgressUpdate(Integer... values) {
            if(values[0] == START_THREAD){
                startThread();
            }
        }

    @Override
    protected Void doInBackground(Void... params) {
        publishProgress(START_THREAD);
        return null;
    }
}

Post a Comment for "Executing A Thread Inside AsyncTask's DoInBackground()"