Does System.exit() close ActivityRecordings and GPS?

I have an app where i record activity and gps information. In a "happy flow" I start the recording with a CreateSession and also turn on gps with Position.enableLocationEvents, and in the end of the flow I do _session.stop(), _session.save() and Position.enableLocationEvents(Position.LOCATION_DISABLE...)

What will happen if I close my app before the end of the happy flow? I have a ConfirmExitDelegate that asks me a Yes/No if I press lap. If I confirm with Yes then I do a System.exit(). Do I need to to a .stop(), .discard() and turn off gps separately or is this handled as a consequense of System.exit()?

  • What I do is just block exiting the app if a session is still being recorded.  Just return "true" from onBack if recording, false if exiting is OK.

    Just doing a System.exit() will not finalize the session and will leave it in an odd state.

  • Thanks for that quick response! In my case, I would want to allow exiting*, but then I guess I need to make sure to do a graceful exit. I.e. stop, discard and turn off gps before I do the System.exit().

    * use case: a referee app where you accidentically started e.g. a 2x45 min game when it should be e.g. 2x20 minutes. Then you do a quick exit and start over. and select a new period duration before you start the game again. I don't want to block the exit until the entire 2x45 has passed. Slight smile

  • It is good practice, when possible, to handle the user quitting properly, but technically you should be able to call System.exit() at any time and the app will exit "cleanly", as per the documentation: https://developer.garmin.com/connect-iq/api-docs/Toybox/System.html

    That said, if you call System.exit() with an active session, the session will be stopped and SAVED which is not what you want (you said in your post you would want the session discarded). And although System.exit() *should* also stop all sensors and cleanly handle all the things you would expect, this has not always been the case. There is an ongoing issue, with some devices, where the GPS and other sensors stay on unnecessarily after the app exists (even though they shouldn't). The workaround is to explicitly stop all the sensors you started (as per this forum post: https://forums.garmin.com/developer/connect-iq/f/discussion/872/battery-drain-when-connectiq-app-is-not-running/)

  • Lot of work, but maybe more user friendly to open a menu and there have relevant options, like resume, reset, exit

  • Lot of work, but maybe more user friendly to open a menu and there have relevant options, like resume, reset, exit

    Yeah, especially since that’s how built-in activities work. Unfortunately, taking the approach of trying to mimic native functionality in a complex app often leads to frustration over having to reinvent in the wheel, and realizing that most CIQ apps will never perfectly mimic native functionaity (by design).