简体   繁体   English

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

[英]Send a React-Native (Expo) app to the background on Android back button press with pure JavaScript?

I'd like to have the Android back button send the app to the background using pure JavaScript, but the only solutions I've been able to find require changes in Java and I'm trying to avoid ejecting from Expo just yet. 我想让Android后退按钮使用纯JavaScript将应用程序发送到后台,但是我能够找到的唯一解决方案需要Java进行更改,而我正试图避免从Expo弹出。

Is there a way to send an app to the background rather than exiting it. 有没有一种方法可以将应用程序发送到后台而不是退出它。

Here is snack example. 这是小吃的例子。 https://snack.expo.io/@nazrdogan/carefree-beef-jerky When you return false from Backhandler app will go background. https://snack.expo.io/@nazrdogan/carefree-beef-jerky当您从Backhandler应用程序返回false时,将进入后台。 if return true you can do your own logic. 如果返回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, }, }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM