How to start an activity from a dialog in Android

后端 未结 7 1711
北海茫月
北海茫月 2021-01-11 12:20

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

7条回答
  •  别那么骄傲
    2021-01-11 12:37

    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.

提交回复
热议问题