Execute code when exiting Windows 8.1 App - c#

I'm attempting to execute some code on my Windows 8.1 app on exit, when a user drags the app down normally (not the extended hold to terminate).
In Windows 8, I used the following code to clear the app's tile on exit:-
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication().Clear();
}
However, this code isn't called in Windows 8.1 when the way the app terminate changes.
How do I recreate my code for Windows 8.1?

In 8.1, the close gesture no longer kills the app. Instead you have to hold it at the button for 1 second and when the icons flips, the app is closed.
Look if the code is executed there.

Related

How to know the app suspension reason in WinRT app?

In my Universal app, (Windows 8.1 Windows Phone8.1), I want to know the reason why my app is suspended like due to a launch of a launcher(Mail, File picker etc) or user pressed the Windows button. Is there a way to determine this?
Unfortunately you can't know what caused the app's suspension. Unlike the LaunchActivatedEventArgs in the OnLaunched method for example, that contain the Kind property the SuspendingEventArgs in the OnSuspending method don't provide any such information.
However, there are only so many reasons that an app can be suspended. Like you said (in windows phone only) it can be suspended due to a launch of a launcher or a protocol and by the windows button.
The windows button is the only suspension way of which you have no control. For all the others you can trick the system and for example set a static global variable that you update when you launch an operation that would suspend your app and check it in OnSuspending

OnNavigatedFrom in Window Phone 8.1

I am moving from Windows Phone 8 to Windows Phone 8.1.
I Created a Windows Phone 8.1 Store Application, Hub App.
The application created the OnNavigatedTo and OnNavigatedFrom methods
protected override void OnNavigatedTo( NavigationEventArgs e )
{
this.navigationHelper.OnNavigatedTo( e );
}
protected override void OnNavigatedFrom( NavigationEventArgs e )
{
this.navigationHelper.OnNavigatedFrom( e );
}
I put a breakpoint in the OnNavigatedFrom and tried to either close the application, or to leave the application and the breakpoint is not hit, i.e. the application doesn't reach the OnNavigatedFrom.
A Windows Phone 8 application is breaking on the OnNavigatedFrom. Is the mechanism is different with WP 8.1? if so how?
Thanks.
The problem seems to occur, because you are running in Debug mode (VS attached). In this situation your program behaves little different in case Navigation/Suspend events, to test it properly you will have to invoke the Suspending event manually (Lifecyce events dropdown). In normal situation both events (OnNavigatedFrom and Suspending) will be called just after you leave the app.
To test it let's put something in OnNavigatedFrom (basing on Hub App from Windows Store templates):
protected async override void OnNavigatedFrom(NavigationEventArgs e)
{
Debug.WriteLine("OnNavigatedFrom");
Hub.Background = new SolidColorBrush(Colors.Red);
this.navigationHelper.OnNavigatedFrom(e);
}
in this case, when you run the app without Visual Studio attached, when you return to the app the background should be red - which means that the event has been fired.
There is, in fact, one more huge (IMO) difference when moving to WP8.1 WinRT - OnNavigatedTo won't be fired when you come back from suspension:
Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.
it's called only when you navigate.
Some more references: Navigating between pages, Lifecycle, Launching, resuming, and multitasking and Guidelines for app suspend and resume.

How to close a Windows Phone 8.1 app

In WP7 and WP8 I just needed to clear the backstack in a page, then press Back button and the app is closed. In WP8.1 I do Frame.BackStack.Clear(), press Back and the app just minimizes.. How to kill it with Back button?
You can add, in your main page definition:
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
Then
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (!e.Handled && Frame.CurrentSourcePageType.FullName == "YourApp.MainPage")
Application.Current.Exit();
}
Warning: As others said, you should not use this and let the system handle the app closure. For example, if you use the Application Insights, I found that they are not sent to Azure when in Release mode
I think the above has been depreceated. Exit is now an event.
Try
Application.Current.Terminate();
you can simply create a button by using XAML and then add this code into your Main page xaml.cs
Application.Current.Exit();
MSDN recommends to not close apps in Windows 8.1:
We recommend that apps not close themselves programmatically unless
absolutely necessary. For example, if an app detects a memory leak, it
can close itself to ensure the security of the user's personal data.
When you close an app programmatically, the system treats this as an
app crash.
https://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx#close

Detecting deactivation and app close in Windows Phone 8.1 XAML

For security reasons, I need to log out the users when they exit the app and show login screen when they return back.
In Windows Phone 8 and Windows Phone 8.1 Silverlight there are Application_Deactivated and Application_Closing methods on the App class (or methods OnClose, OnDeactivate to override in Caliburn.Micro).
The only interesting events seems to be Suspend and Resume, but they do not called when I exit the app using the Start button and get back using the Back button or launching the app from the list.
What are the alternatives for Windows Phone 8.1 XAML?
(Setting ActivationPolicy="Replace" would solve half of the problem but I guess this is not possible, when WMAppManifest.xml is not event a part of a Windows Phone 8.1 XAML project).
The Suspending event will be called just after you navigate away from the app, but not in debug mode. I've build a simple app modyfing LocalSettings upon Suspending event and then acquiring information when Resuming.
You are probably aware, but for the sake of completeness of the answer - some remarks:
before Suspending event, the OnNavigatedFrom event is being called, but when you Resume, the OnNavigatedTo is not called - reference:
Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.
to test Suspending/Resuming with debugger, use Lifecycle events in Debug location tab - more info
reference to Application lifecycle in Windows Runtime apps

capturing dump on Windows Phone 8.1

I am developing a Windows Phone 8.1 app. I want to add a functionality that whenever the app crashes a memory dump is captured and written to a log.
I want to know if there is any way to log the crash dump while the user is using the app on his phone and it crashes. I found this question which is similar to mine but is for Windows 8. It says that we can use the 'Application_UnhandledException' method in App.xaml.cs to obtain the dump. But is this method supported in Windows Phone 8.1 too because I didn't see this in the auto-generated content of App.xaml.cs(which is generated by Visual Studio and contains functions like OnActivated, OnLaunched etc.)
Does the UnhandledException event handler do this thing in Windows Phone 8.1?
The Silverlight 8.1 App.xaml.cs class has an UnhandledException event handler just like 8.0.
WinRT 8.1 apps on the other hand require you to add the handler yourself.
To do this, go to App.xaml.cs and in the constructor, add the following:
this.UnhandledException += App_UnhandledException;
Also add this event handler:
private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// Save the dump here.
}
Do you require explicit dump handling on your own? If you publish through Store you should already be able to access "dumps" (more like stack-traces) from your Store accounts quality page.
http://msdn.microsoft.com/en-us/library/windows/apps/hh967782.aspx
http://blogs.msdn.com/b/windowsstore/archive/2012/06/27/improving-apps-with-quality-reports.aspx

Categories