Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Activity啟動流程源碼解析

Android Activity啟動流程源碼解析

編輯:關於Android編程

在 Android Launcher啟動應用程序流程源碼解析一文中從點擊應用程序圖標startActivity()開始,一直講解到Application和MainActivity的onCreate()方法被調用。中間流程有些高能,高能部分是兩篇博文共有的地方,沒辦法繞開。本文主要講述從Activity#onCreate()到Activity#onResume()的流程。不啰嗦了,Here we go~

無論顯式還是隱式啟動Activity,都會進入到ActivityThread#handleLaunchActivity()

源碼位置:frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread#handleLaunchActivity()

    private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {  
        ......  
        // 加載activity,然後調用onCreate,onStart方法
        Activity a = performLaunchActivity(r, customIntent);  
        if (a != null) {
            // 調用onResume方法
            handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed);
            ...   
        }
        ... 
    }  

注釋有點劇透,,今天的重點全在這個方法裡。首先看下performLaunchActivity()

ActivityThread#performLaunchActivity()

    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    ...
            Activity activity = null;
        try {
            java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
            activity = mInstrumentation.newActivity(
                    cl, component.getClassName(), r.intent);
            ...
            }
            ...
        try {
            Application app = r.packageInfo.makeApplication(false, mInstrumentation);
            if (activity != null) {
                ...
                activity.attach(...)
                ...
                if (r.isPersistable()) {
                   mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
                } else {
                   mInstrumentation.callActivityOnCreate(activity, r.state);
                }
                ...
                if (!r.activity.mFinished) {
                    activity.performStart();
                    r.stopped = false;
                }
                ...
        return activity;
    }

mInstrumentation.callActivityOnCreate這行代碼的作用就是調用Activity#onCreate(),直接看activity.performStart(),這個方法的名字十分吸引人吶~跟進。

源碼位置:frameworks/base/core/java/android/app/Activity.java
Activity#performStart()

    final void performStart() {
        mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
        mFragments.noteStateNotSaved();
        mCalled = false;
        mFragments.execPendingActions();
        mInstrumentation.callActivityOnStart(this);
        if (!mCalled) {
            throw new SuperNotCalledException(
                "Activity " + mComponent.toShortString() +
                " did not call through to super.onStart()");
        }
        mFragments.dispatchStart();
        mFragments.reportLoaderStart();
        mActivityTransitionState.enterReady(this);
    }

真相已經呼之欲出了~,跟進。

源碼位置:frameworks/base/core/java/android/app/Instrumentation.java
Instrumentation#callActivityOnStart()

    public void callActivityOnStart(Activity activity) {
        activity.onStart();
    }

哈哈~又找到一個方法Activity#onStart()。真是輕而易舉~
現在回到ActivityThread#performLaunchActivity()中的handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed),跟進。

源碼位置:frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread#handleResumeActivity()
ActivityThread#ActivityClientRecord()

   final void handleResumeActivity(IBinder token,
            boolean clearHide, boolean isForward, boolean reallyResume) {
        ...
        ActivityClientRecord r = performResumeActivity(token, clearHide);
        ...
    }

    public final ActivityClientRecord performResumeActivity(IBinder token,
            boolean clearHide) {
        ...
        r.activity.performResume();
        ...
    }

中間都是各種狀態效驗,沒撒好說的。直接跟進。

源碼位置:frameworks/base/core/java/android/app/Activity.java
Activity#performResume()

    final void performResume() {
        performRestart();
        ...
        mInstrumentation.callActivityOnResume(this);
        ...
    }

在這裡有兩個分支
1. 在執行onResume()前需要先執行onRestart()和onStart()。例如從上個Activity返回的時候,這裡不作為重點,但也會稍微看下。
2. 直接執行onResume()

分支1:
Activity#performRestart()

    final void performRestart() {
        ...
        mInstrumentation.callActivityOnRestart(this);
        performStart();
    }

這兩個方法在上文都分析過了,不再贅述。

分支2:
源碼位置:frameworks/base/core/java/android/app/Instrumentation.java
Instrumentation#callActivityOnStart()

    public void callActivityOnResume(Activity activity) {
        activity.mResumed = true;
        activity.onResume();

        if (mActivityMonitors != null) {
            synchronized (mSync) {
                final int N = mActivityMonitors.size();
                for (int i=0; i

直接調用Activity#OnResume()。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved