I use AsyncTask in several activities.
i know how to Override onBackPress().
But when an AsyncTask is running i want to give the user the ability to go back even though the asynctask is still running.
How can I do this?
Also does an asynctask still run when activity that initiated it is closed before it finishes?
The async task will continue to run even if your application was closed. You have to be careful with that since your task will leak your context (keep it in memory) if it has a reference to it (so the activity) as long as your task is still running. You can avoid that by referencing your activity by a
WeakReference.You can stop a running task with cancel(true). A cancel will let the task finish its
doInBackgroundbut will never callonPostExecute. You could interrupt your background routine by checking isCanceled() and so return earlier since the task was killed.I deleted one half of my post!
I did so confuse interrupt with a thread stop! Using
truewill of course NOT stop the actual thread. Thanks a lot to Torid and his answer which proved me wrong! Big lesson lernt, thanks for that!