You need to fully background the app to reproduce the issue. If you simply preview the horizontal stack of running apps and bring your app back to the foreground, then the issue does not exist.
This was tested on XS / 12.1. I suspect that OnActivated is being fired twice in FormsAplicationDelegate.
_Edit:_ I just tested iPhone 8 / 12.1, and the issue does not exist there, so this might be scoped to the new models (without the physical home button). I also noticed OnSleep seems to be firing twice as well.
OnResume should fire only once. There are some legitimate reasons for OnActivated to be called multiple times. See here, but the test code isn't doing any of those.
Also, WillEnterForeground is NOT firing sometimes when the app is foregrounded. Why?
https://bugzilla.xamarin.com/show_bug.cgi?id=59980
https://forums.xamarin.com/discussion/32272/onresume-event-fires-twice-on-android
https://forums.xamarin.com/discussion/82322/onresume-is-being-called-multiple-times-when-app-is-resumed-ios
iOS works for me. Tried a dozen times. Only called once. The 82322 comment says, "I'm using 2.3.2.127". The 32272 is reporting for Android. 59980 reports the Android issue but, like Paul, I'm unable to reproduce it. 12/12 restarts and counters on both iOS XS 12.1/Android.
I believe you're seeing this, but we'll need a reproduction to fix it... How many restarts do you have to preform before it manifests?
@kingces95 Sorry, I forgot to mention that the issue happens when you click the app icon on the home screen when foregrounding the app, and you only need to do this a few times to reproduce the issue. I updated the sample code so that the counter is displayed on the screen. I provided the other URLs since people had a similar issue in the past (not because they are necessarily the same issue as the one I reported).
Put a breakpoint on these methods in AppDelegate
public override void WillEnterForeground(UIApplication uiApplication)
{
base.WillEnterForeground(uiApplication);
}
public override void OnActivated(UIApplication uiApplication)
{
base.OnActivated(uiApplication);
}
Usually, we'd expect to see WillEnterForeground get called before OnActivated, but sometimes this method does not fire. When that happens, OnActivated fires twice. Again, you'd need to be tapping on the app icon, and try to switch between background and foreground as fast as possible.
Like this? I get no repro in a dozen tries.. maybe I'm holding it wrong..
Yeah, it should be that way, but I reproduced it on an actual device - not a simulator.
@samhouts Mr Knight says I gotta go buy myself a spanking new iPhone to reproduce this issue... ;)
@samhouts says she's got a new phone and will test it...
Well, I never saw the counter increment at all! It just stayed at 0. Tested on an iPhone XS Max device.
It doesn't make sense. The counter should increment when OnResume is called. Are you backgrounding and foregrounding the app (on a physical device)? Are you testing the second zip I provided? Please also make sure to tap the app icon every time the app needs to be shown.
Yep! I am backgrounding the app and resuming by tapping on the icon on a physical device. It just stays 0. Sometimes the splash background is all that I see. Very odd.
Weird. You might want to uninstall and install the app again?
Here's a test run I made on XS:
https://1drv.ms/v/s!AjlbPgOcTyP2cJq5iWTtFqgMBMk
Notice that after 6, OnResume fired once, but every other time it fired twice.
Also, timing seems to matter somewhat. If you wait long enough between transitions, OnResume fires once, but if you switch back and forth quickly. it fires twice.
@samhouts I did some more testing and found out that the reason why OnResume is getting called twice has to do with DidEnterBackground. If you send the app to the background, it takes 1-2 seconds before DidEnterBackground is called (tested on XS). If you bring it back to the foreground before DidEnterBackground is called, then OnResume fires twice.
Wait 5 seconds before bringing the app to the foreground:
[0:] OnSleep (XF)
[0:] OnResignActivation (Native)
[0:] DidEnterBackground: Background (Native)
[0:] WillEnterForeground (Native)
[0:] OnResume (XF)
[0:] OnActivated (Native)
Rapid state switching does not call DidEnterBackground, hence twice the number of events:
[0:] OnSleep
[0:] OnResignActivation
[0:] OnResume
[0:] OnActivated
[0:]
[0:]
[0:] OnSleep
[0:] OnResignActivation
[0:] OnResume
[0:] OnActivated
This is also the reason why WillEnterForeground is not called.