Reputation: 190
I want to start an activity from an AlertDialog.Builder But I can't s start any activity from OnclickListener of the dialog Interface. from both of the setPositiveButton
and setNegativeButton
(tried 2 ways to show as an example), both of them don't work and doesn't start either menuactivity
or exo5P1
.
public class exo5 extends AppCompatActivity {
private Button button;
private EditText edt1, edt2, edt3, edt4, edt5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exo4);
button = findViewById(R.id.button);
edt1 = findViewById(R.id.editText);
edt2 = findViewById(R.id.editText3);
edt3 = findViewById(R.id.editText8);
edt4 = findViewById(R.id.editText5);
edt5 = findViewById(R.id.editText7);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder ValidationDialog = new AlertDialog.Builder(exo5.this);
ValidationDialog.setTitle("Validation");
ValidationDialog.setMessage("Voulez-vous vraiment valider").setCancelable(false).setPositiveButton("Oui", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Vous Avez bien validé votre choix", Toast.LENGTH_LONG).show();
Intent intent = new Intent( exo5.this,exo5P1.class);
startActivity(intent);
}
}).setNegativeButton("Non", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Vous Avez annulé votre choix", Toast.LENGTH_LONG).show();
Intent intent =new Intent(exo5.this,menuActivity.class);
exo5.this.startActivity(intent);
}
});
AlertDialog Dialog = ValidationDialog.create();
Dialog.show();
}
});
}
}
I also Tried Using getApplicationContext()
in the Intent, but it doesn't work.
Upvotes: 0
Views: 396
Reputation: 322
I tried this in my project and it's launching the activity.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder ValidationDialog = new AlertDialog.Builder(exo5.this);
ValidationDialog.setTitle("Validation");
ValidationDialog.setMessage("Voulez-vous vraiment valider").setCancelable(false).setPositiveButton("Oui", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(exo5.this, MyTestAct.class);
startActivity(intent);
}
}).setNegativeButton("Non", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog Dialog = ValidationDialog.create();
Dialog.show();
}
});
Upvotes: 1