This question is a development of the Java theme : to circumvent the need for an impossible change in the value of a local variable from the outside . From the answers to the question, I realized that the essence of the problem was not the one I had originally suggested, and it deserves a separate question.

I repeat the task. We have a FloatingActionButton; when clicked, a dialog box appears (or popupWindow , it doesn’t matter), and when clicked again, it disappears. Also, the window should disappear when clicked anywhere outside the window.

 private void initFAB(){ fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { LayoutInflater layoutInflater = (LayoutInflater) getBaseContext() .getSystemService(LAYOUT_INFLATER_SERVICE); View popupWindowContent = layoutInflater.inflate(R.layout.popup_window_content, null); final PopupWindow popupWindow = new PopupWindow(popupWindowContent, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setOutsideTouchable(true); if (fab.getTag().toString().equals("0")){ rotateFabForward(); View.OnClickListener onClickListener = new View.OnClickListener() { public void onClick(View view) { //... } }; // ... popupWindow.showAtLocation(fab, Gravity.END | Gravity.BOTTOM, 50, 400); fab.setTag("1"); // visible state } else{ rotateFabBackward(); popupWindow.dismiss(); fab.setTag("0"); // hidden state } } }); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { rotateFabBackward(); fab.setTag("0"); } }); } 

If you close the window just by touching outside the window, but without a button, everything will be fine. When you press the button to close the window, two events are Dismiss : Dismiss and Click . At the same time, they work, or sequentially, I don’t know, but anyway, OnDismissListener finishes execution earlier and we arrive at onClick with a zero tag and do not fall into else .

enter image description here

Anything can be done? Can there be an analogue of stopPropagation from JavaScript?

    1 answer 1

    Try to use AlertDialog with liners for each button and a separate listener for the whole dialogue (setOnDismissListener), if I understood your problem correctly.

      AlertDialog.Builder b = new AlertDialog.Builder(context); b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); b.setPositiveButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); b.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { } }); 

    Updated. When using AlertDialog'a, the background is heated, which shows the user not to press the menu button again, because it is not logical. And all the "attention" is transferred to the dialog box.

    • Thank you for your decision. Unfortunately, I do not understand how it solves the problem. I have only 3 listeners - for the FAB button, the listener for touching outside the OnDismissListener window and the listener for the buttons in the window that is not considered in this task. The problem is that when you click on FAB in order to remove the window, two listeners are triggered at once, because OnDismissListener happens to be the entire area outside the window, including the FAB button. - Bokov Gleb
    • Do you want to re-press the fab button? If yes, then try to make the logic based on boolean values, such as whether the button was pressed. - Ivan Vovk
    • Yes, pressing the FAB button again should close the menu. I have already implemented this , as you said. - Bokov Gleb
    • The problem is the same: it is necessary that when you press FAB again, only its listener worked, and dismissListener did not work. Otherwise, onDismissListener will be written false (menu is hidden) and we will then execute onClick , into which we will come with this fasle value, and the just-closed menu will open again. - Bokov Gleb
    • Edit the question, it seems that you incorrectly set the logic to trigger OnDismiss - Ivan Vovk