my app is using react-native 0.42.0 ,in this version there is not BackHandle and Press Android back button immediately exit application ,if i update the react-native version ,it will be a lot of errors ,so
how can i deal with android back correctly on react-native 0.42.0. Anybody can help me?? help!!help!!
I solved this way:
<Router backAndroidHandler={this.onBackPress}>
and:
onBackPress = () => {
if (Actions.state.index === 0) {
return false
}
Actions.pop()
return true
}
This solved my problem
Firstly my BackHandler works fine, but when i'm on Home screen (consider as one or last screen) the app actually navigating to Sign in page(it's throwing the user out of application)
Below are my files
### AppNavigatorContainer.js
class AppNavigatorContainer extends Component {
addListener = createReduxBoundAddListener("root")
constructor(props) {
super(props)
this.state = {
didReceiveNotificationThatCanBeHandled: false,
notification: null,
didNetworkBecomeUnreachable: false
}
this.onBackPress = this.onBackPress.bind(this)
}
componentWillReceiveProps(nextProps) {
//TODO: Uncomment once VPN problem is fixed
// if (nextProps.isNetworkReachable !== this.props.isNetworkReachable) {
// this.state.didNetworkBecomeUnreachable = !nextProps.isNetworkReachable
// }
}
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', this.onBackPress)
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.onBackPress)
//TODO: Uncomment once VPN problem is fixed
// stopListening()
}
onBackPress() {
console.log(this.props);
const { dispatch, navigationState } = this.props
dispatch(NavigationActions.back());
return true;
}
### AppNavigator.js
import React from 'react'
import {
StackNavigator,
addNavigationHelpers
} from 'react-navigation';
import stackConfiguration from './stackConfiguration'
import routeConfiguration from './routeConfiguration'
export const AppNavigator = StackNavigator(
routeConfiguration,
stackConfiguration
);
export default ({
dispatch,
navigationState,
addListener
}) => {
return (
state: navigationState,
addListener
})} />
)
}
Most helpful comment
I solved this way:
<Router backAndroidHandler={this.onBackPress}>and: