How do I continuously show an AlertDialog in a do while loop until a certain condition is met?

Tom Darious

I have an AlertDialog that I want to display at least once to the user and then continuously display the dialog to the user even after the user clicks "ok" until a certain condition is met.

Here's the code structure I have so far for the AlertDialog:

do {
    val dialogShow: AlertDialog.Builder = AlertDialog.Builder(this@MainActivity)
    dialogShow.setCancelable(false)

    dialogShow.setMessage("Message")
        .setPositiveButton(
            "ok",
            object : DialogInterface.OnClickListener {
                override fun onClick(dialogInterface: DialogInterface, i: Int) {
                    if (checkCondition()) {
                        conditionMet = true
                    } else {
                        // Keep looping
                    }
                }
            })
        .setNegativeButton(
            "cancel",
            object : DialogInterface.OnClickListener {
                override fun onClick(dialogInterface: DialogInterface, i: Int) {
                    conditionMet = true
                    return
                }
            })

    dialogShow.show()
} while (conditionMet == false)

The problem now that I am facing is the AlertDialog will display once, but then never again. Even if conditionMet = false it still won't continue to display. How do I keep displaying the same AlertDialog in a loop?

Henry Twist

By wrapping the show code in a loop, you're showing it continuously. What you probably want to do it re-show the dialog if it is dismissed. So something like this pseudocode:

fun showObtrusiveDialog() {

    ...
    dialog.setPositiveButton {
    
        if(shouldStillBeObtrusive()) showObtrusiveDialog()
        ...
    }.setNegativeButton {

        ...
    }

    dialog.show()
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I stay in a for loop's element until a condition is met?

How do I end a JavaScript while loop when a condition is met

How do I roll a list or numpy array until a certain condition is met?

NodeJS - While loop until certain condition is met or certain time is passed

How do I loop adding the month to each date until the condition is met? google apps script

How do I make a repeating 'if' statement until a condition is met?

how do I make while loop run until a certain input is placed?

Do Until Loop Condition Met - but Loop continues - Powershell

Do action until condition is met

Execute ansible include_tasks until a certain condition is met (kind of while until loop)

How do I make this loop repeat until a certain point?

How do I retain a new strings value during a loop, continuously changing the string until the loop ends?

How to stop the program until a certain condition is met?

How do I make a conditional prompt that will continue to prompt until condition is met

How do I get my text to continiously change based on the result of a httprequest until end condition is met?

How do I keep passing the output of a Halide pipeline back into the pipeline until some condition is met?

A while or until loop, until a line with "REMOVE" content in bash. How do I do this?

How do I show AlertDialog in Flutter

Flutter how do i show a video in alertDialog

VBA Do while loop with If statement that adds a value when condition is met

while-loop and if-statement do different command if condition is met

How do I continuously loop KEYDOWN in pygame?

How do i stop a while loop after a certain time in C

How do I get certain text to appear when a condition is met using XSLT?

How do I perform a math operation on a Python Pandas dataframe column, but only if a certain condition is met?

How do I update a variable and all subsequent variables once a certain condition is met in R?

For each in list, do operation until condition is met

While loop, how do I condition for when input equals nothing?

Continuously re-running a while loop until a certain input