React Native - call method when exit app

Viewed 1997

I would like to call a method to change a value from AsyncStorage. So I tried using AppState to check app is closed.

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");
    }
  });
}

However, it not calling to the inactive, am I doing right ? or there is another better way to check the app is closed. Thank a lot

1 Answers

If you want to know if the app was closed, I think you could use componentWillUnmount of the component.
According to docs:

inactive - This is a state that occurs when transitioning between foreground & background, and during periods of inactivity such as entering the Multitasking view or in the event of an incoming call

So to do something before the app is closed, put it inside the componentWillUnmount.

Related