简体   繁体   English

Android活动生命周期-在onStop()之后和调用onDestory()时会发生什么

[英]Android activity lifecycle - what happens after onStop() and when onDestory() is called

I wanted to ask about the activity's lifecycle. 我想问一下活动的生命周期。

If an activity is going to the background, or the user pressed the home button so the application itself is not visible anymore, the onStop() is called, but not always destroyed. 如果某个活动将转到后台,或者用户按下了主页按钮,则该应用程序本身不再可见,则将onStop() ,但并不总是将其销毁。

1) when the above happens, what actually happens to the activity? 1)当上述情况发生时,活动实际发生了什么? Does it move to the backstack? 它会移到后堆栈吗? or something else? 或者是其他东西?

2) specifically related to the first question - when will the onDestroy() method be invoked in a case where the activity has already stopped, but the onDestroy() wasn't yet called at that moment of stopping? 2)与第一个问题特别相关-如果活动已经停止,但在停止的那一刻尚未调用onDestroy()则何时调用onDestroy()方法?

3) generally about onDestory() - I know onDestroy() is called when the system doesn't have enough resources anymore, or when Android is destroying a portrait/landscape layout in order to load the other one. 3)关于onDestory() )的一般知识-我知道当系统不再有足够的资源时,或者当Android为了加载另一个人而破坏人像/风景布局时,会调用onDestroy() When else can it be called? 什么时候可以叫它?

1) when the above happens, what actually happens to the activity? 1)当上述情况发生时,活动实际发生了什么? Does it move to the backstack? 它会移到后堆栈吗? or something else? 或者是其他东西?

Suppose you have pressed the home button and the current application will be moved to the background state, now the object of the current app activity will be stored in the TASK and this task has all the objects of the activity of the application. 假设您已按下主页按钮,并且当前应用程序将被移至后台状态,那么当前应用程序活动的对象现在将存储在任务中,并且此任务具有该应用程序活动的所有对象。 So this TASK will be in the memory and the Android System will kill this task only when there is shortage of the memory or any user manually kills the app or finish() method is called. 因此,此任务将存储在内存中,并且只有在内存不足或任何用户手动终止应用程序或调用finish()方法时,Android系统才会终止此任务。

2) specifically related to the first question - when will the onDestroy() method be invoked in a case where the activity has already stopped, but the onDestroy() wasn't yet called at that moment of stopping? 2)与第一个问题特别相关-如果活动已经停止,但在停止的那一刻尚未调用onDestroy(),则何时调用onDestroy()方法?

onDestroy() will only be called in the following possibilities onDestroy()仅在以下情况下被调用

  1. User manually kills the application. 用户手动杀死该应用程序。
  2. Android System will reclaim the memory where there is shortage. Android System会在内存不足的地方回收内存。
  3. When finish() is called in the code itself. 在代码本身中调用finish()时

Hope it helps. 希望能帮助到你。

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

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