从AtivityThread的main( )方法开始加载Activity
1、调用Activity的attach( )方法;
Activity
private Window mWindow;
final void attach(...) { attachBaseContext(context); ... mWindow = new PhoneWindow(this); // 创建抽象类Window实体对象 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(); mCurrentConfig = config; }
2、从setContentView( )方法说起
public void setContentView(@LayoutRes int layoutResID) { getWindow().setContentView(layoutResID); initWindowDecorActionBar(); }
PhoneWindow是Window抽象类的唯一实现
@Override public void setContentView(int layoutResID) { if (mContentParent == null) { installDecor(); } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) { mContentParent.removeAllViews(); } if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) { final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID, getContext()); transitionTo(newScene); } else { mLayoutInflater.inflate(layoutResID, mContentParent); /// 将View添加到mContentParent } mContentParent.requestApplyInsets(); final Callback cb = getCallback(); if (cb != null && !isDestroyed()) { cb.onContentChanged(); } } private void installDecor() { if (mDecor == null) { mDecor = new DecorView(getContext(), -1); mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); mDecor.setIsRootNamespace(true); if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) { mDecor.postOnAnimation(mInvalidatePanelMenuRunnable); } } if (mContentParent == null) { mContentParent = generateLayout(mDecor); // 加载具体的布局文件到DecorView中。 ... } ... }
到此布局文件已经添加到DecorView的mContentParent中,但是还没被WindowManager正式添加到Window中。
3、调用Activity的makeVisible( )方法;
Activity
void makeVisible() { if (!mWindowAdded) { ViewManager wm = getWindowManager(); wm.addView(mDecor, getWindow().getAttributes()); mWindowAdded = true; } mDecor.setVisibility(View.VISIBLE); }
此时已经完成了Activity中Window的创建。
WinsdowManager一个借口,它的真正实现是WindowManagerImpl
WindowManagerImpl
private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mDisplay, mParentWindow);
}
public void addView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow) { ... final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params; ... ViewRootImpl root; View panelParentView = null; synchronized (mLock) { ... root = new ViewRootImpl(view.getContext(), display); view.setLayoutParams(wparams); 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); // 向WMS发起显示Activity请求 } catch (RuntimeException e) { ... } }
每个Window都对应着一个View和ViewRootImpl,window和View通过ViewRootImpl建立联系。
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { synchronized (this) { 1、requestLayout 2、向WMS发起显示Activity请求 } }
ViewRootImpl是Framework<应用层>与Native层的沟通桥梁。