BackAndroid模块用来进行拦截硬件设备的Back返回键事件。如果没有设置任何监听函数或者监听函数返回false,那么会调用默认的返回键功能进行退出应用,废话不多说,直接上代码
代码如下:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
BackAndroid,
} from 'react-native';
import LSNavigator from './src/components/common/LSNavigator';
import { navigator } from './src/lib/global/navigator';
AppRegistry.registerComponent('AnyLinks', () => LSNavigator);
BackAndroid.addEventListener('hardwareBackPress', () => {
if (navigator.navigator && navigator.navigator.getCurrentRoutes().length > 1) {
navigator.navigator.pop();
return true;
}
return false;
});