React-native: How to detect the app is killed

Created on 4 Apr 2019  路  9Comments  路  Source: facebook/react-native

馃悰 Bug Report

I need to detect that the app is killed. How can I understand it ? AppState does not work in Android.

Expected Behavior

I am expecting to detect that the app is killed.

handleAppStateChange = (nextAppState: any) => {
console.log(nextAppState)
}

componentWillUnmount = () => {
    AppState.removeEventListener('memoryWarning', this.handleAppStateChange)
}

componentDidMount() {
    AppState.addEventListener('memoryWarning', this.handleAppStateChange)

}

Environment:
OS: macOS 10.14
Node: 10.13.0
Yarn: 1.13.0
npm: 6.4.1
Watchman: 4.9.0
Xcode: Xcode 10.1 Build version 10B61
Android Studio: 3.2 AI-181.5540.7.32.5056338

Packages: (wanted => installed)
react: 16.0.0 => 16.0.0
react-native: 0.48.4 => 0.48.4

Bug Locked

All 9 comments

It looks like you are using an older version of React Native. Please update to the latest release, v0.59 and verify if the issue still exists.

The "Resolution: Old Version" label will be removed automatically once you edit your original post with the results of running react-native info on a project using the latest release.

I think you can do it only in native code, because these callbacks won't be called anyway, or there is no way to make sure that they're executed.

@dulmandakh You can use the following snippet to detect memory warnings. There is no guarantee it will be triggered in time, before you app is killed, though:

import { AppState } from 'react-native';

AppState.addEventListener('memoryWarning', () => {
  console.log('Device running out of memory!');
});

There is no way of knowing if app is killed in JS.

There is no way of knowing if app is killed in JS, because app is killed and JS engine is gone.

What do you want to do or achieve with this?

@dulmandakh so how we can know that reason behind the killing of app at least it should throw a proper red screen or log somewhere.

You can use crash reporting tools like crashlytics to collect crash or
error reports about your app.

>

Actually i want to ask if the app is crashing in dev mode without throwing error then what are options we have?

You can look into application log and see traces. For my current project I have native code, so I keep Android Studio open and use Logcat for reading logs.

Was this page helpful?
0 / 5 - 0 ratings