當我連按兩次 Android 后退按鈕時,我無法退出應用程式。只是卡在第一個底部標簽中。
{input} 是螢屏中按鈕的順序。螢屏上有四個按鈕可以轉到每個視圖。(在同一類組件中)
handleBackButton() {
this.setState({input: null});
this.props.navigation.addListener('willBlur', () => BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton))
return true
}
在同一類組件中的另一個函式內
this.props.navigation.addListener('willFocus', () => BackHandler.addEventListener('hardwareBackPress', this.handleBackButton))
在此處輸入影像描述
uj5u.com熱心網友回復:
當你呼叫 BackHandler.exitApp(); 應用程式將關閉,但仍將保留在 android 的最近選項卡中。
BackHandler.exitApp();
在這里,我將分享一個 react-native BackHandler api 示例。當您想通過單擊回傳按鈕退出應用程式時,您應該使用hardwareBackPress EventListener,讓我們通過一個示例來說明。
import React, { Component } from "react";
import { Text, View, StyleSheet, BackHandler, Alert } from "react-native";
class App extends Component {
backAction = () => {
Alert.alert("Hold on!", "Are you sure you want to go back?", [
{
text: "Cancel",
onPress: () => null,
style: "cancel"
},
{ text: "YES", onPress: () => BackHandler.exitApp() }
]);
return true;
};
componentDidMount() {
this.backHandler = BackHandler.addEventListener(
"hardwareBackPress",
this.backAction
);
}
componentWillUnmount() {
this.backHandler.remove();
}
render() {
return (
<View style={styles.container}>
<Text onPress = {this.backAction} style={styles.text}>Click Back button!</Text>
</View>
);
}
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center"
},
text: {
fontSize: 18,
fontWeight: "bold"
}
});
export default App;
uj5u.com熱心網友回復:
如果您嘗試退出應用程式,請使用
BackHandler.exitApp()
閱讀檔案
uj5u.com熱心網友回復:
import {BackHandler,Alert} from 'react-native';
const backAction = () => {
Alert.alert('AppName', 'Are you sure you want to exit?', [
{
text: 'NO',
onPress: () => null,
style: 'cancel',
},
{text: 'YES', onPress: () => BackHandler.exitApp()},
]);
return true;
};
useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', backAction);
return () => {
BackHandler.removeEventListener('hardwareBackPress', backAction);
};
}, []);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/436947.html
標籤:javascript 反应式
