javascriptwindows-8dom-eventsmicrosoft-metro

How to trap foreground and background events in a Javascript Win8 Metro App


Win8 Metro provides callback for when the process enters the activated and suspended states but what about when the app comes into the foreground or goes into the background? The process isn't necessarily suspended as soon as the app goes into the background and similarly it may come into the foreground while being still activated from the previous launch.

Are there any events in Javascript that can be used to execute code on foregrounding or backgrounding the app?


Solution

  • When the application is placed into the background, but not suspended (yet), you can use the "visibilitychanged" event, and "hidden" property on the document object: http://msdn.microsoft.com/en-us/library/ie/hh773167(v=vs.85).aspx

    These are fired the moment your app is no longer visible (e.g. the user hits the start button, or swipes to another application.

    If your app is then suspended, you will also get the appropriate suspend events then.