MauiContext is null when app is opened with new Intent from background mode (System.InvalidOperationException: MauiContext is null.)

mjs 25 Reputation points
2023-11-30T16:00:34.18+00:00

Hi!

We are currently porting our XAMARIN apps to MAUI and are stuck on an issue.

We have a feature that works more or less like this:

  • a user gets a mail with a link and clicks it
  • the browser opens and a website redirects to the installed app to perform a registration.

When the app is completely shut down, the redirect seems to work normally -> starts app and app runs normally.

But when the app is running in the background, after clicking the link the app goes into foreground and crashes with this error:

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: MauiContext is null.
[mono-rt]    at Microsoft.Maui.Controls.Window.get_MauiContext() in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 336
[mono-rt]    at Microsoft.Maui.Controls.Platform.AlertManager.Subscribe(Window window) in D:\a\_work\1\s\src\Controls\src\Core\Platform\AlertManager\AlertManager.Android.cs:line 28
[mono-rt]    at Microsoft.Maui.Controls.Platform.AlertManager.Subscribe() in D:\a\_work\1\s\src\Controls\src\Core\Platform\AlertManager\AlertManager.cs:line 13
[mono-rt]    at Microsoft.Maui.Controls.Window.OnPageHandlerChanged(Object sender, EventArgs e) in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 652
[mono-rt]    at Microsoft.Maui.Controls.Window.OnPageChanged(Page oldPage, Page newPage) in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 640
[mono-rt]    at Microsoft.Maui.Controls.Window.<>c.<.cctor>b__219_0(BindableObject b, Object o, Object n) in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 26
[mono-rt]    at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) in D:\a\_work\1\s\src\Controls\src\Core\BindableObject.cs:line 633
[mono-rt]    at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) in D:\a\_work\1\s\src\Controls\src\Core\BindableObject.cs:line 559
[mono-rt]    at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in D:\a\_work\1\s\src\Controls\src\Core\BindableObject.cs:line 464
[mono-rt]    at Microsoft.Maui.Controls.Window.set_Page(Page value) in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 122
[mono-rt]    at Microsoft.Maui.Controls.Window..ctor(Page page) in D:\a\_work\1\s\src\Controls\src\Core\Window\Window.cs:line 106
[mono-rt]    at Access.MA.CredentialHolderApp.App.CreateWindow(IActivationState activationState) in C:\Projects
et8\Access.MobileAccess\MauiApps\MobileAccess\src\Access.MA.CredentialHolderApp\App.xaml.cs:line 52
[mono-rt]    at Microsoft.Maui.Controls.Application.Microsoft.Maui.IApplication.CreateWindow(IActivationState activationState) in D:\a\_work\1\s\src\Controls\src\Core\Application\Application.cs:line 428
[mono-rt]    at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(Activity activity, IApplication application, Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\ApplicationExtensions.cs:line 46
[mono-rt]    at Microsoft.Maui.MauiAppCompatActivity.OnCreate(Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\MauiAppCompatActivity.cs:line 35
[mono-rt]    at Access.MA.CredentialHolderApp.MainActivity.<>n__0(Bundle savedInstanceState)
[mono-rt]    at Access.MA.CredentialHolderApp.MainActivity.OnCreate(Bundle savedInstanceState) in C:\Projects
et8\Access.MobileAccess\MauiApps\MobileAccess\src\Access.MA.CredentialHolderApp\Platforms\Android\MainActivity.cs:line 50
[mono-rt]    at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
[mono-rt]    at Android.App.SyncContext.<>c__DisplayClass2_0.<Post>b__0() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:line 36
[mono-rt]    at Java.Lang.Thread.RunnableImplementor.Run() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:line 36
[mono-rt]    at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Java.Lang.IRunnable.cs:line 84
[mono-rt]    at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 26

The according code lines are: MainActivity:

:

    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density),
     IntentFilter(new[] { Intent.ActionView },
                            Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
                            DataScheme = App.DataScheme)]
    public class MainActivity : MauiAppCompatActivity
    {
:
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            FormsContext = this;
            base.OnCreate(savedInstanceState); // <-- line 50
:

and App.xaml.cs:

        protected override Window CreateWindow(IActivationState activationState)
        {
            Page shell = IPlatformApplication.Current.Services.GetService<AppShell>();
            return new Window(shell); // <-- line 52
        }

It is a giant solution written by another team and I have no idea where else I should look or what info I could provide. Do you maybe have an idea what could cause this?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,900 questions
{count} vote

Accepted answer
  1. JessieZhang-MSFT 7,711 Reputation points Microsoft Vendor
    2023-12-05T02:29:33.5433333+00:00

    Hello,

    You can try to add attribute LaunchMode = Android.Content.PM.LaunchMode.SingleTask for your activity.

    From document : android:launchMode, we know that :

    An activity with the "singleTask" launch mode combines the behaviors of "singleInstance" and "singleInstancePerTask": the activity can be instantiated multiple times and can be located anywhere in a task of the same taskAffinity. But the device can only hold one task for locating the "singleTask" activity at the root of the activity task.

    This means that when you click the home button all the activities above the single-task activity will be removed from the stack.

    Lifecycle: Same as in SingleTop mode. The onNewIntent method in the Activity is called back again, keeping only one instance of the Activity after our application is opened. A typical example of this is the home page that appears in the app.

    This pattern is typically used when a user jumps from the home page to another page and wants to return to the home page after performing multiple actions.

    Best Regards,

    Jessie Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.