Dialogs/Alertdialogs: How to "Block Execution" While Dialog Is Up (.Net-Style)

Dialogs / AlertDialogs: How to block execution while dialog is up (.NET-style)

Ted, you don't want to do this, really :) The biggest reason is that if you block the UI thread while you are displaying a Dialog, you will block the thread that's in charge of drawing and handling the events of your Dialog. Which means your dialog will be unresponsive. You will also cause ANRs if the user takes more than a few seconds to click the dialog.

Erich's answer is exactly what you need. I know it's not what you want, but that doesn't matter. We've designed Android to prevent developers from writing synchronous dialogs so you don't really have much of a choice.

modal dialog in Android

I believe you will find your answer here

Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

In general you should not be blocking the code while a dialog is visible, your logic should be different and fit android way of doing things.

Is a Dialog asynchronous on Android?

That line of code:

alert.show();

tells the system to asynchronously show the alert at the next 'tick' of the UI thread, and therefore all code afterwards is executed first.

The UI thread has a message Looper that is handling messages sequentially. Your onCreate() function is being executed because the looper is handling the messages that are a part of the application start up. The onCreate() function must first finish executing before the looper can loop back to the next message which will be the 'make the alert dialog visible' message that was put on the message queue by the call to show().

DialogResult in normal java class and return result

After many hours hunting through the inner depths of google pages, I found this Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style).

It does exactly the job I was after and tested to make sure there are no ANR errors, which there isn't



Related Topics



Leave a reply



Submit