React Native:您能检测到应用程序何时关闭(而不是后台运行)吗?

问题描述 投票:0回答:1

在我的 React Native 应用程序中,我使用 AppState 来尝试检测应用程序何时进入后台,以及何时完全关闭。我想为每个触发单独的函数。但从他们的文档来看,当应用程序在后台关闭时,会发生同样的事情:它从

active
inactive
,然后到
background

有什么方法可以在应用程序关闭和后台运行时触发单独的功能吗?

javascript react-native foreground background-foreground appstats
1个回答
0
投票

您可以查看以下代码:

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
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.