Windows 8 XAML Tips - Detect App Deactivation

By Fons Sonnemans, posted on
2412 Views 1 Comments

Detecting when an Windows 8 Store app is deactivated isn't as easy as you would expect. In the Windows Phone project templates you get the Application_Launching(), Application_Activated(), Application_Deactivated() and Application_Closing() methods in the App.xaml.cs. But those methods don't exists in the Windows Store project templates. There is an OnSuspending() method but you can't use it to detect deactivation. You use it to store your data when suspending the app. This will problably go off a few seconds later than deactivation.


To solve this problem I have used the Activated event of the CoreWindow class. This event can also be used to detect deactivation inspite of it's name. The WindowActivatedEventArgs has a WindowActivationState property of the type CoreWindowActivationState. This enum holds the values CodeActivated, Deactivated and PointerActivated. This event also is fired when the app is closed.


In the following example I have created an app in which a Timer updates a counter every second. In the constructor I have subscribed the page on the Activated event of the CoreWindow object using the GetForCurrentThread() method. The eventhandler stops the timer when the app is deactivead or closed. It restarts the timer when the app is activated again. The counter value is written to the LocalSettings on deactivation and close of the app. In the constructor of the MainPage the value is read from the LocalSettings so the app continues with the last used counter value.

public sealed partial class MainPage : Page {

    private DispatcherTimer _timer = new DispatcherTimer();
    private int _counter;
    private const string Key = "Counter";

    public MainPage() {

        this._timer.Interval = TimeSpan.FromSeconds(1);
        this._timer.Tick += _timer_Tick;

        object value;
        if (ApplicationData.Current.LocalSettings.Values.TryGetValue(Key, out value)) {
            _counter = (int)value;
            this.textBlockCounter.Text = _counter.ToString();

        CoreWindow.GetForCurrentThread().Activated += MainPage_Activated;

    void _timer_Tick(object sender, object e) {
        this.textBlockCounter.Text = _counter.ToString();

    void MainPage_Activated(CoreWindow sender, WindowActivatedEventArgs args) {

        if (args.WindowActivationState == CoreWindowActivationState.Deactivated) {
            Windows.Storage.ApplicationData.Current.LocalSettings.Values[Key] = _counter;
        } else {

The MainPage.xaml only contains a Grid and the TextBox which displays the counter value.


    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock x:Name="textBlockCounter"
                   HorizontalAlignment="Center" />

Closure and download

I hope you like my solution. You can download my code here.




All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.

Leave a comment

Blog comments

Ian Griffiths

09-Jul-2013 6:05
Some (myself included) did not find it obvious that deactivation doesn't necessarily mean your application has gone anywhere. For example, simply pressing WindowsKey+Tab (which brings up the list of Store apps currently running and lets you switch between them - sort of store app verion of Alt Tab) causes your application to be deactivated immediately, even if you don't actually switch to a different app! (You get reactivated again if the user decides not to switch anywhere.) Also, if snapping is in use, simply moving the focus to the other app will cause your app to be deactivated. If you want to know whether your application has been taken completely out of sight, then the core window also offers visibility events. Those turned out to do what I had initially thought activation events would do for me. (Makes sense with hindsight, it just wasn't what I first thought.)