'Hardware Backbutton In react native android not working

I am using react native with navigation. I did not notice but suddenly the back button is not working in the entire app. The header back button on left is working absolutely fine however, the hardware button is not working to go back to the previous screen.

Could anyone suggest anything to solve this?



Solution 1:[1]

import the BackHandler from "react-native"
Add in the componentDidMount - >
BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);

Implement handleBackButton -> 

handleBackButton(){
    this.props.navigation.popToTop();
    return true;
  }

**popToTop** goes back to the first screen in the stack.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 R Desilva