{"record":{"id":"22398abba10ba930","repo":"dotnet/maui","slug":"call-forms-init-before-this","errorCode":null,"errorMessage":"call Forms.Init() before this","messagePattern":"call Forms\\.Init\\(\\) before this","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Compatibility/Core/src/Android/AppCompat/PageExtensions.cs","lineNumber":56,"sourceCode":"\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t_disposed = true;\n\n\t\t\t\tif (disposing)\n\t\t\t\t{\n\t\t\t\t\t(_platform as IDisposable)?.Dispose();\n\t\t\t\t}\n\n\t\t\t\tbase.Dispose(disposing);\n\t\t\t}\n\t\t}\n#pragma warning restore 618\n\n\t\tpublic static Fragment CreateSupportFragment(this ContentPage view, Context context)\n\t\t{\n\t\t\tif (!Forms.IsInitialized)\n\t\t\t\tthrow new InvalidOperationException(\"call Forms.Init() before this\");\n\n\t\t\tif (!(view.RealParent is Application))\n\t\t\t{\n\t\t\t\tApplication app = new DefaultApplication();\n\t\t\t\tapp.MainPage = view;\n\t\t\t}\n\n\t\t\tvar platform = new Platform(context, true);\n\t\t\tplatform.SetPage(view);\n\n\t\t\tvar vg = platform.GetViewGroup();\n\n\t\t\treturn new EmbeddedSupportFragment(vg, platform);\n\t\t}\n\n\t\tclass DefaultApplication : Application\n\t\t{\n\t\t}","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/PageExtensions.cs#L38-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call Forms.Init(activity, bundle) in the host Activity's OnCreate before invoking CreateSupportFragment.","If using a non-Activity context, ensure Forms was initialized with a valid Activity context earlier in the process.","Gate the CreateSupportFragment call behind a check of Forms.IsInitialized and initialize on demand if needed."],"exampleFix":"// before\nvar fragment = myPage.CreateSupportFragment(this); // throws\n\n// after\nprotected override void OnCreate(Bundle bundle)\n{\n    base.OnCreate(bundle);\n    Forms.Init(this, bundle);\n    var fragment = myPage.CreateSupportFragment(this);\n}","handlingStrategy":"validation","validationCode":"if (!Forms.IsInitialized)\n    Forms.Init(activity, bundle);\nvar fragment = page.CreateSupportFragment(context);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call Forms.Init in the host Activity's OnCreate before embedding Forms fragments.","Gate CreateSupportFragment behind a Forms.IsInitialized check.","Document the initialization requirement for developers embedding a single Forms page in a native app."],"tags":["android","forms-init","fragment","embedding","initialization"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}