You will find on this page a section as well as three tutorials explaining how to use the Handlers and in particular how to avoid memory leaks by linking the lifecycle of your thread to your activity.
In fact, if you do not note when your activity is destroyed (passage by the onDestroy method) your Thread is not... She became an orphan; You cannot communicate with it. Worse, it points to your Handler that is associated with the activity. Thus the Garbage Collector detects that these three objects are still "used" and only picks up crumb not. And this is the memory leak.
I wish you a good read, a good download and a good understanding of the use of the Handlers.
Download tutorials
These tutorials demonstrate how to use the handlers.
Each tutorial is an Eclipse project zipped, directly importable into your workspace.
Download in a single file all these tutorials:
Demonstration of the memory leak |
Handler Tutorial with AtomicBoolean |
Handler Tutorial with onRetainNonConfiguration |
AsyncTask Tutorial with AtomicBoolean |
Demonstration of the memory leak AsyncTask |