如何从一个处理程序的所有回调? [英] How to remove all callback from a Handler?

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

问题描述

我有我的子活动一个处理被称为主<一HREF =htt​​p://developer.android.com/reference/android/app/Activity.html>活动。该处理器所使用的子类<一href="http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29">postDelay一些的Runnable,我不能管理他们。现在,在的onStop事件,我需要之前删除他们完成活动(不知何故,我叫光洁度(),但它仍然一次又一次地打电话)。反正有从处理程序清除所有的回调?

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为记号。如果标记为空,所有的回调和消息将被删除。

"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天全站免登陆