Question

Is there any way to restore a default WebViewClient in WebView?

There is a WebView.setWebViewClient(WebViewClient client) method, but no get companion.

I would like to to load URL in WebView and avoid being sent to native browser in case of redirections but then go back to default behavior - links clicked opening in native browser.

To achieve that I would like set my own WebViewClient temporarily and then restore the default one.

WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://example.com");
// TODO: restore default WebViewClient
Was it helpful?

Solution

to go back to the default behavior try:

webview.setWebViewClient(null);

however you can't do that immediately after you call loadUrl, you need to at least wait for WebViewClient.onPageFinished.

OTHER TIPS

Finally the code:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        webView.setWebViewClient(null);
    }
});
webView.loadUrl(url);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top