webview.goBack() 方法不调用 shouldOverrideUrlLoading 方法

发布于 2024-12-08 17:22:30 字数 535 浏览 0 评论 0原文

我希望应用程序中加载的每个页面都经过 url 覆盖方法(我在其中进行一些 html 修改)。我怎样才能做到这一点?

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // do some html modifcations (myhtml)
    view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
    return false;
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
        web1.goBack(); // go back 1 step
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

I want every page loaded in my app go through the url override method ( where i do some html modifications ). How can i achieve this?.

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // do some html modifcations (myhtml)
    view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
    return false;
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
        web1.goBack(); // go back 1 step
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请别遗忘我 2024-12-15 17:22:30

在页面完成方法上,您将获得 url,并且可以重新加载 url

       @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            _dialog.dismiss();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
            try{
                _dialog.dismiss();
                Toast.makeText(Showwebviewlatestnews.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
            }catch (Exception e) {

            }
        }

on page finished method you will get the url and you can reload the url

       @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            _dialog.dismiss();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
            try{
                _dialog.dismiss();
                Toast.makeText(Showwebviewlatestnews.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
            }catch (Exception e) {

            }
        }
谜兔 2024-12-15 17:22:30

尝试返回 shouldOverrideUrlLoading true
这里有一个解释:-

如果主机应用程序想要离开当前的 WebView 并自行处理 url,则返回 true,否则返回 false。 参考

所以这应该做技巧,

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // do some html modifcations (myhtml)
    view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
    return true;
}

并像这样处理 backPressed() -

@Override
public void onBackPressed() {
    if (webview != null && webview.canGoBack()) {
        webview.goBack();
    } else {
        super.onBackPressed();
    }
}

Try to return shouldOverrideUrlLoading true
Here is an explanation:-

True if the host application wants to leave the current WebView and handle the url itself, otherwise return false. Reference

So this should do the trick,

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // do some html modifcations (myhtml)
    view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
    return true;
}

And handle backPressed() like -

@Override
public void onBackPressed() {
    if (webview != null && webview.canGoBack()) {
        webview.goBack();
    } else {
        super.onBackPressed();
    }
}
绿萝 2024-12-15 17:22:30

//将其替换

 public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
    web1.goBack(); // go back 1 step
    return true;
}
return super.onKeyDown(keyCode, event);}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
    web1.goBack(); // go back 1 step
    web1.goBack(); 
    return true;
}
return super.onKeyDown(keyCode, event);}

//Replace this

 public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
    web1.goBack(); // go back 1 step
    return true;
}
return super.onKeyDown(keyCode, event);}

With

public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
    web1.goBack(); // go back 1 step
    web1.goBack(); 
    return true;
}
return super.onKeyDown(keyCode, event);}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文