mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fix(core): android wrong background state + current value accessors (#9883)
This commit is contained in:
committed by
Nathan Walker
parent
f548fdc735
commit
58a720699f
@@ -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(<UnhandledErrorEventData>{
|
||||
eventName: uncaughtErrorEvent,
|
||||
|
||||
@@ -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: <any>profile('onActivityPaused', function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
if ((<any>activity).isNativeScriptActivity) {
|
||||
androidApp.paused = true;
|
||||
setSuspended(true);
|
||||
appCommon.notify(<ApplicationEventData>{
|
||||
eventName: appCommon.suspendEvent,
|
||||
object: androidApp,
|
||||
@@ -439,7 +446,7 @@ function initLifecycleCallbacks() {
|
||||
onActivityStarted: <any>profile('onActivityStarted', function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
activitiesStarted++;
|
||||
if (activitiesStarted === 1) {
|
||||
androidApp.backgrounded = true;
|
||||
setInBackground(false);
|
||||
appCommon.notify(<ApplicationEventData>{
|
||||
eventName: appCommon.foregroundEvent,
|
||||
object: androidApp,
|
||||
@@ -456,7 +463,7 @@ function initLifecycleCallbacks() {
|
||||
onActivityStopped: <any>profile('onActivityStopped', function (activity: androidx.appcompat.app.AppCompatActivity) {
|
||||
activitiesStarted--;
|
||||
if (activitiesStarted === 0) {
|
||||
androidApp.backgrounded = true;
|
||||
setInBackground(true);
|
||||
appCommon.notify(<ApplicationEventData>{
|
||||
eventName: appCommon.backgroundEvent,
|
||||
object: androidApp,
|
||||
|
||||
@@ -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(<ApplicationEventData>{ eventName: resumeEvent, object, ios });
|
||||
notify(<ApplicationEventData>{ 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(<ApplicationEventData>{ eventName: suspendEvent, object, ios });
|
||||
notify(<ApplicationEventData>{ eventName: backgroundEvent, object, ios });
|
||||
const rootView = this._rootView;
|
||||
|
||||
@@ -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 ((<any>activity).isNativeScriptActivity) {
|
||||
setSuspended(false);
|
||||
const args = <application.ApplicationEventData>{
|
||||
eventName: application.resumeEvent,
|
||||
object: application.android,
|
||||
android: activity,
|
||||
};
|
||||
application.notify(args);
|
||||
application.android.paused = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user