I have a silly question. Various questions on SO a...
# android
d
I have a silly question. Various questions on SO and tutorials on the web suggest saving a listener interface in your
AsyncTask
to still keep a reference and publish progress to your fragment/activity/service/anything. Question stands, shouldn't it be
WeakReference
? I'd assume that ART does not garbage collect running threads and if your application is suddenly ended, keeping a hard reference to your context will prevent ART from garbage collecting it.
m
dragas: if your application have suddenly ended, ART VM will be just killed with whole its heap. But if your UI is being killed due to screen rotation, you will get short-time memory leak, so yes, reference should be weak, and AsynkTask — static (if nested). Also, AsynkTasks are unaware of UI lifecycle, so it may be better to use Loaders or MVP.
k
or just don't use
AsyncTask
as there are much better alternatives
a
Nobody use
AsyncTask
those days because their limitations/problems
m
@abdellah wrong, I use `AsyncTaskLoader`s! 🧌
👻 1
a
nice to meet you 🆘
😅 1
d
@kirillrakhman I'd assume a better solution would be using Anko's
doAsync
?
k
doAsync is just a wrapper around
AsyncTask
and only mitigates the problem with leaks by keeping weak references. Using RxJava in combination with, say RxLifecycle will give you a clearer code structure and better leak protection