简体   繁体   English

Android:等待用户界面在AsyncTask中完成

[英]Android: Wait for UI to finish in AsyncTask

In my AsyncTask's doInBackground function, I am calling this to display a confirmation dialog: 在我的AsyncTask的doInBackground函数中,我将其称为显示确认对话框:

void CreateOKDialog(final String message)
{
    runOnUiThread(new Runnable() {
        @Override
        public void run()
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(trip.this);
            builder.setTitle(message);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}

After calling the above function in doInBackground() I Start a new activity. 在doInBackground()中调用上述函数后,我开始一个新活动。 The problem is that since its asynchronous, it does not wait for the above function to complete. 问题在于,由于它是异步的,因此它不等待上述功能完成。 I can just see a popup and it closes. 我可以看到一个弹出窗口,它关闭了。 How to solve this ? 如何解决呢?

        CreateOKDialog("Trip cancelled");
        startActivity(new Intent(trip.this, login.class));

Edit: 编辑:

void CreateOKDialog( String message)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(trip.this);
    builder.setTitle(message);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

I am calling this function: 我正在调用此函数:

protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    if(isError)
        CreateOKDialog("Error cancelling trip");
    else {
        CreateOKDialog("Trip cancelled");
        startActivity(new Intent(trip.this, login.class));
    }
}

It still does not wait for me to press OK, just pops up and disappears. 它仍然不等我按OK,而是弹出并消失。

在异步任务中覆盖onPostExecute并在其中编写对话框和意图代码

Do something like this with your AsyncTask 用您的AsyncTask做类似的事情

public YourAsyncTask extends <X, Y, Z>{

    public interface TaskDoneCallback{
        void onTaskDone();
    }

    private TaskDoneCallback mCallback;

    public YourAsyncTask(TaskDoneCallback callback){
        mCallback = callback;
    }

    @Override
    public Z doInBackground(X... params){
        // your stuff
    }

    @Override
    protected void onPostExecute(Z result){
        if(mCallback != null)
            mCallback.onTaskDone();
    }

}

Then from your activity 然后从你的活动

new YourAsyncTask(new TaskDoneCallback(){

    @Override
    public void onTaskDone(){
        AlertDialog.Builder builder = new AlertDialog.Builder(trip.this);
        builder.setTitle(message);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                startActivity(new Intent(trip.this, login.class));
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
}).execute(x);

What is happening is the AsyncTask will execute, and onPostExecute it will trigger the callback that you pass to the constructor for your Activity. 发生的是AsyncTask将执行,并且onPostExecute将触发您传递给Activity的构造函数的回调。 This will, in turn, build the dialog from within the activity. 反过来,这将从活动内部构建对话框。 Then in a positive onClick response, the new activity will be triggered. 然后 ,在肯定的onClick响应中,将触发新活动。

Last, you will want to look into naming conventions. 最后,您将要研究命名约定。 Classes should very much so be UpperCamelCase, they are important, make sure people don't get them confused with variables :) 类应该是UpperCamelCase,非常重要,它们很重要,请确保人们不要将它们与变量混淆:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM