Skip to content

Commit 8629a34

Browse files
Abbondanzometa-codesync[bot]
authored andcommitted
Allow LogBox and RedBox overlay dismissal via Android back button (#56405)
Summary: Pull Request resolved: #56405 On Android, pressing the hardware back button while the LogBox inspector is open does nothing. `LogBoxDialog` is explicitly set to `setCancelable(false)` and there's no `onBackPressed()` override, so the back button is completely swallowed. The only way to close the inspector is the on-screen Minimize/Dismiss buttons. For RedBox, the dialog is cancelable by default so back press does dismiss it visually, but it bypasses `hideRedboxDialog()`. The dialog reference and content view don't get cleaned up, leaving stale state. This adds proper back button handling for both: **LogBox**: `LogBoxDialog` now takes an `onRequestClose` callback and overrides `onBackPressed()` to invoke it. The surface delegate passes a callback that emits `hardwareBackPress` to the JS runtime, where a new `BackHandler` listener in `LogBoxInspector` calls `onMinimize()`. This keeps `setCancelable(false)` in place (so tapping outside the dialog doesn't dismiss it) while still running the full dismiss flow through JS: state reset, `NativeLogBox.hide()`, dialog cleanup. **RedBox**: The anonymous `Dialog` subclass now overrides `onBackPressed()` to call `hideRedboxDialog()` directly, which dismisses the dialog, destroys the content view, and nulls the reference. Changelog: [Internal] - Allow LogBox and RedBox overlays to respond to Android back button press Reviewed By: alanleedev Differential Revision: D100081099 fbshipit-source-id: cabec9b75fb3e733662457bc34de006a45a11494
1 parent 0d1b70e commit 8629a34

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/react-native/Libraries/LogBox/UI/LogBoxInspector.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import Keyboard from '../../Components/Keyboard/Keyboard';
1212
import View from '../../Components/View/View';
1313
import StyleSheet from '../../StyleSheet/StyleSheet';
14+
import BackHandler from '../../Utilities/BackHandler';
1415
import * as LogBoxData from '../Data/LogBoxData';
1516
import LogBoxLog, {type LogLevel} from '../Data/LogBoxLog';
1617
import LogBoxInspectorBody from './LogBoxInspectorBody';
@@ -30,7 +31,7 @@ type Props = Readonly<{
3031
}>;
3132

3233
export default function LogBoxInspector(props: Props): React.Node {
33-
const {logs, selectedIndex} = props;
34+
const {logs, selectedIndex, onMinimize} = props;
3435
let log = logs[selectedIndex];
3536

3637
useEffect(() => {
@@ -55,6 +56,17 @@ export default function LogBoxInspector(props: Props): React.Node {
5556
Keyboard.dismiss();
5657
}, []);
5758

59+
useEffect(() => {
60+
const subscription = BackHandler.addEventListener(
61+
'hardwareBackPress',
62+
() => {
63+
onMinimize();
64+
return true;
65+
},
66+
);
67+
return () => subscription.remove();
68+
}, [onMinimize]);
69+
5870
function _handleRetry() {
5971
LogBoxData.retrySymbolicateLogNow(log);
6072
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/LogBoxDialogSurfaceDelegate.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ internal class LogBoxDialogSurfaceDelegate(private val devSupportManager: DevSup
6666

6767
dialog = LogBoxDialog(context, reactRootView)
6868
dialog?.let { dialog ->
69-
dialog.setCancelable(false)
69+
dialog.setCancelable(true)
70+
dialog.setCanceledOnTouchOutside(false)
71+
dialog.setOnCancelListener {
72+
devSupportManager.currentReactContext?.emitDeviceEvent("hardwareBackPress")
73+
}
7074
dialog.show()
7175
}
7276
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialogSurfaceDelegate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ internal class RedBoxDialogSurfaceDelegate(private val devSupportManager: DevSup
125125
.apply {
126126
requestWindowFeature(Window.FEATURE_NO_TITLE)
127127
setContentView(checkNotNull(redBoxContentView))
128+
setOnCancelListener { devSupportManager.hideRedboxDialog() }
128129
}
129130
}
130131
dialog?.show()

0 commit comments

Comments
 (0)