dotnet/maui · critical · InvalidOperationException

Activity OnCreate was not called prior to loading the applic

Error message

Activity OnCreate was not called prior to loading the application. Did you forget a base.OnCreate call?

What it means

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.

Source

Thrown at src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs:136

			{
				Profile.FramePartition("Register");
				Registrar.Registered.Register(target, handler);
			}

			Profile.FrameEnd();
		}

		// This is currently being used by the previewer please do not change or remove this
		static void RegisterHandlers()
		{
		}

		protected void LoadApplication(Application application)
		{
			Profile.FrameBegin();
			if (!_activityCreated)
			{
				throw new InvalidOperationException("Activity OnCreate was not called prior to loading the application. Did you forget a base.OnCreate call?");
			}

			if (!_renderersAdded)
			{
				Profile.FramePartition("RegisterHandlers");
				RegisterHandlers();
				_renderersAdded = true;
			}

			if (_application != null)
			{
				_application.PropertyChanging -= AppOnPropertyChanging;
				_application.PropertyChanged -= AppOnPropertyChanged;
			}

			Profile.FramePartition("SetAppIndexingProvider");
			_application = application ?? throw new ArgumentNullException(nameof(application));
			((IApplicationController)application).SetAppIndexingProvider(new AndroidAppIndexProvider(this));

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure LoadApplication(app) is called inside your overridden OnCreate, after base.OnCreate(savedInstanceState).
  2. If you override the parameterless OnCreate(Bundle), confirm it delegates to the internal OnCreate(Bundle, ActivationFlags) which sets _activityCreated.
  3. Move any LoadApplication call out of constructors, field initializers, or lifecycle methods that run before OnCreate.

Example fix

// before
protected override void OnCreate(Bundle bundle)
{
    // forgot base.OnCreate
    LoadApplication(new App());
}

// after
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    Forms.Init(this, bundle);
    LoadApplication(new App());
}
Defensive patterns

Strategy: validation

Validate before calling

// In a debug build, assert the lifecycle flag before LoadApplication.
#if DEBUG
if (!_activityCreated)
    System.Diagnostics.Debug.WriteLine("LoadApplication called before OnCreate completed; ensure base.OnCreate is called first.");
#endif
LoadApplication(app);

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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