Skip to content

Commit 4c94d3a

Browse files
kkafarkligarski
andauthored
chore(examples): add patch to MainActivity to enable predictive back gesture (#3630)
## Description Add patch to MainActivity to enable predictive back gesture This is needed, because for some reason, currently the `OnBackPressedCallback` from `ReactActivity` is added after the one from fragment manager, effectively preventing native dismiss & navigation from working properly. This should be either fixed directly on RN side or we'll need to make it into an installation step / alternatively we can create a hacky workaround & handle this on our end. This is added only to FabricExample because we do plan to support predictive back gesture only on new architecture with new stack implementation. ## Changes This patch uses reflection to disable the `ReactAcctivity`'s `OnBackPressedCallback`. It effectively disables `BackPressed` API from `react-native`. We should be fine with it for now. ## Checklist - [ ] Included code example that can be used to test this change. - [ ] Updated / created local changelog entries in relevant test files. - [ ] For visual changes, included screenshots / GIFs / recordings documenting the change. - [ ] For API changes, updated relevant public types. - [ ] Ensured that CI passes --------- Co-authored-by: Krzysztof Ligarski <63918941+kligarski@users.noreply.github.com>
1 parent 4123e4b commit 4c94d3a

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

FabricExample/android/app/src/main/java/com/fabricexample/MainActivity.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ class MainActivity : ReactActivity() {
2525
override fun onCreate(savedInstanceState: Bundle?) {
2626
supportFragmentManager.fragmentFactory = RNScreensFragmentFactory()
2727
super.onCreate(savedInstanceState)
28+
29+
// Remove this once we have sensible workaround to disable the react callback.
30+
// Currently it prevents fragment manager's callback from triggering, blocking
31+
// native-pop & predictive back gesture.
32+
// See: https://github.com/software-mansion/react-native-screens/pull/3630
33+
try {
34+
val field = ReactActivity::class.java.getDeclaredField("mBackPressedCallback")
35+
field.isAccessible = true
36+
val callback = field.get(this) as androidx.activity.OnBackPressedCallback
37+
callback.isEnabled = false // <--- KILL SWITCH
38+
} catch (e: Exception) {
39+
e.printStackTrace()
40+
}
2841
}
2942

3043
override fun onAttachedToWindow() {

0 commit comments

Comments
 (0)