Skip to content

Commit 71ea4bf

Browse files
Handle back gesture on Android 13+
1 parent d756824 commit 71ea4bf

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

android/src/main/java/com/multiplemodals/RNTModalView.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.multiplemodals
33
import android.app.Activity
44
import android.content.Context
55
import android.content.DialogInterface
6+
import android.os.Build
67
import android.view.KeyEvent
78
import android.view.View
89
import android.view.ViewGroup
@@ -120,7 +121,7 @@ class RNTModalView(context: Context): ViewGroup(context), LifecycleEventListener
120121
}
121122

122123
private fun attachBackHandler(dialog: ModalDialog) {
123-
// Prevent closing from native part as this is handled by JS
124+
// Prevent KEYCODE_BACK from closing the dialog and handle the event in JS.
124125
dialog.setOnKeyListener(object : DialogInterface.OnKeyListener {
125126
override fun onKey(dialog: DialogInterface, keyCode: Int, event: KeyEvent): Boolean {
126127
if (event.action != KeyEvent.ACTION_UP) {
@@ -144,6 +145,17 @@ class RNTModalView(context: Context): ViewGroup(context), LifecycleEventListener
144145
return false
145146
}
146147
})
148+
149+
// Register a back handler to prevent closing and handle the event in JS.
150+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
151+
dialog.onBackInvokedDispatcher.registerOnBackInvokedCallback(
152+
android.window.OnBackInvokedDispatcher.PRIORITY_DEFAULT
153+
) {
154+
UiThreadUtil.runOnUiThread {
155+
eventDispatcher.dispatchEvent(PressBackEvent(surfaceId, id))
156+
}
157+
}
158+
}
147159
}
148160

149161
// Child management

0 commit comments

Comments
 (0)