WeakHandler should not be used without hard reference from outside.
androidmalin opened this issue · 1 comments
WeakHandler should not be used without hard reference from outside.
this is from https://techblog.badoo.com/blog/2014/08/28/android-handler-memory-leaks/
Why is that?
This is what is meant?
Please give an example to explain this.
Hi
WeakHandler should not be garbage collected unless it is not needed anymore. Otherwise all your messages would be garbage collected as well.
So, you can assign WeakHandler to fragment field, for example. But you can not leave WeakHandler without asiigning it, like this:
new WeakHandler().post(new Runnable() {...});
Also wrong usage is to assign it to local variable:
private void method() {
WeakHandler h = new WeakHandler ();
h.post(...);
}
Because it can be garbage collected at any moment after method finishes.
I hope it's more clear now. Sorry for lack of formatting - writing this from my phone.