Home > DeveloperSection > Forums > What is the relationship between the life cycle of an AsyncTask and an Activity?
Chris S
Chris S

Total Post:48

Posted on    August-04-2015 11:27 PM

 Android Android  Android Activity 

 1 Reply(s)
 1836  View(s)
Rate this:
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?

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    August-05-2015 9:19 AM

An AsyncTask is not tied to the life cycle of the Activity that contains it. So, for example, if you start an AsyncTask inside an Activity and the user rotates the device, the Activity will be destroyed (and a new Activity instance will be created) but the AsyncTask will not die but instead goes on living until it completes.

Then, when the AsyncTask does complete, rather than updating the UI of the new Activity, it updates the former instance of the Activity (i.e., the one in which it was created but that is not displayed anymore!). This can lead to an Exception (of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity).

There’s also the potential for this to result in a memory leak since the AsyncTask maintains a reference to the Activty, which prevents the Activity from being garbage collected as long as the AsyncTask remains alive.

For these reasons, using AsyncTasks for long-running background tasks is generally a bad idea . Rather, for long-running background tasks, a different mechanism (such as a service) should be employed.

Don't want to miss updates? Please click the below button!

Follow MindStick