const ABCTabbar = createBottomTabNavigator({
'ANav': {screen: ANav,},
'BNav': {screen: BNav,},
'CNav': {screen: CNav,},
});
const ACTabbar = createBottomTabNavigator({
'ANav': {screen: ANav,},
'CNav': {screen: CNav,},
});
const BCTabbar = createBottomTabNavigator({
'BNav': {screen: BNav,},
'CNav': {screen: CNav,},
});
const CTabbar = createBottomTabNavigator({
'CNav': {screen: CNav,},
});
const ABCTabNavigator = createAppContainer(ABCTabbar);
const ACTabNavigator = createAppContainer(ACTabbar);
const BCTabNavigator = createAppContainer(BCTabbar);
const CTabNavigator = createAppContainer(CTabbar);
export { ABCTabNavigator, ACTabNavigator, BCTabNavigator, CTabNavigator };
複製代碼
class MainNav extends Component {
render() {
if (this.state.permission === 'ABC') {
return (<ABCTabNavigator/>);
} else if (this.state.permission == 'AC') {
return (<ACTabNavigator/>);
} else if (this.state.permission == 'BC') {
return (<BCTabNavigator/>);
} else {
return (<CTabNavigator/>);
}
}
}
複製代碼
const Main = createStackNavigator({
MainNav: {screen: MainNav},
DPage: {screen: DPage},
});
const MainNavigation = createAppContainer(Main);
export default MainNavigation;
複製代碼
import {BackHandler} from 'react-native';
const handleAndroidBackButton = callback => {
BackHandler.addEventListener('hardwareBackPress', callback);
};
const removeAndroidBackButtonHandler = (callback) => {
BackHandler.removeEventListener('hardwareBackPress', callback);
}
export {handleAndroidBackButton, removeAndroidBackButtonHandler};
複製代碼
頁面上的實現:html
import {handleAndroidBackButton, removeAndroidBackButtonHandler} from '../../Util/AndroidBackAction.js'; // 你本身的路徑
export default class D extends Component {
constructor(props) {
super(props);
handleAndroidBackButton(this.onBackAndroid); // 必定要在這裏寫
}
componentWillUnmount() {
removeAndroidBackButtonHandler(this.onBackAndroid); // 必定要在這裏寫
}
onBackAndroid = () => {
this.props.navigation.goBack();
return true;
};
}
複製代碼
還有個按兩下退出應用的,我是寫在了入口的地方(就配置的地方),監聽的方式是同樣的,就是onBackAndroid實現不同react
onBackAndroid = () => {
if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
//最近2秒內按過back鍵,能夠退出應用。
return false;
}
this.lastBackPressed = Date.now();
ToastAndroid.show('再按返回退出應用', ToastAndroid.SHORT);
return true;
};
複製代碼
好啦,大功告成~~~android
結束~撒花~~~react-native