diff --git a/packages/core/application/application-common.ts b/packages/core/application/application-common.ts index 25284557c..a9bdbfd92 100644 --- a/packages/core/application/application-common.ts +++ b/packages/core/application/application-common.ts @@ -170,6 +170,16 @@ export function systemAppearanceChanged(rootView: View, newSystemAppearance: 'da }); } +export let inBackground = false; +export let suspended = false; + +export function setInBackground(value: boolean): void { + inBackground = value; +} +export function setSuspended(value: boolean): void { + suspended = value; +} + global.__onUncaughtError = function (error: NativeScriptError) { global.NativeScriptGlobals.events.notify({ eventName: uncaughtErrorEvent, diff --git a/packages/core/application/index.android.ts b/packages/core/application/index.android.ts index 514fe3713..19f671b8b 100644 --- a/packages/core/application/index.android.ts +++ b/packages/core/application/index.android.ts @@ -16,6 +16,7 @@ import { Observable } from '../data/observable'; import { profile } from '../profiling'; import { initAccessibilityCssHelper } from '../accessibility/accessibility-css-helper'; import { initAccessibilityFontScale } from '../accessibility/font-scale'; +import { inBackground, setInBackground, setSuspended, suspended } from './application-common'; const ActivityCreated = 'activityCreated'; const ActivityDestroyed = 'activityDestroyed'; @@ -48,8 +49,14 @@ export class AndroidApplication extends Observable implements AndroidApplication private _orientation: 'portrait' | 'landscape' | 'unknown'; private _systemAppearance: 'light' | 'dark'; - public paused: boolean; - public backgrounded: boolean; + + get paused() { + return suspended; + } + get backgrounded() { + return inBackground; + } + public nativeApp: android.app.Application; /** * @deprecated Use Utils.android.getApplicationContext() instead. @@ -402,7 +409,7 @@ function initLifecycleCallbacks() { onActivityPaused: profile('onActivityPaused', function (activity: androidx.appcompat.app.AppCompatActivity) { if ((activity).isNativeScriptActivity) { - androidApp.paused = true; + setSuspended(true); appCommon.notify({ eventName: appCommon.suspendEvent, object: androidApp, @@ -439,7 +446,7 @@ function initLifecycleCallbacks() { onActivityStarted: profile('onActivityStarted', function (activity: androidx.appcompat.app.AppCompatActivity) { activitiesStarted++; if (activitiesStarted === 1) { - androidApp.backgrounded = true; + setInBackground(false); appCommon.notify({ eventName: appCommon.foregroundEvent, object: androidApp, @@ -456,7 +463,7 @@ function initLifecycleCallbacks() { onActivityStopped: profile('onActivityStopped', function (activity: androidx.appcompat.app.AppCompatActivity) { activitiesStarted--; if (activitiesStarted === 0) { - androidApp.backgrounded = true; + setInBackground(true); appCommon.notify({ eventName: appCommon.backgroundEvent, object: androidApp, diff --git a/packages/core/application/index.ios.ts b/packages/core/application/index.ios.ts index b9ed79706..7f928e505 100644 --- a/packages/core/application/index.ios.ts +++ b/packages/core/application/index.ios.ts @@ -20,6 +20,7 @@ import { profile } from '../profiling'; import { iOSNativeHelper } from '../utils'; import { initAccessibilityCssHelper } from '../accessibility/accessibility-css-helper'; import { initAccessibilityFontScale } from '../accessibility/font-scale'; +import { setInBackground, setSuspended } from './application-common'; const IOS_PLATFORM = 'ios'; @@ -254,6 +255,8 @@ export class iOSApplication implements iOSApplicationDefinition { private didBecomeActive(notification: NSNotification) { const ios = UIApplication.sharedApplication; const object = this; + setInBackground(false); + setSuspended(false); notify({ eventName: resumeEvent, object, ios }); notify({ eventName: foregroundEvent, object, ios }); const rootView = this._rootView; @@ -265,6 +268,8 @@ export class iOSApplication implements iOSApplicationDefinition { private didEnterBackground(notification: NSNotification) { const ios = UIApplication.sharedApplication; const object = this; + setInBackground(true); + setSuspended(true); notify({ eventName: suspendEvent, object, ios }); notify({ eventName: backgroundEvent, object, ios }); const rootView = this._rootView; diff --git a/packages/core/ui/frame/index.android.ts b/packages/core/ui/frame/index.android.ts index 2e5097c79..056fa2fae 100644 --- a/packages/core/ui/frame/index.android.ts +++ b/packages/core/ui/frame/index.android.ts @@ -20,6 +20,7 @@ import { CSSUtils } from '../../css/system-classes'; import { Device } from '../../platform'; import { profile } from '../../profiling'; import { android as androidApplication } from '../../application'; +import { setSuspended } from '../../application/application-common'; export * from './frame-common'; @@ -1196,13 +1197,13 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks { // and raising the application resume event there causes issues like // https://github.com/NativeScript/NativeScript/issues/6708 if ((activity).isNativeScriptActivity) { + setSuspended(false); const args = { eventName: application.resumeEvent, object: application.android, android: activity, }; application.notify(args); - application.android.paused = false; } }