最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - react native onDestroy event - Stack Overflow

matteradmin5PV0评论

I want an event when the user closes the app from the background. For example, when I press the left button on a Galaxy S7 I am seeing all apps which are in the background. When I swipe the app to the left I destroy the app. Is it possible to call an event when this happens? I think in android lifecycle that is the event onDestroy ()

I want an event when the user closes the app from the background. For example, when I press the left button on a Galaxy S7 I am seeing all apps which are in the background. When I swipe the app to the left I destroy the app. Is it possible to call an event when this happens? I think in android lifecycle that is the event onDestroy ()

Share Improve this question asked May 15, 2018 at 21:37 ottootto 2,0638 gold badges42 silver badges69 bronze badges 2
  • This question is similar to this stackoverflow./questions/38962034/… could you please take a look to confirm that is what you need to do? – ricardoorellana Commented May 15, 2018 at 21:50
  • no this is when somebody close the app. I mean when the app get destroyed. – otto Commented May 15, 2018 at 21:59
Add a ment  | 

2 Answers 2

Reset to default 3

Android have onDestroy lifecycle event

If you want to listen this event on JS, you need to write this function in mainActivity.

Write this function in the MainActivity class:

  private static final String TAG = "MainActivity";

public void onDestroy() {
    super.onDestroy();
        ReactContext reactContext = getReactInstanceManager().getCurrentReactContext();
        WritableMap params = Arguments.createMap();
        params.putString("event", "onDestroy");

        if(reactContext != null) {
            getReactInstanceManager().getCurrentReactContext()
                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit("ActivityStateChange", params);

        }
    Log.i(TAG, "onDestroy: ");
}

Then listen listen event in useEffect:

  useEffect(() => {
DeviceEventEmitter.addListener('ActivityStateChange', e => {
  console.log('onDestroy')
});

return () => DeviceEventEmitter.removeAllListeners();
}, []);

Also if you want inactive event cases on android, take a look at it https://stackoverflow./a/41009169/13890802

You might use React Native Modules to listen to the activity lifecycle onDestroy. You would have to create your own native module.

A native module is a Java class that usually extends the ReactContextBaseJavaModule class and implements the functionality required by the JavaScript.

Post a comment

comment list (0)

  1. No comments so far