luisfer
luisfer

Reputation: 603

Listening in Javascript to back button in Android

I have a HTML5 app in Android. I would like to override this

@Override
public void onBackPressed() {
} 

so the js file, which has a method called back(), can "listen" when the user taps the back button in Android, in order to replicate the navigation behaviour that is currently in my app inside HTML5 from the WebView in Android.

I thought about creating a JavaScriptInterface, but I really don't see which could be the communication process to solve this. Probably it's not my day. Thanks.

Upvotes: 1

Views: 927

Answers (1)

baske
baske

Reputation: 1352

Would this solve your problem?

@Override
public void onBackPressed() {
    yourWebView.loadUrl("javascript:back()");
}

Of course your webpage should have a javascript method called "back()"

Aside: the JavaScriptInterface is used for communication in the other direction: if you want to call a Java method from your WebView's javascript.

Upvotes: 2

Related Questions