简体   繁体   English

Android:在WebView中检测history.back()

[英]Android : Detect history.back() in WebView

A button in my WebView is used to go back using the history.back() JavaScript call. 我的WebView一个按钮用于通过history.back() JavaScript调用返回。 I do not understand much of JavaScript, but after a bit of searching I found that we can use the addJavascriptInterface() method of a WebView . 我对JavaScript不太了解,但是经过一番搜索,我发现我们可以使用WebViewaddJavascriptInterface()方法。

Now, I intend to finish my Activity when the button is clicked. 现在,我打算在单击按钮时完成我的活动。 I landed up having something of this sort: 我着陆时遇到了这样的事情:

public class MyActivity extends Activity {
private static final String sTag = "MyActivity";

private WebView mWebContent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browser_page);


    mWebContent = (WebView) findViewById(R.id.webContent);

    mWebContent.getSettings().setJavaScriptEnabled(true);
    mWebContent.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebContent.getSettings().setBuiltInZoomControls(true);
    mWebContent.addJavascriptInterface(new JavaScriptInterface(), "history"); //history.back();

    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        mWebContent.loadUrl(extras.getString("URL"));
    }
}

    public class JavaScriptInterface {
        JavaScriptInterface() {
        }

    public void back() {
        Log.v(sTag, "back pressed");
        MyActivity.this.finish();
    }
}
}

But unfortunately when the button is pressed the Activity doesn't finish, neither do I get anything in the Log. 但是不幸的是,当按下按钮时,“活动”未完成,“日志”中也没有任何内容。

Here is the html for the button : 这是按钮的html:

<a href="javascript:void(0);">
   <img src="images/btn-go-back.png" onClick="history.back();" border="0" />
</a>

What am I doing wrong? 我究竟做错了什么?

Thanks in advance. 提前致谢。


Edit : Just wanted to clear the fact that changing the html is not an option for me. 编辑:只是想清除一个事实,更改html不是我的选择。 I have no power over the the html code :). 我对html代码无权:)。 I have to handle it in the application. 我必须在应用程序中处理它。


Edit: Tried changing history.back() to window.history.back() (locally), still no change. 编辑:尝试将history.back()更改为window.history.back() (局部),仍然没有更改。


Edit : I was experimenting by locally loading html conent and found out that if I change history.back() to say something like android_app.back() and register my JavaScriptInterface by the name android_app it works fine. 编辑:我正在通过本地加载html conent进行实验,发现如果我将history.back()更改为类似于android_app.back()的名称,并以android_app名称注册我的JavaScriptInterface则效果很好。 Why is that? 这是为什么? Does that mean that we can't use history to register an interface? 这是否意味着我们不能使用history来注册接口? Does the developer docs mention this? 开发人员文档是否提到了这一点?

Read the Navigating web page history section of this article. 阅读本文的“ 浏览网页历史记录”部分。 It tells completely about the android webviews.It tells about how to make webviews handle history. 它完整​​地讲述了android Webviews.It讲述了如何使WebViews处理历史记录。

http://developer.android.com/guide/webapps/webview.html http://developer.android.com/guide/webapps/webview.html

Hope this helps. 希望这可以帮助。

Try <a href="#" onclick="window.history.back()"><img src="bla" /></a> 尝试<a href="#" onclick="window.history.back()"><img src="bla" /></a>

edit: just read you can't change the HTML code .. Don't see a solution then .. 编辑:只是阅读您不能更改HTML代码..然后看不到解决方案。

After a lot of searching and asking I have finally arrived at the conclusion that you cannot add a java script interface, using addJavascriptInterface (Object obj, String interfaceName) , with the interface name as history (or perhaps any javascript keyword). 经过大量搜索和询问,我终于得出一个结论,即不能使用addJavascriptInterface (Object obj, String interfaceName) (以接口名称作为history (或可能是任何javascript关键字))添加Java脚本接口。

Maybe this is written somewhere in the developer docs or manual, but I couldn't find it though. 也许这是写在开发人员文档或手册中的某个地方,但是我找不到。

Check this: 检查一下:

@Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }

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

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