Skip to content

Instantly share code, notes, and snippets.

@adrian-miasik
Last active May 10, 2022 19:06
Show Gist options
  • Save adrian-miasik/7983c00fdb868e2d202dd81e8dc21e4f to your computer and use it in GitHub Desktop.
Save adrian-miasik/7983c00fdb868e2d202dd81e8dc21e4f to your computer and use it in GitHub Desktop.
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