From 935295c9b88a395021255a0069f9ee8f85aca118 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Fri, 18 Mar 2011 17:27:36 -0500 Subject: [PATCH] Bug 110 - When you close an app on Android you see a JS error in logcat. --- framework/assets/js/phonegap.js.base | 29 ++++++++++++++++++++++++ framework/src/com/phonegap/DroidGap.java | 5 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/framework/assets/js/phonegap.js.base b/framework/assets/js/phonegap.js.base index e98a3726..9d39aa94 100755 --- a/framework/assets/js/phonegap.js.base +++ b/framework/assets/js/phonegap.js.base @@ -18,6 +18,8 @@ * onPhoneGapInfoReady Internal event fired when device properties are available * onDeviceReady User event fired to indicate that PhoneGap is ready * onResume User event fired to indicate a start/resume lifecycle event + * onPause User event fired to indicate a pause lifecycle event + * onDestroy Internal event fired when app is being destroyed (User should use window.onunload event, not this one). * * The only PhoneGap events that user code should register for are: * onDeviceReady @@ -26,6 +28,7 @@ * Listeners can be registered as: * document.addEventListener("deviceready", myDeviceReadyListener, false); * document.addEventListener("resume", myResumeListener, false); + * document.addEventListener("pause", myPauseListener, false); */ if (typeof(DeviceInfo) !== 'object') { @@ -241,6 +244,17 @@ PhoneGap.onResume = new PhoneGap.Channel('onResume'); */ PhoneGap.onPause = new PhoneGap.Channel('onPause'); +/** + * onDestroy channel is fired when the PhoneGap native code + * is destroyed. It is used internally. + * Window.onunload should be used by the user. + */ +PhoneGap.onDestroy = new PhoneGap.Channel('onDestroy'); +PhoneGap.onDestroy.subscribeOnce(function() { + PhoneGap.shuttingDown = true; +}); +PhoneGap.shuttingDown = false; + // _nativeReady is global variable that the native side can set // to signify that the native code is ready. It is a global since // it may be called before any PhoneGap JS is ready. @@ -696,6 +710,11 @@ PhoneGap.JSCallbackToken = null; */ PhoneGap.JSCallback = function() { + // Exit if shutting down app + if (PhoneGap.shuttingDown) { + return; + } + // If polling flag was changed, start using polling from now on if (PhoneGap.UsePolling) { PhoneGap.JSCallbackPolling(); @@ -707,6 +726,11 @@ PhoneGap.JSCallback = function() { // Callback function when XMLHttpRequest is ready xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState === 4){ + + // Exit if shutting down app + if (PhoneGap.shuttingDown) { + return; + } // If callback has JavaScript statement to execute if (xmlhttp.status === 200) { @@ -786,6 +810,11 @@ PhoneGap.UsePolling = false; // T=use polling, F=use XHR */ PhoneGap.JSCallbackPolling = function() { + // Exit if shutting down app + if (PhoneGap.shuttingDown) { + return; + } + // If polling flag was changed, stop using polling from now on if (!PhoneGap.UsePolling) { PhoneGap.JSCallback(); diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 470ed9f8..7d8c385f 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -649,7 +649,10 @@ public class DroidGap extends PhonegapActivity { // Make sure pause event is sent if onPause hasn't been called before onDestroy this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};"); - + + // Send destroy event to JavaScript + this.appView.loadUrl("javascript:try{PhoneGap.onDestroy.fire();}catch(e){};"); + // Load blank page so that JavaScript onunload is called this.appView.loadUrl("about:blank");