Android对话框自己消失 [英] Android dialog disappears on its own

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

问题描述

我使用以下代码创建自己的对话框:

  public void ShowMessageDialog(String str){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(str);
builder.setCancelable(false);
builder.setNeutralButton(Ok,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}

它的工作正常,但是当它在这里使用时,对话框看起来就会消失函数:

  public void test(String str){
ShowMessageDialog(About to start new activity);
Intent intent = new Intent(this,PageViewer.class);
startActivity(intent);
}

似乎创建了新的活动,显然会摆脱对话框。但为什么?开启新的活动之前,活动不应该停止吗?



谢谢!

解决方案>

即将发射的意图不等待您的对话框被取消。所以,在对话框显示之后,将启动新的活动。你可以完成你想要的:

  public void ShowMessageDialog(String str){
AlertDialog.Builder builder =新的AlertDialog.Builder(this);
builder.setMessage(str);
builder.setCancelable(false);
builder.setNeutralButton(Ok,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
dialog.dismiss();
Intent intent = new Intent(this,PageViewer.class);
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}

public void test(String str){
ShowMessageDialog(About to start new activity);
}


I'm using the following code to create my own dialog:

public void ShowMessageDialog(String str){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(str);
    builder.setCancelable(false);
    builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {          
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

It works fine but it appears the Dialog disappears on it's own when used inside this function:

public void test(String str){
    ShowMessageDialog("About to start new activity");
    Intent intent = new Intent(this,PageViewer.class);
    startActivity(intent);
}

It seems that the new activity is created and obviously gets rid of the dialog. But why? Shouldn't the activity stop before opening the new one?

Thanks!

解决方案

Intent which is about to fire doesn't wait for your dialog to be canceled. So, right after dialog is shown, new Activity is started. You could accomplish what you want like this:

public void ShowMessageDialog(String str){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(str);
    builder.setCancelable(false);
    builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {          
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            Intent intent = new Intent(this,PageViewer.class);
            startActivity(intent);
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

public void test(String str){
    ShowMessageDialog("About to start new activity");
}

这篇关于Android对话框自己消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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