简体   繁体   English

在webview.goBack()上调用“shouldOverrideUrlLoading”方法

[英]call “shouldOverrideUrlLoading” method on webview.goBack()

I'm loading all my pages inside shouldOverrideUrlLoading method on webview (mainly to track the current page url and do some modifications). 我正在webview上的shouldOverrideUrlLoading方法中加载我的所有页面(主要是跟踪当前页面的URL并进行一些修改)。

My problem is that the back button ( eg: webview.goback() method ) bypasses the above function. 我的问题是后退按钮(例如: webview.goback()方法)绕过了上面的功能。

Is there any way i can explicitly call both the goback() and shouldOverrideUrlLoading() methods. 有什么方法可以显式调用goback()shouldOverrideUrlLoading()方法。

public boolean shouldOverrideUrlLoading(WebView view, String url){
//get current url and do some modifications to html}

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

Looking at your code seems like once the shouldOverrideUrlLoading method is called based on some logic for a specific URL Action you need the Webview go back method. 查看代码似乎一旦根据特定URL Action的某些逻辑调用了shouldOverrideUrlLoading方法,您需要使用Webview返回方法。 I would suggest you to use the following method inside the activity: 我建议你在活动中使用以下方法:

@Override
public void onBackPressed() {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
        webview.goBack();
    } else {
        super.onBackPressed();
    }
}

which does exactly the same as onKeypressed. 这与onKeypressed完全相同。 (Used onBackPressed instead of onKeypressed but write this code inside the Activity class but not inside the custom WebViewClient . (使用onBackPressed而不是onKeypressed但是在Activity类中编写此代码但不在自定义WebViewClient

Call the onBackPressed() inside your shouldOverrideUrlLoading . 调用onBackPressed()的内部shouldOverrideUrlLoading Try this, should work. 试试这个,应该工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM