Page 1 of 1

Quit program on Android

Posted: Mon Nov 18, 2024 5:33 am
by Arctichorse9
Hi. Using your Menu Framework which is great but have a problem quitting the application on Android. Application quit doesn't work on Android. Unity suggests using something like

Code: Select all

{
        AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
        activity.Call<bool>("moveTaskToBack", true);
    }
But I don't know how to work that into the SaveHelper.cs Halt Program function.

It handles quitting the application on the Unity Standalone and Editor choices but somehow have to add Android. Would it be if UNITY_ANDROID ??

And would it be ok to put the above code Unity suggests after that?

Thanks for any help.

Re: Quit program on Android

Posted: Mon Nov 18, 2024 7:33 am
by Tony Li
Hi,

Yes, that's fine. Normally, I think you just let the player minimize or close the app, but to halt it manually you can add it:

Code: Select all

public void HaltProgram()
{
#if UNITY_ANDROID
    AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    activity.Call<bool>("moveTaskToBack", true);
#else
    Application.Quit();
#endif

#if UNITY_EDITOR
    UnityEditor.EditorApplication.isPlaying = false;
#endif
}

Re: Quit program on Android

Posted: Mon Nov 18, 2024 8:36 pm
by Arctichorse9
This works perfectly on Android. I had to type it in rather than copy and paste because the code editor did not like the pasted quotation marks. : ) Thanks very much.

Re: Quit program on Android

Posted: Mon Nov 18, 2024 9:31 pm
by Tony Li
Glad to help! I think I fixed the quotes issue above. Thanks for letting me know.