Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Tombstoning and Back key properly for performance reasons?

Is there a best practice for handling tombstoning and back key properly?

As it is stated in the MSDN docu you should save transient data in the OnNavigatedFrom method. Ok, so the code for saving states when tombstoning is clear.

But now if you press the back key the OnNavigatedFrom method is also called and if you don't add extra checks, you will first save states into the dictionary and shortly after that the page will be destroyed. And so will the PhoneApplicationPage.State dictionary. So the saving code is completely wasted CPU, disk and battery time.

This is what I've done to prevent it:

    protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
    {
        // when navigating back
        if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
        {
            backKeyPressed = true;
        }
    }

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (backKeyPressed)
        {
            // Don't save states on back key!
            backKeyPressed = false;     // set it anyway
            return;
        }

        // Tombstoning
        // save objects
        this.SaveState("text", someText);
        ...
    }

As a reminder: OnNavigatingFrom will only be called when navigating away/back from the page but not when app gets tombstoned.

Side note: Shown code covers only pages that can only navigate back. Thats why I added backKeypressed to OnNavigatingFrom. You need extra checks if the page can navigate to another page.

  1. Is there a better way to do this for every page you create?
  2. Now do I really have to add the backKeyPressed variable and check on every page I create?
  3. Shouldn't the framework provide something for us developer so we don't have to worry much about this?

What are your thoughts about this?

EDIT:

Updated question the make it clearer.

like image 507
Buju Avatar asked Oct 25 '22 15:10

Buju


2 Answers

your approach of checking the direcetion of navigation in OnNavigatingFrom is indeed the recommended practice in order to avoid the unneccessary performance hit of saving state just before the page gets removed from the backstack.

There is one clarification I want to add to your sample code: You should check the 'NavigationMode' property in the 'NavigationCancelEventArgs' to determine whether it's a forward or backward navigation.

Then only save the state in OnNavigatedFrom if it was a forward navigation, as your sample shows. This will help improve your performance when the user navigates backwards.

like image 116
Stefan Wick MSFT Avatar answered Nov 09 '22 07:11

Stefan Wick MSFT


Everything you ever needed to know about tombstoning is covered in Jeff Prorise's 4-part Real-World Tombstoning in Silverlight for Windows Phone 7 blog post series. You may want to pay particular attention to part 2 where Jeff talks about clearing up state when the application quits.

like image 36
Derek Lakin Avatar answered Nov 09 '22 07:11

Derek Lakin