0

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

有没有办法将应用程序发送到后台而不是退出它。

4

1 回答 1

-2

这是小吃的例子。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,
  },
});

于 2018-12-31T10:35:02.573 回答