I am trying to call a function when AppState changes to active. However I am noticing that, whether the app is in the background or not, I am getting an ‘active’ appstate:
// AppState change hook
useEffect(() => {
AppState.addEventListener("change", _handleAppStateChange);
return () => {
AppState.removeEventListener("change", _handleAppStateChange);
};
}, []);
const _handleAppStateChange = (nextAppState) => {
console.log("appState: ", appState);
if (appState.match(/inactive|background/) && nextAppState === "active") {
console.log(">> App has come to the foreground! <<");
_validateCheckInDistance();
_validateTimePeriod();
}
setAppState(nextAppState);
};
appState: active
This is very peculiar, and not sure what I am doing wrong. I am testing this using the Expo app on my Android phone (through metro tunnel).
I have exactly the same behaviour on iOS simulator.
setAppState(nextAppState) doesn’t change the value of appState !
This is the way I solved the issue (AppState issue - Snack)