Reputation: 139
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);
Upvotes: 0
Views: 63
Reputation: 2518
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();
}
});
Upvotes: 1