@@ -374,6 +374,8 @@ class SceneDelegate extends UIResponder implements UIWindowSceneDelegate {
374374 scene : windowScene ,
375375 } as SceneEventData ) ;
376376
377+ Application . ios . _setWindowActive ( nativeWindow , true , windowScene ) ;
378+
377379 const rootView = nativeWindow ?. rootView ;
378380 if ( rootView && ! rootView . isLoaded ) {
379381 rootView . callLoaded ( ) ;
@@ -400,6 +402,8 @@ class SceneDelegate extends UIResponder implements UIWindowSceneDelegate {
400402 window : nativeWindow ,
401403 scene : windowScene ,
402404 } as SceneEventData ) ;
405+
406+ Application . ios . _setWindowActive ( nativeWindow , false , windowScene ) ;
403407 }
404408
405409 sceneWillEnterForeground ( scene : UIScene ) : void {
@@ -422,6 +426,8 @@ class SceneDelegate extends UIResponder implements UIWindowSceneDelegate {
422426 window : nativeWindow ,
423427 scene : windowScene ,
424428 } as SceneEventData ) ;
429+
430+ Application . ios . _setWindowInForeground ( nativeWindow , true , windowScene ) ;
425431 }
426432
427433 sceneDidEnterBackground ( scene : UIScene ) : void {
@@ -445,6 +451,8 @@ class SceneDelegate extends UIResponder implements UIWindowSceneDelegate {
445451 scene : windowScene ,
446452 } as SceneEventData ) ;
447453
454+ Application . ios . _setWindowInForeground ( nativeWindow , false , windowScene ) ;
455+
448456 const rootView = nativeWindow ?. rootView ;
449457 if ( rootView && rootView . isLoaded ) {
450458 rootView . callUnloaded ( ) ;
@@ -454,6 +462,12 @@ class SceneDelegate extends UIResponder implements UIWindowSceneDelegate {
454462 sceneDidDisconnect ( scene : UIScene ) : void {
455463 const windowScene = scene as UIWindowScene ;
456464 const nativeWindow = Application . ios . _getWindowForScene ( windowScene ) ;
465+
466+ // A scene can disconnect without first resigning or backgrounding, so the window has
467+ // to leave the app-level aggregates here as well.
468+ Application . ios . _setWindowActive ( nativeWindow , false , windowScene ) ;
469+ Application . ios . _setWindowInForeground ( nativeWindow , false , windowScene ) ;
470+
457471 if ( nativeWindow ) {
458472 // A disconnect only ends the window session when the app asked for it —
459473 // otherwise iOS may reconnect the same session later. A window with no session
@@ -587,6 +601,11 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
587601 // The window whose root view the app-level root view state mirrors.
588602 private _mirroredWindow : NativeWindow ;
589603
604+ // Application-role windows currently in the foreground / currently active. Membership,
605+ // not a count, so a repeated or missing scene callback cannot drift the aggregate.
606+ private _foregroundWindows = new Set < NativeWindow > ( ) ;
607+ private _activeWindows = new Set < NativeWindow > ( ) ;
608+
590609 private _notificationObservers : NotificationObserver [ ] = [ ] ;
591610
592611 // Strong references to delegates recreated after an in-process soft reboot
@@ -1263,14 +1282,16 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
12631282 pendingWindowContentResolve ( ) ;
12641283 }
12651284
1266- const additionalData = {
1267- ios : UIApplication . sharedApplication ,
1268- } ;
1269- this . setInBackground ( false , additionalData ) ;
1270- this . setSuspended ( false , additionalData ) ;
1271-
1272- // In scene mode the root view belongs to a window, so the scene delegate loads it.
1285+ // A scene app derives app-level state from its windows, so this notification must not
1286+ // drive it as well. In scene mode the root view belongs to a window too, so the scene
1287+ // delegate loads it.
12731288 if ( ! this . supportsScenes ( ) ) {
1289+ const additionalData = {
1290+ ios : UIApplication . sharedApplication ,
1291+ } ;
1292+ this . setInBackground ( false , additionalData ) ;
1293+ this . setSuspended ( false , additionalData ) ;
1294+
12741295 const rootView = this . _rootView ;
12751296 if ( rootView && ! rootView . isLoaded ) {
12761297 rootView . callLoaded ( ) ;
@@ -1279,13 +1300,14 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
12791300 }
12801301
12811302 private didEnterBackground ( notification : NSNotification ) {
1282- const additionalData = {
1283- ios : UIApplication . sharedApplication ,
1284- } ;
1285- this . setInBackground ( true , additionalData ) ;
1286- this . setSuspended ( true , additionalData ) ;
1287-
1303+ // See didBecomeActive: in scene mode the windows drive app-level state.
12881304 if ( ! this . supportsScenes ( ) ) {
1305+ const additionalData = {
1306+ ios : UIApplication . sharedApplication ,
1307+ } ;
1308+ this . setInBackground ( true , additionalData ) ;
1309+ this . setSuspended ( true , additionalData ) ;
1310+
12891311 const rootView = this . _rootView ;
12901312 if ( rootView && rootView . isLoaded ) {
12911313 rootView . callUnloaded ( ) ;
@@ -1392,6 +1414,55 @@ export class iOSApplication extends ApplicationCommon implements IiOSApplication
13921414 return this . _windows . find ( ( nw ) => nw . ios ?. scene === scene ) as IOSNativeWindow | undefined ;
13931415 }
13941416
1417+ /**
1418+ * @internal - Feeds a window's foreground state into the application-level
1419+ * 'foreground'/'background' events, which describe the app as a whole: they are raised
1420+ * when the first application-role window enters the foreground and when the last one
1421+ * leaves it. Windows in any other role never speak for the app.
1422+ */
1423+ _setWindowInForeground ( nativeWindow : NativeWindow | undefined , inForeground : boolean , scene ?: UIScene ) : void {
1424+ if ( this . _trackWindowState ( this . _foregroundWindows , nativeWindow , inForeground ) ) {
1425+ this . setInBackground ( ! inForeground , {
1426+ ios : UIApplication . sharedApplication ,
1427+ scene,
1428+ } ) ;
1429+ }
1430+ }
1431+
1432+ /**
1433+ * @internal - Feeds a window's active state into the application-level 'resume'/'suspend'
1434+ * events, raised when the first application-role window becomes active and when the last
1435+ * active one resigns.
1436+ */
1437+ _setWindowActive ( nativeWindow : NativeWindow | undefined , active : boolean , scene ?: UIScene ) : void {
1438+ if ( this . _trackWindowState ( this . _activeWindows , nativeWindow , active ) ) {
1439+ this . setSuspended ( ! active , {
1440+ ios : UIApplication . sharedApplication ,
1441+ scene,
1442+ } ) ;
1443+ }
1444+ }
1445+
1446+ /**
1447+ * @returns whether the set flipped between empty and non-empty, which is the only point
1448+ * at which app-level state changes.
1449+ */
1450+ private _trackWindowState ( windows : Set < NativeWindow > , nativeWindow : NativeWindow | undefined , member : boolean ) : boolean {
1451+ if ( nativeWindow ?. role !== 'application' ) {
1452+ return false ;
1453+ }
1454+
1455+ const wasPopulated = windows . size > 0 ;
1456+
1457+ if ( member ) {
1458+ windows . add ( nativeWindow ) ;
1459+ } else {
1460+ windows . delete ( nativeWindow ) ;
1461+ }
1462+
1463+ return wasPopulated !== windows . size > 0 ;
1464+ }
1465+
13951466 protected _onWindowRegistered ( nativeWindow : NativeWindow ) : void {
13961467 if ( nativeWindow . isPrimary ) {
13971468 this . mirrorPrimaryWindow ( nativeWindow ) ;
0 commit comments