Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
TheEndless
Channel Surfer

Re: Detect when app closed

"EnTerr" wrote:
"TheEndless" wrote:
Android also has background threads, which would be awesome to have on Roku, but that's a different topic altogether...

It is indeed. Android is a good example of what NOT to do. 🙂 If Android does something one way - and other platforms (iOS, Windows mobile etc) do it differently - it's safe bet to go with "don't do the Android thing". Android started from the wrong premise, "let's turn phones into multitasking computers - that's what people need"

While I don't necessarily disagree with that assessment, I hope you're not suggesting that background/worker threads would not be extremely useful in BrightScript.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
EnTerr
Roku Guru

Re: Detect when app closed

"RokuMarkn" wrote:
The user presses HOME, that causes some kind of call to your exit handler. Now suppose the exit handler goes into a loop? Or decides to conduct a long conversation with a slow server? The user is stuck with a hung system. This can't happen in normal code because HOME will always exit the channel no matter what it's doing. One might envision a system where a second HOME press while in the exit handler would force an exit, although it kind of defeats the purpose of the exit handler.

I like your "double HOME" idea, Mark!
A lot, even. Because:
  • It provides clear resolution from user's point of view, regardless of how the program may misbehave (intentionally or un-intentionally). User's comfort beats developer's, there could be no dead end. The spice must flow.

  • It is clean on your side. Easy to implement as compared to background threads (stub-ing access to graphics/audio, dealing with CPU/network vampires, racy conditions etc) and won't affect foreground channel, for trivial reasons.

  • Users are already used to pressing Home repeatedly on occasions (when screen saver is engaged - or to rotate the home menu to 1st position). Heck, i have been observed pounding Home repeatedly when rushed. Urging => just do it, regardless of developer convenience (you cannot force the viewer stay with you - ultimately they'll yank the power cable if they have to).


Developing on it a little bit, i imagine something like:
  1. User presses Home. Send the app an "Exit Pending/Requested" subtype of event* (via roSystemLogEvent?) and start a 1000ms "time to appeal" period

  2. If within appeal period the app responds back by calling `pleaseDontKillMe(PrettyPlease)`, suspend the death sentence and put the app on a 10 sec "probation". Otherwise kill it.

  3. If during the "probation" (or appeal) period a 2nd Home arrives, we are done - close the app, no further notifications/reprieves. Otherwise at end of probation expunge criminal record (we forget Home was pressed, live in the clear)


This way an app will have a chance to plead its case to the user (like show popup "please, please don't kill me now - i am saving game level/video progress right now, will be done in 5...4...3... etc") - but won't be able to tie them down (remember those ads in the web browsers that when tried to be closed would open new windows?).

It can also be implemented in stages - both on channel's and Roku's side. Meaning the app does not have to call back to delay exit - it has 1000ms to say save to registry its status. Or that at first iteration of API, Roku may not even do #2 - just send notification and kill a second** later.

(*) If they are not subscribed to that event, we are done: old behavior preserved; pre-exit notifications is an opt-in mechanism
(**) All times mentioned are somewhat arbitrary, obviously - but they illustrate the right magnitude of value.
0 Kudos
ishish
Visitor

Re: Detect when app closed

In my opinion the best way for Roku to allow this would be to have the developer create a task node that gets run on exit in the background. You would optionally point to the task node/function in your manifest file and it will run it. The UI would not be blocked as the task would run in a background thread. There would be a time limit of maybe 30 seconds for the task to complete or it will be autokilled. I believe iOS/tvOS have something similar to this with beginBackgroundTaskWithExpirationHandler

In any case this is important since sending a final event enables a variety of use cases.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.