android 生命周期
来源:互联网 发布:电脑麦克风软件 编辑:程序博客网 时间:2024/04/30 20:16
在Android 中,多数情况下每个程序都是在各自独立的 Linux进程中运行的。当一个程序或其某些部分被请求时,它的进程就“出生”了;当这个程序没有必要再运行下去且系统需要回收这个进程的内存用于其他程序时,这个进程就“死亡”了。可以看出,Android程序的生命周期是由系统控制而非程序自身直接控制。这和我们编写桌面应用程序时的思维有一些不同,一个桌面应用程序的进程也是在其他进程或用户请求时被创建,但是往往是在程序自身收到关闭请求后执行一个特定的动作(比如从 main 函数中return)而导致进程结束的。要想做好某种类型的程序或者某种平台下的程序的开发,最关键的就是要弄清楚这种类型的程序或整个平台下的程序的一般工作模式并熟记在心。在 Android 中,程序的生命周期控制就是属于这个范畴——我的个人理解:)
在Android 系统中,当某个 activity调用 startActivity(myIntent)时,系统会在所有已经安装的程序中寻找其 intent filter 和 myIntent 最匹配的一个activity,启动这个进程,并把这个 intent 通知给这个 activity。这就是一个程序的“生”。比如我们在 Homeapplication 中选择 “Web browser”,系统会根据这个 intent 找到并启动 Web browser程序,显示 Web browser 的一个 activity供我们浏览网页(这个启动过程有点类似我们在在个人电脑上双击桌面上的一个图标,启动某个应用程序)。在 Android中,所有的应用程序“生来就是平等的”,所以不光 Android 的核心程序甚至第三方程序也可以发出一个 intent来启动另外一个程序中的一个 activity。Android 的这种设计非常有利于“程序部件”的重用。
从某种意义上讲,垃圾收集机制把程序员从“内存管理噩梦”中解放出来,而 Android的进程生命周期管理机制把用户从“任务管理噩梦”中解放出来。我见过一些 Nokia S60 用户和 Windows Mobile用户要么因为长期不关闭多余的应用程序而导致系统变慢,要么因为不时查看应用程序列表而影响使用体验。Android 使用 Java作为应用程序 API,并且结合其独特的生命周期管理机制同时为开发者和使用者提供最大程度的便利。
Activity lifecycle
Activity有三种基本状态:
- Active:处于屏幕前景(当前task的栈顶Activity处于Active状态),同一时刻只能有一个Activity处于Active状态;
- Paused状态:处于背景画面画面状态,失去了焦点,但依然是活动状态;
- stopped:不可见,但依然保持所有的状态和内存信息。
可以调用finish()结束处理Paused或者stopped状态的Activity。
各种状态之间通过下列的函数调用转换:
voidonCreate(Bundle
savedInstanceState) void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
Activity的生命周期可以分为三组:
- The
entirelifetime of an activity happens between thefirst call to onCreate()
throughto a single final call to
.onDestroy()
The
visiblelifetime of an activity happens between a callto onStart()
untila corresponding call to
.onStop()
The
foregroundlifetime of an activity happens between a callto onResume()
untila corresponding call to
.onPause()
保存Activity状态
Tocapture that state before the activity is killed, you can implementan onSaveInstanceState()
onPause()
Bundle
onCreate()
onStart()
,
,so that either or both of them can recreate the captured state.onRestoreInstanceState()
Unlike onPause()
onSaveInstanceState()
onRestoreInstanceState()
arenot lifecycle methods. They are not alwayscalled.Because onSaveInstanceState()
onPause()
启动另一个Activity的过程
- The currentactivity's
onPause()
methodis called. - Next, thestartingactivity's
onCreate()
,onStart()
,andonResume()
methodsare called in sequence. - Then, if thestarting activity is no longer visible on screen,its
onStop()
methodis called.
service生命周期
Aservice can be used in two ways:
- It can be started and allowed torun until someone stops it or it stops itself. In this mode, it'sstarted by calling
Context.startService()
andstopped by calling
.It can stop itself by callingContext.stopService()
Service.stopSelf()
or
.OnlyoneService.stopSelfResult()
stopService()
callis needed to stop the service, no matter how manytimes startService()
wascalled. It can be operated programmaticallyusing an interface that it defines and exports. Clients establish aconnection to the Service object and use that connection to callinto the service. The connection is established bycalling
,and is closed by callingContext.bindService()
.Multiple clients can bind to the same service. If the service hasnot already beenlaunched,Context.unbindService()
bindService()
canoptionally launch it.
相关的方法:
void onCreate()
voidonStart(Intent
void onDestroy()
The onCreate()
onDestroy()
Context.startService()
.However, Context.bindService()
onStart()
startService()
.
If aservice permits others to bind to it, there are additional callbackmethods for it to implement:
IBinderonBind(Intent
booleanonUnbind(Intent
voidonRebind(Intent
Broadcast receiver lifecycle
只有一个方法:void onReceive(Context
Aprocess with an active broadcast receiver is protected from beingkilled. But a process with only inactive components can be killedby the system at any time, when the memory it consumes is needed byother processes.
Thispresents a problem when the response to a broadcast message is timeconsuming and, therefore, something that should be done in aseparate thread, away from the main thread where other componentsof the user interface run.IfonReceive()
onReceive()
进程的生命周期
Android根据其重要性在内存不足的时候移去重要性最低的进程。重要性由高到低为:
- 前台进程
- 可见进程
- 服务进程
- 后台进程
- 空进程
注意:Because a process running a service is ranked higher than onewith background activities, an activity that initiates along-running operation might do well to start a service for thatoperation, rather than simply spawn a thread — particularly if theoperation will likely outlast the activity.比如播放MP3的时候就要启动一个service。
- android 生命周期
- Activity 生命周期
- Andoid activity 生命周期
- Activity 生命周期
- Vue.js 生命周期
- 深入浅出 iOS 之生命周期
- View Controller 生命周期的…
- View Controller 生命周期的…
- react vue的生命周期
- android 四舍五入
- android o…
- android …
- android scoket
- Android - LayoutInflater
- Android - LayoutInflater
- android生命周期
- android生命周期
- Android生命周期
- java中是传值还是引用
- Java引用
- java.util.Scanner的用法
- android定制(全)
- javascript_use strict
- android 生命周期
- Android应用程序的生命周期
- 深入浅出 iOS 之生命周期
- ios的生命周期
- makefile
- Comparison of Architec…
- Java_全面总结Java泛型
- Java_泛型的理解与等价实现
- HDU 1179 Ollivanders: Makers of Fine Wands since 382 BC. 最大匹配