Chrisの梳羽之礁

A look of quick intelligence and soft refinement
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android学习之二——Activity的生命周期

Posted on 2010-10-15 17:13  Chrisfang6  阅读(794)  评论(0编辑  收藏  举报

 

 

 

 

还是表格说话吧

 

 

 

 

 

Method

Description

Killable?

Next

onCreate()

Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured (see Saving Activity State, later).

Always followed by onStart().

No

onStart()

 

 

 

 

 

 

 

 

 

 

 

可视生命周期  

onRestart()

Called after the activity has been stopped, just prior to it being started again.

Always followed by onStart()

No

onStart()

onStart()

Called just before the activity becomes visible to the user.

Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

No

onResume() 

or

onStop()

 

 

 

前台生命周期 

onResume()

Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.

Always followed by onPause().

No

onPause()

onPause()

Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.

Yes

onResume() 

or

onStop()

onStop()

Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.

Followed either by onRestart() if the activity is coming back to interact with the user, or byonDestroy() if this activity is going away.

Yes

onRestart() 

or

onDestroy()

onDestroy()

Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing()method.

Yes

nothing

 

 

 

 

这里说的可被杀死,是指的非极端情况下。极端情况下,什么都可以发生的哦。

 

onPause()是唯一一个进程被杀死前会调用的方法,onStop()onDestroy()有可能不被调用。所以立遗嘱(保存一些持久化数据)还是在这里做比较好。

当然一些临时性的数据,可以用onSaveInstanceState()onSaveInstanceState()会在onPause()调用前被调用,其生成的Bundle对象会传给onCreate(),以及onStart()之后被调用的onRestoreInstanceState()

 

当一个Activity调用另一个Activity时(姑且称前者A,后者B),则调用顺序为:A.onPause()→B.onCreate()→B.onStart()→B.onResume()→A.onStop()

 

 

Service生命周期

 

调用Context.startService()可以启动一个Service,但无论调多少次,一个Context.stopService()就可以停止这个Service。当然这个Service也可以自己把自己停掉,只要调Service.stopSelf()Service.stopSelfResult()就行了。

 

另外,别的Component想要使用这个Service,可以调Context.bindService()来申请使用,调Context.unbindService()来终止使用。直到所有的绑定Service都终止了,Context.stopService()才会真正起作用。

 

像Activity,Service也有3个方法:

void onCreate() 
void onStart(Intent intent) 
void onDestroy()

 

其中onStart()只能被Context.startService()调起来。也就是说绑定Service的时候最多只能生成一个Service,但并不能启动她。

 

同样的,绑定也有3个方法:

IBinder onBind(Intent intent) 
boolean onUnbind(Intent intent) 
void onRebind(Intent intent)

 

 

Broadcast Receiver生命周期

 

她只有一个方法:

 

void onReceive(Context curContext, Intent broadcastMsg)

当一个broadcast消息到了,这个Broadcast receiver被激活。在onReceive()执行期间这个Broadcast一直处于激活状态。这时她所在的进程一般是不会被删除的。然而,我们常常会把一些耗时的动作放到另外的线程里去做,这样onReceive()很快就返回了,Broadcast receiver也变成了非激活状态。这下就比较悬了,万一系统需要内存开始大开杀戒……。有个办法可以通融一下,就是把这个动作扔到一个Service中去,这样就放心多了。

 

 

进程和生命周期

 

系统内存总有耗完的时候,解决的办法就是拿出屠刀了。当然不能随便杀,哪些能杀,哪些不能杀;哪些先杀,哪些后杀,要定下个规矩来。Android的这个杀人规矩有个名字叫“important hierarchy”。这个规矩把进程分为4等:

 

 

 

 

级别(重要性↓)

认定条件(其中之一)

备注

foreground process

1、里面正跑着一个onResume()的Activity

2、里面正跑着一个Service,上面绑着个onResume()的Activity

3、里面正跑着一个正在处理她3个生命周期函数的Service

4、里面正跑着一个正在onReceive()的Broadcast receiver

这个级别的进程只有在连负担她们自身运行的内存都没有了的情况下才会被杀死。

visible process

1、里面正跑着一个onPause()的Activity

2、里面正跑着一个Service,上面绑着个onPause()的Activity

这个级别的进程只有在不能保证所有的上一级进程所需内存的情况下才会被杀死

Service process

运行了startService()但不属于前面两种的Service

这个级别的进程只有在不能保证前两者运行所需内存的情况下才会被杀死

background process

里面有onStop()的Activity

这些进程又根据LRU(Least Recently Used)来排队。很久没用过的先被杀

empty process

没有任何Component在里面

多半是用来作为提高效率的cache使用的,随便杀

 

 

 

 

当然如果一个进程满足多种类型,系统会尽量把她的级别定得高一些。同时,这些进程的级别也是会改变的。比如进程A为进程B提供服务了,则进程A的级别就会变得不低于进程B。

 

既然一个Service进程的级别比background进程要高,一些耗时的工作当然最好扔到一个Service里去做,比如前面Broadcast receiver生命周期里提到的那样。