Search code examples
androidtimercountdown

Countdown timer problem android


I have created an android quiz application where i have a feature Exam . In Exam i have used a timer. Timer is working fine. The following code segment i use ...

public class MyCount extends CountDownTimer{

    public MyCount(long millisInFuture, long countDownInterval){
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        timerTextView.setText("Times up!");
        result=examAccessoriesObj.calculateExamResult(resultArray);
        j = new Intent().setClass(TermExamTestActivity.this,TermExamResultShowActivity.class);
        preparePassingValue();
        startActivityForResult(j,0);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if((millisUntilFinished/1000%60) < 10){
            timerTextView.setText("Time Left " + millisUntilFinished/60000 + 
                ":0" + (millisUntilFinished / 1000)%60);
        } else {
            timerTextView.setText("Time Left " + millisUntilFinished/60000 + 
                ":" + (millisUntilFinished / 1000)%60);
        }
    }
}

But problem is create when the application is quit with out the time pass or all of the question is visited. And after the quit of application when the counter is passed its time then the show result page is shown . How can i solve this problem. Thanks in advance.


Solution

  • You could cancel the countdown in the onStop()-method of the Activity which uses the countdown by using it's cancel()-method.