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.
From the official App lifecycle documentation:
A note about debugging using Visual Studio: Visual Studio prevents Windows from suspending an app that is attached to the debugger. This is to allow the user to view the Visual Studio debug UI while the app is running. When you're debugging an app, you can send it a suspend event using Visual Studio. Make sure the Debug Location toolbar is being shown, then click the Suspend icon.
That means that the OnSuspending
event won't get fired while you are attached to the Visual Studio debugger. If you want to debug it, manually send the event by selecting the respective Lifecycle Event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With