When using the older API calls for properties access: App.getApp().setProperty(...) I could determine if the app is in background-state at startup like so, because "setProperty" in background throws an exception that can be caught:
(:background)
function isBackground() {
try {
App.getApp().setProperty("dsfg94339fj2e9485hduth3", false);
App.getApp().deleteProperty("dsfg94339fj2e9485hduth3");
return false;
} catch (ex) {
return true;
}
}
I can then use it at startup e.g. like so delegating all calls to the appropriate controllers:
(:background)
class MyApp extends Application.AppBase {
private var appRunner = null;
function initialize() {
AppBase.initialize();
if (isBackground()) {
self.appRunner = new MyAppBackgroundController();
} else {
self.appRunner = new MyAppForegroundController();
}
}
function onStart(state) {
return self.appRunner.onStart(state);
}
// ...
}
Whenever only the new API calls are possible (App.Storage.get/set, App.Properties.get) I cannot leverage the exception anymore, because setting storage from background is now permitted. Do you know of any other way to determine whether or not an app is in background at initialization?
Things I considered:
- "getServiceDelegate" which is only called by background is too late for determination because then "onStart" already happened.
- Illegal Access (out of Bounds) when calling a non-background function from background is an error and not an exception so I cannot catch it
- Module access error (like Toybox.Communications) is also an error and not an exception so not catchable