How to start a new activity class in the AlertDialog popup window by clicking ok

There is a first activity in which users save their details. After clicking the โ€œSaveโ€ button, Alertdialog asks for confirmation or cancellation. if the user clicks the OK button, a new action will begin.

protected final Dialog onCreateDialog(final int id) { Dialog dialog = null; switch(id) { case DIALOG_ID: AlertDialog.Builder builder = new AlertDialog.Builder(AppointInformation.this); builder.setMessage("Information saved successfully ! Add Another Info?") .setCancelable(false) .setPositiveButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { startActivity(new Intent(((Dialog)dialog).getContext(),CheckPatient.class)); } }) .setNegativeButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); dialog = alert; break; default: } return dialog; } 
+6
source share
7 answers

I know this too late, but I want to answer this by helping other people, this works for me:

 Intent intent = new Intent(getContext(),OtherActivity.class); context.startActivity(intent); 

context is the context of the current activity.

+2
source

when changing the code:

 startActivity(new Intent(((Dialog)dialog).getContext(),CheckPatient.class)); 

to

 startActivity(new Intent(getBaseContext(),CheckPatient.class)); 

or

 startActivity(new Intent(Activityname.this,CheckPatient.class)); 
+1
source

In the โ€œOKโ€ or โ€œCancelโ€ button, you can record this,

 Intent intent = new Intent(this, NewActivity.class); startActivity(intent); 
0
source

use this code

 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Intent intent = new Intent(PresentActivity.this, NextActivity.class); startActivity(intent); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.show(); 
0
source

To do this, do something like below.

 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(AndroidAlertDialog.this); myAlertDialog.setTitle("--- Title ---"); myAlertDialog.setMessage("Alert Dialog Message"); myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { Intent intent = new Intent(this, NextActivity.class); startActivity(intent); }}); myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { // do something when the Cancel button is clicked }}); myAlertDialog.show(); 
0
source
  AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext()); alert.setTitle(title); alert.setCancelable(false); alert.setMessage(message); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub startActivity(new Intent(YourActivity.this, NewActivity.class)); } }); alert.show(); 

Use the above code:

0
source

That should do the trick

 Intent intent = new Intent(this, NewActivity.class); startActivity(intent); 
-1
source

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


All Articles