The OnSuspending event is not triggered for my UWP app but this problem only occurs on Windows Phone running Windows 10. It works as expected when running it as a Windows Store app on my local machine or the simulator.
I'm using this event to save my app's settings when the app is closing, but this is obviously causing a major problem for windows phone since this event is not triggered.
As you can see, the OnSuspending event is initialized when the app starts
public App()
{
Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
Microsoft.ApplicationInsights.WindowsCollectors.Session);
this.InitializeComponent();
this.Suspending += OnSuspending;
}
Below is the OnSuspending code that should be called but isn't when running in Windows Phone 10.
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
await Locator.MainPageViewModel.SaveSettings();
deferral.Complete();
}
Any ideas on how I can resolve this or is there a potential work-around?
Thanks.
UPDATE-1:
When I terminate my app by holding the flag key and click on the cross to terminate it, is closes the app but it still doesn't trigger the OnSuspending event, but the .net IDE still runs. When I press F5 to run the app again, it then triggers the OnSuspending event. My app starts but the code stops running in the IDE.