I have one AlertDialog
that does not want to close after selecting a certain action, precisely setPositiveButton
when going to the new one activity
when pressing back it is still showing.
my dialogue
final AlertDialog.Builder alertaDeError2 = new AlertDialog.Builder(MainActivity.this);
alertaDeError2.setTitle("Bienvenido");
alertaDeError2.setMessage("Es tu primera vez aquí. Te recomendamos ir a nuestra guía rápida");
alertaDeError2.setNegativeButton("OMITIR", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
saveValuePreference(getApplicationContext(), false);
}
});
alertaDeError2.setPositiveButton("IR", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertaDeError2.show();
saveValuePreference(getApplicationContext(), false);
Intent myIntent = new Intent(MainActivity.this, PrimerPaso.class);
startActivity(myIntent);
}
});
alertaDeError2.create();
alertaDeError2.show();
Any ideas ?
You can close a dialog with the
dismiss()
. That closes the dialog and removes it from the screen. The methodshow()
returns you aAlertDialog
, which is a subclass ofDialog
, which has the methoddismiss()
to discard it.You can do the following:
When you show the dialog you save it in this variable:
and where you want to close the dialog you simply use
That's for the cases where you really need to close a dialog by events outside of the normal flow. Apparently in your case the reason was different and you found it. :)
After doing the call the del
Intent
method :cancel()
AlertDialog
You are in error if you use the method
show()
for your dialog instance, what this method does is show the dialog otherwise you should change todismiss();
to hide the dialog.