Skip to content Skip to sidebar Skip to footer

Onbackpressed Not Called When Popupwindow Is Showing

Hee guys, So currently I'm using a PopupWindow to display an in app browser. However when pressing the back button it does nothing. I'm using the PopupWindow in another Fragment, t

Solution 1:

Make your PopupWindow not focusable:

finalPopupWindowpopWindow=newPopupWindow(inflatedView, size.x, size.y, false);

Also remove this line which was redundant:

popWindow.setFocusable(true);

Solution 2:

I think you should define a static method removePopupWindow(view v) in MainActivity, and call it inside onBackPressed() like MainActivity.removePopupWindow(popWindow); Hope It will help you.

Solution 3:

Ok, so this question was asked a long time ago, but I think I have a better solution. What I've done is add an OnDismissListener to my popup. In this listener I've added the code I wanted to execute when the popup got dismissed. This way Android still get's to manage the opening and closing of the popup and I just added a single line to make it work.

This is the way to add one:

yourAwesomePopupWindow.setOnDismissListener(newPopupWindow.OnDismissListener() {
        @OverridepublicvoidonDismiss() {
            // Your code on dismiss here
        }
});

Solution 4:

 popup.showAtLocation(popupView, Gravity.CENTER,0,0);
                popupshowing=true;// define this as a global@OverridepublicvoidonBackPressed() {
        if(popupshowing) {
            popup.dismiss();
           popupshowing=false;



        }
        else {
            super.onBackPressed();
        }

    }

Solution 5:

You can use this.

Dialogdialog=newDialog(ActivityMain.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);   dialog.setContentView(R.layout.mydialog);
dialog.setCancelable(true);

dialog.setOnDismissListener(newDialogInterface.OnDismissListener() {
   @OverridepublicvoidonDismiss(DialogInterface dialog) {
      try {
         if (mScannerView!=null) {
            mScannerView.stopCamera();
         }
      } catch(Exception e){
         e.printStackTrace();
      }
   }
});

Post a Comment for "Onbackpressed Not Called When Popupwindow Is Showing"