MENU
AppState
AppState tells if the app is in the foreground or background, and notifies you when the state changes.
EventschangememoryWarningfocus Androidblur Android | MethodsaddEventListener()removeEventListener()PropertiescurrentState |
RESETRUNFULL
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;