Search code examples
javaandroidback

PopupWindow press back button to do the action


I have a Activity and implement PopupWindow class for a apps alert view.
My excepted case is the popupwindow is shown and pressed the device back button that will be called another action.
I tried to implement the onBackPressed in my Activity. But the popupwindow is shown, onBackPressed won't call. When I dismissed the popupwindow, the onBackPressed is called.
So anyone know how can implement the back button method in my case. Thanks.

@Override
public void onBackPressed() {
    super.onBackPressed();
}

Solution

  • You should remove popWindow.setFocusable(true) this part.

    And set final PopupWindow popWindow = new PopupWindow(inflatedView, size.x, size.y, false); like this.

    If you want to see more detail of the answer, you could also check here.