Activity 啟動(dòng)流程分析

Activity 的冷熱啟動(dòng)

Activity的啟動(dòng)分為冷和熱啟動(dòng)兩種浩聋,所謂的冷啟動(dòng)指的是當(dāng)前進(jìn)程不在的時(shí)候瘦麸,啟動(dòng)一個(gè)activity。就相當(dāng)于我們?cè)趌auncher 桌面上點(diǎn)擊某一個(gè)app的圖標(biāo)啟動(dòng)一個(gè)應(yīng)用,并且跳轉(zhuǎn)到對(duì)應(yīng)的activity(通常是MainActivity或者SplashActivity)的流程,launcher其實(shí)也是一個(gè)應(yīng)用進(jìn)程多糠。所謂的熱啟動(dòng)指的就是在應(yīng)用內(nèi)一個(gè)ActivityA跳轉(zhuǎn)到ActivityB的流程累舷。這個(gè)流程比冷啟動(dòng)簡(jiǎn)單的多浩考,這個(gè)過(guò)程我們更關(guān)注當(dāng)前兩個(gè)Activity的生命周期的調(diào)用,這個(gè)流程不是本文的分析重點(diǎn)

時(shí)序圖分析

在分析之前被盈,先奉獻(xiàn)上我自己畫的一張activity啟動(dòng)流程的時(shí)序圖
Activity 啟動(dòng)流程時(shí)序圖

在這張時(shí)序圖里面我們可以看到析孽,完成啟動(dòng)一個(gè)activity可能需要用到的類有
1 ActivityThread
2 ActivityManagerService
3 Zygote
4 Activity
如果當(dāng)前應(yīng)用處于熱啟動(dòng)狀態(tài),那么Android系統(tǒng)就不需要Zygote的介入只怎,但是當(dāng)我們應(yīng)用處于冷啟動(dòng)流程袜瞬,也即是從桌面上點(diǎn)擊了應(yīng)用的icon圖標(biāo)進(jìn)入的流程,就需要Zygote了來(lái)幫我們把目標(biāo)進(jìn)程fork出來(lái)了身堡。具體的話我們看下面的源碼分析邓尤。

Activity 啟動(dòng)流程分析

startActivity開始

 Intent intent=new Intent();
 startActivity(intent);

我們一步一步往里面跟,其實(shí)startActivity方法最后調(diào)用的是startActivityForResult()的方法贴谎。我們來(lái)看一下startActivityForResult的源碼

public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
            @Nullable Bundle options) {
        if (mParent == null) {
            options = transferSpringboardActivityOptions(options);
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode, options);
            if (ar != null) {
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            }
            if (requestCode >= 0) {
                // If this start is requesting a result, we can avoid making
                // the activity visible until the result is received.  Setting
                // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
                // activity hidden during this time, to avoid flickering.
                // This can only be done when a result is requested because
                // that guarantees we will get information back when the
                // activity is finished, no matter what happens to it.
                mStartedActivity = true;
            }

            cancelInputsAndStartExitTransition(options);
            // TODO Consider clearing/flushing other event sources and events for child windows.
        } else {
            if (options != null) {
                mParent.startActivityFromChild(this, intent, requestCode, options);
            } else {
                // Note we want to go through this method for compatibility with
                // existing applications that may have overridden it.
                mParent.startActivityFromChild(this, intent, requestCode);
            }
        }
    }

這里我們可以看到兩個(gè)重要的分支汞扎,如果當(dāng)前的mParent不存在的時(shí)候,那么就調(diào)用

                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode, options);

這里的instrumentation的execstartActivity就是調(diào)用到了ActivityManagerService 的startActivity方法擅这。經(jīng)過(guò)一系列轉(zhuǎn)換后澈魄,會(huì)調(diào)用startProcessLocked方法,這里就開始會(huì)和zygote交互了

 Process.ProcessStartResult startResult = Process.start(entryPoint,
                    app.processName, uid, uid, gids, debugFlags, mountExternal,
                    app.info.targetSdkVersion, app.info.seinfo, requiredAbi, instructionSet,
                    app.info.dataDir, entryPointArgs);

我們這里進(jìn)入到process.start()方法

public static final ProcessStartResult start(final String processClass,
                                  final String niceName,
                                  int uid, int gid, int[] gids,
                                  int debugFlags, int mountExternal,
                                  int targetSdkVersion,
                                  String seInfo,
                                  String abi,
                                  String instructionSet,
                                  String appDataDir,
                                  String[] zygoteArgs) {
        try {
            return startViaZygote(processClass, niceName, uid, gid, gids,
                    debugFlags, mountExternal, targetSdkVersion, seInfo,
                    abi, instructionSet, appDataDir, zygoteArgs);
        } catch (ZygoteStartFailedEx ex) {
            Log.e(LOG_TAG,
                    "Starting VM process through Zygote failed");
            throw new RuntimeException(
                    "Starting VM process through Zygote failed", ex);
        }
    }

在這里startViaZygote會(huì)通過(guò)socket的方法仲翎,把Zygote喚醒(我們?cè)谶@里需要關(guān)注的socket-connect 關(guān)鍵字往前追蹤)

public static final void zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
            throws ZygoteInit.MethodAndArgsCaller {
        if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application from zygote");

        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "RuntimeInit");
        redirectLogStreams();

        commonInit();
        nativeZygoteInit();
        applicationInit(targetSdkVersion, argv, classLoader);
    }

最終的調(diào)用方法就是我們的時(shí)序圖中看到的invokeStaticMain方法痹扇,它會(huì)根據(jù)我們?cè)谥皳饺氲膃ntrypoint,把ActivityThread的main方法調(diào)用起來(lái)溯香。這里我們就進(jìn)入到第三個(gè)關(guān)鍵的類ActivityThread

ActivityThread做了什么

我們首先進(jìn)入ActivityThread的main方法

public static void main(String[] args) {
        Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
        SamplingProfilerIntegration.start();

        // CloseGuard defaults to true and can be quite spammy.  We
        // disable it here, but selectively enable it later (via
        // StrictMode) on debug builds, but using DropBox, not logs.
        CloseGuard.setEnabled(false);

        Environment.initForCurrentUser();

        // Set the reporter for event logging in libcore
        EventLogger.setReporter(new EventLoggingReporter());

        // Make sure TrustedCertificateStore looks in the right place for CA certificates
        final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
        TrustedCertificateStore.setDefaultUserDirectory(configDir);

        Process.setArgV0("<pre-initialized>");

        Looper.prepareMainLooper();

        ActivityThread thread = new ActivityThread();
        thread.attach(false);

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }

        if (false) {
            Looper.myLooper().setMessageLogging(new
                    LogPrinter(Log.DEBUG, "ActivityThread"));
        }

        // End of event ActivityThreadMain.
        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
        Looper.loop();

        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

里面最重要的方法已經(jīng)展現(xiàn)出來(lái)了鲫构,就是attach方法。我們繼續(xù)進(jìn)入attach方法
里面最重要的調(diào)用就是

  final IActivityManager mgr = ActivityManagerNative.getDefault();
            try {
                mgr.attachApplication(mAppThread);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }

這個(gè)時(shí)候又回到ActivityManagerService里面了玫坛。
ActivityManagerService經(jīng)過(guò)一系列的調(diào)用后结笨,就繼續(xù)調(diào)用到ActivityThread里面的bindApplication中了。
ActivityThread的bindApplication主要就是把當(dāng)前的上下文和application綁定起來(lái)。

app 的如何被創(chuàng)建

ActivityThread 里面的handleBindApplication方法就是目標(biāo)應(yīng)用啟動(dòng)的入口禀梳。這個(gè)方法是在前面的bindApplication通過(guò)handler通訊的方法觸發(fā)調(diào)用的杜窄,我們主要關(guān)注handleBindApplication里面的這一句:

                mInstrumentation.callApplicationOnCreate(app);

這一個(gè)方法就把a(bǔ)pplication onCreate() 給創(chuàng)建起來(lái)了。

Activity 如何被啟動(dòng)

Activity真正啟動(dòng)的入口在ActivityThread 里面的scheduleLaunchActivity方法中算途。這個(gè)方法同樣也是有ActivityManagerService觸發(fā)塞耕。我們進(jìn)入ScheduleLaunchActivity中可以看到核心的方法調(diào)用的是

            sendMessage(H.LAUNCH_ACTIVITY, r);

同樣是通過(guò)handler,把當(dāng)前l(fā)auncher_activity的指令發(fā)送出去嘴瓤。最后會(huì)走到

    private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
        // If we are getting ready to gc after going to the background, well
        // we are back active so skip it.
        unscheduleGcIdler();
        mSomeActivitiesChanged = true;

        if (r.profilerInfo != null) {
            mProfiler.setProfiler(r.profilerInfo);
            mProfiler.startProfiling();
        }

        // Make sure we are running with the most recent config.
        handleConfigurationChanged(null, null);

        if (localLOGV) Slog.v(
            TAG, "Handling launch of " + r);

        // Initialize before creating the activity
        WindowManagerGlobal.initialize();

        Activity a = performLaunchActivity(r, customIntent);

        if (a != null) {
            r.createdConfig = new Configuration(mConfiguration);
            reportSizeConfigurations(r);
            Bundle oldState = r.state;
            handleResumeActivity(r.token, false, r.isForward,
                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);

            if (!r.activity.mFinished && r.startsNotResumed) {
                // The activity manager actually wants this one to start out paused, because it
                // needs to be visible but isn't in the foreground. We accomplish this by going
                // through the normal startup (because activities expect to go through onResume()
                // the first time they run, before their window is displayed), and then pausing it.
                // However, in this case we do -not- need to do the full pause cycle (of freezing
                // and such) because the activity manager assumes it can just retain the current
                // state it has.
                performPauseActivityIfNeeded(r, reason);

                // We need to keep around the original state, in case we need to be created again.
                // But we only do this for pre-Honeycomb apps, which always save their state when
                // pausing, so we can not have them save their state when restarting from a paused
                // state. For HC and later, we want to (and can) let the state be saved as the
                // normal part of stopping the activity.
                if (r.isPreHoneycomb()) {
                    r.state = oldState;
                }
            }
        } else {
            // If there was an error, for any reason, tell the activity manager to stop us.
            try {
                ActivityManagerNative.getDefault()
                    .finishActivity(r.token, Activity.RESULT_CANCELED, null,
                            Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
        }
    }

這里面就是兩個(gè)重要的流程了:

        Activity a = performLaunchActivity(r, customIntent);
        handleResumeActivity(r.token, false, r.isForward,
                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);

對(duì)應(yīng)的就是我們activity的生命周期了

總結(jié)

由于activity啟動(dòng)流程里面扫外,比較復(fù)雜的就是應(yīng)用的冷啟動(dòng)的流程,一套代碼流程下來(lái)就是非常的繁瑣廓脆。特別是在最新的Android9以上筛谚,出現(xiàn)了很多的handler和socket通訊的方法調(diào)用,如果代碼不是很熟悉的話停忿,查找整個(gè)調(diào)用棧會(huì)非常的有挑戰(zhàn)性驾讲。這里還是希望大家可以仔細(xì)的查看代碼,follow一下流程席赂,相信大家都會(huì)有不錯(cuò)的收獲和體驗(yàn)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末吮铭,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子颅停,更是在濱河造成了極大的恐慌谓晌,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件癞揉,死亡現(xiàn)場(chǎng)離奇詭異纸肉,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)喊熟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門柏肪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人逊移,你說(shuō)我怎么就攤上這事预吆。” “怎么了胳泉?”我有些...
    開封第一講書人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵拐叉,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我扇商,道長(zhǎng)凤瘦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任案铺,我火速辦了婚禮蔬芥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己笔诵,他們只是感情好返吻,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著乎婿,像睡著了一般测僵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上谢翎,一...
    開封第一講書人閱讀 49,007評(píng)論 1 284
  • 那天捍靠,我揣著相機(jī)與錄音,去河邊找鬼森逮。 笑死榨婆,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的褒侧。 我是一名探鬼主播良风,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼璃搜!你這毒婦竟也來(lái)了拖吼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤这吻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后篙议,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唾糯,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年鬼贱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了移怯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡这难,死狀恐怖舟误,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姻乓,我是刑警寧澤嵌溢,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站蹋岩,受9級(jí)特大地震影響赖草,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜剪个,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一秧骑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦乎折、人聲如沸绒疗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)忌堂。三九已至,卻和暖如春酗洒,著一層夾襖步出監(jiān)牢的瞬間士修,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工樱衷, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留棋嘲,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓矩桂,卻偏偏與公主長(zhǎng)得像沸移,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子侄榴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容