How to get started with a dialog in Android

I created a special dialog, and I would like to start a new action when the OK button is clicked. How can I get the context to set it as the first argument of my Intent constructor?

I can create an intent using getContext() , but I cannot call startActivity . Should I pass the action that invokes the dialog to the dialog constructor? Is this the usual way to get started by clicking on the dialog?

 public class CustomDialog extends Dialog implements OnClickListener { Button okButton, cancelButton; public CustomDialog(Context context) { super(context); setContentView(R.layout.custom_dialog); okButton = (Button) findViewById(R.id.button_ok); okButton.setOnClickListener(this); cancelButton = (Button) findViewById(R.id.button_cancel); cancelButton.setOnClickListener(this); } @Override public void onClick(View v) { if (v == cancelButton) dismiss(); else { Intent i = new Intent(getContext(), ItemSelection.class); startActivity(i); //The method startActivity(Intent) is undefined for the type CustomDialog } } } 
+7
source share
6 answers
 public class CustomDialog extends Dialog implements OnClickListener { Button okButton, cancelButton; Activity mActivity; public CustomDialog(Activity activity) { super(activity); mActivity = activity; setContentView(R.layout.custom_dialog); okButton = (Button) findViewById(R.id.button_ok); okButton.setOnClickListener(this); cancelButton = (Button) findViewById(R.id.button_cancel); cancelButton.setOnClickListener(this); } @Override public void onClick(View v) { if (v == cancelButton) dismiss(); else { Intent i = new Intent(mActivity, ItemSelection.class); mActivity.startActivity(i); } } } 
+16
source

@ dhaag23 You don’t even have to work hard!

Call getContext ()

This returns the Context passed to the dialog constructor.

+2
source
 Intent i = new Intent(getBaseContext(), ItemSelection.class); 

This worked for me, although the structure is different, there is no class for dialogue.

+2
source

Simple, just save the context that is passed to the CustomDialog constructor in a local variable.

+1
source

As Cheezmeister wrote , it is not necessary to acquire Actvitiy. You can simply use the context as follows:

 Intent i = new Intent(getContext(), ItemSelection.class); getContext().startActivity(i); 
+1
source

I suggest you use this. This makes it so simple:

 AlertDialog.Builder dialog = new AlertDialog.Builder(RegistrationActivity.this); dialog.setCancelable(false); dialog.setTitle("Error Alert"); dialog.setMessage(info[1]); dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Intent intent = new Intent(RegistrationActivity.this, RegistrationActivity.class); startActivity(intent); } }) .setNegativeButton("", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); final AlertDialog alert = dialog.create(); alert.show(); 

info[1] is my data that is shown. You can replace this with your own message.

0
source

All Articles