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);
removeCallbacks()
? – BrombergView.removeCallbacks()
will alwaysreturn true;
(at least on ICS - rest probably too) see here – Equal