从源码注释看 Activity 的生命周期方法

onCreate(Bundle savedInstanceState)
Called when the activity is starting.  This is where most initialization  should go:   calling  #setContentView(int)  to inflate the  activity's UI, using  #findViewById  to programmatically interact  with widgets in the UI , calling  #managedQuery(android.net.Uri , String[], String, String[], String)  to retrieve  cursors for data being displayed, etc.

You can call #finish from within this function, in   which case onDestroy() will be immediately called without any of the rest  of the activity lifecycle ( #onStart #onResume #onPause , etc) executing.  Derived classes must call through to the super class's  implementation of this method.  If they do not, an exception will be  thrown.  
@param  savedInstanceState If the activity is being re - initialized after  previously being shut down then this Bundle contains the data it most  recently supplied in  #onSaveInstanceState

onCreate中官方建议的操作是:调用 setContentView(int) 创建UI,调用 findViewById 初始化组件,调用managedQuery获取显示数据的cursor.

如果在onCreate中调用 finish()方法,activity 会直接执行onDestroy方法,而不再执行其他生命.

如果activity是被杀死的, savedInstanceState 会返回onSaveInstanceState()保存的状态.

只有在activity被杀死后创建或者新建时,才会重新调用此方法.  so, 刷新UI的工作,不要此方法中进行.


onRestart()  
Called after #onStop when the current activity is being  re - displayed to the user (the user has navigated back to it).  It will  be followed by  #onStart  and then  #onResume .
For activities that are using raw  Cursor  objects (instead of   creating them through  #managedQuery(android.net.Uri , String[], String, String[], String) , this is usually the place  where the cursor should be requeried (because you had deactivated it in  #onStop.   

onStop方法之后,被重新显示,会执行  onRestart 而不是 onCreate .


onStart()
Called after {@link #onCreate} — or after {@link #onRestart} when  the activity had been stopped, but is now again being displayed to the  user.  It will be followed by {@link #onResume}.   

activity运行到onStart()方法时,已经对用户可见. 可能是在 onCreate 方法后被调用也可能是在 onRestart 方法之后被调用. 

并且,当单例模式的activity在后台被调起时的生命周期为:onNewIntent -> onRestart() -> onStart() -> onResume(). 
so,onStart方法是一定会被调用的方法.可以在此处刷新UI.


onResume()  
Called after #onRestoreInstanceState#onRestart, or   #onPause , for your activity to start interacting with the user.  This is a good place to begin animations, open exclusive - access devices  (such as the camera), etc.  Keep in mind that onResume is not the best indicator that your activity  is visible to the user; a system window such as the keyguard may be in  front.  Use  #onWindowFocusChanged  to know for certain that your  activity is visible to the user (for example, to resume a game).   


activity运行到onResume方法时,已经可以与用户交互,并可获取焦点. 

官方建议将开设动画animations、打开独立访问设备(such as the camera)的操作放在此处.

注意:当activity运行到onResume 时并不一定对用户可见,可能系统锁屏会在最前,需要onWindowFocusChanged去判断.



onPause()  
Called as part of the activity lifecycle when an activity is going into  the background, but has not (yet) been killed.  The counterpart to   #onResume .  

When activity B is launched in front of activity A, this callback will  be invoked on A.  B will not be created until A's  onPause  returns,  so be sure to not do anything lengthy here.  

This callback is mostly used for saving any persistent state the  activity is editing, to present a "edit in place" model to the user and  making sure nothing is lost if there are not enough resources to start  the new activity without first killing this one.  This is also a good  place to do things like stop animations and other things that consume a  noticeable amount of CPU in order to make the switch to the next activity  as fast as possible, or to close resources that are exclusive access  such as the camera.  

In situations where the system needs more memory it may kill paused  processes to reclaim resources.  Because of this, you should be sure  that all of your state is saved by the time you return from  this function.  In general  #onSaveInstanceState  is used to save  per - instance state in the activity and this method is used to store  global persistent data (in content providers, files, etc.)  

After receiving this call you will usually receive a following call  to  #onStop  (after the next activity has been resumed and  displayed), however in some cases there will be a direct call back to  #onResume without going through the stopped state.   

当activity被运行到后台还没有被杀死时调用.

当activity B 要运行在 activity A 上面时,必须先等 A 的onPause()返回之后,才能执行 B 的onResume()方法,so,onPause方法最好不要执行耗时的操作.

官方建议: 在此处执行保存activity编辑的持久化状态以呈现即时点击编辑的UI显示模型,避免在系统资源不足activity被杀死时不丢失数据,也可在此处停止动画、关闭独立访问设备或者其他消耗CPU资源的操作,以便尽快的打开下一个activity.

通常, onSaveInstanceState()  用来保存每一个实例的状态到系统内存中,以便在onCreate中恢复,而  onPause()方法里是把状态直接持久化保存到本地,比如 providers或者file.

onPause()方法之后通常会紧跟着调用onStop,但是也有情况,activity会直接返回到onResume而不通过onStop.


onStop()  
Called when you are no longer visible to the user.  You will next  receive either  #onRestart #onDestroy , or nothing,  depending on later user activity.

Note that this method may never be called, in low memory situations  where the system does not have enough memory to keep your activity's  process running after its #onPause method is called.   

onStop之后可能会走 onRestart,  onDestroy, 或者其他方法.

注意:  onStop可能从不执行,如果activity进程被回收的话.


onDestroy()  
Perform any final cleanup before an activity is destroyed.  This can  happen 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.

Note: do not count on this method being called as a place for  saving data! For example, if an activity is editing data in a content  provider, those edits should be committed in either  #onPause  or   #onSaveInstanceState , not here. This method is usually implemented to  free resources like threads that are associated with an activity, so  that a destroyed activity does not leave such things around while the  rest of its application is still running.  There are situations where  the system will simply kill the activity's hosting process without  calling this method (or any others) in it, so it should not be used to  do things that are intended to remain around after the process goes  away.   

注意: 不要依赖这个方法保存数据!而应该在onPause或者 onSaveInstanceState中进行保存数据. onDestroy()可能从不执行如果activity进程被回收.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值