Example of Hardware back button detection using BackHandler
suggest changeBackAndroid is deprecated. Use BackHandler instead.
The listener has to be a method React will call, so the name is componentWillMount, not ComponentWillMount. Register it from the component so this is that component. onMainScreen and goBack are yours to implement. Return true when you have handled the press.
import { BackHandler } from 'react-native';
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', () => {
if (!this.onMainScreen()) {
this.goBack();
return true;
}
return false;
});
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents