1

I am trying to make the application quit itself when you press the back/return key of a Android phone.

I tried putting this code in the update of a script attached to a gameObject that is present in all my scenes, because of DontDestroyOnLoad:

if (Input.GetKeyDown(KeyCode.Escape)) 
{ 
    Application.Quit(); 
}

I just tested something to see if I get inside the if, when playing on my phone. And I get there. Application.Quit() is the issue here. I hope someone can help me and explain how I can quit the app in Android.

Thanks! :)

Programmer
  • 121,791
  • 22
  • 236
  • 328
Ricardo Frederiks
  • 127
  • 1
  • 3
  • 13
  • maybe this could help you : http://answers.unity3d.com/questions/560661/how-do-i-get-inputgetkeydownkeycodeescape-to-regis.html . "I find that on older phones (on Gingerbread), GetKey() doesn't work either- however, Input.GetAxis() does (once I define an axis that uses menu or escape)" -davididev – Greg Aug 11 '17 at 13:53
  • 1.Where did you put `if (Input.GetKeyDown(KeyCode.Escape))`? You have to narrow down your problem and determine if `Input.GetKeyDown` is being called at-all on Android or if `Application.Quit();` is the issue. – Programmer Aug 11 '17 at 13:54
  • @Programmer I narrowed it down and it seems that Application.Quit() is the issue. I can't figure out why because everywhere on the web it says it should work on Android. – Ricardo Frederiks Aug 11 '17 at 16:37
  • I edited the question to make it more clear that Application.Quit() is the issue. – Ricardo Frederiks Aug 11 '17 at 16:44
  • See my answer for other options you have. – Programmer Aug 11 '17 at 16:51

3 Answers3

3

I know Application.Quit() does not work in the editor,

Yes, it does not work in the Editor but you can use UnityEditor.EditorApplication.isPlaying in the Editor but you have to wrap it around the UNITY_EDITOR macro. See the example below:

void Quit()
{
    #if UNITY_EDITOR
    UnityEditor.EditorApplication.isPlaying = false;
    #else
    Application.Quit();
    #endif
}

I narrowed it down and it seems that Application.Quit() is the issue. I can't figure out why because everywhere on the web it says it should work on Android

The Application.Quit() function should work on Android. If it doesn't then that's a bug.

There is another work-around in Unity.

Here are other workarounds:

1. Set Input.backButtonLeavesApp to true in the Awake function. It should automatically exit the app when the back button is pressed on Android.

void Awake()
{
    Input.backButtonLeavesApp = true;
}

2. If that does not work then kill the process:

System.Diagnostics.Process.GetCurrentProcess().Kill();

If this does not work too then file for a bug report through the Editor.

Programmer
  • 121,791
  • 22
  • 236
  • 328
1

Application.Quit() should work, at least it does in my apps.

Here's another, android specific, way of quitting your app in a somewhat controlled way (better than just killing the process):

AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("finish");
joe1806772
  • 506
  • 6
  • 20
0

Just a little added note for editor App runtime: ExitPlaymode();

    #if UNITY_EDITOR
    UnityEditor.EditorApplication.ExitPlaymode();
    #else
    Application.Quit();
    #endif

works aswell (with LTS 2019.4.16f)