How to Identify when Glance/Widget is Closed (onStop appears to fire on glance termination, not the view)

Hi,

Is it possible to detect when a glance/widget app is stopped?

The onStop() handler on AppBase fires when the glance closes and hands over to the View so that's too early -- I'm just starting then, not stopping!

Once in the view (or ViewLoop in my case) how do I know when the user has clicked (Edge/touch) or backed out (watch Back-button) back to the device "home" page so I can do my application state-saving?  Hopefully it's something easy I have missed.

I have a temporary System.exit() set-up which I am using to see if where an event is fired?

Maybe this is the *wrong way to test or am I missing something in the Simulator? Should Simulator -> File/Quit act as the Device exit methods?  

 Many thanks in advance

  • When you click the glance it stops the app that is running in glance mode and starts the app in view mode. So you'll see a full lifecycle: onStart, ... onStop. What you can do is to save the glance view and the main view in a variable in the app, and look at this variable in onStop and by seeing which one isn't null you'll know which view to "pass" the onStop call.

    However some developers say that in certain app types / device generations onStop is either not called or it's too late to save to the storage (will get exceptions in some cases) so the safest probably would be to save the state after every state change - not sure what it means in your case but maybe after every user click / view switch

  • OK, that’s very helpful.  Maybe the ViewLoop and ViewLoopFactory makes it more complex as there isn’t simply a main view but I get the theory.

    So part #2 was can I test this in the Sim? I certainly see the first onStop() when running the glance in Sim which then exits to the app… some thinking time later ...

    ANSWER; don’t use an Edge to test in the Sim !  Using button-devices, it’s easy to use the cancel/back button on the Sim to replicate exiting the app. However in the Sim the Edge does not actually show the setControlBar title (with back arrow). This shows on a real device and should probably be marked as a Sim bug as it makes testing on stop hard (impossible?)

  • Haven't played much with widgets, even less on edge, but I think you're not really interested in the glance in this question, so just go to the settings in the simulator and let it start in the widget view (though if I remember correctly that might only exist on some devices)

    And yes, if your layout differs between a real device and the simulator, that would be a bug IMHO as well.

  • In one of my apps, I persist the state to Storage in onStop(). This works reliably on devices from Fenix 6, Vivoactive 3, Forerunner x45, and Venu 2 onwards.

    To handle different behavior between glance and widget, I use the method described by  , simply storing a flag that indicates whether the app is currently running as a glance or a widget.