2

I'm doing some research on dialog behavior on android.

I know that android is designed to prevent dialogs to be modal and I also read, for example here Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style), that dialogs are executed asynchronously.

But I do some tests and found, I think, that the execution of the dialog waits until the code placed after the call to show() is executed.

This is my test code:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Question");
    builder.setMessage("Yes or No?");

    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
             dialog.dismiss();
        }

    });

    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();


    System.out.println("MainActivity.onCreate -Before sleep-");
    Toast.makeText(this, "MainActivity.onCreate. -Before sleep-", Toast.LENGTH_LONG).show();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Toast.makeText(this, "MainActivity.onCreate. -Wake up-", Toast.LENGTH_LONG).show();
    System.out.println("MainActivity.onCreate. -Wake up-");

}
}

I mean, when you execute that test code, it executes until the bottom of the onCreate() method, including 5 seconds of pause, and after that, when you are seeging the toast and the messages writed to the log cat, the dialog is displayed.

So I think is not true that dialogs are asynchronous, since it seems that they executed in the UI thread not in a different thread and are executed after some code is finished.

What is really strange is that the execution of the Dialog waits for the execution of code that is written after the line that launches the dialog.

Moreover, I don't understand why the UI Thread is supposed to be blocked by a "modal dialog" but not by a dialog that behaves as I explained.

Which is the difference in the UI Thread's state when the lines after alert.show are executed before the dialog is displayed and when these lines are waiting for execution until the dialog gets an answer from the user (as a modal dialog would behave)?

Thanks in advance for read and sorry for my English.

Community
  • 1
  • 1

2 Answers2

1

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().

Rob Meeuwisse
  • 2,847
  • 1
  • 17
  • 21
0

Dialogs are asynchronous, but if you pause the UI thread in the moment, that the dialog is rendering, right before showing yourself : ).It is also connected with the delay of showing dialogs caused by their animation

Toumash
  • 1,077
  • 11
  • 27