Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling Tombstoning and Back key properly for performance reasons?
    primarykey
    data
    text
    <p>Is there a best practice for handling tombstoning and back key properly?</p> <p>As it is stated in the MSDN docu you should save transient data in the <code>OnNavigatedFrom</code> method. Ok, so the code for saving states when tombstoning is clear.</p> <p>But now if you press the back key the <code>OnNavigatedFrom</code> 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 <strong>wasted CPU, disk and battery time</strong>.</p> <p>This is what I've done to prevent it:</p> <pre><code> 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); ... } </code></pre> <p>As a reminder: <code>OnNavigatingFrom</code> will only be called when navigating away/back from the page but not when app gets tombstoned. </p> <p>Side note: Shown code covers only pages that can only navigate back. Thats why I added backKeypressed to <code>OnNavigatingFrom</code>. You need extra checks if the page can navigate to another page.</p> <ol> <li>Is there a better way to do this for every page you create? </li> <li>Now do I really have to add the <code>backKeyPressed</code> variable and check on every page I create? </li> <li>Shouldn't the framework provide something for us developer so we don't have to worry much about this?</li> </ol> <p>What are your thoughts about this?</p> <p>EDIT:</p> <p>Updated question the make it clearer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload