I am making an app and using handler. In class A, some buttons with texts are displayed, and then using
handler.postDelayed(new Runnable(){@Override public void run() {blankbutton();}}, 10000);
such that when passing 10sec the text on the buttons would be erased.
However, if the user at this time presses the reset button (in another class), say, after 3 sec after the handler counts time, then what will occur is that 7 sec after pressing reset the text on the button would be erased : the handler does not stop.
How could I stop the handler in another class (in the reset class)? I have checked some similar questions and answer for this is to
handler.removeCallbacks(Runnable);
However, under the reset class it says “Runnable cannot be resolved to a variable“
Many thanks!!
Your issue is that you are making an anonymous inner Runnable object. You are leaving yourself no way to refer to it when you are trying to remove the callbacks. Something like this will make it work:
The difference is that this way you are assigning your runnable object to a variable
txtClearRunso that you have a way to refer to it later when you want to pass it in toremoveCallbacks();