【问题标题】:React Native - call method when exit appReact Native - 退出应用程序时调用方法
【发布时间】:2023-03-23 18:08:01
【问题描述】:

我想调用一个方法来更改 AsyncStorage 的值。所以我尝试使用AppState 来检查应用程序是否已关闭。

componentWillMount(){
    AppState.addEventListener('change', state => {
    if (state === 'active') {
      console.log("THIS IS ACTIVE");
    } else if (state === 'background') {
      console.log("THIS IS BG");
    } else if (state === 'inactive') {
      console.log("THIS IS INAVTIVE");
    }
  });
}

但是,它没有调用inactive,我做得对吗?或者有另一种更好的方法来检查应用程序是否已关闭。非常感谢

【问题讨论】:

  • 它只适用于iOS

标签: reactjs react-native


【解决方案1】:

如果你想知道应用是否被关闭,我想你可以使用组件的componentWillUnmount
根据docs

inactive - 这是在 前景和背景,以及在不活动期间,例如 进入多任务视图或来电时

所以要在应用关闭前做点什么,把它放在componentWillUnmount里面。

【讨论】:

  • 试过componentWillUnmount也不打电话,知道吗?
猜你喜欢
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
  • 2018-04-24
  • 2010-10-22
  • 2016-04-20
  • 2021-07-10
  • 1970-01-01
相关资源
最近更新 更多