5

I want to handle back press when pop up window is displayed. In some care I want to dismiss it and in some case I do not want to perform some task in pop up window.

When popup window is displayed, Activity onBackPress will not be called. Then how can in capture back press event when a pop up window is displayed?

Nitesh V
  • 381
  • 6
  • 18
  • [check here](http://stackoverflow.com/a/8606966/2345913) also read its comments – CRUSADER Jul 15 '13 at 13:24
  • Setting background drawable to non null will always close the pop up window. I have some case where i do not want to close the pop up window rather do some task inside pop up window. So is there any way i can capture back press event when pop up window is visible. – Nitesh V Jul 15 '13 at 13:30

5 Answers5

6

You need to call setBackgroundDrawable() on your PopupWindow and set the background to a non null. It sounds strange but if the background isn't set to something on your PopupWindow then it won't be able to detect events from the Activity such as touching outside of the window or back button presses.

I had the same issue just a few days ago. I will try to find the SO answer where someone explains why this is so but it may take me a little bit. In the meantime, give it a try it should work.

Found it

I haven't had a chance to test it but you could try adding a keyEventListener and doing something like this

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
         // put your code here
    }

and add setOutsideTouchable(true) to your PopupWindow object and calling update() on it. If this doesn't work then you may have to just leave the back button disabled when the popup is showing and add your own Button to the window. I haven't found anything else that will allow you to pick up events from the back button being pressed.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Setting background drawable to non null will always close the pop up window. I have some case where i do not want to close the pop up window rather do some task inside pop up window. So is there any way i can capture back press event when pop up window is visible. – Nitesh V Jul 15 '13 at 13:28
  • I have updated with something to try. I hope it helps but I'm not sure what you want to do is possible when using a `PopUpWindow` – codeMagic Jul 15 '13 at 15:03
4

set background drawable like this

popup.setBackgroundDrawable(new BitmapDrawable());

then set OnDismissListener like this

popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //do your code here
            }
        });
Ali
  • 1,857
  • 24
  • 25
3
final PopupWindow popup = new PopupWindow(context);
...
popup.setFocusable(false); //Setting this to true will prevent the events to reach activity below
popup.setBackgroundDrawable(new BitmapDrawable()); //Or anything else, not null!

Then in your activity:

@Override
 public void onBackPressed() {
    //your code
 }
nightsnaker
  • 471
  • 6
  • 16
1

I know its too late but it can help others. There are two option of doing this

1) If popup window focus is not important for you then set

mPopupInfoWindow.setFocusable(false);

because if your popup window is in focus then it will not pass back press event to activity that is why onBackPressed() method is not called

2) If popup window focus maters for you then let it be true and set this Listener in popup window. This code works for me

 mPopupInfoWindow.getContentView().setFocusableInTouchMode(true);
    mPopupInfoWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_BACK) {
                mPopupInfoWindow.dismiss();
                return true;
            }
            return false;
        }
    });
Faisal Khan
  • 2,574
  • 3
  • 20
  • 36
0

I propose an easier alternative that you MAY consider. Other approaches did not work for me.

Solution principle: all button clicks near your popup window will be intercepted, but any BACK button will not be intercepted. So, if you have anything in you popupwindow that takes action, then set an indication just before your call to dismiss(). In your setOnDismissListener() perform an extra action (like getActivity().popupBackStack()).

The advantage of this solution is that you can create your own CustomPopupWindow and implement this strategy. You can hide this implementation in your custom popup window.

Step 1: add near to your instantiation of your Popup Window:

boolean isClickHandled = false; 
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ShapeDrawable());
popupWindow.setTouchInterceptor(new View.OnTouchListener() { // or whatever you want
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        isClickHandled = true;
        return false;
    }
});

If you have buttons inside your popupWindow, have the setOnClickListener.onClick set the isClickHandled = true and dismiss().

In your onDismissListener do something like:

popupWindow.setOnDismissListener(() -> {
        popupWindow.dismiss();
        if ( !isClickHandled) {
            MainActivity.mainActivity.getSupportFragmentManager().popBackStack();
        }
    });
tm1701
  • 7,307
  • 17
  • 79
  • 168