-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resuming app from background service results in System.InvalidOperationException MauiContext is null. #25443
Comments
@RobbiewOnline can you post any code with how you handle Are you reusing the same window across |
Nm, I repro'd |
Actually @RobbiewOnline Can you post the code of how you setup the main page you are setting on the app/window? I'm curious to see your scenario. Possible fix here |
Hi, I wasn't doing anything 'special' in my code, I was following the standard code sample that does the following:
But it would seem when the application is being returned to from a background service, which uses an intent to launch / return to the app, the CreateWindow is called again, which promptly creates a new Window. Somewhere in this lifecycle I get behavioural issues, like the error previously reported. I modified the code this evening to keep a reference to the original Window created and if a reference remains then re-use it. This now launches from the persistent foreground service notification 🕺 So app.xaml.cs window registration becomes this...
I don't know if this is a hack/work-around, or what I should have been doing in the first place? The MainActivity is defined like this, most of it's logging because I was trying to track down the crashes / ANRs.
For context the persistent notification is setup like this (it's a requirement of Android if you want to promote a GPS service to the foreground) and clicking on the notification (after swiping away the app) would result in a new window being created even though the app hasn't been terminated (because it's running as a foreground service), at least that's my interpretation of it ...
Your thoughts are very much appreciated as I'd like to understand whether this is a MAUI lifecycle bug (resuming an app from a persistent foreground notification), or my missunderstanding of creating a Window and assuming when CreateWindow is called it should be created each invocation. |
Yea, I think this is an area where we could throw a better exception or make the code run in a little bit more helpful of a fashion. I've moved this into our servicing milestone so we can give it a little more thought about best bath here I think your workaround is "valid" My suggestion was going to either be
so like this might even work var waitPage = PageManager.GetPleaseWaitPage();
if (waitPage.Window is not null)
waitPage.Window.Page = null;
return new Window(waitPage); |
Thanks @PureWeen From my perspsective if the original Page(s) and Window objects are all still referencable, then there's no point creating a new Window and trying to reassociate the pages to the new Window? It feels like in some Android specific lifecycles (e.g. resuming the app from a background/foreground service system tray banner) then the CreateWindow call should be avoided as everything is still working as-is, no new Window needs to be created. I'm going to tag this issue in with #25274 as these might be related. |
Possibly, the problem is, is that we have limited context as to why CreateWindow is being called. These are all just tied to platform life cycle events. Maybe the user wants a new activity to handle a new intent? maybe they want to handle notifications in a new intent/window? We could add some type registration hooks, or a way for users to specify that an app is a single page app and to always reuse the window/page as singletons. I don't really want to make assumptions for the user why CreateWindow is being called but we could probably add some ways for users to express that intent so it's not as leaky. For example, in your scenario you are already using a singleton |
Description
Detected System.InvalidOperationException MauiContext is null via sentry.io when the app was resumed by the user approx 10 minutes of background services.
It's assumed the user either resumed the app, or clicked not the foreground service notification to resume the app.
The call stack is all deep within MAUI so I'm not sure how to provide more details that will help diagnose this?
Steps to Reproduce
Published my app to Google Play for internal testing then started seeing these exceptions, it's unclear what the user was doing, but logs indicates the following:
Note that Sentry shows this is .NET 9.0.0-rc.2.24473.5 whereas the drop-down in this ticket only let's me choose 9.0.0-rc.2.24503.2 as the closest match.
The Android activity is set to be LaunchMode = LaunchMode.SingleTask
Link to public reproduction project repository
No response
Version with bug
9.0.0-rc.2.24503.2
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 13 / CMA-LX1 + Android 14 / Pixel 8
Did you find any workaround?
Yes - within the
CreateWindow
method I now return the existing Window if one has previously been created, e.g.Relevant log output
No response
The text was updated successfully, but these errors were encountered: