The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to activate an app (XAML)

Learn how to handle activation for your app. The example in this topic overrides the OnLaunched method.

Roadmap: How does this topic relate to others? See:


Step 1: Override the launch handler

When an app is activated, for any reason, the system sends the Activated event. For a list of activation types, see the ActivationKind enumeration.

The Windows.UI.Xaml.Application class defines methods you can override to handle the various activation types. Several of the activation types have a specific method that you can override. For the other activation types, override the OnActivated method.

Define the class for your application.

<Application xmlns=""
             x:Class="AppName.App" >

Override the OnLaunched method. This method is called whenever the user launches the app. The LaunchActivatedEventArgs parameter contains the previous state of your app and the activation arguments.

Note  For Windows Phone Store apps, this method is called each time the user launches the app from Start tile or app list, even when the app is currently suspended in memory. On Windows, launching a suspended app from Start tile or app list doesn’t call this method.

Class App

   Protected Overrides Sub OnLaunched(args As LaunchActivatedEventArgs)
      Window.Current.Content = New MainPage()
   End Sub

End Class

Step 2: Restore application data if app was suspended then terminated

When the user switches to your terminated app, the system sends the Activated event, with Kind set to Launch and PreviousExecutionState set to Terminated or ClosedByUser. The app should load its saved application data and refresh its displayed content.

Protected Overrides Sub OnLaunched(args As Windows.ApplicationModel.Activation.LaunchActivatedEventArgs)
   Dim restoreState As Boolean = False

   Select Case args.PreviousExecutionState
      Case ApplicationExecutionState.Terminated
         ' TODO: Populate the UI with the previously saved application data
         restoreState = True
      Case ApplicationExecutionState.ClosedByUser
         ' TODO: Populate the UI with the previously saved application data
         restoreState = True
      Case Else
         ' TODO: Populate the UI with defaults
   End Select

   Window.Current.Content = New MainPage(restoreState)
End Sub

If the value of PreviousExecutionState is NotRunning, the app failed to save its application data successfully and the app should start over as if it were being initially launched.



For Windows Phone Store apps, the Resuming event is always followed by OnLaunched, even when your app is currently suspended and the user re-launches your app from a primary tile or app list. Apps can skip initialization if there is already content set on the current window. You can check the LaunchActivatedEventArgs.TileId property to determine if the app was launched from a primary or a secondary tile and, based on that information, decide whether you should present a fresh or resume app experience.

Related topics

How to suspend an app
How to resume an app
Application lifecycle
Guidelines for app suspend and resume