开发者

Best practice for delaying program execution in Java (Android)

开发者 https://www.devze.com 2023-04-10 16:07 出处:网络
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 ques

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号