SDL: Android: make sure the activity gets really finished

From 2a8f85938e2cff535e8817c9ef0e47b24b557945 Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Wed, 5 Apr 2023 21:54:36 +0200
Subject: [PATCH] Android: make sure the activity gets really finished

---
 .../src/main/java/org/libsdl/app/SDLActivity.java    | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index de03ba790b28..ff38a337ddbc 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -221,6 +221,7 @@ public enum NativeState {
 
     // This is what SDL runs in. It invokes SDL_main(), eventually
     protected static Thread mSDLThread;
+    protected static int mIsFinishing = 0;
 
     protected static SDLGenericMotionListener_API12 getMotionListener() {
         if (mMotionListener == null) {
@@ -324,6 +325,16 @@ protected void onCreate(Bundle savedInstanceState) {
         Log.v(TAG, "onCreate()");
         super.onCreate(savedInstanceState);
 
+
+        /* Really finish the activity, even if user try to re-start it quickly */
+        if (mIsFinishing != 0 && mIsFinishing < 5) {
+            Log.v(TAG, "finish activity");
+            mIsFinishing += 1;
+            /* Make it clear the activity has to be destroyed, even if finish() seems not to be required here */
+            finish();
+            return;
+        }
+
         try {
             Thread.currentThread().setName("SDLActivity");
         } catch (Exception e) {
@@ -1902,6 +1913,7 @@ public void run() {
         if (SDLActivity.mSingleton != null && !SDLActivity.mSingleton.isFinishing()) {
             // Let's finish the Activity
             SDLActivity.mSDLThread = null;
+            SDLActivity.mIsFinishing = 1;
             SDLActivity.mSingleton.finish();
         }  // else: Activity is already being destroyed