Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onBackPress not called when pop up window is displayed

Tags:

android

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?

like image 272
Nitesh V Avatar asked Jul 15 '13 13:07

Nitesh V


1 Answers

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.

like image 97
codeMagic Avatar answered Nov 03 '22 00:11

codeMagic