How to detect exit for UWP
Asked Answered
G

2

6

Is there any way to detect current UWP going to exit? (close by user or kill the process)

I want to send some request to the server about the application will disconnect, also want to save some data before it exit.

Ginn answered 8/6, 2016 at 5:20 Comment(1)
Um... After so much time longer, I think maybe I should tell some guys which still need to answer about it: Turns out Phone App is different than desktop software. So you can't detected it if user kill the app, but we could detect if App going to background by manually. (tha'ts why we could hear about : "don't kill the app by manually" too much), My advice is for who first time using phone app developing: Let server side do the detect about disconnect .(Like not get response from App for long time)Ginn
J
10

There is no way to detect such case or to prevent user from terminating your app. If you want to save the state or do something before exit, use Suspending event:

The Suspending event is the only indication your app will receive prior to termination (if it happens). Because of this, you should store enough session state (such as the current article being read or the current movie playback position) to recreate the exact same experience during activation. The guidance for content creation apps is to save a user’s work early and often but also commit one final save during Suspending. Saving data prior to suspension is useful because the Suspending event handler has only 5 seconds to complete its operation.

Just remember about limited time.

In fact there are two other events that will be fired (in mobile case, when user holds back button and goes to task switcher): Window.VisibilityChanged and Windows.Activated, but they are also fired when user change the app, show prompt and so on - and there is no way to distinguish those situations.

Jacky answered 8/6, 2016 at 5:28 Comment(6)
It would be very helpful if you could put some example code with it. :-)Ginn
@Ginn What kind of example code? There is nothing special, just subscribe to App.Current.Suspending event.Jacky
Code for check it. I want to know how do you know the application not just change to other application or go system home page, just exit. I just don't know how use visibility and activate for confirm for exitGinn
@Ginn As I've said in the first line - you are not able to detect it. The app will get Suspending event invoked at every case you have mentioned. Though as I think, you should handle this as the app might have been closed in the future. Remember that once app is suspended, then it can be closed by the system when it's low on resources and you won't get any event called in that case.Jacky
If I'm not able to do that, I think that's not the answer which you put in here. But if you think can you should at least give me the code not only the idea. Thank you.Ginn
@Ginn I still don't know what you are not able to do? As for the code, simple event subscription: App.Current.Suspending += yourEventHandler; // in the handler put your job to do, also if it's async, obtain a deferal. Also please remember that you won't be able to debug this without invoking events manually - while debugging PLM is disabled and in that case app behaves little different - you will have to use lifecycle tab.Jacky
J
7

To intercept close in MainPage of the app a restricted capability confirmAppClose was added in Windows 10 version 1703 (build 10.0.15063).

Add following code to your manifest:

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
...
<Capabilities> 
  <Capability Name="internetClient" /> 
  <rescap:Capability Name="confirmAppClose"/> 
</Capabilities> 

Then you can intercept CloseRequested event in SystemNavigationManagerPreview. You can get a deferral if your method requires some time to complete (i.e. save or prompt). Also, you can set Handled to true in order to stop the window from closing (i.e. user cancelled prompt).

public MainPage()
{
    this.InitializeComponent();
    SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += OnMainPageCloseRequest;
}

private void OnMainPageCloseRequest(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
{
    var deferral = e.GetDeferral();

    if (!saved) 
    {
        e.Handled = true; 
        SomePromptFunction(); 
    }

    deferral.Complete();
}

You can use Window.Current.Closed or ApplicationView.GetForCurrentView().Consolidated or CoreWindow.GetForCurrentThread().Closed or CoreApplication.Exiting for all other additional windows.

Unfortunately there is no way to intercept close by killing app's process.

Jacquesjacquet answered 24/6, 2020 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.