In a WebView is there a way for shouldOverrideUrlLoading to determine if it is catching a redirect vs. a user clicking a link?

前端 未结 6 577
时光说笑
时光说笑 2021-02-01 06:06

I would like to allow redirects to happen naturally in the WebView and only catch a new url if it is a happening because a user clicked something.

6条回答
  •  迷失自我
    2021-02-01 06:41

    Adapted from Garrett's answer because I found it didn't quite work as written , gave a few errors etc. And the part about visibility is in my view not needed. I had the exact same issue as the OP and this is giving the behaviour I want:

    webView.setWebViewClient(new WebViewClient()
        {
            private boolean mLoaded = false;
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(mLoaded){ // open in new browser window only if page already finished
                    try
                    {
                        Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        view.getContext().startActivity(intent);
                    }
                    catch (Exception e)
                    {
                        Log.d("webview", e.getMessage());
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            @Override
            public void onPageFinished(WebView webView, String url)
            {
                mLoaded = true;
            }
        }
    

提交回复
热议问题