Android---Activity 生命周期(一)start Activity && destroy Activity

来源:互联网 发布:淘宝千万不要搜索的字 编辑:程序博客网 时间:2024/04/29 17:24

Figure 1. A simplified illustration of the Activity lifecycle, expressed as a step pyramid. This shows how, for every callback used to take the activity a step toward the Resumed state at the top, there's a callback method that takes the activity a step down. The activity can also return to the resumed state from the Paused and Stopped state.


Technically, the activity becomes visible to the user when onStart() is called, but onResume() quickly follows and the activity remains in the Resumed state until something occurs to change that, such as when a phone call is received, the user navigates to another activity, or the device screen turns off.

<翻译>技术上,当调用onStart()方法时,Activity就变为可见状态了,但是很快就会调用onResume()方法,然后Activity就会处于Resumed状态直到发生某些事情来改变这种状态,


1》the activity can exist in one of only three states for an extended period of time:
<翻译>Activity只可能处于(停留一段时间)以下三种状态之一:


1.1》Resumed---活动状态:用户可以与之交互
In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.)


1.2》Paused--- 暂停状态:Activity被部分遮挡,如显示的对话框等
In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code.


1.3》Stopped---停止状态:Activity被全部遮挡
In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.


The other states (Created and Started) are transient and the system quickly moves from them to the next state by calling the next lifecycle callback method. 

That is, after the system calls onCreate(), it quickly calls onStart(), which is quickly followed by onResume().

<翻译>其他的状态(创建 和 开始状态)是瞬时的,系统会通过调用下一下生命周期函数很快的进入下一个状态。

也就是说,系统调用过onCreate()方法之后,就会很快的调用onStart()方法,然后紧接着调用onResume()方法进入Resumed状态,而不会停留在Created 或 Started状态。


2》onDestory()方法

Most apps don't need to implement this method because local class references are destroyed with the activity and your activity should perform most cleanup during onPause() and onStop(). 

<翻译>绝大部分app不需要实现这个方法,因为类内的局部变量会随着Activity的销毁而销毁,并且Activity应该在onPause()方法和onStop方法中执行大部分的清理工作。

However, if your activity includes background threads that you created during onCreate() or other long-running resources that could potentially leak memory if not properly closed, you should kill them during onDestroy().

<翻译>然而,当你的Activity包含在onCreate()方法中创建的后台线程时,或者其他如果不正常关闭就可能导致内存泄露的耗时资源时,你应该在onDestory()方法中清除它们。


2.1》The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: when you call finish() from within the onCreate() method.

<翻译>在所有情况下,当系统调用过onPause()方法和onStop()方法后才会调用onDestory() 方法,除了这一种情况:

当你在onCreate()方法内直接调用finish()方法,此时系统会直接调用onDestroy()方法而不会调用其他的生命周期函数。

0 0
原创粉丝点击