locked
App Lifecycle - When is OnSuspending and OnActivated called? RRS feed

  • Question

  • Hi,

    I am porting a WP8 App to Windows Store. The app should prompt for a passcode everytime it is launched or it was in background for some time.

    While this works fine on WP8 I could not find out how to the same thing on Windows Store. Problem is, that I was not able to create any situation where OnSuspending or OnActivated is called?

    The only "lifecycle event" that I could observe is OnLaunched which is called when the app was not running before and is started using its tile.

    Now I need any kind of notification when the users leaves the app (switches to another app, hits the Windows key to return to the desktop, etc.) and when the user returns to a running/suspended instance of the app. But OnSuspending or OnActivated are not called in these situations.

    Start App -> OnLaunched -> Hit Windows Key -> Return to Start Screen.
    What is the State of the app now? OnSuspending was not called thus it seem that the app is still running. Is this right? When will OnSuspending get called then?

    Start Screen -> Swipe from the left to get back to the app    OR
    Start Screen -> Click on App tile to get back to the app
    Neither OnLaunched nor OnActivated are called. The app is back in the foreground without any notice. When will OnActivated get called then?

    How can I get notified when the app is set to the background and gets back to the foreground?

    Thursday, January 9, 2014 11:21 AM

Answers

All replies

  • http://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx

    The concept of set to the background and back to the foreground is not related to the lifecycle of the app.  Why do you want to know this information in your app.


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, January 9, 2014 1:26 PM
    Moderator
  • How are you testing this? The debugger turns off lifetime management. Debugged apps don't suspend. VS has a suspend menu in the debugging toolbar so you can simulate the event.

    Additionally, the scenarios you mention won't necessarily trigger suspension anyway. Switching to the start screen doesn't suspend the app. You'd need to switch to another app and wait a few seconds. 

    For the lockout scenario checking on resume, launch, and activate should be good. The (non-debug) scenarios where the user "leaves" without suspending will be very quick.

    You could also check for Window Activation with heuristics to detect activation changes within the app (e.g. charms) or run a timer to lock after X minutes, and reset the timer when there is input.

    --Rob

    Thursday, January 9, 2014 4:30 PM
    Moderator
  • Is OnSuspending triggered if we close the app ? Or there's another event for closing the app ?

    By closing I mean, holding the app at the bottom until it turns on the back!
    We need to save something before the app is closed or suspended so that's why I need to know

    Wednesday, February 12, 2014 10:21 PM
  • What happened when you tried?

    There isn't an explicit event when closing the app. You should save state when suspending. I recommend saving data changes earlier and more often. When depends on the data.

    --Rob

    Friday, February 14, 2014 10:12 PM
    Moderator
  • Hmmm well, I thing based on your answer I should indeed save when its needed and not rely on a ClosedEvent to exist or anything of that kind.

    Friday, February 14, 2014 10:17 PM