Detecting deactivation and app close in Windows Phone 8.1 XAML - c#

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

Related

Windows Phone App Restarts instead of Resumes although it Appears in Task List

Controlling suspending, resuming and starting Windows Phone Apps with Windows RT, there are 3 events:
OnLaunching -> OnLaunched respectively
OnSuspending
OnResuming
Normally if an app launches, OnLaunching is fired.
When the app suspends e.g. the screen saver comes up, OnSuspending is fired.
If I go back to the app by opening task list, OnResuming is fired.
Now I have the following problem.
If I switch to another app or the screen saver appears, OnSuspending is fired - this is OK.
But if I go back to my app via task list after about 15-20 minutes, the OnLaunching event is fired instead of OnResuming, although my app appears still in the task list.
It's like the app is newly started !
This happens since I redesigned the app in Windows 8.1 RT.
In the earlier version of my app, which was running in Silverlight, this worked fine. Whenever I went back to my app via task list, OnResuming was fired.
Doing some tests, I found out the following:
Launching an app, the previous execution state can be read from the ActivateEventArgs. So I read them out to see, what application status was before launching.
After starting the app the first time, its state is 'notRunning'.
If I close the app and reopen it, the state is 'closedByUser'.
If the app has been suspended and I revoke it after 15-20 minutes, the state is 'terminated'.
However, the state 'terminated' means, the windows has been closed the app.
Why does windows close my app already after 15-20 minutes and why the app is still in the task list if it has been closed ?
Do you have an idea, what the problem is ?
I was told by Microsoft:
The OS can terminate an app in order to free up resources. But such an up can still appear in the task bar.
If the suspended app was terminated, there is no Resuming event and instead OnLaunched() is called with an ApplicationExecutionState of Terminated. So, the routines to restore the app state must be called within OnLaunched().
Sometimes it's required to distinguish if the app has been relaunched by tasklist or by tile. For this, use the TileId property in the OnLaunched(). TileId is "App" if the app has been started by tile and it is empty if the app has been relaunched by tasklist. TileId could also have another value if you generate eg. a second tile for the app giving them another id.
However, this does not work in Windows 8.1 as the value of TileId is always "App" independent of starting it by tasklist or standard tile.

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.

Windows Phone 8.1 RT app back navigation issue after using DataTranferManager

In my app (WP 8.1 RT) I use the DataTransferManager to share content. After launching the sharing UI, the app returns back to the page(2nd page of the app). And then, when I press the back button it's suppose to go back to the previous page(MainPage) but instead, its exiting the app. Back buttonn is working properly before launching the sharing UI. And this doesn't happen when debugging.
This is the order of the events happening.
Page1(main) -> Page2 -> (Calling DataTransferManager)SharingUI -> Backto Page2. Then back button exits the app.
What could be the cause?
I can't reproduce this issue. How do you handler back button pressing? Try using NavigationHelper instead of using custom BackPressed handler. I hope it will help you.

Windows 8 app lifecycle management: "SaveState" not firing

I'm trying to stop my webcam preview when my app suspends (because it doesn't like resuming).
I'm following the 'Getting Started' guide on MSDN here
I've added await mediaCaptureMgr.StopPreviewAsync(); to the SaveState async method (that's used in example apps).
This works perfectly using Visual Studio's 'Suspend' option (in the Debug Location toolbar) but SaveState doesn't fire when the app is suspended legitimately by switching apps in Windows itself.
I've tested this with the default grid project and the same thing happens (fires when 'Suspending' with VS but not by switching away from it)
Am I supposed to be doing this a different way?
Cheers,
Nick
App suspension does not occur immediately after an app is no longer visible. There is a delay of about 10 seconds before an app is suspended. This (and more detailed) information can be found in this Application lifecycle article.
Also, Task Manager provides a convenient way to view if an app has been suspended. By default, the suspended status is not visible, but can be enabled as shown below.

Categories