No, this is not GCed. It is just perfect to do it this way.
A bit more explanation to avoid confusion:
Although you do not store the link to the handler, it is stored somewhere else. In the sendMessageAtTime method , which is called internally postDelayedbefore the handler places the message in the message queue, it assigns itself to the targetmessage field , so there is still a reference to the handler, and it is not GCed:
public boolean sendMessageAtTime(Message msg, long uptimeMillis)
{
if (queue != null)
{
msg.target = this;
sent = queue.enqueueMessage(msg, uptimeMillis);
}
}
source
share