Nature of Dialogs in Android

As we've mentioned, displaying dialogs in Android is an asynchronous process. Once a dialog is shown, the main thread that invoked the dialog returns and continues to process the rest of the code. This doesn't mean that the dialog isn't modal. The dialog is still modal. The mouse clicks apply only to the dialog, while the parent activity goes back to its message loop.

On some windowing systems, modal dialogs behave a bit differently. The caller is blocked until the user provides a response through the dialog. (This block can be a virtual block instead of a real block.) On the Windows operating system, the message-dispatching thread starts dispatching to the dialog and suspends dispatching to the parent window. When the dialog closes, the thread returns to the parent window. This makes the call synchronous.

Such an approach might not work for a handheld device, where unexpected events on the device are more frequent and the main thread needs to respond to those events. To accomplish this level of responsiveness, Android returns the main thread to its message loop right away.

The implication of this model is that you cannot have a simple dialog where you ask for a response and wait for it before moving on. In fact, your programming model for dialogs must differ in its incorporation of callbacks.

0 0

Post a comment