Skip to content

Commit b9fceb1

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Add BackHandler to dismiss LogBox toasts on back press
Summary: On Android, pressing the hardware back button while LogBox notification toasts or the full inspector overlay are visible has no effect on the JS side. The only way to dismiss notifications is the on-screen X button, and the only way to close the inspector is via Minimize/Dismiss. This adds `BackHandler` listeners to both JS containers: **Notification toasts**: A new `LogBoxNotificationBackHandler` component mounts alongside the toasts and registers a `hardwareBackPress` listener that calls `clearWarnings()` + `clearErrors()`, equivalent to pressing X on every visible toast. The component returns null and auto-cleans the listener on unmount. **Inspector overlay**: `LogBoxInspectorContainer` registers a `hardwareBackPress` listener in `componentDidMount` that calls `_handleMinimize()` (`setSelectedLog(-1)`), closing the overlay non-destructively — same as pressing the Minimize button. Changelog: [Android][Added] - Allow LogBox notification toasts and inspector overlay to be dismissed via Android back button Differential Revision: D101178179
1 parent f8fa76f commit b9fceb1

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

packages/react-native/Libraries/LogBox/LogBoxInspectorContainer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type LogBoxLog from './Data/LogBoxLog';
1212

1313
import View from '../Components/View/View';
1414
import StyleSheet from '../StyleSheet/StyleSheet';
15+
import BackHandler from '../Utilities/BackHandler';
1516
import * as LogBoxData from './Data/LogBoxData';
1617
import LogBoxInspector from './UI/LogBoxInspector';
1718
import * as React from 'react';
@@ -23,6 +24,27 @@ type Props = Readonly<{
2324
}>;
2425

2526
export class _LogBoxInspectorContainer extends React.Component<Props> {
27+
_backHandler: ?{remove: () => void, ...} = null;
28+
29+
componentDidMount() {
30+
this._backHandler = BackHandler.addEventListener(
31+
'hardwareBackPress',
32+
() => {
33+
if (this.props.selectedLogIndex < 0) {
34+
return false;
35+
}
36+
this._handleMinimize();
37+
return true;
38+
},
39+
);
40+
}
41+
42+
componentWillUnmount() {
43+
if (this._backHandler) {
44+
this._backHandler.remove();
45+
}
46+
}
47+
2648
render(): React.Node {
2749
return (
2850
<View style={StyleSheet.absoluteFill}>

packages/react-native/Libraries/LogBox/LogBoxNotificationContainer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import SafeAreaView from '../../src/private/components/safeareaview/SafeAreaView_INTERNAL_DO_NOT_USE';
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 from './Data/LogBoxLog';
1617
import LogBoxLogNotification from './UI/LogBoxNotification';
@@ -22,6 +23,31 @@ type Props = Readonly<{
2223
isDisabled?: ?boolean,
2324
}>;
2425

26+
function LogBoxNotificationBackHandler({
27+
logs,
28+
}: {
29+
logs: ReadonlyArray<LogBoxLog>,
30+
}): React.Node {
31+
const logsRef = React.useRef(logs);
32+
logsRef.current = logs;
33+
34+
React.useEffect(() => {
35+
const subscription = BackHandler.addEventListener(
36+
'hardwareBackPress',
37+
() => {
38+
if (logsRef.current.length === 0) {
39+
return false;
40+
}
41+
LogBoxData.clearWarnings();
42+
LogBoxData.clearErrors();
43+
return true;
44+
},
45+
);
46+
return () => subscription.remove();
47+
}, []);
48+
return null;
49+
}
50+
2551
export function _LogBoxNotificationContainer(props: Props): React.Node {
2652
const {logs} = props;
2753

@@ -60,6 +86,7 @@ export function _LogBoxNotificationContainer(props: Props): React.Node {
6086
);
6187
return (
6288
<SafeAreaView style={styles.list}>
89+
<LogBoxNotificationBackHandler logs={logs} />
6390
{warnings.length > 0 && (
6491
<View style={styles.toast}>
6592
<LogBoxLogNotification

0 commit comments

Comments
 (0)