By default pressing Back button on any activity the Android sistem will close / destroy the activity ( OnDestroy method is called ) See : Activity Lifecycle.

This can be a little annoying when you have some service that perform background work.

You can handle the back button action like this :

  1. @Override
  2.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  3.             if (keyCode == KeyEvent.KEYCODE_BACK) {
  4.  
  5.                 showDialog(DIALOG_CLOSE);
  6.  
  7.                 return true;
  8.             }
  9.             return super.onKeyDown(keyCode, event);
  10.         }

and let the user to decide what to do using an AlertDialog :

  1. @Override
  2.     protected Dialog onCreateDialog(int id) {
  3.         switch (id) {
  4.         case DIALOG_CLOSE :
  5.                 final AlertDialog.Builder closeWindowBuilder = new AlertDialog.Builder(this);
  6.                 closeWindowBuilder.setMessage(this.getResources().getString(R.string.alertCloseWindow))
  7.                    .setCancelable(false)
  8.                    .setPositiveButton(this.getResources().getString(R.string.btnYes), new DialogInterface.OnClickListener() {
  9.                        public void onClick(final DialogInterface dialog, final int id) {
  10.  
  11.                            finish();
  12.                        }
  13.                    })
  14.                    .setNegativeButton(this.getResources().getString(R.string.btnNo), new DialogInterface.OnClickListener() {
  15.                        public void onClick(final DialogInterface dialog, final int id) {
  16.  
  17.                             moveTaskToBack(true);
  18. // Move the task containing this activity to the back of the activity stack. The activity’s order within the task is unchanged.
  19.                        }
  20.                    });
  21.             return closeWindowBuilder.create();
  22.         }
  23.         return null;
  24.         }

Starting with Api Level 5 there it is a specific function in Activity class that can pe override :

  1. @Override
  2.         public void onBackPressed() {
  3.  
  4.                 showDialog(DIALOG_CLOSE);
  5.  
  6.                 super.onBackPressed();
  7.         }