How to make AlertDialog block code?

I am trying to show a message with an OK button. I am using AlertDialog for this purpose, and I realized that it does not block the code. Example:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {} }) .setNegativeButton("", null) .show(); new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 2") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {} }) .setNegativeButton("", null) .show(); //...continue with UI initialization here... } 

When I run the action, it shows Alert2. When I click ok, then Alert1 appears.

I need to have a dialog box with the lock code, so first the message Alert1 should appear, wait for the user to click OK, and then continue to execute the code and display the message Alert2, etc. Example:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); msgBox("Test dlg", "Alert 1"); msgBox("Test dlg", "Alert 2"); //...continue with UI initialization here... } private void msgBox(String title, String msg){ //????? /* WRONG, NON-BLOCKING new AlertDialog.Builder(this).setTitle(title).setMessage(msg) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {} }) .setNegativeButton("", null) .show(); */ } 

What should I write in // ????? place in msgBox method?

MORE EXAMPLE, I need something like this:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (isInitialisationDataFailed()){ msgBox("Alert", "Cannot open activity, sorry"); myActivity.this.finish(); return; } } 

But that does not work. The task completes faster than a warning appears on the screen.

The basic idea is to have messaging code separate from your own method to make it reusable. How to achieve this?

///////////////////////////////// another example:

 private void init(){ //init code here... if (isSomethingWhrong()){ msgbox("wrong stuff will be fixed"); //do fix wrong stuff here... } if (isAnotherthingWrong()){ msgbox("more wrong stuff will be fixed"); //do fix more wrong stuff.... } //continue init code here... } private void msgbox(String msg){ //BLOCKING DIALOG REALISATION here... } 

and alternatively this:

 private void init(){ //init code here... handleWrongStuff(); } private void handleWrongStuff(){ if (isSomethingWhrong()){ new AlertDialog.Builder(activity) .setTitle("Test") .setMessage("wrong stuff will be fixed") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //do fix wrong stuff here... handleMoreWrongStuff(); } }) .setNegativeButton("", null) .show(); } else{ handleMoreWrongStuff(); } } private void handleMoreWrongStuff(){ if (isAnotherthingWrong()){ new AlertDialog.Builder(activity) .setTitle("Test") .setMessage("more wrong stuff will be fixed") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //do fix more wrong stuff here... continueInit(); } }) .setNegativeButton("", null) .show(); } else{ continueInit(); } } private void continueInit(){ //continue init code here... } 

Do you see the difference in complexity? To make the init code work on Android, I need to split it into separate methods, but not into logical blocks, but when I need to show dialogs. In addition, the code for initialization dialogs repeats and becomes ugly and unreadable.

+4
source share
7 answers

Place the code to display the second dialog box in the onClick of Positive button of the First alert dialog box.

+4
source

using this method, you cannot stop execution, but instead you can turn on the button action listener so that when the button is pressed the listener will be called at this time, and you can execute the code, for example. in your case, when you click ok, then display the following warning from the listener.

  new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { new AlertDialog.Builder(YourActivity.this).setTitle("Test dlg").setMessage("Alert 2") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {} }) .setNegativeButton("", null) .show(); } }) .setNegativeButton("", null) .show(); 
+2
source

Dialogs do not block code execution - quite simple. The only way to achieve β€œblock” behavior is to break the execution chain into smaller bits.

Suppose you want to display a total of five dialog boxes during user interface initialization. In your activity you create the following methods:

 private void beforeFirstDialog() { // Do the initialization until you want the first dialog // Show the first dialog // When clicking 'OK' in the first dialog, beforeSecondDialog() is called. } private void beforeSecondDialog() { // Do the initialization until you want the second dialog // Show the second dialog // When clicking 'OK' in the second dialog, beforeThirdDialog() is called. } private void beforeThirdDialog() { // Do the initialization until you want the third dialog // Show the third dialog // When clicking 'OK' in the third dialog, beforeFourthDialog() is called. } private void beforeFourthDialog() { // Do the initialization until you want the fourth dialog // Show the fourth dialog // When clicking 'OK' in the first dialog, beforeFifthDialog() is called. } private void beforeFifthDialog() { // Do the initialization until you want the fifth dialog // Show the fifth dialog // When clicking 'OK' in the first dialog, afterFifthDialog() is called. } private void afterFifthDialog() { // Do what needs to be done after the last dialog. } 
+1
source

This is a late answer, but may give you a close.

The cleanest way to do what you need is to start activity instead of dialog . Simple operation with a single OK button.

When the OK button is clicked, just close the action.
If you need more complex answers, more buttons, text input, etc.
use StartActivityWithResult () to return a custom selection

+1
source

according to the condition.

ShowAlert.displayAlert (the real class name is this.this, "Please enter an error message");

  strDomain.requestFocus(); 

create a new ShowAlert class inside which this code is placed.

public class ShowAlert {

  public static void displayAlert(Activity act,String msg) { AlertDialog.Builder alert = new AlertDialog.Builder(act); alert.setMessage(msg).setPositiveButton("OK", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); } } 
0
source

EditText strDomain;

strDomain is the name of edittext. it, as if this field was zero, I did it, as if it was not filled, it will not go to the next page. What is your case when you want to show a message in a need instance?

0
source

As you (may have discovered) from many of the answers, you shouldn't block Main UI Thread . However, if you have a good reason to block the code while you receive a response from the user, then you can create a thread in which you loop / wait for user input while you display AlertDialog through the MainUI Thread . I have a complete blog here that explains this.

 protected void RunConfirmAction(Action runnableAction) { var confirmThread = new Thread(() => runnableAction()); confirmThread.Start(); } // OnClick (when the user clicks Logout Button, I call this RunConfirmAction(Logout); // the implemtation of the MessageBox waiting looks like this: public DialogResult MessageBoxShow(string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) { if (_CurrentContext != null && MainActivity != null) { Action<bool> callback = OnConfirmCallBack; _IsCurrentlyInConfirmProcess = true; Action messageBoxDelegate = () => MessageBox.Show(((Activity)MainActivity), callback, message, caption, buttons); RunOnMainUiThread(messageBoxDelegate); while (_IsCurrentlyInConfirmProcess) { Thread.Sleep(1000); } } return _ConfirmBoxResult ? DialogResult.OK : DialogResult.No; } private void OnConfirmCallBack(bool confirmResult) { _ConfirmBoxResult = confirmResult; _IsCurrentlyInConfirmProcess = false; } private bool _ConfirmBoxResult = false; private bool _IsCurrentlyInConfirmProcess = false; 

I hope this can help someone, the details of the solution can be found here

0
source

All Articles