How To Add Exit Button In Android Studio - Androidpro.in

In this post we will learn about how to implement exit button in android studio projects app. Here provide java code which code paste in the home activity up to last Carli brasses after implement onBackPressed Method. For more explain watch video which below here.

Home activity code 

@Override
public void onBackPressed() {
    AlertDialog.Builder builder =
new AlertDialog.Builder(this);
    builder.setMessage(
"Do You Want To Exit ? ")
            .setCancelable(
false)
            .setPositiveButton(
"Yes", new DialogInterface.OnClickListener() {
               
@Override
                
public void onClick(DialogInterface dialogInterface, int i) {
                    HomeActivity.
super.onBackPressed();
                }
            })
            .setNegativeButton(
"No", new DialogInterface.OnClickListener() {
               
@Override
               
public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();