Reputation: 10135
I've overridden the Page.OnNavigatedFrom()
method in a UWP app. The method is called when I navigate to another Page. According to the documentation, Page.OnNavigatedFrom()
is:
Invoked immediately after the Page is unloaded and is no longer the current source of a parent Frame.
However, when I terminate the app, the Page's OnNavigatedFrom()
is not called. Shouldn't terminating the app unload the Page?
// Not called when app is terminated
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
}
I appreciate the comments. To be more clear, I should have said that I was closing the app, not terminating the app. (Closing the app first suspends and then terminates the app.) I discovered that putting a call to Frame.GetNavigationState()
in OnSuspending()
caused OnNavigatedFrom()
to be called even when the user closes the app:
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
// Triggers currently loaded Page's OnNavigatedFrom
Frame frame = Window.Current.Content as Frame;
ApplicationData.Current.LocalSettings.Values["NavigationState"] =
frame.GetNavigationState();
deferral.Complete();
}
Upvotes: 1
Views: 1011
Reputation: 32785
Invoked immediately after the Page is unloaded and is no longer the current source of a parent Frame
I think you misunderstood the document. OnNavigatedFrom()
method will invoked when page unloaded and is no longer the current source of frame. It is page life cycle only available in the scenario of navigation stack.
But, terminate is the app life cycle concept. and its priority is higher than the page life cycle.
When you terminate uwp app, OnSuspending
event handler will be invoked, if you want to save page data, you could subscribe this event in page class.
Upvotes: 4