Android: Finish() Of Countdowntimer Is Called Even If Cancel() Is Called
I have used Countdown Timer like this new CountDownTimer(15000, 15) { public void onTick(long millisUntilFinished) { long seconds=millisUntilFinish
Solution 1:
Inside your onClick
set a Boolean (buttonPressed for example) to true.
In your onFinish
check this Boolean:
if (buttonPressed == true)
{
//do nothing
}
else
{
//run code
}
Solution 2:
You could use Timer instead and do something like this:
privateRunnablemUpdateTimeTask=newRunnable() {
publicvoidrun() {
// do your updates here
mUpdateTimeHandler.postDelayed(this, 1000);
}
};
HandlermUpdateTimeHandler=newHandler();
mUpdateTimeHandler.postDelayed(mUpdateTimeTask, 100);
When cancelling the task:
mUpdateTimeHandler.removeCallbacks(mUpdateTimeTask);
Solution 3:
I also don't understand why the onFinish gets called despite calling cancel(). I guess the only workaround for this is to just null check your calls in onFinish to prevent any NPE's.
publicvoidonFinish() {
if (nametext == null || bottombutton == null || changeText == null || ... ) {
return;
}
...
Post a Comment for "Android: Finish() Of Countdowntimer Is Called Even If Cancel() Is Called"