mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 02:54:11 +08:00
fix(application): wrap native classes in initialisers (#10335)
fixes: #10334
This commit is contained in:
@ -13,8 +13,18 @@ declare namespace com {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare class BroadcastReceiver extends android.content.BroadcastReceiver {
|
||||||
|
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void);
|
||||||
|
}
|
||||||
|
|
||||||
|
let BroadcastReceiver_: typeof BroadcastReceiver;
|
||||||
|
function initBroadcastReceiver() {
|
||||||
|
if (BroadcastReceiver_) {
|
||||||
|
return BroadcastReceiver_;
|
||||||
|
}
|
||||||
|
|
||||||
@NativeClass
|
@NativeClass
|
||||||
class BroadcastReceiver extends android.content.BroadcastReceiver {
|
class BroadcastReceiverImpl extends android.content.BroadcastReceiver {
|
||||||
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
|
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
|
||||||
|
|
||||||
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
|
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
|
||||||
@ -31,9 +41,21 @@ class BroadcastReceiver extends android.content.BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BroadcastReceiver_ = BroadcastReceiverImpl;
|
||||||
|
return BroadcastReceiver_;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class NativeScriptLifecycleCallbacks extends android.app.Application.ActivityLifecycleCallbacks {}
|
||||||
|
|
||||||
|
let NativeScriptLifecycleCallbacks_: typeof NativeScriptLifecycleCallbacks;
|
||||||
|
function initNativeScriptLifecycleCallbacks() {
|
||||||
|
if (NativeScriptLifecycleCallbacks_) {
|
||||||
|
return NativeScriptLifecycleCallbacks_;
|
||||||
|
}
|
||||||
|
|
||||||
@NativeClass
|
@NativeClass
|
||||||
@JavaProxy('org.nativescript.NativeScriptLifecycleCallbacks')
|
@JavaProxy('org.nativescript.NativeScriptLifecycleCallbacks')
|
||||||
class NativeScriptLifecycleCallbacks extends android.app.Application.ActivityLifecycleCallbacks {
|
class NativeScriptLifecycleCallbacksImpl extends android.app.Application.ActivityLifecycleCallbacks {
|
||||||
private activitiesCount: number = 0;
|
private activitiesCount: number = 0;
|
||||||
private nativescriptActivity: androidx.appcompat.app.AppCompatActivity;
|
private nativescriptActivity: androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
@ -213,9 +235,21 @@ class NativeScriptLifecycleCallbacks extends android.app.Application.ActivityLif
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NativeScriptLifecycleCallbacks_ = NativeScriptLifecycleCallbacksImpl;
|
||||||
|
return NativeScriptLifecycleCallbacks_;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class NativeScriptComponentCallbacks extends android.content.ComponentCallbacks2 {}
|
||||||
|
|
||||||
|
let NativeScriptComponentCallbacks_: typeof NativeScriptComponentCallbacks;
|
||||||
|
function initNativeScriptComponentCallbacks() {
|
||||||
|
if (NativeScriptComponentCallbacks_) {
|
||||||
|
return NativeScriptComponentCallbacks_;
|
||||||
|
}
|
||||||
|
|
||||||
@NativeClass
|
@NativeClass
|
||||||
@JavaProxy('org.nativescript.NativeScriptComponentCallbacks')
|
@JavaProxy('org.nativescript.NativeScriptComponentCallbacks')
|
||||||
class NativeScriptComponentCallbacks extends android.content.ComponentCallbacks2 {
|
class NativeScriptComponentCallbacksImpl extends android.content.ComponentCallbacks2 {
|
||||||
@profile
|
@profile
|
||||||
public onLowMemory(): void {
|
public onLowMemory(): void {
|
||||||
gc();
|
gc();
|
||||||
@ -239,6 +273,10 @@ class NativeScriptComponentCallbacks extends android.content.ComponentCallbacks2
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NativeScriptComponentCallbacks_ = NativeScriptComponentCallbacksImpl;
|
||||||
|
return NativeScriptComponentCallbacks_;
|
||||||
|
}
|
||||||
|
|
||||||
export class AndroidApplication extends ApplicationCommon implements IAndroidApplication {
|
export class AndroidApplication extends ApplicationCommon implements IAndroidApplication {
|
||||||
static readonly activityCreatedEvent = 'activityCreated';
|
static readonly activityCreatedEvent = 'activityCreated';
|
||||||
static readonly activityDestroyedEvent = 'activityDestroyed';
|
static readonly activityDestroyedEvent = 'activityDestroyed';
|
||||||
@ -286,10 +324,10 @@ export class AndroidApplication extends ApplicationCommon implements IAndroidApp
|
|||||||
this._packageName = nativeApp.getPackageName();
|
this._packageName = nativeApp.getPackageName();
|
||||||
|
|
||||||
// we store those callbacks and add a function for clearing them later so that the objects will be eligable for GC
|
// we store those callbacks and add a function for clearing them later so that the objects will be eligable for GC
|
||||||
this.lifecycleCallbacks = new NativeScriptLifecycleCallbacks();
|
this.lifecycleCallbacks = new (initNativeScriptLifecycleCallbacks())();
|
||||||
this.nativeApp.registerActivityLifecycleCallbacks(this.lifecycleCallbacks);
|
this.nativeApp.registerActivityLifecycleCallbacks(this.lifecycleCallbacks);
|
||||||
|
|
||||||
this.componentCallbacks = new NativeScriptComponentCallbacks();
|
this.componentCallbacks = new (initNativeScriptComponentCallbacks())();
|
||||||
this.nativeApp.registerComponentCallbacks(this.componentCallbacks);
|
this.nativeApp.registerComponentCallbacks(this.componentCallbacks);
|
||||||
|
|
||||||
this._registerPendingReceivers();
|
this._registerPendingReceivers();
|
||||||
@ -395,7 +433,7 @@ export class AndroidApplication extends ApplicationCommon implements IAndroidApp
|
|||||||
|
|
||||||
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void): void {
|
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void): void {
|
||||||
const registerFunc = (context: android.content.Context) => {
|
const registerFunc = (context: android.content.Context) => {
|
||||||
const receiver: android.content.BroadcastReceiver = new BroadcastReceiver(onReceiveCallback);
|
const receiver: android.content.BroadcastReceiver = new (initBroadcastReceiver())(onReceiveCallback);
|
||||||
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter));
|
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter));
|
||||||
this._registeredReceivers[intentFilter] = receiver;
|
this._registeredReceivers[intentFilter] = receiver;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user