AlertDialog does not wait for input

I have an AlertDialog created in a case switch statement that is inside a for loop. AlertDialog uses an EditText to log in. When the window appears, the for loop starts in the background. I want the loop to wait for a message to be sent to continue. Here is the code for AlertDialog:

for(int i=0; i < code.length(); i++){ switch(code.charAt(i)){ case '+': bytes[index]++; break; case '-': bytes[index]--; break; case '<': if(index > 0){ index--; }else{ Toast.makeText(this, "Warning: Index is already at zero", Toast.LENGTH_LONG).show(); } break; case '>': if(index <= 500){ index++; }else{ Toast.makeText(this, "Warning: Maximum bytes reached", Toast.LENGTH_LONG).show(); } break; case ']': if(loop == -1){ Toast.makeText(this, "ERROR: Close bracket before an open bracket!", Toast.LENGTH_LONG).show(); errors++; break; }else{ if(bytes[index] == 0){ loop = -1; }else{ i = loop; } break; } case '[': loop = i; break; case '.': stdout(Character.toString((char)bytes[index])); break; case ',': AlertDialog.Builder alert = new AlertDialog.Builder(this).setCancelable(false); alert.setTitle("Enter Character"); alert.setMessage("Script is requesting input"); final EditText input = new EditText(this); InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter.LengthFilter(1); input.setFilters(FilterArray); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setByte(input.getText().toString().charAt(0)); } }); alert.show(); break; } if(errors > 0){ break; } } 
+1
source share
1 answer

As others have said, Android dialogs are not modal (that is, they do not block background processes). Read the blog post to find out how you get around this by registering callbacks.

+7
source

Source: https://habr.com/ru/post/897534/


All Articles