React Native: Can you detect when app is closed down, as opposed to backgrounded?

331 Views Asked by At

In my React Native app I'm using AppState to try and detect when the app goes to the background, vs when it's completely closed down. I want to trigger separate functions for each. But from their documentation it looks like when the app is backgrounded or closed, the same thing happens: it goes from active to inactive and then to background.

Is there any way to trigger separate functions when the app is closed vs backgrounded?

1

There are 1 best solutions below

0
Alfan Ghinan Rusydi On

You can check below code:

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
  }
}