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
@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.
© 2024 OneMinuteCode. All rights reserved.