Can't you make me turn off the activity when I press OK in the dialog?

Asked 2 years ago, Updated 2 years ago, 119 views

When the user checks the dialog with "Do you want to close the application?" I want to end the activity, but I don't know how to end it. Please let me know.

android android-activity android-dialog activity-finish

2022-09-21 22:02

1 Answers

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Closing Activity")
        .setMessage("Are you sure you want to close this activity?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();    
        }

    })
    .setNegativeButton("No", null)
    .show();
}

Calling the finish() method in this way will terminate the activity.


2022-09-21 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.