簡體   English   中英

使用純JavaScript將React-Native(Expo)應用發送到Android后退按鈕按下的背景嗎?

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

我想讓Android后退按鈕使用純JavaScript將應用程序發送到后台,但是我能夠找到的唯一解決方案需要Java進行更改,而我正試圖避免從Expo彈出。

有沒有一種方法可以將應用程序發送到后台而不是退出它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM