在我的 React Native 应用程序中,我使用 AppState 来尝试检测应用程序何时进入后台,以及何时完全关闭。我想为每个触发单独的函数。但从他们的文档来看,当应用程序在后台或关闭时,会发生同样的事情:它从
active
到inactive
,然后到background
。
有什么方法可以在应用程序关闭和后台运行时触发单独的功能吗?
您可以查看以下代码:
if (Platform.OS === 'android') {
AppState.addEventListener('blur', () => {
// do something when the app closed
});
}
AppState.addEventListener('change', onAppStateChange);
async function onAppStateChange(state: string) {
if (state === 'inactive') {
// do something when the app closed
} else if (state === 'background') {
// do something when the app minimized
} else if (state === 'active') {
// do something when the app opened
}
}