首先需要帶著目的去閱讀源碼
這次的目的是:Activity的顯示,Window起了什么作用?
建議:一邊跟著文章,一邊跟著源碼閱讀,否則會暈
1、activityThread收到handler消息
啟動一個activity,調用performLaunchActivity方法
2、調用activity的attach方法
3、在activity的attach方法
attach方法中為mWindow賦值
mWindow = new PhoneWindow(this, window, activityConfigCallback);
??關鍵點:Activity和Window的橋梁建立,Activity持有了window對象。
4、開始activity的生命周期
在onCreate方法中,調用setContentView方法,調用getWindow的setContentView方法。getWindow返回的值就是上面創(chuàng)建的phoneWindow。
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
5、phoneWindow.setContentView()
其中,先創(chuàng)建了decorView,然后創(chuàng)建我們熟
悉的 R.id.content,最后把傳入的layoutResID放入content中,實現(xiàn)了view的創(chuàng)建。這時候,PhoneWindow就把view創(chuàng)建完成。
??關鍵點:setContenView方法,PhoneWindow創(chuàng)建View。Window和View 的橋梁建立。
兩個橋梁已經(jīng)創(chuàng)建,window就是activity和view的中間節(jié)點,activity對view的控制都需要通過window。
6、ActivityThread.handleResumeActivity 調用目標activity的makeVisible方法
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
7、getWindowManager()返回的是什么?
//Activity.class
public WindowManager getWindowManager() {
return mWindowManager;
}
mWindowManager在哪里創(chuàng)建?
mWindowManager = mWindow.getWindowManager();
調用window的getWindowManager方法
//Window.class
public WindowManager getWindowManager() {
return mWindowManager;
}
window的mWindowManager創(chuàng)建
//Window.class
mWindowManager=((WindowManagerImpl)wm).createLocalWindowManager(this);
//WindowManagerImpl.class
public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
return new WindowManagerImpl(mContext, parentWindow);
}
??關鍵點:所以,activity中使用的getWindowManager就是WindowManagerImpl
WindowManagerImpl實現(xiàn)了WindowManager接口,WindowManager繼承自ViewManager
//ViewManager 接口
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
所以WindowManagerImpl的主要目的就是對View的管理。
8、現(xiàn)在就是WindowManagerImpl調用了addView方法,把decorView傳入。
//WindowManagerImpl.class
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
}
這里出現(xiàn)了mGlobal對象。
private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
這個mGlobal是個單例,WindowManagerImpl對view的操作都是通過WindowManagerGlobal來實現(xiàn)的。
9、WindowManagerGlobal的addView方法,傳入了decorView
//WindowManagerGlobal.class
//addView方法
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// do this last because it fires off messages to start doing things
try {
root.setView(view, wparams, panelParentView);
}
addView方法中,把decorview保存起來,創(chuàng)建了rootViewImpl對象(root),然后調用setView方法。
10、ViewRootImpl的setView方法
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(), mWinFrame,
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel);
調用mWindowSession對象的addToDisplay方法,從方法名中也能看出,是把window添加到顯示屏上。
這里就需要大致找一下mWindowSession是什么?
11、mWindowSession是什么?
//ViewRootImpl.class
mWindowSession = WindowManagerGlobal.getWindowSession();
調用WindowManagerGlobal的getWindowSession方法
//WindowManagerGlobal.class
//getWindowSession()方法
sWindowSession = windowManager.openSession(
new IWindowSessionCallback.Stub() {
@Override
public void onAnimatorScaleChanged(float scale) {
ValueAnimator.setDurationScale(scale);
}
},
imm.getClient(), imm.getInputContext());
sWindowSession是windowManager.openSession方法返回的。這樣,就需要到windowManager中去尋找,windowManager的實現(xiàn)類就是WindowManagerService。
//WindowManagerService.class
@Override
public IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client,
IInputContext inputContext) {
if (client == null) throw new IllegalArgumentException("null client");
if (inputContext == null) throw new IllegalArgumentException(“null inputContext”);
Session session = new Session(this, callback, client, inputContext);
return session;
}
返回的是一個Session對象
我們回到第10步,其中調用了addToDisplay方法,用來添加window。
12、Session的addToDisplay方法
//Session.class
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
int viewVisibility, int displayId, Rect outFrame, Rect outContentInsets,
Rect outStableInsets, Rect outOutsets,
DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel) {
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId, outFrame,
outContentInsets, outStableInsets, outOutsets, outDisplayCutout, outInputChannel);
}
最終調用mService.addWindow方法。而mService是什么?
final WindowManagerService mService;
mService就是WindowManagerService。這樣,window就通過IPC的方式,交給了系統(tǒng)服務,進行顯示。
總結:
Activity創(chuàng)建后,window干了這些事
1、直面用戶的activity創(chuàng)建后,其內(nèi)部創(chuàng)建了Window對象。
2、調用setContentView后,Window對象把我們自己的xml通過inflate方式生成了View,并添加到了decorView中。
3、都創(chuàng)建完成后,就需要顯示了,觸發(fā)了ActivityThread.handleResumeActivity方法,這時,就需要把剛才生成的window顯示到屏幕上
4、window的一路傳遞,最終交給了WindowManagerService處理進行顯示。