How to detect when user has run WP7 XNA Application again while deactivated?

115 views Asked by At

I have a Windows Phone 7 game written with XNA 4.0 in C#.

It seems that the Game.OnExiting method can be avoided if the user does the following:

  1. Starts game through Tile in Windows Phone OS.
  2. During gameplay, press the WINDOWS button to leave the game (resulting in a call to Game.OnDeactivated but not Game.OnExiting).
  3. User starts the game again through the Tile in Windows Phone OS.

I have read much on the WP7 App Lifecycle, but haven't been able to find anything touching on this case where the user runs the same app twice in a row.

I have some logic that only makes sense to be called if the game is shutting down, and is also important that it isn't missed (such as in the case above). I cannot call it in OnDeactivated because it would cause a poor user experience.

Here is the code in question (found inside my implementation of the Game class):

protected override void OnExiting(object sender, EventArgs args)
{
    // This is what is getting missed!
    ForceSave();

    base.OnExiting(sender, args);
}

protected override void OnDeactivated(object sender, EventArgs args)
{
    base.OnDeactivated(sender, args);

    // I don't want to put ForceSave here because it will get called even if the user is not exiting the game.

    ...
}

protected override void OnActivated(object sender, EventArgs args)
{
    base.OnActivated(sender, args);

    ...
}
1

There are 1 answers

4
pinckerman On BEST ANSWER

I don't think that you can handle that case.
I don't know what is your game about, but the easiest solution is to call ForceSave (I suppose it's a saving method) at the end of a level or before showing another menu, you can't save user's progress only before closing the application. Sometimes the game could crash with no reason and you have to prevent this.
In a nutshell, you have to revise your save logic.