Nitesh V
Nitesh V

Reputation: 391

onBackPress not called when pop up window is displayed

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?

Upvotes: 5

Views: 4138

Answers (5)

tm1701
tm1701

Reputation: 7591

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();
        }
    });

Upvotes: 0

Faisal Khan
Faisal Khan

Reputation: 2614

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;
        }
    });

Upvotes: 1

Ali
Ali

Reputation: 1915

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
            }
        });

Upvotes: 4

codeMagic
codeMagic

Reputation: 44571

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.

Upvotes: 6

nightsnaker
nightsnaker

Reputation: 471

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
 }

Upvotes: 3

Related Questions