Ivan Čentéš
Ivan Čentéš

Reputation: 41

How to use {N} application lifecycle events in Angular?

The documentation page for Application Management describes a bunch of application lifecycle events and how to use them in javascript. But I couldn't find out how to use them when I write my {N} application using Angular.

There is a documentation page for Application Management in Angular, but it is the same as for the NativeScript core.

How can I use those events in Angular?

Upvotes: 3

Views: 758

Answers (1)

Dries Cleymans
Dries Cleymans

Reputation: 890

You can listen to the lifecycle events, but you need to add the handlers before the bootstrap because the code after the bootstrap will not be called in iOS.

For example, to listen to the application start event, you must add the listener in your main.ts:

import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app.module";
import { on as applicationOn, launchEvent, ApplicationEventData} from "application";

applicationOn(launchEvent, function (args: ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android.content.Intent class.
        console.log("Launched Android application with the following intent: " + args.android + ".");
    } else if (args.ios !== undefined) {
        // For iOS applications, args.ios is NSDictionary (launchOptions).
        console.log("Launched iOS application with options: " + args.ios);
    }
});

platformNativeScriptDynamic().bootstrapModule(AppModule);

Upvotes: 1

Related Questions