How to Handle Android Back Button Event in C# Unity Game Engine (Quit, Pause, Etc)
Nov 23, 2017ProgrammingComments (7)
In Unity, the Android back button is treated the same as the "Escape" KeyCode. So in order to do something when the user presses the back button on their Android device, you need to check for the Escape key being pressed down and also make sure the user is on the Android platform (if you're cross-platform).

Here's sample code that quits the app when the back button is pressed:

void Update()
{
// Make sure user is on Android platform
if (Application.platform == RuntimePlatform.Android) {

// Check if Back was pressed this frame
if (Input.GetKeyDown(KeyCode.Escape)) {

// Quit the application
Application.Quit();
}
}
}
Comments (7)
Add a Comment
Tom barnard   Nov 03, 2023
Thanks, very useful
Ehrenmann17   Jan 15, 2022
Thanks
DryreL   Dec 27, 2020
Thank you!
Nick   Sep 19, 2019
Thanks for the catch!
Firestorm200   Sep 19, 2019
Simple syntax mistake here just wanted to notify you thanks for the snippet if (Input.GetKeyDown(KeyCode.Escape) { -> Missing ')' Should be if (Input.GetKeyDown(KeyCode.Escape)) {
Lopus312   Apr 14, 2019
Exactly what I was looking for, thank you
Pride   Mar 06, 2019
Straight to the Point, I love it