Handling Android back button in HTML5 application

Posted by Anuraj on Friday, February 28, 2014

HTML5 Javascript Miscellaneous

Today I faced an issue with my HTML5 application. In this application we are showing a JQuery popup window. Client has reported a high priority(?) issue like, when he press the Android phone back button, instead of closing the popup window, application is getting closed.

Here is the code snippet which will help you to handle this kind of scenarios.

@Override
public void onBackPressed() {
	WebView myWebView = (WebView) findViewById(R.id.MyWebView);
	myWebView.loadUrl("javascript:popupclose();");
}

In the code, I am getting the WebView control when back button is pressed, and invoking the popupclose() function, which will close the popup window. This is the simple way to invoke JavaScript function from Android.



Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



MVP
Subscribe
Archives


Copyright © 2017 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub