我正在react-native-router-flux
用作我的导航器。我面临的问题可能很简单,但我似乎无法投入其中。当用户登录到应用程序时,他/她会被重定向到主仪表板场景。从那里,如果用户按下 android 后退按钮,应用程序应该在逻辑上关闭而不是再次导航回登录屏幕,但我无法掌握实现这一点的缺失逻辑。
路由器:
import React, { Component } from 'react'
import {
TouchableOpacity
} from 'react-native'
import { Scene, Router, Actions } from 'react-native-router-flux'
import Login from '../pages/login'
import Dashboard from '../pages/dashboard'
import NavigationDrawer from './navigationDrawer'
import Icon from 'react-native-vector-icons/MaterialIcons';
const Menu = () => {
return(
<TouchableOpacity
onPress={ () => Actions.refresh({key: 'drawer', open: value => !value }) }>
<Icon
name="view-headline"
size={30}
color="white"
/>
</TouchableOpacity>
)
}
class NavigationRouter extends Component {
render() {
return(
<Router>
<Scene initial key="login" component={Login} hideNavBar/>
<Scene key="drawer" component={NavigationDrawer} open={false} >
<Scene key="main">
<Scene initial key="dashboard" title="Dashboard" navigationBarStyle={Styles.navigationBarStyle} component={Dashboard} hideNavBar={false}
renderLeftButton={() => (<Menu/>)} />
</Scene>
</Scene>
</Router>
)
}
}
const Styles = {
navigationBarStyle: {
backgroundColor: "#fda21d"
}
}
export default NavigationRouter;
仪表板场景:
import React, {Component} from 'react'
import {
View,
Text,
BackAndroid
} from 'react-native'
import {Actions} from 'react-native-router-flux'
import Icon from 'react-native-vector-icons/MaterialIcons'
class Dashboard extends Component {
constructor(props) {
super(props)
BackAndroid.addEventListener('hardwareBackPress', function() {
if (this.props.title === 'Dashboard') {
console.log("pressed");
}
})
}
render() {
[...]
}
我尝试使用 Facebook 提供的 BackAndroid API,但它也根本不起作用。