Search code examples
androidlifecycle

Android lifecyle question re: incrementing a counter


My android application tracks the number of correct answers while the user is in process of taking a quiz. If the user's answer matches the correct answer, a counter to track this is incremented. This logic takes place in the "onResume" method.

The problem is, if the user presses the home button from this activity, then re-enters the application, it resumes at this activity, and the counter gets incremented again.

The only solution I can think of is to set the correct answer field to a "magic number" such as 99 when a correct answer happens, and check for that first before going into the answer-checking logic. If it's 99, then skip the answer-checking logic and just redisplay.

Is there a better way to do this?


Solution

  • I'm not quite sure about the real problem you have and in specific why you're incrementing a counter of correct answers in the onResume event. You have for sure some kind of button where the user confirms his answer. Don't you verify the correctness of the given answer there, i.e. in the button's click method?
    Implementing such logic in the onResumeevent is somehow dangerous, because it is called by the underlying Android OS.