Skip to content Skip to sidebar Skip to footer

Listen For Url Fragment Id Change In Webview

It seems that the WebViewClient methods such as shouldInterceptRequest(), onPageStarted(), and shouldOverrideUrlLoading() only listen for URL changes that cause the WebView to load

Solution 1:

I know that this is a really old question but i found out a solution that isn't available anywhere in Stack Overflow. jpetitto's answer works but it messes up javascript if we use hash change for routing or other such features.

I found out that by overriding the method doUpdateVisitedHistory in WebViewClient we can catch url's on hash change. Do see below snippet for example.

mWebView.setWebViewClient(newWebViewClient() {

        @OverridepublicvoiddoUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            super.doUpdateVisitedHistory(view, url, isReload);
            // somecode to run on hash change.
        }

}

Note: this method is fired on hash change and url change.

Solution 2:

As mitvenkatesan described in his comment to the question, this can be achieved by overriding the window.onhashchange() event of the page inside the WebView.

It turns out that this solution has already been addressed in another question.

Post a Comment for "Listen For Url Fragment Id Change In Webview"