Version: 2023.1
Language : English
Handle Android crashes
Building and delivering for Android

Quit a Unity Android application

The Android operating system has a built-in user interface to hide and close applications (refer to Close apps) so you shouldn’t add your own interface to quit your application. Users recognize Android’s interface as the way to close applications so if you create your own, users will have an inconsistent user experience between your application and other Android applications. If you must programmatically close an Android application, it’s best practice to use Activity.moveTaskToBack instead of Application.Quit. Activity.moveTaskToBack pauses the application and moves it to the background, which is closer to the standard Android application lifecycle than what Application.Quit does. For more information, refer to Processes and app lifecycle.

The following code sample shows how to move your application to the back of the activity stack.

using UnityEngine;

public class QuitApplicationUtility
{
    public static void MoveAndroidApplicationToBack()
    {
        AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
        activity.Call<bool>("moveTaskToBack", true);
    }
}
Handle Android crashes
Building and delivering for Android
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961