前面我們講解了Zygote冷啟動(dòng)Application應(yīng)該設(shè)置啟動(dòng)時(shí)間的位置是attachBaseContext
Zygote冷啟動(dòng) 啟動(dòng)時(shí)間收集
首先看ActivityThtread喚起Activity的方法
public void handleMessage(Message msg) {
···
case RELAUNCH_ACTIVITY:
handleRelaunchActivityLocally((IBinder) msg.obj);
break;
···
}
handleRelaunchActivityLocally最終會(huì)走到performLaunchActivity
// Activity resources must be initialized with the same loaders as the
// application context.
appContext.getResources().addLoaders(
app.getResources().getLoaders().toArray(new ResourcesLoader[0]));
appContext.setOuterContext(activity);
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,
r.assistToken);
if (customIntent != null) {
activity.mIntent = customIntent;
}
performLaunchActivity() 中調(diào) OnCreate() 方法前會(huì)調(diào)用 Activity 的 attach() 方法,這里詳細(xì)的可以看這里
Window WindowManager WindowManagerService
可以看到Activity里面的attatch方法
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback) {
//省略部分代碼
...
mWindow = new PhoneWindow(this, window, activityConfigCallback);
mWindow.setWindowControllerCallback(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
//省略部分代碼
...
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
//省略部分代碼
...
}
attatch方法中創(chuàng)建了Window唯一實(shí)現(xiàn)類PhoneWindow類型的mWindow對(duì)象,mWindowManager對(duì)象也是從PhoneWindow獲取的。那么他是在哪里初始化的呢?