Hello, please tell me how in the activity on the result of the answer to the question "Close Yes / No" to refuse to close the activity?

Code in activity

@Override public void onBackPressed() { super.onBackPressed(); backBeginActivity(); } protected void backBeginActivity() { String title = "Закрыть активность?"; String message = "Закрыть?"; String buttonYES = "Да"; String buttonNO = "Нет"; AlertDialog.Builder ad = new AlertDialog.Builder(MainActivitySelectTaxi.this); ad.setTitle(title); // заголовок ad.setMessage(message); // сообщение ad.setPositiveButton(buttonYES, new OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { finish(); //Закрыли } }); ad.setNegativeButton(buttonNO, new OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { //А как не закрывать??? Что написать или как это делается? } }); ad.setCancelable(true); ad.setOnCancelListener(new OnCancelListener() { public void onCancel(DialogInterface dialog) { } }); ad.show(); } 

    2 answers 2

     public void onBackPressed() { //super.onBackPressed(); вот это отсюда убираем backBeginActivity(); } 
    • That is, you need to comment out the call to the superclass constructor, I understand correctly? - Flippy
    • @Flippy is not a constructor, but an onBackPressed() method. Actually, the activity stack is moved in the original method or exit is performed if the stack is empty - rjhdby
    • Thank you, understood) - Flippy

    Just close the dialog, its instance comes to you in the parameters. dialog.dismiss ()

    • In onBackPressed () it will not cancel closing, the callback is triggered when you press the "back" button and the process is "already gone" - pavlofff