React-native-router-flux: Press Android back button immediately exit application

Created on 6 Aug 2017  路  3Comments  路  Source: aksonov/react-native-router-flux

2182

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!!

bug

Most helpful comment

I solved this way:
<Router backAndroidHandler={this.onBackPress}>
and:

onBackPress = () => {
    if (Actions.state.index === 0) {
      return false
    }
    Actions.pop()
    return true
}

All 3 comments

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 (
dispatch,
state: navigationState,
addListener
})} />
)
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sreejithr picture sreejithr  路  3Comments

kirankalyan5 picture kirankalyan5  路  3Comments

rafaelcorreiapoli picture rafaelcorreiapoli  路  3Comments

GCour picture GCour  路  3Comments

moaxaca picture moaxaca  路  3Comments