On Android, the back button belongs to the system, and when no part of your app handles a press, it exits the app. BackHandler lets your screens intercept that press to close a panel, confirm before throwing away a draft, or let it through. When several components register handlers at once, the registration order and each handler's return value decide what happens.

Key terms

  • BackHandler: the Android-only API that detects the system back action and lets you control how the app responds.
  • hardwareBackPress: the event name passed to BackHandler.addEventListener.
  • BackHandler.exitApp(): invokes the default back button handling, which exits the app.
  • onRequestClose: the Modal prop called when the user presses the hardware back button on Android.

Last registered, first called

Back handlers run in reverse order: the last one registered is called first. On each press, BackHandler loops from the newest handler to the oldest and stops at the first one that returns a truthy value. A handler that returns false, null or undefined passes the press on to the handler registered before it. If none returns true, or none is registered, BackHandler calls exitApp() and the default back behavior exits the app.

BackHandler.addEventListener('hardwareBackPress', handler) returns a subscription, and its remove() deletes that handler from the list. Call it in your effect's cleanup.

The list holds each function once. Registering the same function a second time does nothing, and the function keeps its original position, so it does not move to the front of the line. Since there is only one entry, a single remove() takes it out for every place that registered it.

What returning true means

Returning true means the press is handled. The event stops there, no handler registered earlier runs, and the app does not exit. Returning false lets the event continue to the other listeners and, after them, to the system's default back action. A handler that should only act sometimes returns true in that case and false otherwise.

Each handler receives a HardwareBackPressEvent whose timeStamp is the native time of the press. Return true or false explicitly. The BackHandler reference lists null as a valid return, but the TypeScript type is boolean | undefined, so return null fails type checking.

On iOS, BackHandler is a no-op: addEventListener returns a remove() that does nothing, and exitApp() does nothing either. Shared screens can register handlers without a platform check.

Asking before discarding a draft

The NoteEditor example below handles back only when the note has unsaved edits. With no edits, its handler returns false, so earlier handlers or the default exit take over. With edits, it shows an alert and returns true:

tsx
if (!hasChanges) {
  return false;
}

The press is consumed while the alert is on screen, and the Discard button calls onClose when the user taps it. To exit the app from such a dialog instead, call BackHandler.exitApp() in the button's onPress.

Removing a handler and registering it again changes its place in line. remove() deletes it from the list, and addEventListener pushes each new handler onto the end, which is where the loop starts. A handler that is removed and registered again therefore becomes the last registered and runs first, ahead of handlers added in between.

Back presses while a Modal is open

While a Modal is open, BackHandler publishes no events, so the handlers above stay silent. The back press goes to the modal's onRequestClose instead. The docs mark this prop as required on Android and TV, but its type makes it optional and nothing enforces it. On Android, the modal's dialog intercepts the back key itself and calls onRequestClose rather than closing, which leaves your code to decide whether to hide it:

tsx
<Modal visible={filtersOpen} onRequestClose={() => setFiltersOpen(false)}>

Leave onRequestClose out, and pressing back on Android does nothing. The dialog still intercepts the key and reports the press, but no code hides the modal, so it stays open. Any back logic a modal needs, such as asking before closing a half-filled form, belongs in onRequestClose, because a BackHandler registered by the screen behind it never hears the press.

Code Examples

tsx
import {useEffect, useState} from 'react';
import {Alert, BackHandler, TextInput} from 'react-native';

type Props = {
  initialText: string;
  onClose: () => void;
};

export function NoteEditor({initialText, onClose}: Props) {
  const [text, setText] = useState(initialText);
  const hasChanges = text !== initialText;

  useEffect(() => {
    const subscription = BackHandler.addEventListener(
      'hardwareBackPress',
      () => {
        if (!hasChanges) {
          return false;
        }
        Alert.alert('Discard changes?', 'Your edits to this note will be lost.', [
          {text: 'Keep editing', style: 'cancel'},
          {text: 'Discard', style: 'destructive', onPress: onClose},
        ]);
        return true;
      },
    );
    return () => subscription.remove();
  }, [hasChanges, onClose]);

  return <TextInput multiline value={text} onChangeText={setText} />;
}
✓ Completed