Android java close app

To close apps on Android, swipe up from the bottom of the screen and hold until the recent apps menu pops up (if you use gesture navigations). If you use button navigation, tap on the recent apps button. Swipe up to close individual apps or tap the Close all button to close all background apps.

@Override
    public void onBackPressed() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Do You Want To Exit From Application?");
        alertDialogBuilder
                .setMessage("Click Yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                moveTaskToBack(true);
                                android.os.Process.killProcess(android.os.Process.myPid());
                                System.exit(1);
                            }
                        })
 
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
 
                        dialog.cancel();
                    }
                });
 
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }