Martyn
Martyn

Reputation: 16622

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 state of my js app)

2, call some js that sits within my web page from within android. I can't change any JS within the site, unfortunately, so can't custom write any js to put on the site especially for the job, the only stuff I have control over is the Android app.

Can anyone think of a way of doing either of these?

Thanks in advance.

M

Upvotes: 3

Views: 4830

Answers (3)

MartyIX
MartyIX

Reputation: 28646

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 + "\";", new ValueCallback<String>() {
     @Override
     public void onReceiveValue(String value) {
         // Yes, I like to log data :-)
         Log.d("MyApp", "onReceiveValue(value): " + value);
     }
});

Upvotes: 0

nembleton
nembleton

Reputation: 2502

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

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

Upvotes: 4

Adam Paxton
Adam Paxton

Reputation: 1432

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

Upvotes: 1

Related Questions