How to recognize if I am currently in a background service?

Hi all, I'd love a bit of help:

I need to save some data into App.getApp.setProperty(...) at the app startup, and it is only possible to save data from an app, not from a background service.

However, if I put this code into myApp.initialize() or myApp.onStart(), these methods get called when the background service initializes as well - and calling setProperty will lead to a crash of a background service.

What is a correct check for 'Am I currently in an app or in a background service'?

Thanks!
  • In AppBase there are things that run for both the main app and the background, things that only run in the main app, and things that only run for the background.

    For start up, what you can do is have your code in getInitiallView, as that only runs in the main app (no view in the background), or you can have it in initialize for the main view.

    If you want to save things when you exit, it's a bit trickier, in that onStop runs for both. But there's a trick. I use a variable called "inBackground" and initialize it to false. Then, in the background process, I set it to true. (globals are not common between the main app and the background) So in onStop, I can check the value and if false, I can save things away.

    update: I should have mentioned, you can change it to "true" in getServiceDelegate() as that only runs in the background process.