如何从处理程序中删除所有回调? [英] How to remove all callbacks from a Handler?

查看:362
本文介绍了如何从处理程序中删除所有回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的子活动中有处理程序通过主活动。子类使用此处理程序 postDelay 一些Runnables,我无法管理它们。现在,在 onStop 事件中,我需要在完成Activity之前删除它们(不知怎的,我调用 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 the onStop event, I need to remove them before finishing 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为令牌的邮件。 如果令牌 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆