I think I know the answer to this, but does an AsyncTask continue to live on once its calling Activity has been finish()
ed?
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "Your data is processing.", Toast.LENGTH_LONG);
finish();
}
Edit: so far two different answers :)
If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes. And when it completes, the AsyncTask won't update the UI of the new Activity.
AsyncTask is not tied to the life cycle of the Activity. Then when the AsyncTask does complete, it updates the old activity instance rather the new activity.
An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .
Just for the Record: If you start a AsyncTask outside the UI-Thread the onPreExecute will not be executed from the UI-Thread (but from the caller Thread). This will lead to an exception. However the onPostExecute Method will always be executed on the UI-Thread.
The AsyncTask is tied to a UI thread and if the Activity is finished the async task is canceled.
[update] - Hackbod's comment below is correct. It should be noted that AsyncTasks are meant to be short lived and as such not worry so much about this issue. An AsycTask is only truly gone when it is completed OR the process is killed which may or may not happen after finish is called.
It keeps running until the onPostExecute
finishes.
I've experienced some weirdness with Async Tasks where if an activity is killed by the OS and when the app resumes, the AsyncTasks sometimes do not run at all. I am unsure why they get into this state. If you execute your AsyncTasks in your own executor thread, this does not happen. I might be running into some odd circumstance. Thought it might be useful to post here anyway :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With