Aaron
Aaron

Reputation: 2755

Android Webview: Disable confirm navigation

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;
    }
}

Upvotes: 3

Views: 2626

Answers (3)

K4i
K4i

Reputation: 41

For me overriding onJsBeforeUnload as followed did it:

@Override
public boolean onJsBeforeUnload(WebView view, String url, String message, JsResult result) {
     result.confirm();
     return true;
}

Upvotes: 4

Alex Chu
Alex Chu

Reputation: 31

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.

Upvotes: 3

Bethan
Bethan

Reputation: 971

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);
        }
    });

Upvotes: 0

Related Questions