Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneGame.Host

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
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, 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.

C#
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():

C#
game.Platform.Injector.Back(game.Window.Id);
game.Step();

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 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 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.

All onegame recipes