From ee07f60b07617fb470ec74294b5148a0dd8ec3aa Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Sun, 11 Jun 2023 21:04:46 -0700 Subject: [PATCH] Fix issue causing the last edited project to open while switching to another one. Fixes https://github.com/godotengine/godot/issues/76562 --- .../java/editor/src/main/AndroidManifest.xml | 6 +- .../org/godotengine/editor/GodotEditor.kt | 68 +++++++++++++++---- .../godot/utils/ProcessPhoenix.java | 9 ++- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/platform/android/java/editor/src/main/AndroidManifest.xml b/platform/android/java/editor/src/main/AndroidManifest.xml index 95c1acbfe5e..cc89b759c8f 100644 --- a/platform/android/java/editor/src/main/AndroidManifest.xml +++ b/platform/android/java/editor/src/main/AndroidManifest.xml @@ -35,7 +35,7 @@ @@ -53,7 +53,7 @@ android:name=".GodotEditor" android:configChanges="orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode" android:process=":GodotEditor" - android:launchMode="singleTask" + android:launchMode="singleInstance" android:screenOrientation="userLandscape" android:exported="false"> ? + val processNameSuffix: String + when (godotInstanceId) { GAME_ID -> { - GAME_PROCESS_NAME_SUFFIX + processNameSuffix = GAME_PROCESS_NAME_SUFFIX + targetClass = GodotGame::class.java } EDITOR_ID -> { - EDITOR_PROCESS_NAME_SUFFIX + processNameSuffix = EDITOR_PROCESS_NAME_SUFFIX + targetClass = GodotEditor::class.java } PROJECT_MANAGER_ID -> { - PROJECT_MANAGER_PROCESS_NAME_SUFFIX + processNameSuffix = PROJECT_MANAGER_PROCESS_NAME_SUFFIX + targetClass = GodotProjectManager::class.java + } + else -> { + processNameSuffix = "" + targetClass = null } - else -> "" } + + if (targetClass == javaClass) { + Log.d(TAG, "Force quitting $targetClass") + ProcessPhoenix.forceQuit(this) + return true + } + if (processNameSuffix.isBlank()) { return false } @@ -208,8 +242,16 @@ open class GodotEditor : FullScreenGodotApp() { val runningProcesses = activityManager.runningAppProcesses for (runningProcess in runningProcesses) { if (runningProcess.processName.endsWith(processNameSuffix)) { - Log.v(TAG, "Killing Godot process ${runningProcess.processName}") - Process.killProcess(runningProcess.pid) + if (targetClass == null) { + // Killing process directly + Log.v(TAG, "Killing Godot process ${runningProcess.processName}") + Process.killProcess(runningProcess.pid) + } else { + // Activity is running; sending a request for self termination. + Log.v(TAG, "Sending force quit request to $targetClass running on process ${runningProcess.processName}") + val forceQuitIntent = Intent(this, targetClass).putExtra(EXTRA_FORCE_QUIT, true) + startActivity(forceQuitIntent) + } return true } } diff --git a/platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.java b/platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.java index 2cc37b627a8..9c7a8040201 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.java +++ b/platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.java @@ -26,6 +26,7 @@ import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.os.Process; import java.util.ArrayList; @@ -90,8 +91,12 @@ public final class ProcessPhoenix extends Activity { */ public static void forceQuit(Activity activity, int pid) { Process.killProcess(pid); // Kill original main process - activity.finish(); - Runtime.getRuntime().exit(0); // Kill kill kill! + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + activity.finishAndRemoveTask(); + } else { + activity.finish(); + } + Runtime.getRuntime().exit(0); // Kill kill kill! } // -- GODOT end --