I have a problem when react native app goes to background or foreground. I am trying to call my action in a set interval when app in background or foreground but unable to find any solution.
In my app when user click on start navigation button and it redirect user to the Default map application of phone. I need to update my Database in a time interval when app not active.
I have used Appstate to check when app is not active. And set setInterval function to call action in a set of time interval.
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('App has come to the foreground!');
this._interval = setInterval( () => {
console.log('check');
}, 5000);
if(global.routeName && global.routeName != '' && global.routeName == "OrderDetail"){
this.props.navigation.navigate("OrderDetail",{orderId: global.orderId});
}
}else{
this._interval = setInterval( () => {
console.log('check 2');
}, 5000);
}
this.setState({appState: nextAppState});
};
I expected when app goes to background or foreground setInterval should be call in a set of interval, but the actual i'm unable to get my output.