如何从处理程序中删除所有回调? [英] How to remove all callback from a Handler?
问题描述
我有一个来自我的子活动的处理常式,由主要的活动。子类使用此处理程序来 postDelay 一些Runnables,我不能管理它们。现在,在onStop事件中,我需要在完成活动之前删除它们(不知何故,我调用finish(),但它仍然一次又一次调用)。是否还有从处理程序中删除所有回调?
I have a handler from my sub-Activity that was called by the main Activity. This handler is used by sub-classes to postDelay some Runnables, and I can't manage them. Now, in onStop event, I need to remove them before finish the activity (somehow I called finish(), but it still call again and again). Is there anyway to remove all callbacks from a Handler?
谢谢。
推荐答案
在我的经验中,调用这个工作太棒了!
In my experience calling this worked great!
handler.removeCallbacksAndMessages(null);
在removeCallbacksAndMessages的文档中,它表示...
In the docs for removeCallbacksAndMessages it says...
删除任何挂起的回调邮件,并发送其obj为令牌的邮件。 如果token为
null
,所有回调和邮件都将被移除。
Remove any pending posts of callbacks and sent messages whose obj is token. If token is
null
, all callbacks and messages will be removed.
这篇关于如何从处理程序中删除所有回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!