A Metro style app is in one of
four Lifecycle states at any given time:
Not running, Running , Suspended or Terminated
As your app transitions among its states, it receives lifecycle events that help
you provide a consistent and high performance experience for your users.
Application Launch: An app is launched whenever it is activated by the user
but the process is in the NotRunning state and Windows displays a splash screen
for the app.
The primary tasks for the app are to register event handlers and set up any
custom UI it needs for loading. After completion of activation, an app enters
the Running state and the splash screen is torn down.
Application activation: An app can be activated by the user through a
variety of contracts and extensions.
To participate in activation,
your app must register to receive the Activated | activated event. Your app's
activation event handler can test to see why it was activated and whether it was
already in the Running state.
An app can be activated as follows:
Activation type-Description
Cached file: The user wants to save a file that the app provides content management for.
Camera: The user wants to capture photos or video from an attached camera.
Contact picker: The user wants to pick contacts.
Device: The app handles AutoPlay.
File: An app launched a file whose file type this app is registered to handle.
File open picker: The user wants to picks files or folders that are provided by the app.
File save picker: The user wants to save a file and selected the app.
Launch: The user launched the app or tapped a content tile.
Print task: The app handles print tasks.
Protocol: An app launched a URL whose protocol this app is registered to handle.
Search: The user wants to search with the app.
Share target: The app is a target for a share operation.
Handling Activation in
JavaScript: In the activated event handler code, your app can check the session
State object to determine if it should load saved data or if it should display
the default home view. Session State determines the last state of your app
automatically by examining eventArgs.detail.previousExecutionState. If the
session State is populated, use it to restore your session data, otherwise use
your app's default data.
var
app = WinJS.Application;
function
activatedHandler(eventArgs)
{
if
(eventArgs.detail.kind ==
Windows.ApplicationModel.Activation.ActivationKind.launch)
{
// Check
whether the session state variables are valid.
// If so,
retrieve the currency and range values saved in the checkpoint handler
if
(app.sessionState)
{
currency = app.sessionState.lastSeencurrency;
range = app.sessionState.lastSeenRange;
}
// If
not, use the app's default values
else
{
currency =
"DOW";
range =
"1M";
}
// Initialize
the WinJS controls asynchronously behind the app splash screen
// Start
retrieving the latest stock data asynchronously after the controls are ready
eventObject.setPromise(WinJS.UI.processAll().then(function
()
{
Api.initializeUsercurrency();
}));
}
}
app.addEventListener("activated",
activatedHandler);
Application Suspend: An
app is suspended when the user switches to another app or windows enters a
low-power mode. However, there are some apps that need to continue to run to
complete background tasks.
When the user moves an app to the background, Windows waits a few seconds to see
whether the user immediately switches back to the app. If the user does not
switch back, Windows suspends the app. Your app must be registered for the
suspending or checkpoint (JavaScript) events, which it receives when Windows
wants to suspend it so that the app data can be kept in persistent storage.
If your app doesn't return from its suspending event handler within 5 seconds of
receiving the suspending event, Windows will terminate it. It's important not to
do any heavy operations in your suspending event handler. Save your app data and
return quickly.
Example of handling suspend in JavaScript: You can use the checkpoint event to
handle suspending.
var app = WinJS.Application;
function checkpointHandler()
{
// The
checkpoint event gives us the chance to save application data.
// In this case,
we save the current currency info that the user is viewing.
app.sessionState.lastSeencurrency = selectedcurrency;
app.sessionState.lastSeenRange = selectedRange;
// Send client
side notifications
Tile.sendTileUpdate(selectedcurrency, currencyInfo.change,
currencyInfo.lastSale, currencyInfo.lastSaleTime, currencyInfo.open);
}
app.addEventListener("checkpoint",
checkpointHandler);
Application Visiblity:
When the user switches from your app to another app, your app is no longer
visible but remains in the running state until Windows can suspend it (for about
10 seconds).
If the user switches away from your app but activates or switches back to it
before Windows can suspend it, the app remains in the running state. You can use
the VisibilityChanged | msvisibilitychange event if your app needs to do
something when the user switches away and back.
Application Resume: A suspended app is resumed when the user switches to
it or when Windows comes out of a low-power state.
When an app is resumed from the Suspended state, it enters the Running state and
continues from where it was when it was suspended. No app data is lost, as it
was stored in memory. Therefore, most apps don't need to do anything when they
are resumed. However, the app could have been suspended for hours. If an app
registered an event handler for the Resuming | resuming event, it is called when
the app is resumed from the Suspended state.
Example of Handling Resume in JavaScript: In JavaScript, you can use the Windows
Runtime resuming event in the Windows.UI.WebUI.WebUIApplication namespace. This
is a specialized resuming event meant only for JavaScript apps.
function
resumingHandler()
{
//Update the
selected currency with the latest news, currency info, and chart data
News.requestNews(currency);
Api.getcurrencyInfoData(currency).then(function
(data)
{
displaycurrencyInfo(data);
});
Api.getHistoryData(range).then(function
(chartData)
{
CanvasChart.drawChart("chartCanvas",
chartData, Chart.getChartOptions(false));
});
// Send client
side notifications
Tile.sendTileUpdate(currency, currencyInfo.change, currencyInfo.lastSale,
currencyInfo.lastSaleTime, currencyInfo.open);
}
Windows.UI.WebUI.WebUIApplication.addEventListener("resuming",
resumingHandler);
Application Crash: Apps
are required to follow the system crash experience i.e to simply return to the
Start screen.
So that users can return to what they were doing as quickly as possible, you
shouldn't provide a warning dialog or other notification because that'll cause a
delay for the user. The disappearance of the app should make it clear to the
user that something went wrong. Windows reports the error to Microsoft and
Microsoft provides a subset of this error data to you so that you can use it to
improve your app.
Application Close: Usually, the user doesn't need to close apps; they can
let Windows manage them.
There's no special event to indicate that the user has closed an app. After an
app has been closed by the user, it's suspended and terminated, entering the
NotRunning state within about 10 seconds. The Suspending | suspending event is
called when the app is suspended and you can use this event handler to save
relevant application and user data to persistent storage.
Application Removal: When the user deletes your app, the app is removed
along with all its local data and it does not affect user data.
Application lifecycle programming interfaces: