Răsfoiți Sursa

Fixed java.lang.ClassNotFoundException: Didn't find class "android.window.OnBackInvokedCallback" on Android 8.0

Sam Lantinga 1 lună în urmă
părinte
comite
6880bed495

+ 22 - 19
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java

@@ -787,25 +787,7 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
         }
     }
 
-    static OnBackInvokedCallback backButtonCallback = new OnBackInvokedCallback() {
-        Handler mBackKeyHandler;
-
-        @Override
-        public void onBackInvoked() {
-            if (mBackKeyHandler == null) {
-                mBackKeyHandler = new Handler(Looper.getMainLooper());
-            }
-
-            onNativeKeyDown(KeyEvent.KEYCODE_BACK);
-            mBackKeyHandler.postDelayed(new Runnable() {
-                @Override
-                public void run() {
-                    onNativeKeyUp(KeyEvent.KEYCODE_BACK);
-                }
-            }, 500);
-        }
-    };
-
+    static OnBackInvokedCallback backButtonCallback;
     static boolean mBackKeyTrapEnabled = false;
     public static void setBackButtonTrapEnabled(boolean enabled) {
 
@@ -819,6 +801,27 @@ public class SDLActivity extends Activity implements View.OnSystemUiVisibilityCh
             return;
         }
 
+        if (backButtonCallback == null) {
+            backButtonCallback = new OnBackInvokedCallback() {
+                Handler mBackKeyHandler;
+
+                @Override
+                public void onBackInvoked() {
+                    if (mBackKeyHandler == null) {
+                        mBackKeyHandler = new Handler(Looper.getMainLooper());
+                    }
+
+                    onNativeKeyDown(KeyEvent.KEYCODE_BACK);
+                    mBackKeyHandler.postDelayed(new Runnable() {
+                        @Override
+                        public void run() {
+                            onNativeKeyUp(KeyEvent.KEYCODE_BACK);
+                        }
+                    }, 500);
+                }
+            };
+        }
+
         if (enabled) {
             mSingleton.getOnBackInvokedDispatcher().registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT, backButtonCallback);
         }