---
title: "What is the relationship between the life cycle of an AsyncTask and an Activity?"  
description: "What is the relationship between the life cycle of an AsyncTask and an Activity?"  
author: "Anonymous User"  
published: 2015-08-04  
updated: 2015-08-05  
canonical: https://www.mindstick.com/forum/33396/what-is-the-relationship-between-the-life-cycle-of-an-asynctask-and-an-activity  
category: "android"  
tags: ["android", "android activity"]  
reading_time: 2 minutes  

---

# What is the relationship between the life cycle of an AsyncTask and an Activity?

What is the [relationship](https://yourviews.mindstick.com/view/80819/live-in-relationship-protecting-the-right-to-live-together) between the life [cycle of an AsyncTask](https://answers.mindstick.com/qa/32781/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) and an [Activity](https://www.mindstick.com/forum/12894/how-to-force-recreation-of-activity-fragment-on-restore)? What problems can this [result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency) in? How can these problems be avoided?

## Replies

### Reply by Anonymous User

An [AsyncTask](https://www.mindstick.com/forum/158886/what-is-the-purpose-of-asynctask-in-android-and-when-should-it-be-used) is not tied to the [life cycle](https://www.mindstick.com/articles/23194/the-life-cycle-of-software-development) 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.


---

Original Source: https://www.mindstick.com/forum/33396/what-is-the-relationship-between-the-life-cycle-of-an-asynctask-and-an-activity

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
