Android dialog disappears on its own

前端 未结 1 860
迷失自我
迷失自我 2021-01-18 08:54

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

public void ShowMessageDialog(String str){
    AlertDialog.Builder builder = new AlertDialog.Builder(t         


        
1条回答
  •  醉梦人生
    2021-01-18 09:32

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

    0 讨论(0)
提交回复
热议问题