USING JAVA SCRIPT
When an app is launched, activation and loading events are fired in a specific order but you can't detect the activation type in the DOMContentLoaded event, so its event handler should not perform tasks related to activation and mostly performs one-time tasks such as registering to handle other events and to set up the UI to replace the splash screen and it should not include code that depends on the saved state.
Order of event firing:
-
DOMContentLoaded: Raised by the System only when a page is refreshed.
-
Activated: Raised by the System only when a running app is activated.
-
window.load: Raised by the System only when a page is refreshed.
Step 1: Register for the DOMContentLoaded event.
Registeration of this event occurs when all Document Object Model (DOM) and script content has been loaded, but not necessarily the images and content. It is recommended to set up any custom loading UI for the app in domcontentloadedHandler.
Example:
document.addEventListener("DOMContentLoaded", domcontentloadedHandler, false);
Step 2: Register for the activated event.
Register for the activated event in the global scope. This example sets activatedHandler as the activation handler and this enables you to retrieve the previous state of your app and the activation arguments.
Example:
var app = WinJS.Application;
app.addEventListener("activated", activatedHandler, false);
app.start();
Step 3: Restore application data when an app is suspended then terminated.
The Activated event is sent by the system when the user switches to your terminated app. In the Activated event, check the sessionState object to see whether your session state variable is defined. If defined then the app should load its saved application data and refresh its displayed content.
Example:
function activatedHandler(eventArgs)
{
if (eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.launch)
{
// Check whether my session state variables are valid.
// If so, retrieve the application data saved in the checkpoint handler
if (app.sessionState)
{
// TODO: Populate the UI with the previously saved application data
}
else
{
// TODO: Populate the UI with defaults
}
// Initialize the WinJS controls
WinJS.UI.processAll();
}
}
Step 4: Retrieve the launch arguments.
When the system launches your app, there may be additional context for the activation. Check the WebUILaunchActivatedEventArgs.arguments property as follows.
Example:
function activatedHandler(eventArgs)
{
if (eventArgs.detail.kind == Windows.ApplicationModel.Activation.ActivationKind.launch)
{
if (eventArgs.detail.arguments !== '')
{
// TODO: Parse the arguments string
}
}
}
USING C#
Step 1: Override the launch handler.
When an app is started, the system sends an Activated event. The Application.OnActivated method is invoked when the app is activated by some means other than normal launching.
Define the class for your application:
Example:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AppName.App">

Join the conversation! Your thoughts help the community grow.