Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

withAndroidPredictiveBackCompat.cjs

99 lines3.5 KB

1const { withMainActivity } = require("expo/config-plugins");
2​
3// react-native 0.85 registers its own always-enabled callback, but only on Android 16 with
4// targetSdk 36 (ReactActivity's enforced-predictive-back workaround) — on Android 13-15
5// nothing is registered, the system consumes every back gesture itself, and JS back handling
6// (React Navigation pops, BackHandler listeners) silently dies: each gesture just backgrounds
7// the app.
8​
9const CALLBACK_PROPERTY = `
10 // Routes predictive-back gestures to JS on Android 13-15, where react-native
11 // registers no OnBackPressedDispatcher callback of its own (it only does on
12 // Android 16+). Registered in onCreate; added by withAndroidPredictiveBackCompat.
13 private val predictiveBackCompatCallback = object : OnBackPressedCallback(true) {
14 override fun handleOnBackPressed() {
15 isEnabled = false
16 onBackPressed()
17 isEnabled = true
18 }
19 }
20`;
21​
22const CALLBACK_REGISTRATION = `
23 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && Build.VERSION.SDK_INT < Build.VERSION_CODES.BAKLAVA) {
24 onBackPressedDispatcher.addCallback(this, predictiveBackCompatCallback)
25 }`;
26​
27// Wraps the template's invokeDefaultOnBackPressed: the default action ends in
28// ComponentActivity.onBackPressed(), which re-enters the dispatcher — with the
29// compat callback still enabled that would bounce back into JS forever instead
30// of backgrounding the app.
31const INVOKE_DEFAULT_WRAPPER = `override fun invokeDefaultOnBackPressed() {
32 predictiveBackCompatCallback.isEnabled = false
33 try {
34 invokeDefaultOnBackPressedLegacy()
35 } finally {
36 predictiveBackCompatCallback.isEnabled = true
37 }
38 }
39​
40 private fun invokeDefaultOnBackPressedLegacy() {`;
41​
42function insertAfter(contents, anchor, insertion, description) {
43 const index = contents.indexOf(anchor);
44 if (index === -1) {
45 throw new Error(
46 `withAndroidPredictiveBackCompat: could not find ${description} in MainActivity — the Expo template changed; update the plugin anchors.`,
47 );
48 }
49 const end = index + anchor.length;
50 return contents.slice(0, end) + insertion + contents.slice(end);
52​
53module.exports = function withAndroidPredictiveBackCompat(config) {
54 if (config.android?.predictiveBackGestureEnabled !== true) {
55 return config;
56 }
57​
58 return withMainActivity(config, (nextConfig) => {
59 let contents = nextConfig.modResults.contents;
60 if (nextConfig.modResults.language !== "kt") {
61 throw new Error("withAndroidPredictiveBackCompat: MainActivity must be Kotlin.");
62 }
63 if (contents.includes("predictiveBackCompatCallback")) {
64 return nextConfig;
65 }
66​
67 contents = insertAfter(
68 contents,
69 "import android.os.Bundle",
70 "\nimport androidx.activity.OnBackPressedCallback",
71 "the android.os.Bundle import",
72 );
73 contents = insertAfter(
74 contents,
75 "class MainActivity : ReactActivity() {",
76 CALLBACK_PROPERTY,
77 "the MainActivity class declaration",
78 );
79 contents = insertAfter(
80 contents,
81 "super.onCreate(null)",
82 CALLBACK_REGISTRATION,
83 "the super.onCreate call",
84 );
85​
86 if (!contents.includes("override fun invokeDefaultOnBackPressed() {")) {
87 throw new Error(
88 "withAndroidPredictiveBackCompat: could not find invokeDefaultOnBackPressed in MainActivity — the Expo template changed; update the plugin anchors.",
89 );
90 }
91 contents = contents.replace(
92 "override fun invokeDefaultOnBackPressed() {",
93 INVOKE_DEFAULT_WRAPPER,
94 );
95​
96 nextConfig.modResults.contents = contents;
97 return nextConfig;
98 });
99};