Android学习Android开发经验谈Android开发

Activity 知识梳理(1) - Activity生命周期

2017-02-20  本文已影响275人  泽毛

一、概述

学习Activity生命周期,首先我们要明白,学习它的目的不仅在于要知道有哪些生命周期,而是在于明白各回掉函数调用的时机,以便在合适的时机进行正确的操作,如初始化变量、页面展示、数据操作、资源回收等。平时的工作习惯都是,onCreate(xxx)初始化,onResume()注册、拉取数据,onPause()反注册,onDestroy()释放资源,这篇文章总结了一些和关键生命周期相关联的一些要点。

二、金字塔模型

Activity 生命周期金字塔模型.png
在官方文档中,把Activity的生命周期理解成为一个金字塔模型,它是基于下面两点:

这个模型中包含了Activity的六种状态:

在这六种状态当中,只有ResumedPausedStopped这几种状态在用户没有进一步操作时会保持在该状态,而其余的,都会在执行完相应的回调函数后快速跳过。

三、关键生命周期

3.1 protected void onCreate(Bundle savedInstanceState)

3.2 protected void onStart()

首先我们看一下onStart()方法调用的地方,通过下面这段代码,我们可以知道ActivityonStart()最初是通过Activity#performStart()方法调用过来的:

<!-- Activity.java -->
private Instrumentaion mInstrumentation;

final void performStart() {
    mInstrumentation.callActivityOnStart(this);
}

<!-- Instrumentaion.java -->
public void callActivityOnStart(Activity activity) {
   activity.onStart();
}

Activity#performStart()方法是由ActivityThread#performLaunchActivity(调过来的:

<!-- ActivityThread.java -->
private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    Activity a = performLaunchActivity(r, customIntent); //performCreate, performStart()
    if (a != null) { 
        ....
        handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed); //performResume()
       ....
   }
}
//首先看一下调用performCreate, performStart的地方。
private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    mInstrumentation.callActivityOnCreate(activity, r.state); //performCreate
    ....
    if (!r.activity.mFinished) { 
        activity.performStart(); 
        r.stopped = false; 
    }
    if (!r.activity.mFinished) { 
       if (r.state != null) {         
           mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);  //1.onRestoreIntanceState()
       } 
    } 
    if (!r.activity.mFinished) { 
        activity.mCalled = false; 
        mInstrumentation.callActivityOnPostCreate(activity, r.state); //2.onPostCreate
        if (!activity.mCalled) { 
            throw new SuperNotCalledException( "Activity " + r.intent.getComponent().toShortString() + " did not call through to super.onPostCreate()"); 
       } 
    }
    ...    
}
//这是performResume的入口。
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume) {        
    ActivityClientRecord r = performResumeActivity(token, clearHide);
}
//最后看一下performResume真正执行的地方。
public final ActivityClientRecord performResumeActivity(IBinder token, boolean clearHide) {
    try { 
        if (r.pendingIntents != null) { 
            deliverNewIntents(r, r.pendingIntents); //3.onNewIntent()
            r.pendingIntents = null; 
        } 
       if (r.pendingResults != null) { 
            deliverResults(r, r.pendingResults); //4.onActivityResult()
            r.pendingResults = null; 
      } 
      r.activity.performResume(); 
}

3.3 protected void onResume()

final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume) {
    ....
    ActivityClientRecord r = performResumeActivity(token, clearHide);
    ....
    if (r.window == null && !a.mFinished && willBeVisible) {
        r.window = r.activity.getWindow();
        View decor = r.window.getDecorView();
        WindowManager.LayoutParams l = r.window.getAttributes();
        ...
        wm.addView(decor, l);
    }
}

3.4 protected void onPause()

3.5 protected void onStop()

3.6 protected void onDestroy()

上一篇 下一篇

猜你喜欢

热点阅读