Xamarin.forms: [Bug] Lifecycle events do not fire in UWP.

Created on 19 Jun 2020  路  7Comments  路  Source: xamarin/Xamarin.Forms

Lifecycle events do not fire on UWP.

Steps to Reproduce

  1. Create a Xarmin.Forms UWP app.
  2. Wire up OnSleep and OnResume with a logging statement or other code.
  3. Run while not attached to debugger.
  4. Minimize app, go to another app. Come back to app.
  5. Alternatively, lock the desktop while app is running and then unlock.

Note, we also tried updating to Prism.Unity.Forms 7.2.0 and updating to Unity 5.11.7 but that didn't seem to alleviate the issue.

This is not related as I'm not attached to the debugger, sending events from the debugger works:
https://xamarin.github.io/bugzilla-archives/46/46526/bug.html

Note the following Microsoft documentation:
https://docs.microsoft.com/en-gb/windows/uwp/launch-resume/app-lifecycle?redirectedfrom=MSDN#app-suspend

App suspend
When the user minimizes an app Windows waits a few seconds to see whether the user will switch back to it. If they do not switch back within this time window, and no extended execution, background task, or activity sponsored execution is active, Windows suspends the app. An app is also suspended when the lock screen appears as long as no extended execution session, etc. is active in that app.

https://docs.microsoft.com/en-gb/windows/uwp/launch-resume/app-lifecycle?redirectedfrom=MSDN#running-in-the-background

Running in the foreground
Running in the foreground means that your app's UI is visible.

The LeavingBackground event is fired just before your application UI is visible and before entering the running in foreground state. It also fires when the user switches back to your app.

Expected Behavior

Code in OnSleep or OnResume should run.

Actual Behavior

Code in OnSleep or OnResume doesn't run.

Basic Information

  • Version with issue: 4.7.0.968
  • Last known good version: uknown
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:

    • UWP: 18362

  • Affected Devices: Windows 10 Desktop
  • Nuget Packages: Prism.Unity.Forms 6.3.0, Unity 4.0.1

Workaround

Tried wiring up the following and this didn't work either:
Application.Current.Suspending += Current_Suspending;
Application.Current.Resuming += Current_Resuming;
Application.Current.LeavingBackground += Current_LeavingBackground;

lifecycleevents 4 UWP bug

All 7 comments

I've got the feeling that currently windows rarely suspends uwp applications so maybe the events didn't fire because they didn't actually happen.

edit: or perhaps I've just been seeing this bug for so long that I've assumed it is the normal behaviour...

It looks like the Xamarin.Forms code should be reacting to the EnteredBackground and LeavingBackground events rather than Suspending and Resuming events, from version 1607 of Windows 10

As a quick experiment, I added to my App.xaml.cs:

        EnteredBackground += (s, e) => { Application.Current?.SendSleep(); };
        LeavingBackground += (s, e) => { Application.Current?.SendResume(); };

It looks like the Xamarin.Forms code should be reacting to the EnteredBackground and LeavingBackground events rather than Suspending and Resuming events, from version 1607 of Windows 10

Thanks for the suggestion. I just tried that and I'm not seeing that event handler get hit either. Will add it to my report. However, I'm not able to find that SendResume() method.

image

Worst thing about troubleshooting this "resume" issue and searching online is you get a lot of tech resumes. 馃槅

I haven't checked, but it might be that you need to target a newer Windows build. I am targeting 18362.
If that is the case, it might require a check of UniversalApiContract level before deciding what to do.

@johnshardman It looks like that event only fires when I minimize the app and not just switch to a different app when it's not minimized. That seems inline with the documentation mentioned above regarding App Suspend. However, there seems to be a lifecycle event not being handled here:

https://docs.microsoft.com/en-gb/windows/uwp/launch-resume/app-lifecycle?redirectedfrom=MSDN#running-in-the-background

Running in the foreground
Running in the foreground means that your app's UI is visible.

The LeavingBackground event is fired just before your application UI is visible and before entering the running in foreground state. It also fires when the user switches back to your app.

We are targeting 18362 as well:
image

We recently took on updating Prism and Unity and tried updating to Prism.Unity.Forms 7.2.0 and updating to Unity 5.11.7 but that didn't seem to alleviate the issue. I've updated the report above with this info.

Was this page helpful?
0 / 5 - 0 ratings