serkancks
serkancks

Reputation: 67

Android Studio Firebase quizapp timer

i am trying to code a timer for my quizapp.

i have coded this. it update my questions every 5 secound. i have 5 questions in the firebase. the method qupdate() is updating my buttons and question textview.

if i dont click any button the questions updates every 5 secound and after 25 second i am getting to the endmenu.

My problem is: when i press a button the timer is not reseting. sample: i open the quiz i am clicking a button after 2 secounds and i am getting the new questions an after 3 secounds the timer is updating my question.

How can i reset the timer to 5 secounds when a button is pressed so that i have (if i clicked or not) 5 seconds left.

  int delay = 5000; // delay for 5 sec.
    int period = 5000; // repeat every sec.
    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask()
    {
        int count = 0;

        public void run()
        {
            qupdate();
            count ++;

            if(count == 5)
                this.cancel();
        }

    }, delay, period);

}

Upvotes: 0

Views: 133

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599491

It looks like you need to cancel the timer and start a new one.

So in your click handler:

timer.cancel();

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
  ...
}, delay, period);

Upvotes: 1

Related Questions