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?
            Asked
            
        
        
            Active
            
        
            Viewed 434 times
        
    0
            
            
        - 
                    By exiting do you mean actually killing the app? swiping it away? – Auticcat Oct 22 '19 at 10:17
 - 
                    @Auticcat killing the app – sera Oct 22 '19 at 10:20
 - 
                    Use `applicationWillTerminate(_ application: UIApplication)` function in iOS. From there you can call your shared React-Native component functions – Balaji Kondalrayal Oct 22 '19 at 10:21
 - 
                    check this https://stackoverflow.com/questions/51023044/how-to-call-api-when-app-is-killed-in-react-native – Gaurav Roy Oct 22 '19 at 10:22
 
2 Answers
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