Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

Android: AlertDialog inside another AlertDialog Action

  1. When I click on confirmDialogAlert "Cancel" the secondDialogAlert is opening

         AlertDialog.Builder secondDialogAlert = new AlertDialog.Builder(class.this);
         secondDialogAlert
                 .setTitle("!Alert Second Dialog")
                 .setMessage("Successfully opened")
                 .setIcon(android.R.drawable.ic_dialog_alert)
                 .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> {
                     Toast.makeText(getApplicationContext(), "Second Dialog Opened Successfully.", Toast.LENGTH_LONG).show();
                 }).setNegativeButton(android.R.string.cancel, null).show();
    
         AlertDialog confirmDialogAlert = new AlertDialog.Builder(class.this)
                 .setTitle("!Alert First Dialog")
                 .setMessage("Are you sure want to continue?")
                 .setIcon(android.R.drawable.ic_dialog_alert)
                 .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> {
                     secondDialogAlert.show();
                 }).setNegativeButton(android.R.string.cancel, null).show();
         confirmDialogAlert.show();
    
question from:https://stackoverflow.com/questions/65931748/android-alertdialog-inside-another-alertdialog-action

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Both dialog opens because you are invoking .show() on both dialogs. To solve this you need to change it like this.

In your original code you invoked .show() which will display the dialog. What you must do is invoke .create(). Then when you want to display the dialog invoke .show();

 AlertDialog.Builder secondDialogAlert = new AlertDialog.Builder(class.this);
 secondDialogAlert
         .setTitle("!Alert Second Dialog")
         .setMessage("Successfully opened")
         .setIcon(android.R.drawable.ic_dialog_alert)
         .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> {
             Toast.makeText(getApplicationContext(), "Second Dialog Opened Successfully.", Toast.LENGTH_LONG).show();
         }).setNegativeButton(android.R.string.cancel, null).create(); // .show() changed to .create()

 AlertDialog confirmDialogAlert = new AlertDialog.Builder(class.this)
         .setTitle("!Alert First Dialog")
         .setMessage("Are you sure want to continue?")
         .setIcon(android.R.drawable.ic_dialog_alert)
         .setPositiveButton(android.R.string.ok, (dialog, whichButton) -> {
             secondDialogAlert.show();
         }).setNegativeButton(android.R.string.cancel, null).create(); // invoke create() instead of show because you have invoked it below
 confirmDialogAlert.show();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...