Android WebView: is it possible to change the hash of a url?

I need to detect URL hash changes in Android WebView, but cannot find a way to do this. shouldOverrideUrlLoading()It fires when the page loads, but it does not start for subsequent hash changes after the page loads.

Is this possible with Android WebView?

+5
source share
1 answer

Maybe.

You must declare a Javascript interface as follows:

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

And link your webview to the Javascript interface as follows:

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

Then you need to write javascript code when loading the page to call the doStuff function when the hash changes.

webview.setWebViewClient(new WebViewClient() {  

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

Hope this helps. I tested it and it works.

+7
source

All Articles