What happens on Activity.finish() with an AsyncTask still running in background?
Does it just pop the Activity off the Activity Stack, but wait to destroy the Activity object until the AsyncTask fully completes (since the AsyncTask is an inner class of my Activity)?
Also, would it act any differently if the AsyncTask were a public, non-inner class that held no references to the instance of the Activity?
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. Indeed it updates the former instance of the activity that is not displayed anymore.
You can either cancel the AsyncTask in the onStop method of your activity or you can let your async task finish, and not loose its progress and relink it to the next instance of your activity.
AsyncTask allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. Then when the AsyncTask does complete, it updates the old activity instance rather the new activity. This can lead to an Exception: java.
AsyncTask instances can only be used one time.
I've tried the same thing with the Thread, and my observation is it keeps running the thread.
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