GVillani82
GVillani82

Reputation: 17429

AsyncTask nested calls

Supposing I'm using the following AsycTask (called A) for sending data over internet:

private class A extends AsyncTask<Void, Void, Void>{
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = ProgressDialog.show(context, "Notification", "Sending message...");
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        sendMessage();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if(pd!=null && pd.isShowing())
            pd.dismiss();           
    }
}

Inside the doInbackground is called the my method sendMessage(). I will not go into details of this method, just immagine that this method executes a Thread (suppouse that it is called B).

So, the question is:

The code inside the onPostExecute() of AsyncTask A will be executed after the end of the Thread B?

If not, how can make possible that the onPostExecute will be executed only when the AsyncTask inside the sendMessage() will be terminated?

Upvotes: 0

Views: 2439

Answers (2)

Ion Aalbers
Ion Aalbers

Reputation: 8030

you could let the sendMessage() return the Thread object and then call

Thread.join()

This will cause the doInBackground to wait until the thread is finished.

Upvotes: 1

Ravi Bhatt
Ravi Bhatt

Reputation: 1948

Well it is pretty simple. Just call that AsyncTask B in onPostExecute of AsyncTask A.

Edit : Thread inside thread is not possible from ICS onwards. It will not give you any error, but it will not work too and your app will stuck there.

Upvotes: 1

Related Questions