android tutorial - Avoid leaking activities with asynctask in android | Developer android - android app development - android studio - android app developement
Avoid leaking activities with asynctask in android
A word of caution: AsyncTask has many gotcha's apart from the memory leak described here. So be careful with this API, or avoid it altogether if you don't fully understand the implications. There are many alternatives (Thread, EventBus, RxAndroid, etc).
One common mistake with AsyncTask is to capture a strong reference to the host Activity (or Fragment):
This is a problem because AsyncTask can easily outlive the parent Activity, for example if a configuration change happens while the task is running. The right way to do this is to make your task a static class, which does not capture the parent, and holding a weak reference to the host Activity: