Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP: Detect app gaining/losing focus

Tags:

c#

uwp

I want to be able to prevent the screen saver from triggering while my app is in use by using the DisplayRequest class, but I only want to do this while it's the active app. If the user switches to another window/app then I want to act like a good citizen and allow the screensaver again.

I can't see an obvious way to detect when a UWP app gets/loses focus (or is activated/deactivated) and a quick search around doesn't offer any insights. Can anyone point me in the right direction?

like image 870
Mike Goatly Avatar asked Jan 12 '16 22:01

Mike Goatly


3 Answers

It's actually quite simple:

Window.Current.Activated += Current_Activated;

private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
    if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
    {
        // do stuff
    }
    else
    {
        // do different stuff
    }
}
like image 122
Tamás Deme Avatar answered Nov 13 '22 22:11

Tamás Deme


We can use CoreWindow.Activated event to detect when a UWP app is activated or deactivated. This method is fired when the window completes activation or deactivation. And for a UWP app, we usually only have one window. So we can add some code like following in Application.OnLaunched method to detect:

Window.Current.CoreWindow.Activated += (sender, args) =>
{
    if (args.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
    {
        System.Diagnostics.Debug.WriteLine("Deactivated " + DateTime.Now);
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("Activated " + DateTime.Now);
    }
};

Besides this, you can also use CoreApplication.GetCurrentView method or CoreWindow.GetForCurrentThread method to get the CoreWindow.

like image 44
Jay Zuo Avatar answered Nov 14 '22 00:11

Jay Zuo


You should not handle the changes of app's activation state.

Please, refer to How to prevent screen locks in your UWP apps.

Automatic request handling

Without any additional coding, though, your RequestActive() call will also be deactivated when your app no longer has the focus—in other words, when your app goes into a suspended state. On Windows Phone mobile and on a Windows 10 PC or Surface Pro in Tablet mode, “your app no longer having focus” means whenever your app is no longer in the foreground (snapped apps still count as being in the foreground, by the way). On a Windows 10 PC in desktop mode, however, it will mean whenever your app is minimized. In desktop mode, note that even when another app window covers up your app window, your app is still considered by the UWP app lifecycle to be running in the foreground.

The really cool thing is that when your app comes back into the foreground or gets de-minimized, the RequestActive() call will automatically get reactivated. You don’t have to do a thing!

This also means that if another app happens to make a request to keep the display active, it cannot hijack the behavior of every other app on the same device. The request is only good as long as the user is working with that app. Once the app is dismissed or minimized, Windows 10 goes back to its regular power conservation habits, even if the app forgets to call RequestRelease().

Finally, when your app is terminated, all remaining display active requests are automatically cleaned up for you.

It's all made for your needs.

like image 5
Alexey Avatar answered Nov 13 '22 23:11

Alexey