AlertDialog inside AlertDialog

I am trying to make an AlertDialog inside an AlertDialog, but when I run the code, secound AlertDialog is not showing

This is my code, I want to do it as if the user clicked the Cancel button in the first AlertDialog, the second AlertDialog appears, the user can enter his name and both metrics, and the name will go to ScoreActivity using intern.

void generateAlertDialog(final long timeSpent) {
        // 1. Instantiate an AlertDialog.Builder with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(
                GameActivity.this);
        // 2. Chain together various setter methods to set the dialog
        // characteristics
        builder.setMessage(
                "The time stayed in the game is " + timeSpent + "s")
                .setTitle("You Lose!!")
                .setPositiveButton("Replay",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                // User clicked OK button
                                finish();
                                Intent intent = new Intent(getApplicationContext(), GameActivity.class);
                                startActivity(intent);
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int id) {
                                AlertDialog.Builder builder2 = new AlertDialog.Builder(
                                        GameActivity.this);
                                builder2.setMessage(
                                        "Please input you name")
                                        .setTitle("Score");
                                final EditText input = new EditText(GameActivity.this);
                                input.setInputType(InputType.TYPE_CLASS_TEXT);
                                builder2.setView(input);
                                builder2.setPositiveButton("Show Score LIst",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                                int id) {
                                                finish();
                                                Intent intent = new Intent(getApplicationContext(), ScoreActivity.class);
                                                m_Text = input.getText().toString();
                                                intent.putExtra("name", m_Text);
                                                intent.putExtra("result", timeSpent);
                                                startActivity(intent);

                                            }
                                        });
                                builder2.setNegativeButton("Cancel",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                                int id) {
                                                finish();
                                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                                startActivity(intent);

                                            }
                                        });

                                // User cancelled the dialog

                            }
                        });

        // 3. Get the AlertDialogfrom create()
        AlertDialog dialog = builder.create();

        // 4. Show dialog
        dialog.show();
    }
+4
source share
1 answer

You never call builder2.create()or method show()anyone builder2.create(). You need to do both of these things right where you have the comment "// User canceled the dialog."

+4
source

Source: https://habr.com/ru/post/1608756/