xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Lifecycle events do not fire in UWP. #11136

Open mnxamdev opened 4 years ago

mnxamdev commented 4 years ago

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

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;

MitchBomcanhao commented 4 years ago

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...

johnshardman commented 4 years ago

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(); };
mnxamdev commented 4 years ago

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

mnxamdev commented 4 years ago

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

johnshardman commented 4 years ago

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.

mnxamdev commented 4 years ago

@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

mnxamdev commented 4 years ago

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.