Detect when app closed

I’m looking to add some analytics tracking an application and need to track a session start / session end. Session start is easy. But how can I detect when a users closes the app? Do I have any chance to run code before the app actually closes? I’d like to send a session end beacon in that case.

If I remember right I used to deal with this in some old Silverlight development, as I had 3 seconds to do whatever I needed on a user initiated exit. This made any web service type calls such as logging, analytics, or a session close event very problematic. Our solution was to do any local work we needed, save our state to a registry type marker, and process it on the next spinup. This took a fraction of the time we were allotted, and worked well enough overall.

I totally agree, that’s was what I meant by worked well enough overall. We were always one session behind on the results, but still gave us a good overall view of the stats we needed, and sessions would autoclose after 20 minutes of inactivity, so this would in some cases release the session before then (when those users come back within 20).

btw, I’ve been working on our Roku app for the past several months, and your posts have been beyond invaluable on countless occasions. First chance I have to say just how much I have appreciated your insights and advice.