简体   繁体   English

通过滑动从后台删除活动后未调用Activity的onDestroy()方法

[英]Activity's onDestroy() method not called after removing activity from background by swiping

I have implemented ActivityLifecycleCallbacks in a private class inside my Application class. 我在Application类中的私有类中实现了ActivityLifecycleCallbacks

If Home button or Back button is used, lifecycle's onStop() and onPause() methods are getting called. 如果使用Home按钮或Back按钮,则会onStop()生命周期的onStop()onPause()方法。 If I kill the activity by swiping out from background, onDestroy() method is not getting called. 如果我通过从后台onDestroy()出来杀死活动,则不会调用onDestroy()方法。 It gets called when I start my activity next time. 下次开始我的活动时会调用它。 I am using moveTaskToBack(true) in case of Back press. 在Back press的情况下我使用moveTaskToBack(true)

What must be the issue and which lifecycle method should get called when we swipe out from background? 当我们从后台刷出时,应该是什么问题以及应该调用哪个生命周期方法?

From the documentation of onDestroy() : onDestroy()文档

Note: do not count on this method being called as a place for saving data! 注意:不要指望这种方法被称为保存数据的地方! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. 例如,如果某个活动正在编辑内容提供程序中的数据,那么这些编辑应该在onPause()或onSaveInstanceState(Bundle)中提交,而不是在此处。 This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. 通常实现此方法是为了释放与活动相关联的线程之类的资源,以便在其应用程序的其余部分仍在运行时,被破坏的活动不会留下这些东西。 There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away. 在某些情况下,系统会简单地杀死活动的托管进程,而不会在其中调用此方法 (或任何其他方法 ),因此它不应该用于执行在进程消失后保留的内容。

Such a situation is swiping the app out of the recent tasks list. 这种情况是将应用程序从最近的任务列表中删除。

Check out this answer on how to get notified when the app is getting swiped out of Recents. 查看这个答案 ,了解如何在应用程序被淘汰的时候收到通知。

I remember reading a similar question on StackOverflow. 我记得在StackOverflow上读过类似的问题。 Here was the top answer: 这是最佳答案:

Handle exit application from Task Manager Handle exit application from Task Manager 从任务管理器 处理退出应用程序从任务管理 处理退出应用程序

Unfortunately there isn't a good answer for handling this situation. 不幸的是,处理这种情况并不是一个好的答案。 If the app is force-killed, onDestroy method isn't necessarily called. 如果应用程序被强制终止,则不一定要调用onDestroy方法。 According to the documentation. 根据文件。

Note: do not count on this method being called as a place for saving data! 注意:不要指望这种方法被称为保存数据的地方! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. 例如,如果某个活动正在编辑内容提供程序中的数据,那么这些编辑应该在onPause()或onSaveInstanceState(Bundle)中提交,而不是在此处。

If you can, clean up in the onPause() method. 如果可以,请在onPause()方法中清理。 In order for the user to get to that screen to kill the app, it has to have been backgrounded and thus onPause() would be called. 为了让用户进入该屏幕以杀死应用程序,它必须已经后台化,因此将调用onPause()。 (see documentation) (见文件)

从后台滑动应用程序,即从最近清除应用程序会导致整个进程和任务被onDestroy() ,并且当用户从某个活动中退回时,可能不会调用onDestroy()时间,然后只有该活动被杀死而不是该进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从后台滑动后未调用Activity的onDestroy方法 - Activity's onDestroy method is not being called after swiping from background 片段的onActivityCreated()在Activity的onDestroy()之后调用 - Fragment's onActivityCreated() is called after onDestroy() of Activity 如果未调用活动的 onDestroy() 方法,如何更新远程数据库中的状态? - How to update status in remote database if the activity's onDestroy() method is not called? 在调用onDestroy()方法之后,Activity实例是否真的被破坏了? - Is the Activity instance really destroyed after onDestroy() method is called? 为什么要调用新Activity的onDestroy()? - Why new Activity's onDestroy() is called? Activity OnDestroy从未调用过? - Activity OnDestroy never called? 将数据保存在activity的onDestroy方法中 - Save data in activity's onDestroy method 在Activity.onDestroy()方法之后,为什么我仍然可以获取此活动的实例? - After Activity.onDestroy() method, why I can still get this activity's instance? 在Activity.finish()之后,onDestroy无法立即执行 - onDestroy from activity not executing right after Activity.finish() 即使在调用onDestroy()之后,活动实例仍然存在 - Activity instance still exists even after onDestroy() is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM