admin管理员组文章数量:1024135
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
2 Answers
Reset to default 3Android 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.
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
2 Answers
Reset to default 3Android 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.
本文标签: javascriptreact native onDestroy eventStack Overflow
版权声明:本文标题:javascript - react native onDestroy event - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745588987a2157773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论