question

LoganStach-1629 avatar image
0 Votes"
LoganStach-1629 asked RichardZhang-MSFT commented

Does Application.Suspend activate when the user intentionally closes the app in UWP?

I have a UWP app, and I want to save my data when the user closes the application. I created an OnSuspending event for when the app suspends and call my Write() method to save data within it. When I activate the Suspend event when debugging, the code seems to work as intended (that is, the code saves). However, whenever I press the close button in the top right corner and close my app, when I start debugging again, it doesn't have the saved data.

Is this expected behavior? I know that when debugging you have to call the Suspending events manually. Is it just because I'm debugging, and once I actually deploy my app it will save as intended?

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered RichardZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

When the application is closed, the Suspending event is indeed triggered. If the expected behavior is not achieved when the user closes the application, can you provide a minimum runnable demo to help us test?



At the same time, you can try another solution to the behavior of the user closing the application.

If you want to handle when the user closes the app, UWP can listen for events when the app is closing. Please follow the steps below:

1. modify the package.appxmanifest (with code mode).


<Package

...

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"

IgnorableNamespaces="... rescap">

...

<Capabilities>

...

<rescap:Capability Name="confirmAppClose"/>

</Capabilities>

</Package>


2. handle the CloseRequest event.

 SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += (s, e) =>
 {
     // do something...
 };

It should be noted that this function requires that your application has a minimum system version of 15063, and if you need to submit the application to the Microsoft Store, you need to explain why you use the API when submitting.

Thanks.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The suspending event does indeed seem to be triggering. When I debug my application and activate the Suspending lifecycle event, the code works as expected. However, when debugging, if I try to activate it by clicking the &#34;X&#34; button, the code does not. My question is, is this expected behavior? Or should the code be activating at this point?


0 Votes 0 ·

Hi, I created a new project and tested it. The specific test method is to write the current moment to LocalSettings in the OnSuspending callback, and read the value when the app starts again. I found that the value did write. The Suspending event was triggered. And what's the basis of the judgment that you cannot trigger? Can you provide code for my testing?

0 Votes 0 ·

Hello, does my reply solve your problem? Do you have any other questions?

0 Votes 0 ·

Hi, If you have any other questions, please feel free to contact us.

0 Votes 0 ·