Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to Relaunch Your Unity Android Application
/// <summary>
/// Restarts our Android application and re-opens the process.
/// </summary>
private static void RestartAndroidProcess()
{
using AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000;
const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000;
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject intent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage",
Application.identifier);
// Start new activity
intent.Call<AndroidJavaObject>("setFlags",
kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK);
currentActivity.Call("startActivity", intent);
currentActivity.Call("finish");
// Kill current process
AndroidJavaClass process = new AndroidJavaClass("android.os.Process");
int pid = process.CallStatic<int>("myPid");
process.CallStatic("killProcess", pid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment