是Android上的一个对话框异步? [英] Is a Dialog asynchronous on Android?

查看:179
本文介绍了是Android上的一个对话框异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Android上的对话框行为一些研究。

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

我知道Android的目的是prevent对话框是模态的,我也看了,例如这里的Dialogs / AlertDialogs:如何与QUOT;块执行"而对话是向上(.NET式),即对话是异步执行的。

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.

不过,我做了一些测试,发现,我认为,对话等待,直到code放置在调用后的执行,以显示()被执行。

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.

这是我的测试code:

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-");

}
}

我的意思是,当你执行一个测试code,它执行直到onCreate()方法的底部,包括暂停5秒,之后,当你seeging的烤面包和所著的消息登录猫,会显示该对话框。

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.

所以,我认为是不正确的对话框是异步的,因为它似乎他们在UI线程不会在不同的线程执行,执行 一些code完成后。

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.

什么是真正奇怪的是,对话的执行等待该是启动对话的行之后写code的执行。

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.

此外,我不明白为什么UI线程应该由一个模态对话框,而不是由表现为我解释一个对话框被阻止。

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.

这是当alert.show后线显示和对话之前执行在UI线程的状态的差别时,这些线路都在等待执行,直到对话框能从用户得到一个答案(作为一个模式对话框会表现得)?

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.

推荐答案

code的那行:

alert.show();

告诉系统在UI线程的下一个'滴答'异步显示警报,因此所有code后首先执行。

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

UI线程有一个消息尺蠖被处理消息顺序。正在执行的的onCreate()函数,因为弯针正在处理是应用程序的一部分启动的消息。该的onCreate()函数必须先完成可以循环到下一个消息,这将是被提上了消息队列通过调用了使警报对话框可见消息显示(弯针前执行)

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

这篇关于是Android上的一个对话框异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆