I'm using the AppState to reset the states of the App when the AppState is "background", but when I ask for a Permission, for example Camera or Location, the AppState change to "background", so it reset all my states. There's a way to control when the Pop Up permission appear so it doesn't reset the states on pop up Permissions?
<TouchableOpacity
onPress={() => {
setChangeAppState(false);
FunctionsWorkersList.chooseSource(item);
}}>
Here i change the state for not doing nothing on background, but it keeps reseting me the states there:
useEffect(() => {
if (changeAppState == false) {
console.log("state changed")
} else {
const appStateListener = AppState.addEventListener(
'change',
nextAppState => {
if (
(nextAppState == 'background' ||
nextAppState == 'inactive') &&
changeAppState == false
) {
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [
{ name: 'HomeWorkerScreen' },
{ name: 'GetLogsScreen' },
{ name: 'WorkersListScreen' },
{ name: 'CardsListScreen' },
],
}),
);
}
},
);
return () => {
appStateListener?.remove();
};
}
}, [changeAppState]);
It seems like don't have time to change the state?
CodePudding user response:
You can keep a flag to denote whether the background state is because of permission popup or any other piece of code which will cause background state to trigger. And then reset your state accordingly.
here is sample based on your code:
useEffect(() => {
const appStateListener = AppState.addEventListener(
'change',
nextAppState => {
if ((nextAppState == 'background' || nextAppState == 'inactive') &&
(your_flag_which_will_be_true_if_are_asking_permission == false) {
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [
{ name: 'HomeWorkerScreen' },
{ name: 'GetLogsScreen' },
{ name: 'WorkersListScreen' },
{ name: 'CardsListScreen' }
]
})
);
}
}
);
return () => {
appStateListener?.remove();
};
}, []);