title: WP8: Fast app resume + secondary tile + MainPage = 2 instances
body: <p>I'm having the same problem posed here:</p>
  
  <p><a href="http://social.msdn.microsoft.com/Forums/wpapps/en-us/af8615e7-8e90-4069-aa4d-3c4a84a6a3d0/windows-phone-8-fast-app-resume-with-deeplinks?forum=wpdevelop" rel="nofollow">http://social.msdn.microsoft.com/Forums/wpapps/en-us/af8615e7-8e90-4069-aa4d-3c4a84a6a3d0/windows-phone-8-fast-app-resume-with-deeplinks?forum=wpdevelop</a></p>
  
  <p>I'm no C# or WP expert, so please bear with me.</p>
  
  <ul>
  <li>I have secondary tiles which link to "/MainPage.xaml?id=XX". </li>
  <li>I have fast app resume enabled. (ActivationPolicy="Resume" in the app manifest)</li>
  <li>I only have one page in my app: MainPage.xaml.</li>
  </ul>
  
  <p><strong>Problem</strong>: When I resume the app using a secondary tile ("/MainPage.xaml?id=XX"), I get a brief view of the previous instance (that would have resumed) and then the MainPage initializes again, creating a new instance. In effect, the app is loading from scratch after giving me a peek of what was previously open.</p>
  
  <p>That is obviously undesired behavior. I want to use the existing instance to perform my task.</p>
  
  <hr>
  
  <p><em><strong>Attempt 1</em></strong>: 
  Use <code>e.Cancel = true;</code> to cancel the navigation to the MainPage.xaml:<br>
  <em>(using the App.xaml.cs code from the official <a href="http://code.msdn.microsoft.com/wpapps/Fast-app-resume-backstack-f16baaa6" rel="nofollow">Fast App Resume sample</a> to identify how the app was launched)</em></p>
  
  <pre><code>...
  else if (e.NavigationMode == NavigationMode.New &amp;&amp; wasRelaunched)
  {
    // This block will run if the previous navigation was a relaunch
    wasRelaunched = false;
  
    if (e.Uri.ToString().Contains("="))
    {
      // This block will run if the launch Uri contains "=" (ex: "id=XX") which
      // was specified when the secondary tile was created in MainPage.xaml.cs
      sessionType = SessionType.DeepLink;
  
      e.Cancel = true; // &lt;======================== Here
  
      // The app was relaunched via a Deep Link.
      // The page stack will be cleared.
    }
  }
  ...
  </code></pre>
  
  <p><em><strong>Problem</em></strong>: In doing so, my OnNavigatedTo event handlers never fire, so my query string is never parsed.</p>
  
  <pre><code>protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
  {
    String navId;
    if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
    {
      if (NavigationContext.QueryString.TryGetValue("id", out navId))
      {
        MessageBox.Show(navId.ToString()); // Not reached
      }
    }
    ...
  </code></pre>
  
  <hr>
  
  <p><em><strong>Attempt 2</em></strong>: 
  Use <code>e.Cancel = true;</code> to cancel the navigation to the MainPage.xaml, <strong><em>AND</em></strong> pass the Uri to a method in MainPage:  </p>
  
  <pre><code>// App.xaml.cs
  ...
  else if (e.NavigationMode == NavigationMode.New &amp;&amp; wasRelaunched)
  {
    // This block will run if the previous navigation was a relaunch
    wasRelaunched = false;
  
    if (e.Uri.ToString().Contains("="))
    {
      // This block will run if the launch Uri contains "=" (ex: "id=XX") which
      // was specified when the secondary tile was created in MainPage.xaml.cs
      sessionType = SessionType.DeepLink;
  
      e.Cancel = true;
  
      MainPage.GoToDeepLink(e.Uri); // &lt;======================== Here
  
      // The app was relaunched via a Deep Link.
      // The page stack will be cleared.
    }
  }
  ...
  
  // MainPage.xaml.cs
  public static void GoToDeepLink(Uri uri) // &lt;======================== Here
  {
    // Convert the uri into a list and navigate to it.
    string path = uri.ToString();
    string id = path.Substring(path.LastIndexOf('=') + 1);
  
    MyList list = App.ViewModel.ListFromId(Convert.ToInt32(id));
    pivotLists.SelectedItem = list;
  }
  </code></pre>
  
  <p><em><strong>Problem</em></strong>: I get an error that <em>pivotLists</em> is non-static and thus requires an object reference. I think that in order to get this to work I'd need to create a new instance of MainPage (<code>MainPage newMainPage = new MainPage();</code>) and call <code>newMainPage.pivotLists.SelectedItem = list;</code> -- BUT I don't know how to use newMainPage instead of the existing one/replace it... or if that's something I want/won't cause further problems/complications.</p>
  
  <hr>
  
  <p>I don't know what the solution is to this problem, and I may be going in the completely wrong direction. Please keep all suggestions in simple terms with code examples if you can, I'm still learning.</p>
  
  <p>Thanks for any help.</p>