BackAndroid模塊用來進(jìn)行攔截硬件設(shè)備的Back返回鍵事件日杈。如果沒有設(shè)置任何監(jiān)聽函數(shù)或者監(jiān)聽函數(shù)返回false梗醇,那么會(huì)調(diào)用默認(rèn)的返回鍵功能進(jìn)行退出應(yīng)用,廢話不多說焦蘑,直接上代碼
代碼如下:
/**
* 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;
});