使用纯JavaScript将React-Native(Expo)应用发送到Android后退按钮按下的背景吗?

时间:2018-12-29 19:06:22

标签: javascript android react-native expo

我想让Android后退按钮使用纯JavaScript将应用程序发送到后台,但是我能够找到的唯一解决方案需要Java进行更改,而我正试图避免从Expo弹出

有没有一种方法可以将应用发送到后台而不是退出。

1 个答案:

答案 0 :(得分:0)

这是小吃的例子。 https://snack.expo.io/@nazrdogan/carefree-beef-jerky 当您从Backhandler应用返回false时,它将进入后台。如果返回true,则可以执行自己的逻辑。

import * as React from 'react';
import { Text, View, StyleSheet, BackHandler } from 'react-native';
import { Constants } from 'expo';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card, Button } from 'react-native-paper';

export default class App extends React.Component {
  componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
  }
  componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
  }
  handleBackPress = () => {
    //if you return false. it will goes background
    return false;
  };
  render() {
    return (
      <View style={styles.container}>
        <Text>Test</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});