mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 13:51:27 +08:00
Merge pull request #4202 from NativeScript/app-displayed-event
App "displayed" event
This commit is contained in:
@ -21,6 +21,7 @@ import { UnhandledErrorEventData, iOSApplication, AndroidApplication, CssChanged
|
|||||||
|
|
||||||
export const launchEvent = "launch";
|
export const launchEvent = "launch";
|
||||||
export const suspendEvent = "suspend";
|
export const suspendEvent = "suspend";
|
||||||
|
export const displayedEvent = "displayed";
|
||||||
export const resumeEvent = "resume";
|
export const resumeEvent = "resume";
|
||||||
export const exitEvent = "exit";
|
export const exitEvent = "exit";
|
||||||
export const lowMemoryEvent = "lowMemory";
|
export const lowMemoryEvent = "lowMemory";
|
||||||
@ -45,6 +46,7 @@ export let ios = undefined;
|
|||||||
export const on: typeof events.on = events.on.bind(events);
|
export const on: typeof events.on = events.on.bind(events);
|
||||||
export const off: typeof events.off = events.off.bind(events);
|
export const off: typeof events.off = events.off.bind(events);
|
||||||
export const notify: typeof events.notify = events.notify.bind(events);
|
export const notify: typeof events.notify = events.notify.bind(events);
|
||||||
|
export const hasListeners: typeof events.hasListeners = events.hasListeners.bind(events);
|
||||||
|
|
||||||
let app: iOSApplication | AndroidApplication;
|
let app: iOSApplication | AndroidApplication;
|
||||||
export function setApplication(instance: iOSApplication | AndroidApplication): void {
|
export function setApplication(instance: iOSApplication | AndroidApplication): void {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
} from ".";
|
} from ".";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
notify, lowMemoryEvent, orientationChangedEvent, suspendEvent, resumeEvent,
|
notify, hasListeners, lowMemoryEvent, orientationChangedEvent, suspendEvent, resumeEvent, displayedEvent,
|
||||||
setApplication, livesync, Observable
|
setApplication, livesync, Observable
|
||||||
} from "./application-common";
|
} from "./application-common";
|
||||||
|
|
||||||
@ -181,7 +181,19 @@ function initLifecycleCallbacks() {
|
|||||||
androidApp.startActivity = activity;
|
androidApp.startActivity = activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
androidApp.notify(<AndroidActivityBundleEventData>{ eventName: ActivityCreated, object: androidApp, activity: activity, bundle: savedInstanceState });
|
androidApp.notify(<AndroidActivityBundleEventData>{ eventName: ActivityCreated, object: androidApp, activity, bundle: savedInstanceState });
|
||||||
|
|
||||||
|
if (hasListeners(displayedEvent)) {
|
||||||
|
let rootView = activity.findViewById((<any>android).R.id.content);
|
||||||
|
let onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({
|
||||||
|
onGlobalLayout() {
|
||||||
|
notify({ eventName: displayedEvent, object: androidApp, activity });
|
||||||
|
let viewTreeObserver = rootView.getViewTreeObserver();
|
||||||
|
viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onActivityDestroyed: function (activity: android.app.Activity) {
|
onActivityDestroyed: function (activity: android.app.Activity) {
|
||||||
|
12
tns-core-modules/application/application.d.ts
vendored
12
tns-core-modules/application/application.d.ts
vendored
@ -11,6 +11,11 @@ import { NavigationEntry, View, Observable, EventData } from "../ui/frame";
|
|||||||
*/
|
*/
|
||||||
export var launchEvent: string;
|
export var launchEvent: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String value used when hooking to displayed event.
|
||||||
|
*/
|
||||||
|
export var displayedEvent: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String value used when hooking to uncaughtError event.
|
* String value used when hooking to uncaughtError event.
|
||||||
*/
|
*/
|
||||||
@ -189,6 +194,13 @@ export function hasListeners(eventName: string): boolean;
|
|||||||
*/
|
*/
|
||||||
export function on(event: "launch", callback: (args: LaunchEventData) => void, thisArg?: any);
|
export function on(event: "launch", callback: (args: LaunchEventData) => void, thisArg?: any);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This event is raised after the application has performed most of its startup actions.
|
||||||
|
* Its intent is to be suitable for measuring app startup times.
|
||||||
|
* @experimental
|
||||||
|
*/
|
||||||
|
export function on(event: "displayed", callback: (args: EventData) => void, thisArg?: any);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This event is raised when the Application is suspended.
|
* This event is raised when the Application is suspended.
|
||||||
*/
|
*/
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
notify, launchEvent, resumeEvent, suspendEvent, exitEvent, lowMemoryEvent,
|
notify, launchEvent, resumeEvent, suspendEvent, exitEvent, lowMemoryEvent,
|
||||||
orientationChangedEvent, setApplication, livesync
|
orientationChangedEvent, setApplication, livesync, displayedEvent
|
||||||
} from "./application-common";
|
} from "./application-common";
|
||||||
|
|
||||||
// First reexport so that app module is initialized.
|
// First reexport so that app module is initialized.
|
||||||
@ -16,6 +16,8 @@ class Responder extends UIResponder {
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let displayedOnce = false;
|
||||||
|
|
||||||
class Window extends UIWindow {
|
class Window extends UIWindow {
|
||||||
public content;
|
public content;
|
||||||
|
|
||||||
@ -138,7 +140,13 @@ class IOSApplication implements IOSApplicationDefinition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private didBecomeActive(notification: NSNotification) {
|
private didBecomeActive(notification: NSNotification) {
|
||||||
notify(<ApplicationEventData>{ eventName: resumeEvent, object: this, ios: utils.ios.getter(UIApplication, UIApplication.sharedApplication) });
|
let ios = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
|
||||||
|
let object = this;
|
||||||
|
notify(<ApplicationEventData>{ eventName: resumeEvent, object, ios });
|
||||||
|
if (!displayedOnce) {
|
||||||
|
notify(<ApplicationEventData>{ eventName: displayedEvent, object, ios });
|
||||||
|
displayedOnce = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private didEnterBackground(notification: NSNotification) {
|
private didEnterBackground(notification: NSNotification) {
|
||||||
|
Reference in New Issue
Block a user