Windows Store RT: Save game/application state when closing app via close gesture or Alt+F4

If you has already family with Windows Phone development. You may often saving application/game state by overriding MainPage’s OnNavigatedFrom event. But with Windows Store app (Windows RT). The event is not fire when user is closing application by “close gesture” or Alt+F4 keyboard function.

So the question is how to save application/game state in that case?

According to MSDN “When the user closes an app by pressing Alt+F4 or using the close gesture, the app is suspended for 10 seconds and then terminated. Windows notifies your app when it suspends it, but doesn’t provide additional notification when it terminates the app. That means your app must handle the suspended event and use it to save its state and release its exclusive resources and file handles immediately.”

Meaning that you should implement your code in the suspending event. So the process of saving application/game state when use close app via close gesture/Alt+F4 is:

  1. Handle Suspending event in the App.xaml.cs constructor. In the Suspending event, saving the application/game state to Application Data (ex: Windows.Storage.ApplicationData.Current.LocalFolder)
    public App()
                this.Suspending += OnSuspending;
    private async void OnSuspending(object sender, SuspendingEventArgs e)
                var deferral = e.SuspendingOperation.GetDeferral();
                //TODO: Save application state and stop any background activity
                await SuspensionManager.SaveAsync();
                await SaveGameState();
                await UpdateTile();
     async System.Threading.Tasks.Task SaveGameState()
            //Save application or game state to Windows.Storage.ApplicationData.Current.LocalFolder here
  2. In the MainPage.xaml.cs load the state from Application Data in the OnNavigatedTo event.
     protected async override void OnNavigatedTo(NavigationEventArgs e)
                //Load application/game state from Windows.Storage.ApplicationData.Current.LocalFolder here
                await LoadGameState();

This Post Has 0 Comments

  1. Ahmed

    Excellent way of explaining, and good piece of writing to get information on the topic of my presentation subject,
    which i am going to convey in college.

  2. Sherryl

    Wonderful goods from you, man. I’ve be aware your stuff previous to and you
    are just extremely excellent. I actually like what you have
    acquired here, really like what you’re stating and
    the way during which you are saying it. You make it entertaining and you continue to care for
    to stay it sensible. I can’t wait to read far more from you.

    That is really a great site.

  3. You really make it seem so easy with your presentation but
    I find this topic to be really something which I think I
    would never understand. It seems too complicated and extremely broad for me.

    I am looking forward for your next post, I will
    try to get the hang of it!

Leave a Reply