Yevgeny Simkin
Yevgeny Simkin

Reputation: 28439

Is there a way to remove anonymous Runnables from a Handler?

Handler h = new Handler();
h.postDelayed(new Runnable(){...}, 5000);

Is there any way to clear this prior to the 5 second expiration or does the Handler not have a public method that I can use to null out the anonymous Runnable?

Upvotes: 1

Views: 530

Answers (1)

Simon Dorociak
Simon Dorociak

Reputation: 33515

Is there any way to clear this prior to the 5 second expiration

I think this should make a trick (but Runnable needs to be not anonymous):

handler.removeCallBacks(runnable);

For anonymous runnables should work:

handler.removeCallbacksAndMessages(null);

What source says:

Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed.

Upvotes: 4

Related Questions