Skip to content Skip to sidebar Skip to footer

Changing The Android Webview Hash Without Reloading Page

I'm got a custom webview setup which is working pretty well, but I'd like to be able to either: 1, change the url hash without the webview reloading the page (it would lose the sta

Solution 1:

Or if you just want to change the hash, you can use:

view.loadUrl("javascript:location.hash=\"#"+fragment+"\"");

Solution 2:

For #2, your Activity can invoke JavaScript methods. All you have to do is call the loadUrl method with the appropriate JavaScript call:

mWebView.loadUrl("javascript:wave()");

From: http://developer.android.com/resources/articles/using-webviews.html

Solution 3:

I know this question is old but this is what works for me in Android 4.4:

String fragment = "#test"
mWebView.evaluateJavascript("location.hash=\"" + fragment + "\";", newValueCallback<String>() {
     @OverridepublicvoidonReceiveValue(String value) {
         // Yes, I like to log data :-)Log.d("MyApp", "onReceiveValue(value): " + value);
     }
});

Post a Comment for "Changing The Android Webview Hash Without Reloading Page"