Call service function on application launch


#1

Hi!
I am trying to call a DI service function everytime the application is launched.
I read the documentation on application lifecycle here
https://docs.nativescript.org/angular/core-concepts/application-lifecycle#start-application

  1. How would I “inject” the service in main.ts before the bootstrap?
  2. I tried to import applicationOn with
    import { applicationOn } from 'application';
    but it seems that application doesn’t export applicationOn. Where can I find it?

#2

Technically you can’t access services before it’s created, application launch happens only once per app so why don’t you just inject the service in your app component and directly invoke the service method in it’s constructor?


#3

Hi! Thanks for your response!
The problem is that the app doesn’t always pass through the app.component.

The case I’m trying to solve is when user presses the home screen and than after a while re-opens the app. In that scenario the app, as it’s still loaded in RAM,doesn’t go through the regular bootstrap but simply resumes on the current page before leaving.


#4

That is not actually launch event, it’s resume. And for that you can add a event in your app component where you can have your service instance injected.


#5

awesome fixed it!! thanks!