超详细的生命周期图-你能回答全吗

3,297 阅读3分钟

超详细的Activity与Fragment的生命周期图,可能大家会说你这篇文章也太水了吧。就这么一个破图。可是我觉得它写的很详细,有些方法是哪些情况下会运行,哪些情况不会运行,写的一清二楚。不知道大家能回答对多少。

强烈建议大家把图片右键另存到本地,然后本地放大看。看的更清楚!!

好了我们先来逐个看看Activity中一些平时不常用的函数:

1. onPostCreate , onPostResume

onPostCreate是指onPostCreate方法是指onCreate方法彻底执行完毕的回调,onPostResume同理.

2.onUserInteraction ,onUserLeaveHint

我们先来看onUserInteraction:

/** 
  * Called whenever a key, touch, or trackball event is dispatched to the 
  * activity.  Implement this method if you wish to know that the user has 
  * interacted with the device in some way while your activity is running. 
  * 
  * <p>All calls to your activity's {@link #onUserLeaveHint} callback will 
  * be accompanied by calls to {@link #onUserInteraction}. 
  * 
  * activity无论分发按键事件、触摸事件或者轨迹球事件都会调用Activity#onUserInteraction()。 
  * 如果你想知道用户用某种方式和你正在运行的activity交互,可以重写Activity#onUserInteraction()。 
  * 所有调用Activity#onUserLeaveHint()的回调都会首先回调Activity#onUserInteraction()。 
  */  

activity在分发各种事件的时候会调用该方法,注意:启动另一个activity,Activity#onUserInteraction()会被调用两次,一次是activity捕获到事件,另一次是调用Activity#onUserLeaveHint()之前会调用Activity#onUserInteraction()。

那一定有人会问这个方法我们有什么用处呢。我们看到我们可以用这个方法来监控用户有没有与当前的Activity进行交互,那我们就可以针对这个来假设场景,有个APP要求N分钟后用户没有进行操作,那就自动出来动态壁纸,或者进行锁屏界面,或者跳到登录界面重新登录等!那我们只需要写个倒计时,然后每次调用了onUserInteraction方法,就把时间重置即可。。多方便!!


我们再看onUserLeaveHint:

/**  
  * Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.  
  * For example, when the user presses the Home key, {@link #onUserLeaveHint} will be called, but 
  * when an incoming phone call causes the in-call Activity to be automatically brought to the foreground,  
  *{@link #onUserLeaveHint} will not be called on the activity being interrupted.  
  * 
  * 当用户的操作使一个activity准备进入后台时,此方法会像activity的生命周期的一部分被调用。例如,当用户按下Home键, 
  * Activity#onUserLeaveHint()将会被回调。但是当来电导致来电activity自动占据前台,Activity#onUserLeaveHint()将不会被回调。 
  */  

用户手动离开当前activity,会调用该方法,比如用户主动切换任务,短按home进入桌面等。系统自动切换activity不会调用此方法,如来电,灭屏等。

我们一般监听返回键,肯定是重写onKeyDown方法,但是Home键和Menu键就不好监听了。但是有了这个方法。我们可以做统一的监听了。比如要监听用户点了Home键跳回到桌面后。我们要求这个APP自动跳转到解锁界面。我们只要在这里做监听出来即可。

3. onContentChanged

onContentChanged()是Activity中的一个回调方法,当Activity的布局改动时,即setContentView()或者addContentView()方法执行完毕时就会调用该方法。那我们可以用来干嘛呢。比如我们平时写的findViewById方法,也可以统一放到这个方法下:

@Override  
public void onContentChanged() {  
    super.onContentChanged();  
  
    mGalleryButton = (Button) findViewById( R.id.button1 );  
    mEditButton = (Button) findViewById( R.id.button2 );  
}

4. onAttachedToWindow

onAttachedToWindow是在第一次onDraw前调用的。也就是我们写的View在没有绘制出来时调用的,但只会调用一次。 比如,我们写状态栏中的时钟的View,在onAttachedToWindow这方法中做初始化工作,比如注册一些广播等等…… 而且如果要修改window窗口的尺寸,不会在onCreate方法中进行修改,而是在onAttachedToWindow方法中进行修改。 在Android4.0前面,如果想屏蔽Home键事件,还可以在onAttachedToWindow这么写:

@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
  
  this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
  super.onAttachedToWindow();
}

只是现在在Android4.0后面这样已经无效了,会报错:

java.lang.IllegalArgumentException: Window type can not be changed after the window is added.