之前讲过Servlet的生命周期。Servlet的生命周期相对来讲比较少,一共就那么几个方法。Activity的生命周期相对来讲还是比较多的。
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup). There are two methods almost all subclasses of Activity will implement:
正是因为有了这个窗口,就涉及到有的时候这个窗口会显示出来,有的时候这个窗口会隐藏起来。有的时候这个东西会彻底的销毁,这个时候我当前的Activity就会处于一个不同的状态。怎么知道我Activity状态之间进行切换?实际上它就是通过生命周期方法的方式来表示状态的一些变化。
整个Activity它主要处于如下四种状态:
An activity has essentially four states:If an activity in the foreground of the screen (at the top of the stack), it is active or running.
运行或者激活的状态,这个时候Activity处于前台。这就是一个处于前台的Activity。
If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
如果这个东西可以看见但是不能被操作,可见但是不能被操作。这个时候它属于一个paused暂停的状态。paused activity是完全活着的。只有当系统的内存极其紧张的时候,才可能把这个处于暂停状态的activity给它干掉。
但是暂停状态不是太常见。大部分情况咱们使用各种应用的时候很少会出现说可见但是不能被操作的。有些极端的情况比如咱们可以给它创建一个窗口形式的Activity,这个Activity并不是铺满整个屏幕的,它是一个窗口。这个时候它下面的Activity并不是完全被挡住,有一部分是可以被看见的。这个时候它就处于一个暂停的状态。
还有我可以把当前的应用变成一个透明的应用。我在一个应用上面get另外一个应用,这个应用是透明的。也就是说整个的界面是完全透明,可以看见下载。这个时候我后面的应用就处于暂停的状态。
If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
如果一个Activity完全被另外一个Activity挡住了,处于停止的状态stop。停止状态的Activity完全被另外一个Activity挡住,不被用户看到也不能被操作。
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
最后一个就是属于销毁状态了。系统可以把当前的Activity drop丢掉了为了节省内存,把Activity干掉呢调它的finish方法或者把它的进程都杀掉。
就是通过生命周期这些方法来获得不同状态的。
生命周期方法有这么一个图。一共有这么几个。