unoplatform/uno · error · InvalidOperationException
Activity must provide lifecycle events.
Error message
Activity must provide lifecycle events.
What it means
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.
Source
Thrown at src/AddIns/Uno.UI.Foldable/FoldableApplicationViewSpanningRects.Android.cs:40
/// and exposing the properties needed to make UI change when required.
/// FUTURE: implement an event for layout changes, so we can respond to folding state changes in app code too
/// </remarks>
[Preserve]
public partial class FoldableApplicationViewSpanningRects : IApplicationViewSpanningRects, INativeDualScreenProvider
{
private (SurfaceOrientation orientation, List<Rect> result) _previousMode = EmptyMode;
private static readonly (SurfaceOrientation orientation, List<Rect> result) EmptyMode =
((SurfaceOrientation)(-1), null);
private readonly List<Rect> _emptyList = new List<Rect>(0);
public FoldableApplicationViewSpanningRects(object owner)
{
var lifecycleEvents = ViewManagement.ApplicationViewHelper.GetActivityLifecycleEvents();
if (lifecycleEvents is null)
{
throw new InvalidOperationException("Activity must provide lifecycle events.");
}
lifecycleEvents.Create += OnCreateEvent;
lifecycleEvents.Start += OnStartEvent;
lifecycleEvents.Stop += OnStopEvent;
}
private void OnCreateEvent(object sender, Android.OS.Bundle savedInstanceState)
{
windowInfoTrackerCallbackAdapter = new WindowInfoTrackerCallbackAdapter(WindowInfoTracker.Companion.GetOrCreate(ContextHelper.Current as Android.App.Activity));
windowMetricsCalculator = WindowMetricsCalculator.Companion.OrCreate; // HACK: source method is `getOrCreate`, binding generator munges this badly :(
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"DualMode: FoldableApplicationViewSpanningRects.OnCreateEvent");
}
}
private void OnStartEvent(object sender, EventArgs args)
{View on GitHub (pinned to 0418340488)
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.
Example fix
// before: construct unconditionally
var spanning = new FoldableApplicationViewSpanningRects(this); // throws if no lifecycle events
// after: guard before construction
if (ViewManagement.ApplicationViewHelper.GetActivityLifecycleEvents() is not null)
{
var spanning = new FoldableApplicationViewSpanningRects(this);
} Defensive patterns
Strategy: validation
Validate before calling
// Before constructing, verify lifecycle events are available
var lifecycle = ViewManagement.ApplicationViewHelper.GetActivityLifecycleEvents();
if (lifecycle is not null)
{
var spanning = new FoldableApplicationViewSpanningRects(this);
} Try / catch
try {
var spanning = new FoldableApplicationViewSpanningRects(this);
} catch (InvalidOperationException ex) when (ex.Message.Contains('lifecycle events')) {
// Activity does not provide lifecycle events — skip foldable feature
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- FoldableHingeAngleSensor must be initialized on the UI Threa
- Value cannot be null. (Parameter 'storeContext')
- A DragDropExtension has already been enabled
- The current DragDropExtension does not match the provided ar
- No pending drag and drop data.
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/7f63e9204031859b.
Report an issue: GitHub.