I was testing the behavior of AlertDialog to integrate in a bigger component. I am unable to show the same dialog again. Here is the test code:
public class MainActivity extends AppCompatActivity {
private AlertDialog alertDialogACCreationRetry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alertDialogACCreationRetry = new AlertDialog.Builder(this)
.setTitle("Account creation failed")
.setMessage("There was a problem connecting to the Network. Check your connection.")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alertDialogACCreationRetry.show();
alertDialogACCreationRetry.show();
}
}
I have tried putting the alertDialogACCreationRetry.show();
inside the Retry button but it still won't show. I have also tried putting alertDialogACCreationRetry.dismiss();
inside the Retry button and then calling alertDialogACCreationRetry.show();
outside, it still doesn't show. More so it is frightening that it doesn't give me an exception on reshowing it if that is not supposed to be allowed.
So, my question is this: Will I have to create a new Dialog every time after once it is dismissed(automatically) on pressing a button?
public void showAlertDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Time");
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
// call function show alert dialog again
showAlertDialog();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With