Last active
May 10, 2022 19:06
-
-
Save adrian-miasik/7983c00fdb868e2d202dd81e8dc21e4f to your computer and use it in GitHub Desktop.
How to Relaunch Your Unity Android Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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