title: Windows Phone 7: Tombstoning with URIs?
body: <p>I'm building a wp7 app in Silverlight. All of my app's state is stored in <code>NavigationContext.QueryString</code>. If this could be saved on application deactivation, and that page navigated to on application reactivation, that would take care of my requirements for tombstoning.</p>
  
  <p>However, I'm not quite sure how to do this. I was thinking about saving <code>NavigationContext.QueryString</code> to the <code>State</code> dictionary in <code>App.xaml.cs::Application_Deactivated()</code>, but that code doesn't have access to <code>NavigationContext.QueryString</code>. Is there another way I can do this? </p>
  
  <p>I suppose I could just save the query string to the State dictionary every time I navigate, then restore that when the app is re-activated. Or is there a better approach?</p>
  
  <p><strong>Update</strong>: Based on <strong>indyfromoz</strong>'s answer, I'd like to implement the following</p>
  
  <pre><code>OnNavigatedToHandler()
  {
       // save NavigationContext.QueryString in the State dictionary
  }
  </code></pre>
  
  <p>To reduce redundancy, I thought I'd implement this in a class that inherits from <code>PhoneApplicationPage</code>, then have all the rest of my pages inherit from that class. However, I then get the problem that all the page classes are <code>partial</code> because they are also defined in generated code. I don't want to change the generated code, because rechanging it every time it gets regenerated would be a huge pain.</p>
  
  <p>Is there a better way to do this?</p>
  
  <p><strong>Update 2</strong>: Here is what I am hacking together now in the main page of my app (the one that is navigated to on startup):</p>
  
  <pre><code>public partial class MainPivot : PhoneApplicationPage
  {
      public MainPivot()
      {
          InitializeComponent();
          Loaded += new RoutedEventHandler(MainPivot_Loaded);
          PhoneApplicationService.Current.Deactivated += new EventHandler&lt;DeactivatedEventArgs&gt;(App_Deactivated);
  
          MessageBox.Show("launching main pivot (state count: " + PhoneApplicationService.Current.State.Count + ")");
          if (PhoneApplicationService.Current.State.Count != 0)
          {
              Debug.Assert(PhoneApplicationService.Current.State.ContainsKey(QueryStringKey), 
                  "State is initialized, but contains no value for the query string");
  
              string oldQueryString = (string)PhoneApplicationService.Current.State[QueryStringKey];
              MessageBox.Show("Old query string: " + oldQueryString);
              NavigationService.Navigate(new Uri(oldQueryString));
          }
      }
  
      public readonly string QueryStringKey = "queryString";
  
      void App_Deactivated(object sender, DeactivatedEventArgs e)
      {
          PhoneApplicationService.Current.State[QueryStringKey] = NavigationService.Source;
      }
  
      // ...
  </code></pre>
  
  <p>It works (sorta) but it's ugly.</p>
  
  <p><strong>Update 3</strong>: Looks like the wp7 OS will automatically reload the correct page in a page-based app. I am in fact using pages, so perhaps there's not that much work I need to do here.</p>
  
  <p>However, it doesn't seem to be working. I launch the app, go to a page, hit "Start", then hit "Back". The screen says "Resuming..." but seems to hang there. Is my code supposed to be responding in some way at this point? Is there a way I can keep the debugger attached even after hitting "Start"?</p>