HTML and CSS Reference
In-Depth Information
Resuming the Application
Switching back to the application will resume it. You will see that the h1 elements displayed at
the top of the layout show that both the resuming and suspending events were sent by Windows,
as shown in Figure 5-1 .
Figure 5-1. Using the DOM to report when the suspending and resuming events are received
The state of a resumed application is exactly as it was at the moment it was suspended.
Your layout, data, event handlers, and everything else will be just as it was. You don't have to call
processAll when handling the resuming event, for example.
Your application could have been suspended for a long time, especially if the device was
put into a low-power state (such as sleeping). Network connections will have been closed by
any servers you were talking to (which is why you should close them explicitly when you get
the suspending event) and will have to be reopened when your application is resumed. You
will also have to refresh data that may have become stale. This includes location data, since the
device may have been moved during the period your app was suspended.
Windows allows users to terminate Metro apps by pressing Alt + F4. I am not certain
that this feature will survive to the final version of Windows 8, but it is something you may need
to consider for your app. There is no helpful warning event that gives you the opportunity to tidy
up your data and operations. Instead, Windows just terminates your application's process.
Tip
Adding a Background Activity
Now that I have conrmed that my app can get and respond to the resuming and suspend-
ing events, I can add some functionality that requires a recurring background task. For this
example, I am going to use the geolocation service to report on the current device location. To
do this, I have created a new JavaScript file called location.js , the contents of which are shown
in Listing 5-2.
Listing 5-2. Tracking the Device Location
/// <reference path="//Microsoft.WinJS.0.6/js/base.js" />
/// <reference path="//Microsoft.WinJS.0.6/js/ui.js" />
(function () {
"use strict";
var currentPromise;
var tracking = false;
function trackLocation() {
currentPromise = new WinJS.Promise(function (complete) {
var geo = new Windows.Devices.Geolocation.Geolocator();
 
 
Search WWH ::




Custom Search