Prevent back button from closing a dialog box

前端 未结 7 1331
别那么骄傲
别那么骄傲 2021-01-30 05:17

I am trying to prevent an AlertDialog box from closing when pressing the back button in Android. I have followed both of the popular methods in this thread, and with System.out.

7条回答
  •  时光取名叫无心
    2021-01-30 05:32

    In JQuery Mobile a popup adds a hash to the url, the following code allows the back to dismiss the popup when open and return to the app when closed. You could use the same logic for a custom ui framework.

    @Override
    public void onBackPressed() {
    
        // check if modal is open #&ui-state=dialog
    
        if (webView.getVisibility() == View.VISIBLE && webView.getUrl().contains("#&ui-state=dialog")) {
            // don't pass back button action
            if (webView.canGoBack()) {
                webView.goBack();
            }
        } else {
            // pass back button action
            super.onBackPressed();
        }
    }
    

提交回复
热议问题