为什么张贴和放大器;取消了在不同bahviour一个查看和处理程序结果的可运行? [英] Why is posting & cancelled a runnable on a View and Handler result in different bahviour?
问题描述
我已经打有关与的Runnable
和发现,如果你 postDelayed
A 的Runnable
在查看
然后去除回调将无法正常工作,但是如果你做同样的,但发布可运行
在处理程序
然后去除回调不工作。
为什么这项工作(的Runnable
的run()code从不被执行):
可运行的可运行=新的Runnable(){
@覆盖
公共无效的run(){
//执行一些code
}
};处理程序处理程序=新的处理程序();
handler.postDelayed(可运行,10000);
handler.removeCallbacks(可运行);
在这里,因为这不(的Runnable
的run()code总会被执行):
可运行的可运行=新的Runnable(){
@覆盖
公共无效的run(){
//执行一些code
}
};查看查看= findViewById(R.id.some_view);
view.postDelayed(可运行,10000);
view.removeCallbacks(可运行);
如果在查看
不附加到一个窗口,我可以看到这种情况发生,什么看起来礼貌像Android的一个bug。战术上,因此,它可能是时间问题,确保你不张贴或删除的Runnable
后才查看
附着到窗
如果你碰巧有一个示例项目躺在附近进行复制这个问题,我想看看它。否则,我会尽量做我自己,所以我可以有一些我可以用它来报告我的presumed错误。
更新
由于在评论中提到的, removeCallbacks()
上更多的普通部件的作品,所以才出现,这是一个的WebView
特异性问题,每OP的样本code。
I've been playing about with Runnable
s and have discovered that if you postDelayed
a Runnable
on a View
then removing the callback won't work, however if you do the same but post the Runnable
on a Handler
then removing the callback does work.
Why does this work (Runnable
run() code never gets executed):
Runnable runnable = new Runnable() {
@Override
public void run() {
// execute some code
}
};
Handler handler = new Handler();
handler.postDelayed(runnable, 10000);
handler.removeCallbacks(runnable);
where as this doesn't (Runnable
run() code always gets executed)?:
Runnable runnable = new Runnable() {
@Override
public void run() {
// execute some code
}
};
View view = findViewById(R.id.some_view);
view.postDelayed(runnable, 10000);
view.removeCallbacks(runnable);
If the View
is not attached to a window, I can see this happening, courtesy of what looks like a bug in Android. Tactically, therefore, it may be a question of timing, making sure that you do not post or remove the Runnable
until after the View
is attached to the window.
If you happen to have a sample project lying around that replicates this problem, I'd like to take a look at it. Otherwise, I will try making my own, so I can have something I can use to report my presumed bug.
UPDATE
As mentioned in the comments, removeCallbacks()
on more ordinary widgets works, so it appears this is a WebView
-specific problem, per the OP's sample code.
这篇关于为什么张贴和放大器;取消了在不同bahviour一个查看和处理程序结果的可运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!