BackHandler React Native
Overview
In a React Native app, managing the back button functionality is vital for a seamless user experience. The BackHandler module allows control over the behavior when the user presses the back button on Android or hardware back button on certain devices. It provides methods and event listeners for custom actions and navigation control on back button presses.
Pattern
The pattern for using the BackHandler module in React Native involves the following steps :
-
Import the BackHandler module:
Start by importing the BackHandler module from the React Native core library :
-
Add an Event Listener:
Next, add an event listener to capture the back button press event. This event listener will define the custom logic to be executed when the back button is pressed. The event listener is typically added in the componentDidMount lifecycle method for class components or within the useEffect hook for function components.
-
Define the Back Button Handling Logic :
In the event listener, define the custom logic to be executed when the back button is pressed. This can include actions like navigating to the previous screen, showing a confirmation dialog before exiting the app, or any other desired behavior.
-
Remove the Event Listener (if necessary) :
If you no longer need the event listener, make sure to remove it to avoid memory leaks. This is typically done in the componentWillUnmount lifecycle method for class components or by returning a cleanup function from the useEffect hook for function components.
By following this pattern, you can effectively use the BackHandler module in React Native to handle back button events and define the desired behavior when the back button is pressed. Remember to define the appropriate handling logic and ensure that the event listener is properly added and removed to maintain the integrity of your application's navigation flow.
Example
Here's an example of using the BackHandler module in both a function component and a class component.
Function Component
Class Component
Usage with React Navigation
React Navigation is a popular navigation library for React Native applications. To integrate the BackHandler module with React Navigation, you can follow the steps below :
-
Import the required modules:
In your component file, import the necessary modules from React Navigation and the BackHandler module :
-
Define the back button handling logic:
Inside your component, define the logic to handle the back button press. This logic can include actions like navigating to the previous screen, showing a confirmation dialog before exiting the app, or any other desired behavior.
-
Use the useFocusEffect hook :
Wrap the back button handling logic inside the useFocusEffect hook provided by React Navigation. This hook allows you to run effects when the screen comes into focus and clean them up when it goes out of focus.
Note: Make sure to pass an empty dependency array [] as the second argument to the useFocusEffect hook to ensure that the effect is only run once when the screen mounts and is cleaned up when the screen unmounts.
-
Complete the component:
Finish the component implementation, including the rendering logic and any other functionality you require.
By using the useFocusEffect hook from React Navigation, you can add the necessary event listener for the back button and remove it when the screen loses focus. This approach ensures that the back button handling is specific to the currently focused screen and avoids conflicts or unexpected behavior across different screens in your navigation stack.
Make sure to import the necessary modules, define the back button handling logic, and use the useFocusEffect hook to integrate the BackHandler module with React Navigation effectively. This enables you to control the back button behavior within your React Native app's navigation flow.
Backhandler hook
The BackHandler React Native provides a hook called useBackHandler (introduced in React Native version 0.62) that simplifies the usage of the BackHandler module in function components. The useBackHandler hook allows you to define the back button action directly within the component function.

Methods
addEventListener(eventName, handler):
The addEventListener() method is used to register an event listener for a specific event. In the case of the BackHandler module, the event is typically 'hardwareBackPress', which corresponds to the back button press event on Android devices or the hardware back button on some physical devices.
Syntax:
This method returns an instance of BackHandlerSubscription, which can be stored in a variable (e.g., backHandler). This subscription allows you to later remove the event listener using the removeEventListener() method.
Example:
exitApp():
The exitApp() method is used to exit the application programmatically. When called, this method terminates the app and returns the user to the operating system or home screen. It effectively simulates the behavior of the user pressing the home button or navigating away from the app.
Syntax:
Example:
removeEventListener(eventName, handler):
The removeEventListener() method is used to remove a previously registered event listener. It takes the event name and the handler function as arguments. The event listener will no longer respond to the specified event after it is removed.
Syntax:
Example:
By using these methods provided by the BackHandler module, you can add event listeners for the back button press event, exit the app programmatically, and remove event listeners when they are no longer needed. This allows you to customize the back button behavior and control the navigation flow in your React Native application.
Conclusion
- The BackHandler React Native allows you to handle the hardware back button events on Android devices and some physical devices.
- By using the BackHandler module, you can define custom logic when the back button is pressed, such as navigating to the previous screen, showing a confirmation dialog before exiting the app or performing any other desired action.
- The BackHandler module provides methods like addEventListener(), exitApp(), and removeEventListener() to manage the back button behavior.
- The addEventListener() method registers an event listener for the 'hardwareBackPress' event and allows you to define the back button handling logic.
- The exitApp() method is used to exit the application programmatically, simulating the behavior of the user pressing the home button or navigating away from the app.
- The removeEventListener() method removes a previously registered event listener, preventing it from responding to the specified event.
- It is important to properly add and remove event listeners to avoid memory leaks and ensure the integrity of your application's navigation flow.
- The BackHandler module can be used in both function components and class components, with different patterns for each.
- Integration with React Navigation is possible by using the useFocusEffect hook or adding the event listener within lifecycle methods.
- BackHandler provides a way to enhance the user experience by controlling the back button behavior and implementing custom actions based on the application's requirements.