Activity 的启动流程就不赘述了,之前写过一篇裹脚布式的文章,庖丁解牛 Activity 启动流程 ,感兴趣的可以看看。这里直接跳到 ActivityThread.performLaunchActivity()
方法。
ActivityThread.java
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
…
// 获取 ComponentName
ComponentName component = r.intent.getComponent();
…
// 创建 ContextImpl 对象
ContextImpl appContext = createBaseContextForActivity®;
…
// 反射创建 Activity 对象
activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
…
// 创建 Application 对象
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
…
// attach 方法中会创建 PhoneWindow 对象
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback);
// 执行 onCreate()
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
…
return activity;
}
mInstrumentation.callActivityOnCreate()
方法最终会回调 Activity.onCreate()
方法。到这里,`se
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
tContentView()方法就被执行了。
setContentView()逻辑很复杂,但干的事情很直白。创建
DecorView,然后根据我们传入的布局文件 id 解析 xml,将得到的 view 塞进 DecorView 中。注意,到现在,我们得到的只是一个 **空壳子 View 树**,它并没有被添加到屏幕上,其实也不能添加到屏幕上。所以,在
onCreate()` 回调中获取视图宽高显然是不可取的。
看完 onCreate()
, 我们跳过 onStart()
,里面没干啥太重要的事情,直接来到 onResume()
。
注:Activity 的生命周期是由
ClientLifecycleManager
类来调度的,具体原理可以看这篇文章 从源码看 Activity 生命周期 。
ActivityThread.java
public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,
String reason) {
…
// 1. 回调