diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiBaseActivity.java b/android/titanium/src/java/org/appcelerator/titanium/TiBaseActivity.java index e25807825f5..a6a5a81ea65 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiBaseActivity.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiBaseActivity.java @@ -951,7 +951,7 @@ public void onBackPressed() } // Handle app exit ourselves since the above window proxy did not handle the back event. - boolean exitOnClose = true; + boolean exitOnClose = (TiActivityWindows.getWindowCount() <= 1); if (this.window != null) { exitOnClose = TiConvert.toBoolean(this.window.getProperty(TiC.PROPERTY_EXIT_ON_CLOSE), exitOnClose); } @@ -1689,19 +1689,33 @@ protected void fireOnDestroy() private boolean shouldFinishRootActivity() { + // Do not finish root activity if disabled globally. (Typically done when restarting LiveView.) if (TiBaseActivity.canFinishRoot == false) { return false; } + // This method only applies to "Ti.UI.Window" based activities. + // If this is the root activity, then let it do its default finish handling. if (this instanceof TiRootActivity) { return false; } - boolean exitOnClose = (TiActivityWindows.getWindowCount() <= 1); + // Determine if this activity's "Ti.UI.Window" reference is still in the global collection. + // - Will not be in the collection if its close() method was called. + // - Will be in collection when pressing Back button or finish() was called natively. + boolean isTiWindowOpen = false; + if (this.launchIntent != null) { + int windowId = + this.launchIntent.getIntExtra(TiC.INTENT_PROPERTY_WINDOW_ID, TiActivityWindows.INVALID_WINDOW_ID); + if (windowId != TiActivityWindows.INVALID_WINDOW_ID) { + isTiWindowOpen = TiActivityWindows.hasWindow(windowId); + } + } + + // If this is the last "Ti.UI.Window" activity, then exit by default unless "exitOnClose" property was set. + boolean exitOnClose = (TiActivityWindows.getWindowCount() <= (isTiWindowOpen ? 1 : 0)); if ((this.window != null) && this.window.hasProperty(TiC.PROPERTY_EXIT_ON_CLOSE)) { exitOnClose = TiConvert.toBoolean(this.window.getProperty(TiC.PROPERTY_EXIT_ON_CLOSE), exitOnClose); - } else if (this.launchIntent != null) { - exitOnClose = this.launchIntent.getBooleanExtra(TiC.INTENT_PROPERTY_FINISH_ROOT, exitOnClose); } return exitOnClose; } diff --git a/android/titanium/src/java/org/appcelerator/titanium/proxy/TiWindowProxy.java b/android/titanium/src/java/org/appcelerator/titanium/proxy/TiWindowProxy.java index 3ec3de49cbb..445b8fa8364 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/proxy/TiWindowProxy.java +++ b/android/titanium/src/java/org/appcelerator/titanium/proxy/TiWindowProxy.java @@ -556,16 +556,15 @@ protected void fillIntent(Activity activity, Intent intent) intent.putExtra(TiC.PROPERTY_EXTEND_SAFE_AREA, value); } - boolean exitOnClose = false; if (hasProperty(TiC.PROPERTY_EXIT_ON_CLOSE)) { - exitOnClose = TiConvert.toBoolean(getProperty(TiC.PROPERTY_EXIT_ON_CLOSE), exitOnClose); - } else { - // If launching child activity from Titanium root activity, then have it exit out of the app. + // Use proxy's assigned "exitOnClose" property setting. + boolean exitOnClose = TiConvert.toBoolean(getProperty(TiC.PROPERTY_EXIT_ON_CLOSE), false); + intent.putExtra(TiC.INTENT_PROPERTY_FINISH_ROOT, exitOnClose); + } else if (activity.isTaskRoot() || (activity == TiApplication.getInstance().getRootActivity())) { + // We're opening child activity from Titanium root activity. Have it exit out of app by default. // Note: If launched via startActivityForResult(), then root activity won't be the task's root. - exitOnClose = activity.isTaskRoot() || (activity == TiApplication.getInstance().getRootActivity()); - setProperty(TiC.PROPERTY_EXIT_ON_CLOSE, exitOnClose); + intent.putExtra(TiC.INTENT_PROPERTY_FINISH_ROOT, true); } - intent.putExtra(TiC.INTENT_PROPERTY_FINISH_ROOT, exitOnClose); // Set the theme property if (hasProperty(TiC.PROPERTY_THEME)) {