[Bug] Strange app lifecycle when resuming app from lock screen notification
Describe the bug
On Android, when resuming an app by tapping on a local notification from the lock screen, the app resumes, then goes to sleep, then resumes again.
Is this an android bug, a xamarin forms bug, or a bug in the localnotification plugin ?
To Reproduce
Steps to reproduce the behavior:
- Open the sample app and tap on "send local notification"
- Turn the phone off and on again
- From the lock screen, tap on the sample app notification in order to resume the app (should require double tap and unlock)
- The app resumes, however it also immediately goes to sleep and resumes again (app.xaml.cs onResume and onSleep)
Expected behavior
The app should simply resume. No resume-sleep-resume strange cycle.
Platform:
- Android (api 30)
- Device: android emulator ; pixel 5
@tranb3r Can you have a look at #150
@thudugala Thank you for answering. It seems to me that #150 is a totally different issue. But maybe I've missed something ? What do you recommend exactly ? Do you think you can fix the sample app ?
https://github.com/thudugala/Plugin.LocalNotification/issues/156#issuecomment-841480667
@tranb3r Can you try setting LaunchMode = LaunchMode.SingleTask onto the Activity attribute declaration of MainActivity.cs ?
@thudugala I've tested every LaunchModes. It does not make a difference.
I just notice in Plugin.LocalNotification/Sample/NuGet/LocalNotification.Sample/App.xaml.cs the OnResume and OnSleep function are rewritten (i.e. overridden). When you override these function you need to call base.OnResume() and base.OnSleep() to maintain the state of application respectively, (or write your own logic) if you don't do then application did the above behaviour. I solve it in my app with this fix.
I didn't test on Sample app. But I had same issue in my live app and solve by adding those 2 function call respectively.
@archergod I've tested your fix proposal, both on the sample app and on my app. It doesn't work. Are you sure it's fixed in your app ? Maybe you can share your code.
Besides, base.OnSleep and base.OnResume in Xamarin.Forms.Application are empty, so it's not suprising this fix proposal is not working.
protected virtual void OnResume()
{
}
protected virtual void OnSleep()
{
}
protected virtual void OnStart()
{
}
conceptually if function get compiled in final version it will overwrite the previous version. so even empty function will overwrite previous method. If you don't need those method just remove them (as per guide) otherwise even empty function need call.
I am not at desk for couple of days to test myself, but that is how I fixed it. it is not this plugin issue for sure. It is the flow of xamarin to manage state or not. maybe I miss some settings. when I get chance I test and let you know.
Sorry if I wasn't clear. The code with empty methods is Xamarin.Forms, not my code. So basically, you're calling empty methods when you add base.OnSleep() and base.OnResume(). Or maybe I'm missing something...
@tranb3r Just to try this out, can you remove OnResume(), OnSleep(), OnStart() if you are not using them or add base call if you are using them. Just to try out @archergod suggestion
I've already reported here that I've tested calling base.OnSleep and base.OnResume, both and the sample and on my own app. It doesn't work. Which is expected since base methods are not doing anything.
I am not sure why it didn't work for you. I see the sample app are old and using older v8 code in Nuget Folder. But to help you further, I have Xamarin Essential in my app and found this code that looks significant to issue
(savedInstanceState is important in above picture. but I recommend reading little before trying).
the problem is mention here as : https://stackoverflow.com/questions/61948076/xamarin-forms-app-restarts-instead-of-resume-on-ios-app-switching
https://social.msdn.microsoft.com/Forums/en-US/c285f18b-683c-4f9e-afe3-e0871e368395/forms-app-restarts-every-time-when-i-bring-it-back-from-the-background?forum=xamarinforms
So clearly it is not plugin issue, but some configuration is problem. Though sample apps need to be fixed for issue as well, but that is other topic I guess.
@archergod I'm sorry but I don't understand your explanation. Your links are about app restart (not sleep/resume), and the issue mentionnend has been fixed in a more recent version of xamarin.forms.
So clearly it is not plugin issue, but some configuration is problem. Though sample apps need to be fixed for issue as well, but that is other topic I guess.
Fixing the sample app is exactly what this topic is about!
So, if you have the code of an app on which this issue is fixed, could you please share it?
FYI
https://github.com/xamarin/Xamarin.Forms/blob/caab66bcf9614aca0c0805d560a34e176d196e17/Xamarin.Forms.Platform.Android/FormsApplicationActivity.cs#L270
@tranb3r
@thudugala So what? Please explain your findings...
@tranb3r can you try changing PendingIntentFlags to fix that helps
var notification = new NotificationRequest
{
Android.PendingIntentFlags = AndroidPendingIntentFlags.CancelCurrent;
};
@thudugala Just tested it, same result. By the way, isn't CancelCurrent the default value for PendingIntentFlags in AndroidOptions ?
@tranb3r yes. Try setting it to different values
@thudugala Same behavior with all values.
Hi @thudugala This issue is not fixed, so why did you close it?
@tranb3r Is this still happening in .Net Maui?
@tranb3r Is this still happening in .Net Maui?
Yes. And it's easily reproducible with the sample app.