Handle application exit

后端 未结 1 1504
暖寄归人
暖寄归人 2021-01-15 23:13

I want to run some cleanup code(like unregistering scheduled notifications) when a user quits the application by using the Alt-F4 or swipe down gesture. Is there an

相关标签:
1条回答
  • 2021-01-15 23:29

    There is no special event that indicates that an app is being closed:

    There's no special event to indicate that the user has closed an app. After an app has been closed by the user, it's suspended and terminated, entering the NotRunning state within about 10 seconds. If an app has registered an event handler for the Suspending | suspending event, it is called when the app is suspended. You can use this event handler to save relevant application and user data to persistent storage.

    So you'll want to handle a suspend/resume instead. The gory details for handling a suspend are here, but here's a summary:

    1. Register for the checkpoint event that will tell your app that it's being suspended.
    2. Save whatever data you need to save in the event handler for that event.
    3. Release resources, suspend notifications, etc. in the event handler as well.

    On resume, you can check if the app was closed by the user using the ApplicationExecutionState enum. That may or may not be relevant to you, since there doesn't seem to be a way to differentiate why checkpoint event was fired and your only option is to save your state in the event handler no matter why it happened.

    There are additional suspend/resume guidelines here, and you may find this sample app helpful.

    0 讨论(0)
提交回复
热议问题