0
public void exit(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.mipmap.ic_launcher_round);
    builder.setTitle("Likee Likes");
    builder.setMessage("Do you really wanna Exit?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    builder.create();
    builder.show();

}

**I am using this code to confirm my user either exit or not. when my user click the "yes" button the app doesn't close and get back to the previous activity. Is there any mistake with this code? **

i am trying to close my app by user confirmation.

1 Answers1

0

I assumes you use AlertDialog in the another activity rather than your first activity, so when you use finish, you are close the activity that isn't the first activity. If you you want to close you app, you can try use startActivityForResult to process some job accordingto the requestCode.

But now startActivityForResult is deprected, you can try to use new way to do this: OnActivityResult method is deprecated, what is the alternative?.

You can reference to this too How to quit android application programmatically

Jeff Lin
  • 19
  • 5