{"record":{"id":"dea01db99ece58ae","repo":"dotnet/maui","slug":"activity-oncreate-was-not-called-prior-to-loading","errorCode":null,"errorMessage":"Activity OnCreate was not called prior to loading the application. Did you forget a base.OnCreate call?","messagePattern":"Activity OnCreate was not called prior to loading the application\\. Did you forget a base\\.OnCreate call\\?","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs","lineNumber":136,"sourceCode":"\t\t\t{\n\t\t\t\tProfile.FramePartition(\"Register\");\n\t\t\t\tRegistrar.Registered.Register(target, handler);\n\t\t\t}\n\n\t\t\tProfile.FrameEnd();\n\t\t}\n\n\t\t// This is currently being used by the previewer please do not change or remove this\n\t\tstatic void RegisterHandlers()\n\t\t{\n\t\t}\n\n\t\tprotected void LoadApplication(Application application)\n\t\t{\n\t\t\tProfile.FrameBegin();\n\t\t\tif (!_activityCreated)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"Activity OnCreate was not called prior to loading the application. Did you forget a base.OnCreate call?\");\n\t\t\t}\n\n\t\t\tif (!_renderersAdded)\n\t\t\t{\n\t\t\t\tProfile.FramePartition(\"RegisterHandlers\");\n\t\t\t\tRegisterHandlers();\n\t\t\t\t_renderersAdded = true;\n\t\t\t}\n\n\t\t\tif (_application != null)\n\t\t\t{\n\t\t\t\t_application.PropertyChanging -= AppOnPropertyChanging;\n\t\t\t\t_application.PropertyChanged -= AppOnPropertyChanged;\n\t\t\t}\n\n\t\t\tProfile.FramePartition(\"SetAppIndexingProvider\");\n\t\t\t_application = application ?? throw new ArgumentNullException(nameof(application));\n\t\t\t((IApplicationController)application).SetAppIndexingProvider(new AndroidAppIndexProvider(this));","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs#L118-L154","documentation":"Thrown by FormsAppCompatActivity.LoadApplication when the internal _activityCreated flag is false. That flag is set to true only inside the overridden OnCreate, so the guard ensures the Android Activity lifecycle has progressed past OnCreate before the Forms application is loaded. Calling LoadApplication before that point means the toolbar, content view, and window setup that OnCreate performs have not run.","triggerScenarios":"LoadApplication is invoked from the Activity constructor, a field initializer, or an overridden OnCreate that forgot to call base.OnCreate. It can also fire if LoadApplication is called from OnStart/OnResume because the user's OnCreate never ran (e.g., a configuration change path that bypassed the Forms OnCreate).","commonSituations":"Developer overrides OnCreate(Bundle) and writes custom logic but omits base.OnCreate(savedInstanceState). Calling LoadApplication in a method invoked before OnCreate completes (e.g., a static initializer or early callback). Copying a sample that placed LoadApplication outside OnCreate.","solutions":["Ensure LoadApplication(app) is called inside your overridden OnCreate, after base.OnCreate(savedInstanceState).","If you override the parameterless OnCreate(Bundle), confirm it delegates to the internal OnCreate(Bundle, ActivationFlags) which sets _activityCreated.","Move any LoadApplication call out of constructors, field initializers, or lifecycle methods that run before OnCreate."],"exampleFix":"// before\nprotected override void OnCreate(Bundle bundle)\n{\n    // forgot base.OnCreate\n    LoadApplication(new App());\n}\n\n// after\nprotected override void OnCreate(Bundle bundle)\n{\n    base.OnCreate(bundle);\n    Forms.Init(this, bundle);\n    LoadApplication(new App());\n}","handlingStrategy":"validation","validationCode":"// In a debug build, assert the lifecycle flag before LoadApplication.\n#if DEBUG\nif (!_activityCreated)\n    System.Diagnostics.Debug.WriteLine(\"LoadApplication called before OnCreate completed; ensure base.OnCreate is called first.\");\n#endif\nLoadApplication(app);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call base.OnCreate(savedInstanceState) first in your overridden OnCreate.","Place Forms.Init and LoadApplication together at the top of OnCreate to keep initialization ordered.","Use a shared base Activity class so all activities follow the same lifecycle ordering.","Add a build-time analyzer or a unit test that checks the Activity's OnCreate calls base.OnCreate."],"tags":["android","activity","lifecycle","oncreate","initialization"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}