Ответ 1
Похоже, вы можете обнаружить предыдущее состояние и сравнить его со следующим состоянием. Вы не можете обнаружить, что приложение закрывается по сравнению с переходом в фоновый режим, из того, что я могу найти в Интернете, но вы можете определить, было ли оно inactive
(закрытым) или в background
.
Пример из React Native Docs
import React, {Component} from 'react'
import {AppState, Text} from 'react-native'
class AppStateExample extends Component {
state = {
appState: AppState.currentState
}
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.setState({appState: nextAppState});
}
render() {
return (
<Text>Current state is: {this.state.appState}</Text>
);
}
}