Set BackHandler to return from a detail view without leaving your Android app.
dotnet add package RecursiveAI.OneAppRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
// host: AppHost; page: DetailPage. // BackHandler takes a Func<bool>. // true: you handled Back. // false: let OneApp dismiss an overlay // or leave the app if none is open. host.MainWindow.BackHandler = page.GoBack;
The example is the full program below: unzip it and dotnet run.
dotnet run previews the window on desktop, but desktop platforms don’t raise the Android Back event. To test Back, run oneapp run . --android from the unzipped project on an Android device or emulator.
Full program
To run on Android, connect a device or emulator and install the Android SDK and NDK plus the OneApp tool:
dotnet tool install -g RecursiveAI.OneApp.CliThe zip’s Demo.csproj already includes OneApp and OneUI. If building the project by hand, add the template compiler too:
dotnet add package RecursiveAI.OneUIFor Android it also sets these properties:
<OneAppId>org.example.backdemo</OneAppId> <OneAppName>Back Demo</OneAppName> <InvariantGlobalization>true</InvariantGlobalization> <PublishAot>true</PublishAot>
Program.cs installs the handler before starting the event loop. Keep Main as a static method returning int for the generated Android entry point:
using OneApp; static class Program { public static int Main(string[] args) { var page = new DetailPage(); using var host = AppHost.Create(new AppOptions(page)); host.MainWindow.BackHandler = page.GoBack; host.Run(); return 0; } }
OneUI compiles DetailPage.html into the other half of the DetailPage partial class. The heading reads the Heading property:
<main style="padding: 32px"> <h1 id="screen">{Heading}</h1> <p>Swipe in from the edge to go back.</p> </main>
DetailPage.cs handles the first Back by changing the view’s state. On the Orders screen it returns false:
using OneUI.Components; public partial class DetailPage { [State] public partial bool OnOrders { get; set; } public string Heading => OnOrders ? "Orders" : "Order details"; public bool GoBack() { if (OnOrders) return false; OnOrders = true; Console.WriteLine("Back: returned to Orders"); return true; } }
With an Android device or emulator connected, run:
oneapp run . --androidThe app opens on Order details. With gesture navigation enabled, swipe inward from the left or right edge. On an Android 14 emulator, the heading changed to Orders and the app stayed open. Its log read:
Back: returned to OrdersSwipe again from Orders and Android returns to the previous screen. If you have more than one Android target connected, use --serial to pick the one you intend to test.
Variations
To use OneApp’s default Back behavior for every request, clear the handler instead of assigning page.GoBack:
// host is the AppHost created above. host.MainWindow.BackHandler = null;
Related recipes
The short answer
In a OneApp Android app, assign a Func<bool> to AppWindow.BackHandler. Return true after handling Back yourself, for example after returning from an order’s details to the order list. Return false when your view has nothing to go back to. Don’t return true unconditionally: it also stops the framework from dismissing its own overlays or closing the window.
What is the Back gesture on Android?
With gesture navigation enabled, the user swipes inward from a screen edge to go back. Android’s Back button in three-button navigation requests the same action. BackHandler responds to the Back request; it isn’t an app-level touch handler. You don’t need to track a finger position or decide whether a drag started close enough to the edge. This sample handles the completed Back action, not a predictive Back animation preview.
How do I handle Android Back when a dialog is open?
A custom handler runs before OneApp’s default behavior. If an overlay should close before your view navigates, let that request fall through by returning false. With the handler unset or returning false, OneApp sends Escape to an open modal dialog, menu, or list box. If none is open, it closes the window. Closing the last main window ends the app. See Back in the mobile guide for the order of these actions.
Decide which layer owns navigation before wiring the callback. In this example there’s no overlay, so the detail view handles the first Back and the root view lets OneApp handle the second. If your app puts a dialog over that detail view, check whether the dialog is open before changing the view’s state. That way one gesture dismisses the dialog without taking the user away from the order they were reading.