Reputation: 133
I am trying to make a AlertDialog inside AlertDialog , but when I run the code , the secound AlertDialog didnt appear
This is my code , I want to make it like if the user click cancel in the first AlertDialog , the second AlertDialog will be appear , user can type his name and both the score and the name will pass 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();
}
Upvotes: 4
Views: 590
Reputation: 2198
You never call builder2.create()
or the show()
method of whatever builder2.create()
returns. You need to do both of those things right where you have the comment "// User cancelled the dialog".
Upvotes: 4