开发者

Handling Tombstoning and Back key properly for performance reasons?

开发者 https://www.devze.com 2023-02-21 22:41 出处:网络
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 stat

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("te开发者_StackOverflowxt", 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消