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

This commit is contained in:
Sam Lantinga
2026-07-29 19:45:40 -07:00
parent 91863e149a
commit 6880bed495

View File

@@ -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);
}