---
title: "Handle the Android Back gesture in a C# app"
url: https://recursiveai.org/oneapp/cookbook/handle-android-back-gesture-csharp/
description: "Use OneApp’s BackHandler to return from a detail view on Android, then let the next Back gesture follow the app’s default behavior."
version: "1.2.1"
---

Set `BackHandler` to return from a detail view without leaving your Android app.

```shell
dotnet add package RecursiveAI.OneApp
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
// 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](https://recursiveai.org/oneapp/cookbook/handle-android-back-gesture-csharp/handle-android-back-gesture-csharp.zip) 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](https://recursiveai.org/oneapp/docs/mobile/#tools) plus the OneApp tool:

```shell
dotnet tool install -g RecursiveAI.OneApp.Cli
```

The zip’s `Demo.csproj` already includes OneApp and OneUI. If building the project by hand, add the template compiler too:

```shell
dotnet add package RecursiveAI.OneUI
```
For Android it also sets these properties:

```xml
<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:

```csharp
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:

```html
<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`:

```csharp
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:

```shell
oneapp run . --android
```

The 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:

```text
Back: returned to Orders
```

Swipe 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`:

```csharp
// host is the AppHost created above.
host.MainWindow.BackHandler = null;
```

## Related recipes

- [Build a desktop app with HTML and CSS in C#](https://recursiveai.org/oneapp/cookbook/build-desktop-app-with-html-css/)
- [Install the OneApp dotnet new template](https://recursiveai.org/oneapp/cookbook/install-oneapp-dotnet-new-template/)

## 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](https://recursiveai.org/oneapp/docs/mobile/#back) 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.
