I created a custom dialog and I'd like to start a new activity when OK is clicked. How can I get the context to set it as first argument of my Intent constructor?
I can create the intent using getContext()
, but I can't call startActivity
. Shall I pass the activity calling the dialog to the dialog's constructor? Is it the usual way to start an activity by clicking a 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
}
}
}