I think you are calling alertdialog and finish()
both are at same time. Try to finish the activity in the alert dialog as
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_LONG).show();
finish();// here calling finish if user click ok button.
}