title: Windows Phone 7: Difficulty with app opening/closing events on emulator
body: <p>I'm experiencing odd behavior in the wp7 emulator.</p>
  
  <p>I have a dead simple app that's mostly directly from the template generated by VS 2010.</p>
  
  <p>From <strong>App.xaml</strong>:</p>
  
  <pre><code>    &lt;!--Required object that handles lifetime events for the application--&gt;
      &lt;shell:PhoneApplicationService 
          Launching="Application_Launching" Closing="Application_Closing" 
          Activated="Application_Activated" Deactivated="Application_Deactivated"/&gt;
  </code></pre>
  
  <p>Tombstoning code from <strong>App.xaml.cs</strong>:</p>
  
  <pre><code>    private void LoadSettings()
      {
          IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
  
          ICollection&lt;TicTacSpot&gt; getSpots;
          if (settings.TryGetValue&lt;ICollection&lt;TicTacSpot&gt;&gt;("spots", out getSpots))
          {
              Spots = getSpots;
          }
  
          if (Spots == null)
          {
              Spots = new List&lt;TicTacSpot&gt;();
          }
      }
  
      private void SaveSettings()
      {
          IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
          settings["spots"] = Spots;
      }
  
      // Code to execute when the application is launching (eg, from Start)
      // This code will not execute when the application is reactivated
      private void Application_Launching(object sender, LaunchingEventArgs e)
      {
          LoadSettings();
      }
  
      // Code to execute when the application is activated (brought to foreground)
      // This code will not execute when the application is first launched
      private void Application_Activated(object sender, ActivatedEventArgs e)
      {
          LoadSettings();
      }
  
      // Code to execute when the application is deactivated (sent to background)
      // This code will not execute when the application is closing
      private void Application_Deactivated(object sender, DeactivatedEventArgs e)
      {
          SaveSettings();
      }
  
      // Code to execute when the application is closing (eg, user hit Back)
      // This code will not execute when the application is deactivated
      private void Application_Closing(object sender, ClosingEventArgs e)
      {
          SaveSettings();
      }
  </code></pre>
  
  <p>Seems straightforward enough. I set breakpoints in all these methods. When I hit F5 to deploy the app, the event handlers that are hit are:</p>
  
  <p><code>Application_Launching()</code>
  <code>Application_Deactivated()</code></p>
  
  <p>Oddly, these are hit, even though the emulator doesn't show the app opening or closing.</p>
  
  <p>In the emulator, I then open the app, play around, close it, then re-open it. I use both the "back" and "start" buttons to close it. Despite this, I am unable to get any event handlers to be hit again.</p>
  
  <p>What am I doing wrong here?</p>