Ad

How Do I Respond To An App State Change? (React Native)

- 1 answer

I want to save the state of my app when the system decides to kill it randomly (when for example it was in the background for a long time). How do I intercept the OS's request to shutdown before my state is lost?

Ad

Answer

React-native provides an API called AppStateIOS which allows you to get notified when your app switches states. It doesn't provide notifications for all of the app lifecycle state changes, but it does let you know when your app goes to background.

The background state should be enough. When your app goes to the background it gets suspended (assuming you are not using background tasks or background services), and in this state, the OS can terminate your app at any moment if it needs more resources. You won't be able to know when your app gets terminated, so when it goes to the background - this is the point where you should save your state in case your apps gets terminated later.

Ad
source: stackoverflow.com
Ad