0

What is the life cycle when exiting an app with Swipe in Ios Simulator? I tried to log every lifecycle, but nothing happened. I want to execute a specific function when the app exits, but I'm not sure what to do. Has anyone had a similar problem with me?

sera
  • 73
  • 1
  • 9

2 Answers2

0

There's no way to detect if the app is closing, but you can check if the app is in background using AppState.

In your parent component, add:

import { AppState } from 'react-native';

...

componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
}

...

_handleAppStateChange = (nextAppState) => {
    //based of nextAppState, do what you need
}

Source: https://facebook.github.io/react-native/docs/appstate

Auticcat
  • 4,359
  • 2
  • 13
  • 28
0

In iOS AppDelegate.swift

func applicationWillTerminate(_ application: UIApplication) {
    // get your shared RCTView
    sharedRCTView.appProperties = [..., "isTerminate": true];
}

In React-Native Component

static getDerivedStateFromProps(nextProps, prevState){
   if(nextProps.isTerminate!==prevState.isTerminate){
     // Do your stuff
     return { someState: nextProps.isTerminate};
  }
  else return null;
}
Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38