我正在構建一個 iOS 模塊并希望連接到 AppDelegate 方法,例如applicationWillResignActive,我該如何實作?
uj5u.com熱心網友回復:
您正在嘗試使用applicationWillResignActive該方法將在每次應用程式進入后臺時被觸發。
您可以通過使用AppState在React Native 中實作這一點 ,它可以告訴您應用程式是在前臺還是后臺,并在狀態更改時通知您。
例子:
import React, { useRef, useState, useEffect } from "react";
import { AppState, StyleSheet, Text, View } from "react-native";
const AppStateExample = () => {
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
useEffect(() => {
const subscription = AppState.addEventListener("change", nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
console.log("AppState", appState.current);
});
return () => {
subscription.remove();
};
}, []);
return (
<View style={styles.container}>
<Text>Current state is: {appStateVisible}</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
});
export default AppStateExample;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/334686.html
標籤:javascript 迅速 目标-c 反应原生
上一篇:為什么getItem在我的代碼中使用asyncStorage回傳'Promise{"_U":0,"_V":0,"_W":null,
