mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-19 23:13:04 +08:00
fix(core): Application handling of nativeApp instance
This commit is contained in:
@ -46,9 +46,15 @@ export class AndroidApplication extends Observable implements AndroidApplication
|
||||
private _systemAppearance: 'light' | 'dark';
|
||||
public paused: boolean;
|
||||
public nativeApp: android.app.Application;
|
||||
/**
|
||||
* @deprecated Use Utils.android.getApplicationContext() instead.
|
||||
*/
|
||||
public context: android.content.Context;
|
||||
public foregroundActivity: androidx.appcompat.app.AppCompatActivity;
|
||||
public startActivity: androidx.appcompat.app.AppCompatActivity;
|
||||
/**
|
||||
* @deprecated Use Utils.android.getPackageName() instead.
|
||||
*/
|
||||
public packageName: string;
|
||||
// we are using these property to store the callbacks to avoid early GC collection which would trigger MarkReachableObjects
|
||||
private callbacks: any = {};
|
||||
@ -165,7 +171,7 @@ export { androidApp as android };
|
||||
let mainEntry: NavigationEntry;
|
||||
let started = false;
|
||||
|
||||
function ensureNativeApplication() {
|
||||
export function ensureNativeApplication() {
|
||||
if (!androidApp) {
|
||||
androidApp = new AndroidApplication();
|
||||
appCommon.setApplication(androidApp);
|
||||
@ -207,6 +213,7 @@ export function addCss(cssText: string, attributeScoped?: boolean): void {
|
||||
const CALLBACKS = '_callbacks';
|
||||
|
||||
export function _resetRootView(entry?: NavigationEntry | string): void {
|
||||
ensureNativeApplication();
|
||||
const activity = androidApp.foregroundActivity || androidApp.startActivity;
|
||||
if (!activity) {
|
||||
throw new Error('Cannot find android activity.');
|
||||
@ -225,6 +232,7 @@ export function getMainEntry() {
|
||||
}
|
||||
|
||||
export function getRootView(): View {
|
||||
ensureNativeApplication();
|
||||
// Use start activity as a backup when foregroundActivity is still not set
|
||||
// in cases when we are getting the root view before activity.onResumed event is fired
|
||||
const activity = androidApp.foregroundActivity || androidApp.startActivity;
|
||||
@ -269,14 +277,17 @@ export function getNativeApplication(): android.app.Application {
|
||||
}
|
||||
|
||||
export function orientation(): 'portrait' | 'landscape' | 'unknown' {
|
||||
ensureNativeApplication();
|
||||
return androidApp.orientation;
|
||||
}
|
||||
|
||||
export function systemAppearance(): 'dark' | 'light' {
|
||||
ensureNativeApplication();
|
||||
return androidApp.systemAppearance;
|
||||
}
|
||||
|
||||
global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
|
||||
ensureNativeApplication();
|
||||
if (androidApp && androidApp.paused) {
|
||||
return;
|
||||
}
|
||||
|
9
packages/core/application/index.d.ts
vendored
9
packages/core/application/index.d.ts
vendored
@ -360,6 +360,13 @@ export function systemAppearance(): 'dark' | 'light' | null;
|
||||
*/
|
||||
export let android: AndroidApplication;
|
||||
|
||||
/**
|
||||
* Used internally for backwards compatibility, will be removed in the future.
|
||||
* Allowed Application.android.context to work (or Application.ios). Instead use Utils.android.getApplicationContext() or Utils.android.getPackageName()
|
||||
* @internal
|
||||
*/
|
||||
export function ensureNativeApplication(): void;
|
||||
|
||||
/**
|
||||
* This is the iOS-specific application object instance.
|
||||
* Encapsulates methods and properties specific to the iOS platform.
|
||||
@ -468,6 +475,7 @@ export class AndroidApplication extends Observable {
|
||||
|
||||
/**
|
||||
* The application's [android Context](http://developer.android.com/reference/android/content/Context.html) object instance.
|
||||
* @deprecated Use Utils.android.getApplicationContext() instead.
|
||||
*/
|
||||
context: any /* android.content.Context */;
|
||||
|
||||
@ -495,6 +503,7 @@ export class AndroidApplication extends Observable {
|
||||
|
||||
/**
|
||||
* The name of the application package.
|
||||
* @deprecated Use Utils.android.getPackageName() instead.
|
||||
*/
|
||||
packageName: string;
|
||||
|
||||
|
@ -353,8 +353,16 @@ let iosApp: iOSApplication;
|
||||
/* tslint:enable */
|
||||
export { iosApp as ios };
|
||||
|
||||
export function ensureNativeApplication() {
|
||||
if (!iosApp) {
|
||||
iosApp = new iOSApplication();
|
||||
setApplication(iosApp);
|
||||
}
|
||||
}
|
||||
|
||||
// attach on global, so it can be overwritten in NativeScript Angular
|
||||
(<any>global).__onLiveSyncCore = function (context?: ModuleContext) {
|
||||
ensureNativeApplication();
|
||||
iosApp._onLivesync(context);
|
||||
};
|
||||
|
||||
@ -383,15 +391,13 @@ export function getMainEntry() {
|
||||
}
|
||||
|
||||
export function getRootView() {
|
||||
ensureNativeApplication();
|
||||
return iosApp.rootView;
|
||||
}
|
||||
|
||||
let started = false;
|
||||
export function run(entry?: string | NavigationEntry) {
|
||||
if (!iosApp) {
|
||||
iosApp = new iOSApplication();
|
||||
setApplication(iosApp);
|
||||
}
|
||||
ensureNativeApplication();
|
||||
|
||||
mainEntry = typeof entry === 'string' ? { moduleName: entry } : entry;
|
||||
started = true;
|
||||
@ -461,11 +467,13 @@ export function addCss(cssText: string, attributeScoped?: boolean): void {
|
||||
}
|
||||
|
||||
export function _resetRootView(entry?: NavigationEntry | string) {
|
||||
ensureNativeApplication();
|
||||
mainEntry = typeof entry === 'string' ? { moduleName: entry } : entry;
|
||||
iosApp.setWindowContent();
|
||||
}
|
||||
|
||||
export function getNativeApplication(): UIApplication {
|
||||
ensureNativeApplication();
|
||||
return iosApp.nativeApp;
|
||||
}
|
||||
|
||||
@ -506,6 +514,7 @@ function setViewControllerView(view: View): void {
|
||||
}
|
||||
|
||||
function setRootViewsCssClasses(rootView: View): void {
|
||||
ensureNativeApplication();
|
||||
const deviceType = Device.deviceType.toLowerCase();
|
||||
|
||||
CSSUtils.pushToSystemCssClasses(`${CSSUtils.CLASS_PREFIX}${IOS_PLATFORM}`);
|
||||
@ -518,6 +527,7 @@ function setRootViewsCssClasses(rootView: View): void {
|
||||
}
|
||||
|
||||
function setRootViewsSystemAppearanceCssClass(rootView: View): void {
|
||||
ensureNativeApplication();
|
||||
if (majorVersion >= 13) {
|
||||
const systemAppearanceCssClass = `${CSSUtils.CLASS_PREFIX}${iosApp.systemAppearance}`;
|
||||
CSSUtils.pushToSystemCssClasses(systemAppearanceCssClass);
|
||||
@ -526,10 +536,12 @@ function setRootViewsSystemAppearanceCssClass(rootView: View): void {
|
||||
}
|
||||
|
||||
export function orientation(): 'portrait' | 'landscape' | 'unknown' {
|
||||
ensureNativeApplication();
|
||||
return iosApp.orientation;
|
||||
}
|
||||
|
||||
export function systemAppearance(): 'dark' | 'light' {
|
||||
ensureNativeApplication();
|
||||
return iosApp.systemAppearance;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user