Description
Description
Recently implemented a change to use our own Page / Shell with MainPage obsoleted on .net9. Before adding removing MainPage / Current.Shell references and adding in the CreateWindow and the app's own common Page into App.xaml.cs, this particular crash never occurred using the same Maui workload.
This is different than issue 25443, as this app uses shell and 25443 does not.
After retrieving breadcrumb information from Sentry, this crash doesn't appear that this is an issue with the CreateWindow per se, but a problem in the Maui / Android lifecycle which mistakenly calls CreateWindow as part of the app teardown - on some devices.
There are two Sentry breadcrumb logs in the log area below: log 1 - one time sequential, log 2 - two time breaks
This crash does not occur on iOS, only on Android.
Have not been able to reproduce this either on simulator or hardware device.
Stack trace::
System.InvalidOperationException: MauiContext is null.
?, in IMauiContext Window.get_MauiContext()
?, in void AlertManager.Subscribe(Window window) x 2
?, in void Window.OnPageHandlerChanged(object sender, EventArgs e)
?, in void Window.OnPageChanged(Page oldPage, Page newPage)
?, in static Window()+(BindableObject b, object o, object n) => { }
?, in void BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, bool silent)
?, in void BindableObject.SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
?, in void BindableObject.SetValue(BindableProperty property, object value)
?, in void Window.set_Page(Page value)
?, in new Window(Page page)
File "App.xaml.cs", line 174, in Window App.CreateWindow(IActivationState activationState)
?, in IWindow Application.Microsoft.Maui.IApplication.CreateWindow(IActivationState activationState)
?, in void ApplicationExtensions.CreatePlatformWindow(Activity activity, IApplication application, Bundle savedInstanceState)
?, in void MauiAppCompatActivity.OnCreate(Bundle savedInstanceState)
File "MainActivity.cs", line 34, in void MainActivity.OnCreate(Bundle savedInstanceState)
?, in void Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState)
?, in void JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0)
In case it might help, here's CreateWindow in App.xaml.cs
public static Page OurPage;
public static Shell? OurShell => OurPage as Shell;
...
protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(OurShell ?? OurPage);
}
OurPage is set in the initialization of the NavigationService.
Steps to Reproduce
The app has 2 shells: logged in shell and a logged out shell. The default page of the logged out shell is a LoginPage.
The below log is what occurred to one user, but all breadcrumbs point to the same pattern, app starts to shut down / log out and after window is destroyed, CreateWindow is called and app crashes.
- App launched
- User may or may not log in
- If logged in OK, switch shell to logged in shell
- If logged in OK, user can perform actions
- User logs out or closes app
- If logged in OK - they will be using logged in shell; change shell to the logged out shell
- Logged out shell shows LoginPage
- LoginPage is disposed
- Logged out shell is disposed
- Window is destroyed
- CreateWindow is called
- Crash with MauiContext is null
Link to public reproduction project repository
N/A
Version with bug
8.0.40 SR5, android workload: 34.0.95
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
No response
Affected platforms
Android
Affected platform versions
Android 15 down to 9 (our full API range); happens on many recent google and samsung models, some Motorola and other device manufacturers according to the GPS
Did you find any workaround?
No workaround.
Relevant log output
:::::::: log 1 start ::::::::::::
Exception - This event error 05:25:28.000 AM
System.InvalidOperationException: MauiContext is null.
Ui Lifecycle info 05:25:28.414 AM
Window.Destroying
Navigation info 05:25:28.414 AM
App.PageDisappearing
{
Page: LoggedOutShell
}
Ui Lifecycle info 05:25:28.414 AM
LoggedOutShell.Disappearing
Navigation info 05:25:28.414 AM
App.PageDisappearing
{
Page: LoginPage,
Page.Name: ParentPage
}
Ui Lifecycle info 05:25:28.413 AM
LoginPage.Disappearing
{
LoginPage.Name: ParentPage
}
Ui Lifecycle info 05:25:28.405 AM
Window.Stopped
Ui Lifecycle info 05:25:28.397 AM
Window.Deactivated
Ui Rendering info 05:25:27.964 AM
LoginPage.LayoutChanged
{
LoginPage.Name: ParentPage
}
Ui Lifecycle info 05:25:27.700 AM
Window.Activated
Ui Lifecycle info 05:25:27.659 AM
Window.Created
Navigation info 05:25:27.584 AM
App.PageAppearing
{
Page: LoggedOutShell
}
Navigation info 05:25:27.584 AM
App.PageAppearing
{
Page: LoginPage,
Page.Name: ParentPage
}
Ui Lifecycle info 05:25:27.582 AM
LoginPage.Appearing
{
LoginPage.Name: ParentPage
}
Ui Lifecycle info 05:25:27.549 AM
LoggedOutShell.Appearing
:::::::: log 1 end ::::::::::::
:::::::: log 2 start ::::::::::::
Exception - This event error 11:43:57.000 AM
System.InvalidOperationException: MauiContext is null.
Ui Lifecycle info 10:36:59.352 AM
Window.Destroying
Navigation info 10:36:59.337 AM
App.PageDisappearing
{
Page: LoggedOutShell
}
Ui Lifecycle info 10:36:59.332 AM
LoggedOutShell.Disappearing
Navigation info 10:36:59.332 AM
App.PageDisappearing
{
Page: LoginPage,
Page.Name: ParentPage
}
Ui Lifecycle info 10:36:59.329 AM
LoginPage.Disappearing
{
LoginPage.Name: ParentPage
}
Ui Lifecycle info 10:35:55.872 PM
Window.Stopped
Ui Lifecycle info 10:35:55.862 PM
Window.Deactivated
Ui Rendering info 10:35:31.551 PM
LoginPage.LayoutChanged
{
LoginPage.Name: ParentPage
}
Ui Rendering info 10:35:31.441 PM
Entry.Focused
HTTP info 10:35:05.057 PM
GET: https://www.example.com/api/verb2 [200]
Ui Lifecycle info 10:35:04.752 PM
Window.Activated
Ui Lifecycle info 10:35:04.749 PM
Window.Resumed
Ui Lifecycle info 10:34:49.966 PM
Window.Stopped
Ui Lifecycle info 10:34:49.952 PM
Window.Deactivated
HTTP info 10:34:41.997 PM
GET: https://www.example.com/api/verb2 [200]
Ui Lifecycle info 10:34:41.699 PM
Window.Activated
Ui Lifecycle info 10:34:41.695 PM
Window.Resumed
Ui Lifecycle info 10:34:35.372 PM
Window.Stopped
Ui Lifecycle info 10:34:35.367 PM
Window.Deactivated
HTTP info 10:34:18.925 PM
GET: https://www.example.com/api/verb2 [200]
Ui Rendering info 10:34:18.811 PM
LoginPage.LayoutChanged
{
LoginPage.Name: ParentPage
}
Navigation info 10:34:18.745 PM
App.PageAppearing
{
Page: LoggedOutShell
}
Ui Lifecycle info 10:34:18.745 PM
LoggedOutShell.Appearing
Navigation info 10:34:18.742 PM
App.PageAppearing
{
Page: LoginPage,
Page.Name: ParentPage
}
Navigation info 10:34:18.718 PM
App.PageDisappearing
{
Page: LoggedInShell
}
Ui Lifecycle info 10:34:18.718 PM
LoggedInShell.Disappearing
{
Page: LoggedInShell
}
Ui Lifecycle info 10:34:18.718 PM
LoggedInShell.Disappearing
Navigation info 10:34:18.718 PM
App.PageDisappearing
{
Page: LoggedInActionPage,
Page.Name: LoggedInActionPage
}
:::::::: log 2 end ::::::::::::