I have a layout with some buttons, and I want after one of them is pushed to cancel the timeout, with the code that I have now I press one of the buttons and it redirects me to another activity but the timer dons't stop and after the 3 seconds it redirects me again, how do I cancel the timer if one of the buttons is pressed? This is the timer:
int timeout = 3000; seconds Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { finish(); Intent homepage = new Intent(Act1.this, Act2.class); startActivity(homepage); } }, timeout);
Check the documentation and you will find a Timer.cancel()
. Keep a reference to the timer and call that whenever you want to make it stop.
void cancel()
Terminates this timer, discarding any currently scheduled tasks.
Example:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTimer.cancel();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.