3

Is it possible to disable a confirm navigation pop up in android webview from a website that I am viewing?

enter image description here

I tried this one and I what I did is just return 'true' without showing any pop-up, but the navigation pop up still shows up. I want to disable it and I would like to just automatically navigate without any warning.

Here's the CustomWebChromeClient for my webview

public class CustomWebChromeClient extends WebChromeClient {

    @Override
    public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
        return true;
    }
}
Community
  • 1
  • 1
Aaron
  • 2,591
  • 4
  • 27
  • 45

3 Answers3

3

You can override onJsBeforeUnload to always return true.

onJsBeforeUnload is the callback that gets invoked before the confirm navigation dialog pops up.

https://developer.android.com/reference/android/webkit/WebChromeClient.html#onJsBeforeUnload(android.webkit.WebView, java.lang.String, java.lang.String, android.webkit.JsResult)

Edit: As mentioned by AndyW, confirm or cancel methods should be invoked on jsResult argument otherwise the app might freeze because of the pending javascript dialog.

Shuvam Shah
  • 357
  • 2
  • 6
  • 13
Alex Chu
  • 31
  • 2
  • 1
    Worth noting that returning true will prevent the dialog, but you will still need to call a method from the JsResult object. E.g jsResult.cancel() – AndyW Jul 03 '18 at 08:57
  • As pointed by AndyW . Always call JsResult.cancel() or JsResult.confirm() before returing true , or else the WebView will not work and show abnormal behaviour – Mohit Singh Mar 20 '20 at 14:37
3

For me overriding onJsBeforeUnload as followed did it:

@Override
public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
     result.confirm();
     return true;
}
valerybodak
  • 4,195
  • 2
  • 42
  • 53
K4i
  • 31
  • 1
0

Why don't you create a webView in xml and use that webview in activity instead of using a class with webChromeClient, the below steps may help you.

step 1: Create a webview in your xml file and call this view in Java

webView = (WebView) findViewById(R.id.exampleWebView);

step 2:

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);

step 3:

webView.loadUrl(getResources().getString(R.string.olo_non_loggedin_url));
webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url,
                Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            pBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            pBar.setVisibility(View.GONE);
        }
    });
Bethan
  • 971
  • 8
  • 23