Event to detect return from suspend?
Hi, is there any event that gets triggered when an app returns from being suspended?
Thanks!
4 Answers
-
Accepted Answer
I don't know if you got an answer on this as it's 3 weeks old, but if you didn't
Ti.App.addEventListener('resume', function(){ Ti.API.info('resumed'); });
in the app.js works for me,
Rob
-
I have done some heavy testing of this stuff over iOS and Android in SDK 1.7.2, this is what I have found:
Application event callbacks can be registered, though the means to do so on each platform is different. iOS allows you to detect closing and reopening the app (well, pausing and resuming is the terminology there) via
pause
andresume
events inTi.App
. Android allows you to watch events via an Activity, which (if you are interested in your app's main thread) will most often beTi.Android.currentActivity
. These events should mimic the flow documented on the Android developers site. I have found the following events to be reliable on Android:- on opening - create > start > resume
- on minimising (pressing the home button) - pause > stop
- on closing (closing the main activity by closing a window with
exitOnClose = true
) - pause > stop (destroy is never called) - on reopening a prelaunched app - start > resume
Now, for the problems…
- iOS will cleanup memory when pausing apps if the app is taking a lot of memory, resulting in the
pause
event not being called when closed due to the app being killed first. This makes it entirely useless, especially on older iOS devices (the original iPhone will even force-kill a 1-window app with a single label!) - Complex Android applications often pause themselves when the Titanium VM gets too stressed, resulting in random flows on load such as create > start > resume > pause > stop even while the app is still running.
Basically, don't use these methods, they are rubbish. Just do your cleanup / data persistence immediately when the action occurs, and if your code really needs these features, complain about it until Appcelerator decides to fix them :p
-
I think you can use:
Titanium.App.addEventListener('pause',function(e){ //do something before pausing }); Titanium.App.addEventListener('resume',function(e){ //do something after resuming });
-
Okay, so I couldn't get any 'pause' or 'resume' events to trigger. Anyone have any other ideas or examples? My understanding was that apps would have a chance to save state before being suspended. I assume Ti is taking care of this for us, but it would be nice if our apps could detect when they are "unsuspended".
Thanks for any advice!