mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
keep some variable from early GC to speed up start time (#5823)
* keep some variable from early GC to speed up start time * do not clear callbacks, as they shouldn't use much memory
This commit is contained in:
@ -45,6 +45,8 @@ export class AndroidApplication extends Observable implements AndroidApplication
|
|||||||
public foregroundActivity: android.app.Activity;
|
public foregroundActivity: android.app.Activity;
|
||||||
public startActivity: android.app.Activity;
|
public startActivity: android.app.Activity;
|
||||||
public packageName: string;
|
public packageName: string;
|
||||||
|
// we are using these property to store the callbacks to avoid early GC collection which would trigger MarkReachableObjects
|
||||||
|
private callbacks: any = {};
|
||||||
|
|
||||||
public get currentContext(): android.content.Context {
|
public get currentContext(): android.content.Context {
|
||||||
return this.foregroundActivity;
|
return this.foregroundActivity;
|
||||||
@ -63,10 +65,11 @@ export class AndroidApplication extends Observable implements AndroidApplication
|
|||||||
this.packageName = nativeApp.getPackageName();
|
this.packageName = nativeApp.getPackageName();
|
||||||
this.context = nativeApp.getApplicationContext();
|
this.context = nativeApp.getApplicationContext();
|
||||||
|
|
||||||
let lifecycleCallbacks = initLifecycleCallbacks();
|
// we store those callbacks and add a function for clearing them later so that the objects will be eligable for GC
|
||||||
let componentCallbacks = initComponentCallbacks();
|
this.callbacks.lifecycleCallbacks = initLifecycleCallbacks();
|
||||||
this.nativeApp.registerActivityLifecycleCallbacks(lifecycleCallbacks);
|
this.callbacks.componentCallbacks = initComponentCallbacks();
|
||||||
this.nativeApp.registerComponentCallbacks(componentCallbacks);
|
this.nativeApp.registerActivityLifecycleCallbacks(this.callbacks.lifecycleCallbacks);
|
||||||
|
this.nativeApp.registerComponentCallbacks(this.callbacks.componentCallbacks);
|
||||||
|
|
||||||
this._registerPendingReceivers();
|
this._registerPendingReceivers();
|
||||||
}
|
}
|
||||||
@ -235,14 +238,15 @@ function initLifecycleCallbacks() {
|
|||||||
|
|
||||||
const subscribeForGlobalLayout = profile("subscribeForGlobalLayout", function (activity: android.app.Activity) {
|
const subscribeForGlobalLayout = profile("subscribeForGlobalLayout", function (activity: android.app.Activity) {
|
||||||
const rootView = activity.getWindow().getDecorView().getRootView();
|
const rootView = activity.getWindow().getDecorView().getRootView();
|
||||||
let onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({
|
// store the listener not to trigger GC collection before collecting the method
|
||||||
|
this.onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({
|
||||||
onGlobalLayout() {
|
onGlobalLayout() {
|
||||||
notify({ eventName: displayedEvent, object: androidApp, activity });
|
notify({ eventName: displayedEvent, object: androidApp, activity });
|
||||||
let viewTreeObserver = rootView.getViewTreeObserver();
|
let viewTreeObserver = rootView.getViewTreeObserver();
|
||||||
viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener);
|
viewTreeObserver.removeOnGlobalLayoutListener(this.onGlobalLayoutListener);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
|
rootView.getViewTreeObserver().addOnGlobalLayoutListener(this.onGlobalLayoutListener);
|
||||||
});
|
});
|
||||||
|
|
||||||
const lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks({
|
const lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks({
|
||||||
|
Reference in New Issue
Block a user