Play with this demo

In this Activity, you can :

Read below to learn more.

Basic AsyncTask usage.

In this app, the first serie of activities is dedicated to AsyncTasks. It aims to highlight all the problems related to using the AsyncTask framework of Android for implementing background processing. This serie is largely inspired by a question on Stack Over Flow : Is AsyncTask really conceptually flawed or am I just missing something ? and the answers it received from the community.

This Activity uses a simple AsyncTask.

Memory leak issue

In this Activity, the AsyncTask is an inner class of the Activity. This looks like a natural choice : as the AsyncTask will need to manipulate the views of the Activity when the task is complete or in progress. Using an inner class of the Activity seems convenient : inner classes can access directly any field of the outer class.

Nevertheless, it means the inner class will hold an invisible reference on its outer class instance : the Activity.

On the long run, this produces a memory leak : if the AsyncTask lasts for long, it keeps the activity "alive" whereas Android would like to get rid of it as it can no longer be displayed. The activity can't be garbage collected and that's a central mechanism for Android to preserve resources on the device.

Depending on the size in memory of your Activity, you will quickly get an OutOfMemoryException, as none of those activities will get garbage collected by the Android system. In this tutorial, we simulated this failure : every instance of this class of Activity as a big buffer that fills up the ram. Rotate your device, and click on start. Repeat the process again and again. After 20-25 times you should get a memory exception due to the memory leak problem we mention here.

If you don't understand why activities must die, please refer to the activity life cycle of the Activity documentation.

The AsyncTask and Activity life cycle

AsyncTasks don't follow Activity instances' life cycle. If you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes. This a nice mechanism for long-running background operations. Nevertheless, it's not managed correctly by AsyncTasks and most benefit of it are lost.

Click on the start button, then rotate your device. As you can see, once the Activity is recreated, the former AsyncTask doesn't update the UI anymore. Indeed it updates the former instance of the activity 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 (here we don't do that, we use data members to hold references on views).

But even if you don't get an exception, you just lost the link between UI and background task. We will demonstrate a workaround in the next demo Activity.

Canceling an AsyncTask

To cancel an AsyncTask is easy, but only if the AsyncTask supports it ! Invoking the cancel method will have no effect by itself. But, if the AsyncTask, during its doInBackground method regularly checks the isCancelled flag, then it can decide to stop itself on demand. We implemented such a solution in all our AsyncTask if you are looking for sample code.


Other examples propose some workarounds to these issues. Go back to the menu and click on the next demo Activity. You can play with the app and experiment all limitations mentionned above. Please don't tell us this Activity crashes, it will crash on design !