How to know the app suspension reason in WinRT app? - c#

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

Related

How i can maximize UWP after launch?

I'm launch my app when the user logs in to Windows. I have the appropriate Extension (StartupTask class) set in Package.appxmanifest, and I can get the app to launch when I log in to Windows, as expected. However, the app minimize after start. How i can his maximize after start?
I tried the ways from this thread
the documentation explains this:
UWP apps must call RequestEnableAsync from a UI thread to trigger a user-consent dialog. If the user consents, the UWP app will then start on startup or user log in. Note that UWP startup apps will start minimized.
Minimizing when starting the application is the default behavior. This is to ensure a good user experience for Store app by default. Currently, UWP does not provide related APIs to modify this behavior.

UWP How to hide the app?

In my application their is one option to hide the app. Whenever user clicks on hide the app window as well as the app icon from task bar should be get hidden.
How to achieve this in UWP?
This feature is not supported (yet) on UWP.
If you look at the UWP Skype, it always remain visible from the taskbar along with all the other apps. A UWP app cannot register by itself a global shortcut to be launched.
Minimized UWP applications are suspended by the OS when it wants/needs and will not be able to perform any job. If you hide your application you will be suspended and will not be able to perform any work. The only exception being if you request an extended execution time, your application will be allowed to run while minimized. They will still be limits but they are more battery/lock screen bounds.
If you really want to hide your app, you should just close your app window and rely on some background tasks to perform the work you might want to do when hidden/stopped.
This should be a good reading:
Windows 10 universal Windows platform (UWP) app lifecycle
Support your app with background tasks
Postpone app suspension with extended execution

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.

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

How to disable Suspending || Closing || Terminating event for my Metro Style App on Windows 8 Pro

I'm a french developer and I need to develop an Metro Style app for Windows 8 Pro who is always launched. I wanted to know how can I disable the close event of my app. My app need to be in front all the time and the user couldn't quit the app.
I thought I could disable all the shortcut with the GPO but the close gesture (drag the app from the top to the bottom) need to me disabled too.
I hope I was clear and everybody will understand the question :-). Feel free to ask me more specific questions.
Cordially Renaud.
The short answer is: You can't.
The operating system and the user control the lifetime of Metro Style apps, you can't block the user from switching away from your app and once your app is no longer in the foreground, the application is suspended and the system can terminate the application at any moment.
Similarly, the user close gesture cannot be blocked.
In Windows 8.1 you may find Kiosk Mode to be what you need.

Categories