简体   繁体   English

是否始终仅由OS调用onDestroy()方法?

[英]Is onDestroy() method always called only by OS?

I am beginning to learn android development in two video courses I have watched and one book I have read it said onDestroy() is called by OS when it feels to freeup memory. 我开始在我看过的两个视频课程和一本我读过的书中开始学习android开发,这本书说OS会在释放内存时调用onDestroy() Nobody even mentioned that it will be called when user closes the app. 甚至没有人提到用户关闭应用程序时会调用它。 That created me doubt if onDestroy() is called when the app is closed by the user. 这使我怀疑用户关闭应用程序时是否调用了onDestroy()

Someone please clarify. 请有人澄清。

When a user switches away from an app in Android, the onPause() is called, followed by onStop(). 当用户离开Android中的应用程序时,将调用onPause(),然后调用onStop()。 At this point, the app is no longer visible to the user and is suspended, but still available to be resumed. 此时,该应用对用户不再可见,并且已被挂起,但仍可以继续使用。

If the OS decides to terminate the app, when it does, onDestroy() will be called. 如果操作系统决定终止应用程序,则在终止时将调用onDestroy()。 That may be a few seconds after onStop, or many hours or days later. 可能是在onStop之后几秒钟,或者是数小时或数天之后。

When the user navigates back to the app, if onDestroy() was never called, then the app was never terminated, and onCreate() will not be called either. 当用户导航回该应用程序时,如果从未调用过onDestroy(),则该应用程序将永远不会终止,并且也不会调用onCreate()。 onStart() and onResume() will always be called. onStart()和onResume()将始终被调用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM