How to prevent the back button on android from closing the app

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dava

The title says it all

1 Like
:bust_in_silhouette: Reply From: volzhs

Uncheck Quit On Go Back options at Project settings > General > Application > Config for Godot 3.0
for 2.x, uncheck Auto Accept Quit

Note that you can also add custom behavior to handle quit requests, see Handling quit requests in the Godot documentation.

Calinou | 2018-03-21 11:08

1 Like

Just in case anyone ends up here…

Unchecking Quit On Go Back works but if you still want to quit the app when pressing back when on the main screen this is what worked for me (C#):

public override void _Ready()
{
    GetTree().Root.GoBackRequested += OnBackMobile;
}

...

private void OnBackMobile()
{
    if (!Engine.HasSingleton("AndroidRuntime")) { return; }

    var android = Engine.GetSingleton("AndroidRuntime");
    if (android != null)
    {
        var activity = android.Call("getActivity").AsGodotObject();
        activity?.Call("moveTaskToBack", true);
    }
}