---
title: "Handle Android Back in a C# game menu"
url: https://recursiveai.org/onegame/cookbook/handle-android-back-in-game-menu/
description: "Close a C# game’s pause menu when Android sends Back. Test the same button or gesture event on your desktop with OneGame’s input injector."
version: "2.5.3"
---

# Handle Android Back in a C# game menu

**onegame** is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGame.Host) · [GitHub](https://github.com/recursive-ai-oss/onegame) · [Docs](https://recursiveai.org/onegame/docs/) · [Download example (.zip)](https://recursiveai.org/onegame/cookbook/handle-android-back-in-game-menu/handle-android-back-in-game-menu.zip)

Close the pause menu in `OnBack` and return `true`. This handles both the Android Back button and gesture without quitting.

```shell
dotnet add package RecursiveAI.OneGame.Host
```

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

```csharp
using OneGame.Core;
using OneGame.Host;
using OneGame.UI;

sealed class MenuGame : GameDefinition
{
    private Element? menu;

    public override bool OnBack(
        GameInstance game)
    {
        if (menu?.Parent is null) return false;
        menu.RemoveFromParent();
        return true;
    }

    public override string Name => "MenuGame";
    public override void RegisterEntities(
        EntityRegistry registry) { }
    public override void Setup(World world) { }
    public override void DefineScreens(
        ScreenSet screens) =>
        screens.Add("main", _ =>
            Ui.Column(menu = Ui.Label("Paused")));

    static void Main()
    {
        var definition = new MenuGame();
        var options = new GameOptions
        {
            Headless = true, Gpu = GpuMode.Off
        };
        using var game = GameInstance.Create(
            definition, options);
        game.Step();
        var injector = game.Platform.Injector;
        injector.Back(game.Window.Id);
        game.DrainInput();
        bool closed =
            definition.menu?.Parent is null;
        Console.WriteLine(closed);
        Console.WriteLine(game.QuitRequested);
    }
}
```

Prints whether the menu closed, then whether the game asked to quit:

```text
True
False
```

Unzip [the example](https://recursiveai.org/onegame/cookbook/handle-android-back-in-game-menu/handle-android-back-in-game-menu.zip), then `dotnet run`.

## The full program

The example shows a `Paused` label as the menu. The first headless frame attaches that label to the UI; `Injector.Back` then queues a system-back event, and `DrainInput()` processes it. The example sends Back twice so you can see what happens once there’s no menu left. In your Android game, carry over only the `OnBack` override, not the input injector or the test settings `Headless = true, Gpu = GpuMode.Off`.

```csharp
using OneGame.Core;
using OneGame.Host;
using OneGame.UI;

sealed class MenuGame : GameDefinition
{
    private Element? menu;

    public override bool OnBack(GameInstance game)
    {
        if (menu?.Parent is null) return false;
        menu.RemoveFromParent();
        return true;
    }

    public override string Name => "MenuGame";
    public override void RegisterEntities(EntityRegistry registry) { }
    public override void Setup(World world) { }
    public override void DefineScreens(ScreenSet screens) =>
        screens.Add("main", _ => Ui.Column(menu = Ui.Label("Paused")));

    static void Main()
    {
        var definition = new MenuGame();
        using var game = GameInstance.Create(definition,
            new GameOptions { Headless = true, Gpu = GpuMode.Off });
        game.Step();
        Console.WriteLine($"Menu open: {definition.menu?.Parent is not null}");
        game.Platform.Injector.Back(game.Window.Id);
        game.DrainInput();
        Console.WriteLine($"Menu open: {definition.menu?.Parent is not null}");
        Console.WriteLine($"Quit requested: {game.QuitRequested}");
        game.Platform.Injector.Back(game.Window.Id);
        game.DrainInput();
        Console.WriteLine($"Second back requests quit: {game.QuitRequested}");
    }
}
```

```text
Menu open: True
Menu open: False
Quit requested: False
Second back requests quit: True
```

If `OnBack` returns `false` and no UI element handles `Cancel`, the host requests to quit.

## Variations

To process Back along with the next frame instead of immediately, replace the **first** `DrainInput()` in the full example with `Step()`:

```csharp
game.Platform.Injector.Back(game.Window.Id);
game.Step();
```

## Related recipes

- [Navigate a game menu with a controller](https://recursiveai.org/onegame/cookbook/navigate-game-menu-with-controller/)
- [Build a game UI screen](https://recursiveai.org/onegame/cookbook/game-ui-screen/)
- [Export a game to Android](https://recursiveai.org/onegame/cookbook/export-web-android-ios/)

---

## The short answer

Override `GameDefinition.OnBack`. If the pause menu is open, close it and return `true`; the host stops dispatching Back there. If there’s nothing for the game to close, return `false`. The UI then has a chance to handle `Cancel`. If that goes unhandled too, the app asks to quit.

## How do you handle the Back button on Android without exiting the game?

[OneGame’s platform guide](https://github.com/recursive-ai-oss/onegame/blob/108780cf6e4f6df4e5922271e04433340db6479d/docs/interfaces/platform.md#L127) describes the event as system Back on key release, not a regular game action. The Android gesture arrives through the same callback. You don’t need a key binding or separate gesture detector. The callback runs on the game thread while the host handles input, so the example removes the menu there.

Use the menu’s real open state. In the sample, `menu.Parent` tells you whether the menu is still attached. That also works if another button closes it first: the next Back won’t get swallowed by a stale Boolean flag. If your menu lives in a screen stack, check which screen is on top and pop just that screen. This makes one press do one thing rather than closing the entire stack.

## What should Back do on the title screen?

A title screen with no overlay can leave Back unhandled. If an overlay is open, dismiss it first and consume that event so the same press doesn’t also quit the game. The [host’s Back dispatch](https://github.com/recursive-ai-oss/onegame/blob/108780cf6e4f6df4e5922271e04433340db6479d/src/OneGame.Host/GameInstance.cs#L954-L966) checks `OnBack` before UI `Cancel`, so don’t consume Back in the game handler when you want the UI to handle it instead.

Test the two presses separately. After the first, check the menu is gone **and** `QuitRequested` is false. After the second, check `QuitRequested` with no UI left to cancel. The input injector covers the game and UI response without an Android export; it doesn’t exercise a phone’s gesture recognition.
