locked
Which event can I have to use when I switch from my UWP app to another uwp or destop app and come back RRS feed

  • Question

  • Hi All,

    I am developing a UWP app in that I want to run a set of code whenever user get back to the app after switching from it.

    Here when user switching to another app it may be a UWP or desktop app that time my app still running and open but focus is not on it. When user switching back to my app want to run some set of code.

    Like how we are having a got focus and lost focus on a UI element, Similarly want to know do we have such kind of events for the app.

    I have used Got focus event on the page but it is not firing at all. Later I get to know that we have some events on Window.Current.CoreWindow in that first I used VisibilityChanged event but it is firing only when I minimise and maximise the app.

    Later I used PointerEntered event using this I am able to achieve my requirement. But I want to know is it the correct event I am using or do we have any other event which is more better than pointer entered.

    Note: It is an UWP app so I am targeting on multiple devices, I want an event that works on all targeted devices.

    Thanks.

    Saturday, March 26, 2016 11:12 AM

Answers

  • With the help of CoreWindow.Activated event also I can achieve my requirement. The below event fires in all device categories I think.

    Window.Current.CoreWindow.Activated += Current_Activated;

    https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.corewindow.activated.aspx?f=255&MSPPError=-2147217396

    [Old Proposal]

    After some research I came up with the following solution First I registered VisibilityChanged event for Mobile Device and PointerEntered for remaining devices

     if ((Windows.Foundation.Metadata.ApiInformation.IsTypePresent(("Windows.Phone.UI.Input.HardwareButtons"))))
                    {
                        Window.Current.CoreWindow.VisibilityChanged += CoreWindow_VisibilityChanged;
                    }
                    else
                    {
                        Window.Current.CoreWindow.PointerEntered += Current_PointerEntered;
                    }

    In mobile phone when I switch to another app my app visibility changes and when again user come back to the app the visibilityChanged event fires and I will execute some set of code under this event.

    When it comes to PC the visibilityChanged only runs when the user minimizes the app, but in my case the app will not minimize it still opens only but focus is on another app. In pc we can do multi tasking means both apps will open at a time side by side right.

    That's why here I registered PointerEntered event which will fire whenever user come the app means focus on the app.

    This way I achieved my requirement. If I am doing wrong please comment here.






    Saturday, March 26, 2016 11:13 AM
  • Application Activated and Suspended and probably what you want. [Edit] oops didn't see 'desktop'

    http://pauliom.wordpress.com


    • Edited by pkr2000 Monday, March 28, 2016 8:38 AM
    • Proposed as answer by Krunal Parekh Tuesday, March 29, 2016 6:12 AM
    • Marked as answer by Krunal Parekh Monday, April 4, 2016 10:01 AM
    Saturday, March 26, 2016 12:05 PM
  • Hello Narendra Macha,

    Please see: App lifecycle

    "The OnActivated event data includes a PreviousExecutionState property that tells you which state your app was in before it was activated. This property is one of the values from the ApplicationExecutionState enumeration."

    Please see: Handle app activation

    With Regards,

    Krunal Parekh 


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Tuesday, March 29, 2016 6:12 AM
    • Marked as answer by Krunal Parekh Monday, April 4, 2016 10:01 AM
    Monday, March 28, 2016 7:03 AM

All replies

  • With the help of CoreWindow.Activated event also I can achieve my requirement. The below event fires in all device categories I think.

    Window.Current.CoreWindow.Activated += Current_Activated;

    https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.corewindow.activated.aspx?f=255&MSPPError=-2147217396

    [Old Proposal]

    After some research I came up with the following solution First I registered VisibilityChanged event for Mobile Device and PointerEntered for remaining devices

     if ((Windows.Foundation.Metadata.ApiInformation.IsTypePresent(("Windows.Phone.UI.Input.HardwareButtons"))))
                    {
                        Window.Current.CoreWindow.VisibilityChanged += CoreWindow_VisibilityChanged;
                    }
                    else
                    {
                        Window.Current.CoreWindow.PointerEntered += Current_PointerEntered;
                    }

    In mobile phone when I switch to another app my app visibility changes and when again user come back to the app the visibilityChanged event fires and I will execute some set of code under this event.

    When it comes to PC the visibilityChanged only runs when the user minimizes the app, but in my case the app will not minimize it still opens only but focus is on another app. In pc we can do multi tasking means both apps will open at a time side by side right.

    That's why here I registered PointerEntered event which will fire whenever user come the app means focus on the app.

    This way I achieved my requirement. If I am doing wrong please comment here.






    Saturday, March 26, 2016 11:13 AM
  • Application Activated and Suspended and probably what you want. [Edit] oops didn't see 'desktop'

    http://pauliom.wordpress.com


    • Edited by pkr2000 Monday, March 28, 2016 8:38 AM
    • Proposed as answer by Krunal Parekh Tuesday, March 29, 2016 6:12 AM
    • Marked as answer by Krunal Parekh Monday, April 4, 2016 10:01 AM
    Saturday, March 26, 2016 12:05 PM
  • Hello Narendra Macha,

    Please see: App lifecycle

    "The OnActivated event data includes a PreviousExecutionState property that tells you which state your app was in before it was activated. This property is one of the values from the ApplicationExecutionState enumeration."

    Please see: Handle app activation

    With Regards,

    Krunal Parekh 


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Tuesday, March 29, 2016 6:12 AM
    • Marked as answer by Krunal Parekh Monday, April 4, 2016 10:01 AM
    Monday, March 28, 2016 7:03 AM