onCreate()[/code] through to a single final call to [code]onDestroy()[/code].
The [b]visible lifetime[/b] of an activity happens between a call to[code]onStart()[/code] until a corresponding call to [code]onStop()[/code].
The [b]foreground lifetime[/b] of an activity happens between a call to [code]onResume()[/code] until a corresponding call to[code]onPause()[/code].
[img]http://files.jb51.net/file_images/article/201212/201212060900505.png[/img]
保存Activity状态
To capture that state before the activity is killed, you can implement an [code]onSaveInstanceState()[/code] method for the activity. Android calls this method before making the activity vulnerable to being destroyed — that is, before [code]onPause()[/code] is called. It passes the method a [code]Bundle[/code] object where you can record the dynamic state of the activity as name-value pairs. When the activity is again started, the Bundle is passed both to [code]onCreate()[/code] and to a method that's called after [code]onStart()[/code],[code]onRestoreInstanceState()[/code], so that either or both of them can recreate the captured state.
Unlike [code]onPause()[/code] and the other methods discussed earlier, [code]onSaveInstanceState()[/code] and [code]onRestoreInstanceState()[/code]are not lifecycle methods. They are not always called.[b]Because [code]onSaveInstanceState()[/code] is not always called, you should use it only to record the transient state of the activity, not to store persistent data.[/b][b]Use [code]onPause()[/code] for that purpose instead.[/b][b]启动另一个Activity的过程[/b] The current activity's [code]onPause()[/code] method is called. Next, the starting activity's [code]onCreate()[/code], [code]onStart()[/code], and [code]onResume()[/code] methods are called in sequence. Then, if the starting activity is no longer visible on screen, its [code]onStop()[/code] method is called. service生命周期
A service can be used in two ways: It can be started and allowed to run until someone stops it or it stops itself. In this mode, it's started by calling[code]Context.startService()[/code] and stopped by calling [code]Context.stopService()[/code]. It can stop itself by calling[code]Service.stopSelf()[/code] or [code]Service.stopSelfResult()[/code]. Only one [code]stopService()[/code] call is needed to stop the service, no matter how many times [code]startService()[/code] was called.
It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by calling[code]Context.bindService()[/code], and is closed by calling [code]Context.unbindService()[/code]. Multiple clients can bind to the same service. If the service has not already been launched, [code]bindService()[/code] can optionally launch it.
相关的方法:
[code]void onCreate()[/code]
[code]void onStart(Intent [i]intent[/i])[/code]
[code]void onDestroy()[/code]
The [code]onCreate()[/code] and [code]onDestroy()[/code] methods are called for all services, whether they're started by[code]Context.startService()[/code] or [code]Context.bindService()[/code]. However, [code]onStart()[/code] is called only for services started by[code]startService()[/code].
If a service permits others to bind to it, there are additional callback methods for it to implement:
[code]IBinder onBind(Intent [i]intent[/i])[/code]
[code]boolean onUnbind(Intent [i]intent[/i])[/code]
[code]void onRebind(Intent [i]intent[/i])[/code]
[img]http://files.jb51.net/file_images/article/201212/201212060900506.png[/img]
Broadcast receiver lifecycle
只有一个方法:void onReceive(Context [i]curContext[/i], Intent [i]broadcastMsg[/i])
A process with an active broadcast receiver is protected from being killed. But a process with only inactive components can be killed by the system at any time, when the memory it consumes is needed by other processes.
This presents a problem when the response to a broadcast message is time consuming and, therefore, something that should be done in a separate thread, away from the main thread where other components of the user interface run. If[code]onReceive()[/code] spawns the thread and then returns, the entire process, including the new thread, is judged to be inactive (unless other application components are active in the process), putting it in jeopardy of being killed. The solution to this problem is for [code]onReceive()[/code] to start a service and let the service do the job, so the system knows that there is still active work being done in the process. 进程的生命周期
Android根据其重要性在内存不足的时候移去重要性最低的进程。重要性由高到低为:
[list=1]前台进程 可见进程 服务进程 后台进程 空进程[/list][b]注意[/b]:Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. 比如播放MP3的时候就要启动一个service。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有