Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 311
Ivan Čentéš Avatar asked Jan 17 '26 13:01

Ivan Čentéš


1 Answers

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);
like image 167
Dries Cleymans Avatar answered Jan 20 '26 04:01

Dries Cleymans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!