Skip to content

Commit d2b6b61

Browse files
alemartSiegeLord
authored andcommitted
Android: handle the back button / gesture on API level 33+ in a way that preserves backward compatibility
1 parent 28fd1b5 commit d2b6b61

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

android/gradle_project/allegro/src/main/java/org/liballeg/android/AllegroActivity.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import java.util.Vector;
2626
import android.os.Build;
2727
import android.view.View;
28+
import android.view.KeyEvent;
29+
import android.window.OnBackInvokedCallback;
30+
import android.window.OnBackInvokedDispatcher;
2831

2932
public class AllegroActivity extends Activity
3033
{
@@ -339,6 +342,22 @@ public void onCreate(Bundle savedInstanceState)
339342
requestWindowFeature(Window.FEATURE_NO_TITLE);
340343
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
341344

345+
if(Build.VERSION.SDK_INT >= 33) {
346+
// handle the back button / gesture on API level 33+
347+
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
348+
OnBackInvokedDispatcher.PRIORITY_DEFAULT, new OnBackInvokedCallback() {
349+
@Override
350+
public void onBackInvoked() {
351+
// these will be mapped to ALLEGRO_KEY_BACK
352+
KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
353+
KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
354+
dispatchKeyEvent(keyDown);
355+
dispatchKeyEvent(keyUp);
356+
}
357+
}
358+
);
359+
}
360+
342361
Log.d("AllegroActivity", "onCreate end");
343362
}
344363

android/gradle_project/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
android:versionCode="1"
55
android:versionName="1.0">
66
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7-
<application android:label="${APP_ID}">
7+
<application android:label="${APP_ID}"
8+
android:enableOnBackInvokedCallback="true">
89
<activity android:name=".MainActivity"
910
android:launchMode="singleTask"
1011
android:screenOrientation="unspecified"

0 commit comments

Comments
 (0)