Activity 的啟動流程原始碼剖析(三)

2021-09-20 04:35:14 字數 3389 閱讀 4414

android 27

v4 27.1.1

我都是粘的裡面比較關鍵的原始碼,還希望配合原始碼閱讀

這是本篇的大概流程,先熟悉一下吧

流程

public final void schedulelaunchactivity(intent intent, ibinder token, int ident,

activityinfo info, configuration curconfig, configuration overrideconfig,

compatibilityinfo compatinfo, string referrer, ivoiceinteractor voiceinteractor,

int procstate, bundle state, persistablebundle persistentstate,

listpendingresults, listpendingnewintents,

boolean notresumed, boolean isforward, profilerinfo profilerinfo)

接著看 sendmessage 方法

private void sendmessage(int what, object obj, int arg1, int arg2, boolean async) 

mh.sendmessage(msg);

}

在這裡我們看到了它實際上是使用了 h 這個類,這個類了就是個 handler

我們接下來找到 h 處理 launch_activity 的**

case launch_activity:  break;
private void handlelaunchactivity(activityclientrecord r, intent customintent, string reason) 

}} else catch (remoteexception ex) }}

上面的**是 activity 主要的初始化流程

我們先看 performlaunchactivity()、然後在看 handleresumeactivity()

private activity performlaunchactivity(activityclientrecord r, intent customintent) 

// 建立contextimpl

activity activity = null;

try

} catch (exception e)

}try

if (debug_configuration) slog.v(tag, "launching activity "

+ r.activityinfo.name + " with config " + config);

window window = null;

if (r.mpendingremovewindow != null && r.mpreservewindow)

//activity attach 裡面**可以自己看一看,各種屬性設定 phonewindow 的宣告,各種事件的監聽都在裡面

r.embeddedid, r.lastnonconfigurationinstances, config,

r.referrer, r.voiceinteractor, window, r.configcallback);

if (customintent != null)

r.lastnonconfigurationinstances = null;

checkandblockfornetworkaccess();

activity.mstartedactivity = false;

int theme = r.activityinfo.getthemeresource();

if (theme != 0)

activity.mcalled = false;

//建立 activity

if (r.ispersistable()) else

if (!activity.mcalled)

r.activity = activity;

r.stopped = true;

if (!r.activity.mfinished)

//呼叫 activity 的 onrestoreinstancestate

if (!r.activity.mfinished)

} else if (r.state != null)

}//呼叫 onpostcreate

if (!r.activity.mfinished) else

if (!activity.mcalled) }}

r.paused = true;

mactivities.put(r.token, r);

} catch (supernotcalledexception e) catch (exception e)

return activity;

}

上述**中有注釋的相關**,都可以跟進去檢視一番,難度不是很大

我們再看一下 handleresumeactivity() 方法

final void handleresumeactivity(ibinder token,

boolean clearhide, boolean isforward, boolean reallyresume, int seq, string reason)

unschedulegcidler();

msomeactivitieschanged = true;

// 裡面呼叫了 activity 的 onresume()

r = performresumeactivity(token, clearhide, reason);

}

從上面得知大部分 activity 生命週期的方法都是 instrumentation 來實現的

這一篇章我沒有把**貼太全,粘太全的**太亂了,我用中文注釋的地方希望都點進去看看。

最後再放一張完整的圖

activity 啟動流程.jpg

Activity啟動流程

0x01 public void startactivity intent intent 最終走到 public void startactivityforresult requirespermission intent intent,int requestcode,nullable bundle ...

Activity啟動流程

activity啟動流程很多文章都已經說過了,這裡說一下自己的理解。activity啟動流程分兩種 後邊啟動activity的流程是一樣的,區別是前邊判斷程序是否存在的那部分。activity啟動的前提是已經開機,各項程序和ams等服務已經初始化完成,在這裡也提一下那些內容。ipc 跨程序通訊,an...

activity啟動流程

所有程序都是由init程序直接或間接fork出來的 android系統啟動時,init程序會fork出zygote,意為 受精卵 後面的所有程序都是zygote 出來的 在zygote程序初始化時會啟動systemserver程序,平時所用到的ams pms wms 網路等服務都是在systemse...