Question

My android app uses AsyncTask to download some data from a website.

But when I press the back button of my android device immediately after the activity starts, worker thread's onPostExecute method is called, which is wierd because android called onDestroy method prior to onPostExecute and the onPostExecute method runs on the main UIThread which I think doesn`t exist anymore.

Can anyone help me what I don`t understand?

Was it helpful?

Solution

An AsyncTask is basically executing what you want in the background as a separate thread from the UI. So when you quit the UI this doesn't necessarily mean that you've killed the AsyncTask. It will continue it's regular life cycle and end in onPostExecute. If you want to kill the AsyncTask too then you will have to call the cancel() function for the AsyncTask.

Know this though, you cannot actually kill an AsyncTask this will be done by Android itself. So you will have to wait a while till the current task is killed (if you call cancel()) for you to restart this particular AsyncTask.

You should also read up on onCancelled() methods. For more information checked out the documentation.

If I've made any mistakes, please correct me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top