I have used following method to exit the application by prompting alertbox to user.  

But my application is asking 2 times to leave.

Code: 

```
    @Override
    public void onBackPressed()
    {
        new AlertDialog.Builder(this)
        .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();
    }

```