{"record":{"id":"7f63e9204031859b","repo":"unoplatform/uno","slug":"activity-must-provide-lifecycle-events","errorCode":null,"errorMessage":"Activity must provide lifecycle events.","messagePattern":"Activity must provide lifecycle events\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AddIns/Uno.UI.Foldable/FoldableApplicationViewSpanningRects.Android.cs","lineNumber":40,"sourceCode":"\t/// and exposing the properties needed to make UI change when required.\n\t/// FUTURE: implement an event for layout changes, so we can respond to folding state changes in app code too\n\t/// </remarks>\n\t[Preserve]\n\tpublic partial class FoldableApplicationViewSpanningRects : IApplicationViewSpanningRects, INativeDualScreenProvider\n\t{\n\t\tprivate (SurfaceOrientation orientation, List<Rect> result) _previousMode = EmptyMode;\n\n\t\tprivate static readonly (SurfaceOrientation orientation, List<Rect> result) EmptyMode =\n\t\t\t((SurfaceOrientation)(-1), null);\n\n\t\tprivate readonly List<Rect> _emptyList = new List<Rect>(0);\n\n\t\tpublic FoldableApplicationViewSpanningRects(object owner)\n\t\t{\n\t\t\tvar lifecycleEvents = ViewManagement.ApplicationViewHelper.GetActivityLifecycleEvents();\n\t\t\tif (lifecycleEvents is null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"Activity must provide lifecycle events.\");\n\t\t\t}\n\n\t\t\tlifecycleEvents.Create += OnCreateEvent;\n\t\t\tlifecycleEvents.Start += OnStartEvent;\n\t\t\tlifecycleEvents.Stop += OnStopEvent;\n\t\t}\n\n\t\tprivate void OnCreateEvent(object sender, Android.OS.Bundle savedInstanceState)\n\t\t{\n\t\t\twindowInfoTrackerCallbackAdapter = new WindowInfoTrackerCallbackAdapter(WindowInfoTracker.Companion.GetOrCreate(ContextHelper.Current as Android.App.Activity));\n\t\t\twindowMetricsCalculator = WindowMetricsCalculator.Companion.OrCreate; // HACK: source method is `getOrCreate`, binding generator munges this badly :(\n\t\t\tif (this.Log().IsEnabled(LogLevel.Debug))\n\t\t\t{\n\t\t\t\tthis.Log().Debug($\"DualMode: FoldableApplicationViewSpanningRects.OnCreateEvent\");\n\t\t\t}\n\t\t}\n\t\tprivate void OnStartEvent(object sender, EventArgs args)\n\t\t{","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.UI.Foldable/FoldableApplicationViewSpanningRects.Android.cs#L22-L58","documentation":"Thrown by the FoldableApplicationViewSpanningRects constructor when ViewManagement.ApplicationViewHelper.GetActivityLifecycleEvents() returns null. This class relies on the host Activity implementing/exposing Jetpack Window Manager lifecycle hooks (Create, Start, Stop). If the activity does not provide lifecycle events (the helper has no registered lifecycle source), the foldable spanning-rects feature cannot function.","triggerScenarios":"Constructing FoldableApplicationViewSpanningRects when the current Activity has not registered with ApplicationViewHelper for lifecycle events, or the app's MainActivity does not implement the required lifecycle interface for the Uno foldable add-in.","commonSituations":"Using the Uno.UI.Foldable add-in without configuring the MainActivity to provide lifecycle events; the add-in is loaded in a context (e.g. a non-Activity host, a test harness) where GetActivityLifecycleEvents returns null; running on a non-foldable device where the foldable add-in is still initialized.","solutions":["Ensure the app's MainActivity is configured to provide lifecycle events to ApplicationViewHelper (register the activity lifecycle events source at startup).","Guard construction of FoldableApplicationViewSpanningRects with a check that GetActivityLifecycleEvents is non-null, or only instantiate it on devices that report a hinge/fold.","Verify the Uno.UI.Foldable NuGet add-in is correctly wired and the activity implements the expected interface."],"exampleFix":"// before: construct unconditionally\nvar spanning = new FoldableApplicationViewSpanningRects(this); // throws if no lifecycle events\n\n// after: guard before construction\nif (ViewManagement.ApplicationViewHelper.GetActivityLifecycleEvents() is not null)\n{\n    var spanning = new FoldableApplicationViewSpanningRects(this);\n}","handlingStrategy":"validation","validationCode":"// Before constructing, verify lifecycle events are available\nvar lifecycle = ViewManagement.ApplicationViewHelper.GetActivityLifecycleEvents();\nif (lifecycle is not null)\n{\n    var spanning = new FoldableApplicationViewSpanningRects(this);\n}","typeGuard":null,"tryCatchPattern":"try {\n    var spanning = new FoldableApplicationViewSpanningRects(this);\n} catch (InvalidOperationException ex) when (ex.Message.Contains('lifecycle events')) {\n    // Activity does not provide lifecycle events — skip foldable feature\n}","preventionTips":["Register the Activity lifecycle events source with ApplicationViewHelper at startup.","Only initialize the foldable add-in on devices that report dual-screen/fold capability.","Ensure the MainActivity implements the interface expected by the foldable add-in."],"tags":["android","foldable","lifecycle","jetpack-window-manager","csharp","add-in"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}