What happens if the user navigates away or closes the app while I still have a reference to the Activity the user just closed in my AsyncTask?

Technology CommunityCategory: AndroidWhat happens if the user navigates away or closes the app while I still have a reference to the Activity the user just closed in my AsyncTask?
VietMX Staff asked 3 years ago

Short answer – Memory Leaks. As long as some part of the app like an AsyncTask still holds a reference to the Activity it will not be destroyed. It will stick around until the AsyncTask is done or releases its reference in some other way. This can have very bad consequences like your app crashing, but the worst consequences are the ones you don’t notice: your app may keep reference to Activities which should have been released ages ago and each time the user does whatever leaks the Activity the memory on the device might get more and more full until seemingly out of nowhere Android kills your app for consuming too much memory.