dotnet/maui · critical · InvalidOperationException

call Forms.Init() before this

Error message

call Forms.Init() before this

What it means

Thrown by the ContentPage.CreateSupportFragment extension method when Forms.IsInitialized is false. Creating a SupportFragment for a ContentPage requires the Forms platform to be initialized (registrar, services) so that renderers can be resolved for the page's content. The guard mirrors FormsAppCompatActivity.InternalSetPage's check.

Source

Thrown at src/Compatibility/Core/src/Android/AppCompat/PageExtensions.cs:56

					return;
				}

				_disposed = true;

				if (disposing)
				{
					(_platform as IDisposable)?.Dispose();
				}

				base.Dispose(disposing);
			}
		}
#pragma warning restore 618

		public static Fragment CreateSupportFragment(this ContentPage view, Context context)
		{
			if (!Forms.IsInitialized)
				throw new InvalidOperationException("call Forms.Init() before this");

			if (!(view.RealParent is Application))
			{
				Application app = new DefaultApplication();
				app.MainPage = view;
			}

			var platform = new Platform(context, true);
			platform.SetPage(view);

			var vg = platform.GetViewGroup();

			return new EmbeddedSupportFragment(vg, platform);
		}

		class DefaultApplication : Application
		{
		}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Call Forms.Init(activity, bundle) in the host Activity's OnCreate before invoking CreateSupportFragment.
  2. If using a non-Activity context, ensure Forms was initialized with a valid Activity context earlier in the process.
  3. Gate the CreateSupportFragment call behind a check of Forms.IsInitialized and initialize on demand if needed.

Example fix

// before
var fragment = myPage.CreateSupportFragment(this); // throws

// after
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    Forms.Init(this, bundle);
    var fragment = myPage.CreateSupportFragment(this);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Forms.IsInitialized)
    Forms.Init(activity, bundle);
var fragment = page.CreateSupportFragment(context);

Prevention

When it happens

Trigger: Calling view.CreateSupportFragment(context) from native Android code (e.g., a host Activity or a Fragment that is not a FormsAppCompatActivity) without first calling Forms.Init(context, bundle). Common when embedding a Forms ContentPage inside a native Android app via fragments.

Common situations: Native Android app integrating a single Forms ContentPage and forgetting Forms.Init in the host Activity's OnCreate. Embedding scenarios where the initialization is assumed but skipped. Unit tests calling CreateSupportFragment without bootstrapping Forms.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/22398abba10ba930. Report an issue: GitHub.