2017-03-24 134 views
1

我在Android反應本機應用程序中有一個導航器。 我正在使用navigator.push()導航到其他頁面。看起來很自然,後退按鈕會彈出導航器並返回一頁,但這不是發生了什麼事。搜索了很多後,我得到了同樣的問題的功能,但我認爲我把它放在錯誤的方式,請看看這幫助我。在ReactNative中處理BackAndroid

var React = require('react-native'); 
var { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    Navigator, 
    TouchableOpacity, 
} = React; 

var SCREEN_WIDTH = require('Dimensions').get('window').width; 
var BaseConfig = Navigator.SceneConfigs.FloatFromRight; 
componentWillMount =() => { 
BackAndroid.addEventListener('hardwareBackPress',() => Actions.pop()); 
}; 
var PageOne = React.createClass({ 
    _handlePress() { 
    this.props.navigator.push({id: 2,}); 
    }, 

    render() { 
    return (
     <View style={[styles.container, {backgroundColor: 'green'}]}> 
     <Text style={styles.welcome}>Greetings!</Text> 
     <TouchableOpacity onPress={this._handlePress}> 
      <View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}> 
      <Text style={styles.welcome}>Go to page two</Text> 
      </View> 
     </TouchableOpacity> 
     </View> 
    ) 
    }, 
}); 

var PageTwo = React.createClass({ 
    _handlePress() { 
    this.props.navigator.pop(); 
    }, 

    render() { 
    return (
     <View style={[styles.container, {backgroundColor: 'purple'}]}> 
     <Text style={styles.welcome}>This is page two!</Text> 
     <TouchableOpacity onPress={this._handlePress}> 
      <View style={{paddingVertical: 10, paddingHorizontal: 20, backgroundColor: 'black'}}> 
      <Text style={styles.welcome}>Go back</Text> 
      </View> 
     </TouchableOpacity> 
     </View> 
    ) 
    }, 
}); 

var SampleApp = React.createClass({ 
    _renderScene(route, navigator) { 
    if (route.id === 1) { 
     return <PageOne navigator={navigator} /> 
    } else if (route.id === 2) { 
     return <PageTwo navigator={navigator} /> 
    } 
    }, 

    _configureScene(route) { 
    return CustomSceneConfig; 
    }, 

    render() { 
    return (
     <Navigator 
     initialRoute={{id: 1, }} 
     renderScene={this._renderScene} 
     configureScene={this._configureScene} /> 
    ); 
    } 
}); 

var styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    color: 'white', 
    }, 
}); 

AppRegistry.registerComponent('SampleApp',() => SampleApp); 
module.exports = SampleApp; 

回答

1

BackAndroid應該返回正常工作。

試試這個:

componentWillMount =() => { 
     BackAndroid.addEventListener('hardwareBackPress',() => 
      const lengthOfNavigator = this.props.navigator.getCurrentRoutes(); 
      if (lengthOfNavigator.length !== 1) { 
       this.props.navigator.pop(); 
       return true; 
      } else { 
       return false; 
      } 
     ); 
    }; 

getCurrentRoutes是用來獲取所有部件推入導航棧。

+0

你能告訴我應該在哪裏添加這個代碼位混淆... –

+0

你可以在index.android.js或你的根文件中添加這段代碼,並將此代碼包裝在componentDidMount() – JainZz

+0

中,你可以修改我的代碼這對我來說更好理解 –