Android: Wait on User Input from Dialog

Android: wait on user input from dialog?

Thanks for all the feedback, I was able to solve this using a background thread along with a wait() and notify(). I recognize this isn't the greatest idea for the given paradigm, but it was necessary to conform to a library that I am working with.

How do I wait for user input when opening a alert dialog in android

Yes, just call the dialog.show() of the second dialog in the onClick of the first dialog's setPositiveButton. This way the second dialog will be forced to wait until the first one is dismissed.

ANDROID: How to wait for click on Alert Dialog?

SOLVED!

This solution is waiting on click event really and return value.

private boolean resultValue;

public boolean getDialogValueBack(Context context)
{
final Handler handler = new Handler()
{

@Override
public void handleMessage(Message mesg)
{
throw new RuntimeException();
}
};

AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Return True", new
DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
resultValue = true;
handler.sendMessage(handler.obtainMessage());
}
});
alert.setNegativeButton("Return False", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
resultValue = false;
handler.sendMessage(handler.obtainMessage());
}
});
alert.show();

try{ Looper.loop(); }
catch(RuntimeException e){}

return resultValue;
}

Any way to wait for the value from MaterialAlertDialogBuilder? Android - Kotlin

What exactly do you need to wait for?

If you have a dialog with an EditText, and you want to ensure the dialog stays open until the user types something that you consider valid, then you need to delegate all this validation, keep a reference to your dialog, and act accordingly once your ViewModel responds.

In short.

Fragment/Activity: Creates a Dialog and holds a reference.

val nameInputDialog =  ... show()

Inside the onPositiveButton:

val nameToInput = inputNameDialog.text.toString()
viewModel.inputNameChanged(nameToInput)

In your ViewModel:

fun inputNameChanged(name: String) {
//validate (preferably delegate to a validator/useCase so you can UT it)

if (validator.isValid(name)) {
_uiState.postValue(YourSealedClass.Valid(name))
} else {
_uiState.postValue(YourSealedClass.Empty())//for e.g.
}
}

And back in your Fragment/Activity where you observe this...

viewModel.uiState.observe(viewLifecycleOwner, Observer { state ->
when(state) {
is YourSealedClass.Valid -> dialog.dismiss()
is YourSealedClass.Empty -> {
Toast.makeText(...).show() //a toast is bad for this, but it works.
}
}

And this is how Google recommends you work in a "reactive" way.

Android Wait for user input at AlertDialog to proceed

What I understand is that the changeLog will run every first run of the app. So you can override onDismiss() of your changeLog AlertDialog. Then just put your code for firstRun check

@Override
public void onDismiss(DialogInterface dialog)
{
// firstRun Check
// call function to run AlertDialog code for first check if firstRun == true else close dialog
}

Proper way to receive user input in Android and then continue execution

I did not know that Android dialogs are asynchronous, and thus cannot interrupt execution, as explained in dozens of similar questions

All Android UI is asynchronous.

I initially thought I would implement READ commands by showing a modal Dialog.

There is nothing intrinsically wrong with this. However, READ would need to suspend execution of your interpreter until the user has completed the dialog.

Is there an easier way?

Off the cuff...

Step #1: Run your interpreter on a background thread.

Step #2: Have your READ code invoke your dialog on the main application thread, then block on a Semaphore to block your background thread.

Step #3: Have your dialog code get the results of the user input over to your interpreter by some means, then release the Semaphore when the dialog goes away.

Step #4: Your interpreter code continues.



Related Topics



Leave a reply



Submit