Handling Android back button in HTML5 application

February 28, 2014 by Anuraj

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.

Copyright © 2024 Anuraj. 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. Powered by Jekyll. Hosted with ❤ by GitHub