{"record":{"id":"da295aded0942c92","repo":"unoplatform/uno","slug":"foldablehingeanglesensor-must-be-initialized-on-th","errorCode":null,"errorMessage":"FoldableHingeAngleSensor must be initialized on the UI Thread","messagePattern":"FoldableHingeAngleSensor must be initialized on the UI Thread","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AddIns/Uno.UI.Foldable/FoldableHingeAngleSensor.Android.cs","lineNumber":60,"sourceCode":"\t\t{\n\t\t\tget\n\t\t\t{\n\t\t\t\treturn _hingeSensor != null;\n\t\t\t}\n\t\t}\n\n\t\tpublic FoldableHingeAngleSensor(object owner)\n\t\t{\n\t\t\t_hingeSensor = GetHingeSensor();\n\t\t}\n\n\t\tinternal static bool HasHinge => GetHingeSensor() is not null;\n\n\t\tprivate static Sensor GetHingeSensor()\n\t\t{\n\t\t\tif (!(ContextHelper.Current is Activity currentActivity))\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"FoldableHingeAngleSensor must be initialized on the UI Thread\");\n\t\t\t}\n\n\t\t\t_sensorManager ??= SensorManager.FromContext(currentActivity);\n\n\t\t\tif (_sensorManager is null)\n\t\t\t{\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tvar sensors = _sensorManager.GetSensorList(Android.Hardware.SensorType.All);\n\n\t\t\treturn sensors.FirstOrDefault(s => s.Name.Contains(HINGE_SENSOR_NAME, StringComparison.OrdinalIgnoreCase)); // NAME - generic foldable device/s\n\t\t}\n\n\t\tpublic event EventHandler<NativeHingeAngleReading> ReadingChanged\n\t\t{\n\t\t\tadd\n\t\t\t{","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.UI.Foldable/FoldableHingeAngleSensor.Android.cs#L42-L78","documentation":"Thrown by FoldableHingeAngleSensor.GetHingeSensor when ContextHelper.Current is not an Activity. The sensor lookup requires an Activity context to obtain the SensorManager. If ContextHelper.Current is null or a non-Activity context (e.g. a Service or Application context), the cast fails and the error fires. The message attributes this to thread issues because ContextHelper.Current is typically only an Activity on the UI thread.","triggerScenarios":"FoldableHingeAngleSensor is constructed or HasHinge is accessed when ContextHelper.Current is not an Activity — typically because initialization ran off the UI thread, before the Activity was created, or after it was destroyed (ContextHelper.Current set to a non-Activity).","commonSituations":"Initializing the hinge sensor from a background thread, a constructor that runs before the Activity is available, a background service context, or during app startup before ContextHelper.Current is populated.","solutions":["Ensure FoldableHingeAngleSensor (and HasHinge checks) run on the UI thread where ContextHelper.Current is guaranteed to be the Activity.","Post the initialization to the UI thread: run on CoreDispatcher / Activity.RunOnUiThread before constructing the sensor.","Delay sensor initialization until after the Activity's OnCreate/OnResume."],"exampleFix":"// before: constructed off UI thread or before Activity ready\nvar sensor = new FoldableHingeAngleSensor(owner); // ContextHelper.Current not Activity → throws\n\n// after: dispatch to UI thread\nawait Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>\n{\n    if (ContextHelper.Current is Android.App.Activity)\n    {\n        var sensor = new FoldableHingeAngleSensor(owner);\n    }\n});","handlingStrategy":"validation","validationCode":"// Before constructing the sensor, verify we are on the UI thread with an Activity\nif (ContextHelper.Current is Android.App.Activity activity)\n{\n    var sensor = new FoldableHingeAngleSensor(owner);\n}","typeGuard":null,"tryCatchPattern":"try {\n    var sensor = new FoldableHingeAngleSensor(owner);\n} catch (InvalidOperationException ex) when (ex.Message.Contains('UI Thread')) {\n    // Re-dispatch to the UI thread and retry\n}","preventionTips":["Initialize FoldableHingeAngleSensor on the UI thread (e.g. in OnCreate/OnResume).","Guard HasHinge checks behind a UI-thread assertion.","Delay sensor construction until ContextHelper.Current is confirmed to be an Activity."],"tags":["android","foldable","sensors","ui-thread","csharp","add-in"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}