Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Event to detect return from suspend?

Hi, is there any event that gets triggered when an app returns from being suspended?

Thanks!

— asked August 17th 2010 by karlo kilayko
  • ios
  • iphone
  • uiapplicationexitsonsuspend
0 Comments

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

    — answered September 6th 2010 by Robert Greenock
    permalink
    3 Comments
    • Actually, I wasn't able to get it to work if it was only in app.js, for some reason. I'll muck about with it again soon, but for now, it seems to work for me if I have it on each screen/tab's own js context. Not sure why, but at least it's working :)

      — commented September 6th 2010 by karlo kilayko
    • This event doesn't work.

      — commented June 14th 2011 by Justin Toth
    • The pause/resume events work for iOS only.

      — commented January 10th 2013 by Michael Stelly
  • 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 and resume events in Ti.App. Android allows you to watch events via an Activity, which (if you are interested in your app's main thread) will most often be Ti.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

    — answered August 30th 2011 by pospi
    permalink
    0 Comments
  • 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
    });
    
    — answered August 17th 2010 by Pedro Sousa
    permalink
    3 Comments
    • those look good! let me give them a shot

      — commented August 17th 2010 by karlo kilayko
    • This doesn't work.

      — commented June 14th 2011 by Justin Toth
    • pause and resume are for iOS only.

      — commented December 3rd 2012 by Michael Stelly
  • 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!

    — answered August 18th 2010 by karlo kilayko
    permalink
    2 Comments
    • I need this as well, were you able to figure it out?

      — commented June 14th 2011 by Justin Toth
    • from my experience, you need to stick this event listener in the app.js file.

      — commented October 29th 2011 by Tracy Hayman
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.