简体   繁体   English

window.beforeunload 在 WebViewClient.shouldOverrideUrlLoading 之前调用

[英]window.beforeunload called before WebViewClient.shouldOverrideUrlLoading

In the Android app with the WebView and WebViewClient configured, after the window.open(url, '_system') called from the js, the js event window.beforeunload is fired before the Android WebViewClient.shouldOverrideUrlLoading is called. In the Android app with the WebView and WebViewClient configured, after the window.open(url, '_system') called from the js, the js event window.beforeunload is fired before the Android WebViewClient.shouldOverrideUrlLoading is called. Is this a proper and intended behavior?这是一个适当的和预期的行为吗? Is there a way to intercept the url open without the window.beforeunload event fired?有没有办法拦截 url 打开而不触发 window.beforeunload 事件?

see this example code, maybe help you:看这个示例代码,也许对你有帮助:

    final WebView webView = (WebView)findViewById(R.id.webview);

    // Enable javascript
    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebViewClient(new WebViewClient() {

        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            final Uri uri = Uri.parse(url);
            return customUriHanlder(uri);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            final Uri uri = request.getUrl();
            return customUriHanlder(uri);
        }

        private boolean customUriHanlder(final Uri uri) {
            Log.i(TAG, "Uri =" + uri);
            final String host = uri.getHost();
            final String scheme = uri.getScheme();
            // you can set your specific condition
            if (true) {
                // Returning false means that you are going to load this url in the webView itself
                return false;
            } else {
                // Returning true means that you need to handle what to do with the url
                // open web page in a Browser
                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
            }
        }

    });

    // Load the webpage
    webView.loadUrl("https://google.com/");

with customUriHanlder function you can load your url in vebview itself or in browser.使用customUriHanlder function 您可以在 vebview 本身或浏览器中加载 url。
on google documentation shouldOverrideUrlLoading : If a WebViewClient is provided, returning true causes the current WebView to abort loading the URL, while returning false causes the WebView to continue loading the URL as usual. on google documentation shouldOverrideUrlLoading : If a WebViewClient is provided, returning true causes the current WebView to abort loading the URL, while returning false causes the WebView to continue loading the URL as usual.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebViewClient.shouldOverrideUrlLoading中的FragmentTransaction抛出IllegalStateException - FragmentTransaction in WebViewClient.shouldOverrideUrlLoading throws IllegalStateException Window.beforeunload 事件在 Android 本机应用程序的 WebView 中触发,无论 shouldOverrideUrlLoading 返回值如何 - Window.beforeunload event is triggered in WebView in Android native app regardless of shouldOverrideUrlLoading return value android 2.2中的WebViewClient不应该调用OverlyUrlLoading - WebViewClient in android 2.2 shouldOverrideUrlLoading not called Android-具有多个webViewClient的多个webView:在新的webViewClient上未调用shouldOverrideUrlLoading - Android - multiple webViews with multiple webViewClient : shouldOverrideUrlLoading not called on new webViewClient WebViewClient 未调用 shouldOverrideUrlLoading - WebViewClient not calling shouldOverrideUrlLoading WebViewClient - onPageStarted()vs shouldOverrideUrlLoading()? - WebViewClient - onPageStarted() vs shouldOverrideUrlLoading()? WebViewClient不会打开在ShouldOverrideUrlLoading()中处理的页面 - WebViewClient will not open page handled in shouldOverrideUrlLoading() Webview shouldOverrideUrlLoading 没有被调用 - Webview shouldOverrideUrlLoading not getting called shouldoverrideurlloading不称为Webview Android - shouldoverrideurlloading not called Webview Android 未调用Android webview shouldOverrideUrlLoading - Android webview shouldOverrideUrlLoading is not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM