java - Android Threading: This Handler class should be static or leaks might occur -
this question has answer here:
i using handler
object continue ui work after finished time consuming task in seperate thread. had problem of above lint warning , following approach.
[ sample handler object type 1 ] ->
handler responsehandler = new handler() { @override public void handlemessage(message msg) { super.handlemessage(msg); toast.maketext(mainactivity.this, "finished long running task in seperate thread...", toast.length_long).show(); } };
[ sample handler object type 2 ] ->
handler responsehandler = new handler(new handler.callback() { @override public boolean handlemessage(message msg) { toast.maketext(mainactivity.this, "finished long running task in seperate thread...", toast.length_long).show(); return false; // return value ???? } });
in seperate thread(other ui) when time consuming task done, executes following line control ui thread(basically handler obj).
responsehandler.sendemptymessage(0);
the program works fine both types of handler objects, 1st type getting lint warning saying this handler class should static or leaks might occur.
therefore started using 2nd type of handler object avoid lint warning problem i've got is, not sure meaning of return value (true/false) in 2nd way , works either. searched in google didn't exact answer explained return value.
yes, saw question had asked in many places in stackoverflow reagrding lint warning, question return type in 2nd way , confirm if it's alright way solve issue using 2nd type of handler obj.
questions ->
1). know exacly return value means(true/false) ?
2). correct thing have done rid of lint warning?
thanks...
each handler bound looper
of thread, each message
placed on data structure, message queue.
message
has target
variable points handler
, callback
variable points runnable
.
so, if using anonymous class create handler
object (like in first example), know anonymous/non-static inner classes hold reference outer objects (activity ?). so, message posted on queue, might holding references handler
target, , handler
in turn holding reference outer class, activity
.
now, message can stay in message queue long intervals of time, long thread running. meanwhile, activity may have been dismissed. won't garbage collected due obscure indirect reference of it, message has. note looper , message queue stay around long thread running.
in second example, not creating anonymous handler
class. using handler constructor , passing anonymous callback
object. might stop lint complaining, doubt approach. avoid inner classes, avoid passing activity or context references handler.
update:
the handler's dispatchmessage()
gets messages due processed, checks if callback has been provided, if callback provided, doesn't calls handlemessage()
, if callback's handlemessage()
returns true:
public void dispatchmessage(message msg) { if (msg.callback != null) { handlecallback(msg); } else { if (mcallback != null) { if (mcallback.handlemessage(msg)) { return; } } handlemessage(msg); //--won't called if return true. } }
Comments
Post a Comment