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

Logan Stach 41 Reputation points

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?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points


    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).**




    IgnorableNamespaces="... rescap">




    <rescap:Capability Name="confirmAppClose"/>



    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.