I'm having a weird issue (or maybe it is expected behavior that I am misunderstanding) with my Toast notification. All I want is to have the toast navigate back to the same page that the user was on when they hit the Start button.
Scenario:
User opens app, they are shown Page1.xaml. User taps button to navigate to Page2.xaml. From here, they hit the Start button. Here is the Toast notification I set up on Page2.xaml.cs:
Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
toast.Content = "App is still running!";
toast.Title = AppResources.ApplicationTitle;
toast.NavigationUri = new Uri("/Pages/Page2.xaml?fromToast=true", UriKind.Relative);
toast.Show();
I'm passing fromToast
on the querystring so that I can handle "resuming" correctly. However, when I tap the Toast notification that shows up, my OnNavigatedTo
event fires twice on Page2.xaml.
The first time OnNavigatedTo
fires on Page2.xaml after tapping the Toast, there are no query string parameters, but then right after, it fires again with my fromToast
param.
Why is it firing twice? Is this expected? If I remove the NavigationUri
property from my toast
, tapping the Toast only takes the app back to Page1.xaml which is not what I want.
Has anyone seen this before?
This is the normal behavior when Fast Resume
is enabled. You have to understand how it works to decide how you want to handle user experience. From the MSDN site:
With Fast Resume, when an app is resumed, the system creates a new page instance for the target of the launch point and this page is placed on top of the app’s existing backstack.
So basically tapping on the toast will cause a new instance of Page2 be created and hence NavigatedTo
being called with no query strings. Since your toast has a deep link, another navigation will be requested causing another instance of Page2 be created and the NavigatedTo
called with fromToast=true
query string.
You can determine the navigation mode by inspecting e.NavigationMode
property. It should be NavigationMode.Reset
the first time then NavigationMode.New
on the second time.
Also be aware that by default launching an app from a toast will clear the backstack so you'll put your users into an infinite loop of toasts.
A good read on how fast resume works and how to handle different scenarios is here: Fast app resume for Windows Phone 8
My advice is that you don't really need to tell the user the app is still running. This is the default behavior of apps in WP and most users already know that.