Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens on Activity.finish() with AsyncTask still running in background?

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?

like image 677
stormin986 Avatar asked Apr 28 '10 02:04

stormin986


People also ask

What happens when the activity gets closed abruptly while AsyncTask is executing?

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.

How do I stop AsyncTask when activity is destroyed?

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.

What is the relationship between the life cycle of an AsyncTask and an activity what problems can this result in How can these problems be avoided?

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.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.


1 Answers

I've tried the same thing with the Thread, and my observation is it keeps running the thread.

like image 50
Karan Avatar answered Sep 20 '22 13:09

Karan