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 not fire for any subsequent hash changes once the page has loaded.
Is this even possible with the Android WebView?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With