美文网首页
android webview按返回键一直加载当前页面

android webview按返回键一直加载当前页面

作者: lichao666 | 来源:发表于2022-01-12 16:56 被阅读0次

    当前页面是重定向的,监听返回键调用webview.goback(),就一直加载当前页面
    处理方式是,http开头的url,用super.shouldOverrideUrlLoading(view, request)处理
    非http开头的,用intent处理

    private String currentUrl = "";
    mWebview.setWebViewClient(new MyWebViewClient());
    private class MyWebViewClient extends WebViewClient {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                currentUrl = url;
            }
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(loadUrl(view, url)){
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url = String.valueOf(request.getUrl());
                if(loadUrl(view, url)){
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, request);
            }
        }
    private boolean loadUrl(WebView view, String url){
            LogUtil.LogE("ZLWebPlatformActivity shouldOverrideUrlLoading " + url);
            if(url.startsWith("http")) {
                return false;
            }
            try{
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            }catch (Exception e){
                e.printStackTrace();
            }
            return true;
        }
    

    相关文章

      网友评论

          本文标题:android webview按返回键一直加载当前页面

          本文链接:https://www.haomeiwen.com/subject/yziwcrtx.html