[TIMOB-26542] Android: Add Ti.App "close" event support
GitHub Issue | n/a |
---|---|
Type | Improvement |
Priority | Low |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2019-09-10T22:17:58.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 8.3.0 |
Components | Android |
Labels | android, app, close, event, parity |
Reporter | Joshua Quick |
Assignee | Joshua Quick |
Created | 2018-11-10T03:04:51.000+0000 |
Updated | 2019-10-03T10:51:59.000+0000 |
Description
*Summary:*
The
Ti.App
module supports a "close" event that is currently only supported by iOS. This event gets fired just before the application get terminated by the operating system.
https://docs.appcelerator.com/platform/latest/#!/api/Titanium.App-event-close
We should add support on Android for parity. However, the "close" event should be fired just before Titanium's JavaScript runtime gets terminated instead. The reason is because pressing the back button on the root window will terminate Titanium's JavaScript runtime (which is okay) but the Android application process will still be alive in the background (this keeps its notifications alive in the status bar). Tapping the app icon again will create a new JavaScript runtime and re-execute the "app.js" while re-using the same application process. This is the normal native Android behavior.
This feature may be useful to "hyperloop" developers who need to do cleanup operations upon exit.
*Code Example:*
Ti.App.addEventListener("close", function() {
Ti.API.info("### App 'close' event was fired.");
});
Ti.UI.createWindow().open();
*Recommended Solution:*
Remove the existing "close" firing event code in the Java TiRootActivity.onDestroy()
method. It doesn't work because the JavaScript runtime has already been terminated by this point. It's too late.
[TiRootActivity.java](https://github.com/appcelerator/titanium_mobile/blob/master/android/titanium/src/java/org/appcelerator/titanium/TiRootActivity.java)
Instead, use the new Java KrollRuntime.OnDisposingListener
that we've added to Titanium 8.0.0. This listener will be called just before the JavaScript runtime gets terminated/disposed.
[KrollRuntime.java](https://github.com/appcelerator/titanium_mobile/blob/master/android/runtime/common/src/java/org/appcelerator/kroll/KrollRuntime.java)
PR (master): https://github.com/appcelerator/titanium_mobile/pull/10637
FR Passed. Waiting for CR to merge.
Improvement verified in SDK version 8.3.0.v20191002165629