Reputation: 7710
I'm implementing a game in Silverlight that has many timers. I stop all these timers each time I navigate from this page.
In some cases, when I navigate from the game page to menu page, the game is still running and I hear sounds of the game, although the timers are all stopped.
So is there a way to kill a WP7 page completely when navigating from it?
Thanks.
UPDATE:
the scenario is as follows:
1- The player starts the game. 2- When the game ends, the player is navigated to results page. 3- When the player is in results page and presses back key, it will be handled as follows:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
NavigationService.Navigate(new Uri("/Pages/MainMenu.xaml", UriKind.Relative));
}
4- The problem is that the constructor of the game page is executed although I'm telling him to navigate to the main menu not to go back to the game page.
Upvotes: 1
Views: 195
Reputation: 789
There is no way for you to "kill" a page. If you are doing a forward navigation, then the page will remain (and continue to run) in the background so that you may go back to it using the back button. If you are doing a back navigation, the page will be garbage-collected at some point (assuming you have unhooked all your events, disposed of any unmanaged resources, etc.)
Update:
So the first thing I need point out is that you are misleading the user here. They are pressing the back button, but instead a forward navigation is done. Not only will this confuse the user at that moment, but what happens when they are on the MainMenu page and press the back button? Do they go back to the Results page (that would be the last thing in the backstack)? But you've already taking a detour out of the backstack, so what the user remembers the road back being and what it ends up being is already different. This is the type of thing that could cause your submission to fail (5.2.4.1 – Back button: previous pages).
As far as number 4 goes, navigating back to a page will not call the constructor. That will only be called when page is first created. A backwards or forwards navigation to that page, on the other hand, will call the OnNavigatedTo method, so check to see what work is being done there.
Here is my recommendation to fix both problems: when the game ends and the user has been navigated to the Results page, remove the Game page from the backstack entirely using the NavigationService.RemoveBackEntry(). This will allow the page to be picked up by the garbage collector (assuming it doesn't have any other objects holding a reference to it (see above)) AND will no longer be in the path the user takes when they press the back button. If the previous page is the MainMenu page (I'm assuming it is, based on the name), then your problem is solved. If it is not, it may be time to rethink your navigation path...
Upvotes: 2