13

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
        }
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jul
  • 36,404
  • 64
  • 191
  • 318

7 Answers7

19
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);
    }
  }
}
EricLarch
  • 5,653
  • 5
  • 31
  • 37
  • It seems that I can't call startActivity: "The method startActivity(Intent) is undefined for the type CustomDialog" – jul Feb 21 '11 at 20:19
  • I made the correction. You can either do it this way, or make the declaration of the Dialog class inside your activity which will give access directly to the `Activity`'s method – EricLarch Feb 21 '11 at 20:31
5
Intent i = new Intent(getBaseContext(), ItemSelection.class);

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

lorless
  • 4,126
  • 8
  • 30
  • 41
4

@dhaag23 You don't even have to do that much work!

Call getContext()

This returns the Context passed to the Dialog's constructor.

Cheezmeister
  • 4,895
  • 3
  • 31
  • 37
2

Simple, just save the context that gets passed into the CustomDialog constructor in a local variable.

dhaag23
  • 6,106
  • 37
  • 35
1

Like Cheezmeister wrote it is not nessesary to get the Actvitiy. You can simply use the context like this:

Intent i = new Intent(getContext(), ItemSelection.class);
getContext().startActivity(i);
Community
  • 1
  • 1
rekire
  • 47,260
  • 30
  • 167
  • 264
1

I suggest you use this. It 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 which is shown. You can replace this with your own message.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Debasish Ghosh
  • 1,867
  • 20
  • 29
0

If you are working on a DialogFragment you can do it that way:

public class MyDialogFragment : DialogFragment
    {
        private Context _Context;
        ...

         public override void OnActivityCreated(Bundle savedInstanceState)
        {
            _Context = Context;

            ...


            alertDialog.SetButton("Start Activity Button", delegate
            {
                var uri = Android.Net.Uri.Parse("https://.....");
                var intent = new Intent(Intent.ActionView, uri);
                _Context.StartActivity(intent);
Aiko West
  • 791
  • 1
  • 10
  • 30