Apr 8, 2021

How to add exit button in android studio - Androidpro.in

In this post shows how to implement exit button in android app project in android studio.

Press back button and when you reach home page on your android app then if press again exit for quit from app then ask from user you want to exit.

Step 1 Go to main activity where you want to show exit button (Exit From App) 

Step 2 Open java file and paste code which code provide below down the main section of activity.

@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();



Whatsapp Button works on Mobile Device only