Android WebView: Is it possible to detect URL hash change?

前端 未结 1 1221
抹茶落季
抹茶落季 2020-12-19 04:15

I need to detect URL hash changes in an Android WebView but can\'t find any way to do so. shouldOverrideUrlLoading() fires on the initial page load, but does n

相关标签:
1条回答
  • 2020-12-19 05:04

    It is possible.

    You have to declare a Javascript Interface like this :

    private class MyJSI {
             public void doStuff()
             {
             }
    }
    

    And link your webview with the Javascript Interface like this :

    webView.addJavascriptInterface(new MyJSI(), "myjsi");
    

    Then, you have to write some javascript code when the page is loaded, to call the function doStuff on hash change.

    webview.setWebViewClient(new WebViewClient() {  
    
             public void onPageFinished(WebView view, String url)  
             {
                     view.loadUrl("javascript:window.onhashchange = function() { myjsi.doStuff(); };");
             }
    });
    

    I hope it helps. I have tested it, and it works.

    0 讨论(0)
提交回复
热议问题