Home > Software design >  Code section shows one dialog, then another, but the second dialog opens immediately over the first
Code section shows one dialog, then another, but the second dialog opens immediately over the first

Time:12-09

I have an activity where the user is taking a test. If they get an answer wrong, I use a dialog to tell them what the right answer was. When the test is over, I use another dialog tell them what their total score is before ending the Activity.

Unfortunately, if they get the wrong answer on the last question, it is immediately displaying the ending dialog without first showing them the correction dialog. As the Activity closes, I then briefly see the correction dialog pop up. How can I force it to show the dialogs in order, and wait for them to be dismissed / OKed?

This function is defined within the onCreate function, if that matters. I can show the Dialog classes if necessary, but they're just standard AlertDialog builders.

        fun evaluateAnswer(guess: Int) {
            if (guess == correct) {
                correctAnswers  
            } else {
                val correction = CorrectionDialogFragment(guess)
                correction.show(supportFragmentManager, "correction")
            }

            // Do the stuff that happens regardless of whether guess was correct
            
            // There is still more to the test
            if (totalAnswers < testLength) {
                updateView(matchups[totalAnswers])
            }
            // End the test
            else {
                val end = EndTestDialogFragment(percentage)
                end.show(supportFragmentManager, "end")
            }
        }

CodePudding user response:

A function always runs straight through from beginning to end without any pausing in the middle (unless it's a suspend function in a coroutine and calls some other suspend functions that do pause). So, since you are not returning from this function when you show the Correction dialog, it runs all the rest of the code in this function before that Correction dialog even appears (it can only appear in the next loop of the main thread), so the End dialog shows on top of it.

I would break this into two functions:

fun evaluateAnswer(guess: Int) {
    if (guess == correct) {
        correctAnswers  
        continueTest() // ADD THIS HERE
    } else {
        val correction = CorrectionDialogFragment(guess)
        correction.show(supportFragmentManager, "correction")
    }
}

fun continueTest() {
    // Do the stuff that happens regardless of whether guess was correct

    // There is still more to the test
    if (totalAnswers < testLength) {
        updateView(matchups[totalAnswers])
    }
    // End the test
    else {
        val end = EndTestDialogFragment(percentage)
        end.show(supportFragmentManager, "end")
    }
}

Then in your code where you respond to returning from fragments, when you respond to a CorrectionDialogFragment being returned from, you directly call continueTest() instead of evaluateAnswer().

Unrelated to what you're asking, but don't forget to back up and restore correctAnswers and totalAnswers to your saved instance state.

  • Related