0

I am making a quiz app for Android.

The user has 30 seconds to answer as many questions as they can.

I create a small gap in execution time between questions so the user can see if they got the question right:

new Handler().postDelayed(new Runnable() {  
   public void run()  { displayNextQuestion(); }
    } , 1000);

At the end of the 30 seconds, the user is supposed to be redirected to a results screen. However, if the 30 seconds expires during that wait time, then the user is brought to the results screen for a moment, only to be redirected back to the next question. How can I disable that moment of wait time once the 30 seconds has elapsed?

Should be a simple answer, I just don't know how to work with threads/execution.

Thanks

Rob
  • 762
  • 2
  • 21
  • 44

2 Answers2

1

Just use a boolean to check if the results should be displayed, and then do something like if(!resultsDisplayed){ displayNextQuestion(); }

Lincoded
  • 425
  • 2
  • 13
1

You can save your runnable and the handler to instance variables and call myHandler.removeCallbacks(myRunnable) to discard the pending action.

Knickedi
  • 8,742
  • 3
  • 43
  • 45