AsyncTask and leaks

Android studio warning:

This AsyncTask class should be static or leaks might occur
A static field will leak contexts.
Non-static inner classes have an implicit reference to their outer class. If that outer class is for example a Fragment or Activity, then this reference means that the long-running handler/loader/task will hold a reference to the activity which prevents it from getting garbage collected.
Similarly, direct field references to activities and fragments from these longer running instances can cause leaks.
ViewModel classes should never point to Views or non-application Contexts.


Why we ignore it?

Our AsyncTask is an inner class (non-static inner class) so that it can call methods on the outer class. This can be dangerous because the AsyncTask has a reference to the outer class. When the Fragment/Activity goes through the destruction part of its lifecycle (if you rotate or hit the back button), the AsyncTask could keep that Fragment/Activity in memory (causing a memory leak).

This is why we are very careful to cancel the AsyncTask at the appropriate time in the lifecycle. If you do not cancel it, then you may have a memory leak.