{"record":{"id":"8e3282eb9196d350","repo":"AvaloniaUI/Avalonia","slug":"activity-window-must-be-set","errorCode":null,"errorMessage":"Activity.Window must be set.","messagePattern":"Activity\\.Window must be set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Android/Avalonia.Android/Platform/AndroidInsetsManager.cs","lineNumber":34,"sourceCode":"namespace Avalonia.Android.Platform\n{\n    internal sealed class AndroidInsetsManager : WindowInsetsAnimationCompat.Callback, IInsetsManager, IOnApplyWindowInsetsListener, ViewTreeObserver.IOnGlobalLayoutListener, IInputPane\n    {\n        private readonly Activity _activity;\n        private readonly TopLevelImpl _topLevel;\n        private bool _displaysEdgeToEdge;\n        private bool? _systemUiVisibility;\n        private SystemBarTheme? _statusBarTheme;\n        private bool? _isDefaultSystemBarLightTheme;\n        private Color? _systemBarColor;\n        private InputPaneState _state;\n        private Rect _previousRect;\n        private Insets? _previousImeInset;\n        private bool _displayEdgeToEdgePreference;\n        private readonly bool _usesLegacyLayouts;\n        private readonly bool _isDisplayEdgeToEdgeForced;\n\n        private AndroidWindow Window => _activity.Window ?? throw new InvalidOperationException(\"Activity.Window must be set.\"); \n        \n        public event EventHandler<SafeAreaChangedArgs>? SafeAreaChanged;\n        public event EventHandler<InputPaneStateEventArgs>? StateChanged;\n\n        public InputPaneState State\n        {\n            get => _state; set\n            {\n                var oldState = _state;\n                _state = value;\n\n                if (oldState != value && Build.VERSION.SdkInt <= BuildVersionCodes.Q)\n                {\n                    var currentRect = OccludedRect;\n                    NotifyStateChanged(value, _previousRect, currentRect, TimeSpan.Zero, null);\n                    _previousRect = currentRect;\n                }\n            }","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Android/Avalonia.Android/Platform/AndroidInsetsManager.cs#L16-L52","documentation":"AndroidInsetsManager reads the Activity's Window via a property that throws if Window is null. Activity.getWindow() is null until the Activity has a window attached (after super.onCreate and setContentView). Accessing Window before that point (e.g. during early init or on a not-yet-attached activity) triggers the exception.","triggerScenarios":"Calling any AndroidInsetsManager API that resolves Window (SafeArea, edge-to-edge, system bars) before the Activity's Window is set — i.e. before onCreate completes or before setContentView ran.","commonSituations":"Creating the insets manager in the constructor before super.onCreate; reacting to insets during very early lifecycle hooks; using the manager on a detached/recreated activity before the window is reattached.","solutions":["Access Window only after Activity.Attach/onCreate and setContentView have run (post super.OnCreate).","Defer insets operations to OnAttachedToWindow / OnResume where Window is guaranteed set.","Guard the access: `if (_activity.Window is null) return;` at call sites instead of throwing in the property.","Ensure the insets manager is constructed after the activity has a window."],"exampleFix":"// before\nprivate AndroidWindow Window => _activity.Window ?? throw new InvalidOperationException(\"Activity.Window must be set.\");\n\n// after (nullable property; callers guard)\nprivate AndroidWindow? WindowOrNull => _activity.Window;\n\nprivate AndroidWindow Window => WindowOrNull\n    ?? throw new InvalidOperationException(\n        \"Activity.Window is null; call insets APIs only after OnCreate/setContentView (e.g. in OnResume or OnAttachedToWindow).\");","handlingStrategy":"validation","validationCode":"// only resolve Window once the activity has one\nif (_activity.Window is null) return;","typeGuard":"static bool HasWindow(Activity a) => a.Window is not null;","tryCatchPattern":"// guard at call sites instead of catching; move insets work to OnResume/OnAttachedToWindow.","preventionTips":["Access Window only after onCreate/setContentView.","Defer insets work to OnAttachedToWindow.","Make the Window property nullable and guard callers."],"tags":["android","activity-lifecycle","window","insets","edge-to-edge","platform"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}