原文转载:http://www.javashuo.com/article/p-mxdqjswj-cv.htmlhtml
在文章开头贴出的第一段AcitityThread.handleLauncherActivity()方法的代码中,执行完performLaunchAcitity()建立好Acitivity后,便会执行到handleResumeActivity()方法,该方法代码以下。
数组
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) { ...// TODO Push resumeArgs into the activity for consideration // 该方法执行过程当中会调用到Acitity的onResume()方法,返回的ActivityClientRecord对象描述的便是建立好的Activity r = performResumeActivity(token, clearHide, reason); if (r != null) { final Activity a = r.activity;//返回以前建立的Acitivty if (localLOGV) Slog.v( TAG, "Resume " + r + " started activity: " + a.mStartedActivity + ", hideForNow: " + r.hideForNow + ", finished: " + a.mFinished); final int forwardBit = isForward ? WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0; // If the window hasn't yet been added to the window manager, // and this guy didn't finish itself or start another activity, // then go ahead and add the window. // 判断该Acitivity是否可见,mStartedAcitity记录的是一个Activity是否还处于启动状态 // 若是还处于启动状态则mStartedAcitity为true,表示该activity还未启动好,则该Activity还不可见 boolean willBeVisible = !a.mStartedActivity; // 若是启动的组建不是全屏的,mStartedActivity也会是true,此时依然须要willBeVisible为true如下的if逻辑就是针对这种状况的校订 if (!willBeVisible) { try { willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible( a.getActivityToken()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } if (r.window == null && !a.mFinished && willBeVisible) { r.window = r.activity.getWindow(); View decor = r.window.getDecorView(); decor.setVisibility(View.INVISIBLE); ViewManager wm = a.getWindowManager(); WindowManager.LayoutParams l = r.window.getAttributes(); a.mDecor = decor; l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; l.softInputMode |= forwardBit; //PreserverWindow,通常指主题换了或者configuration变了状况下的Acitity快速重启机制 if (r.mPreserveWindow) { a.mWindowAdded = true; r.mPreserveWindow = false; // Normally the ViewRoot sets up callbacks with the Activity // in addView->ViewRootImpl#setView. If we are instead reusing // the decor view we have to notify the view root that the // callbacks may have changed. ViewRootImpl impl = decor.getViewRootImpl(); if (impl != null) { impl.notifyChildRebuilt(); } } if (a.mVisibleFromClient && !a.mWindowAdded) { a.mWindowAdded = true; //调用了WindowManagerImpl的addView方法 wm.addView(decor, l); } ... }
重点来看wm.addView()方法,该方法中的decor参数为Acitity对应的Window中的视图DecorView,wm为在建立PhoneWindow是建立的WindowManagerImpl对象,该对象的addView方法实际调用到到是单例对象WindowManagerGlobal的addView方法(前文有提到)。在看addView代码前,我先来看看WindowManagerGlobal对象成员变量。异步
private static WindowManagerGlobal sDefaultWindowManager; private static IWindowManager sWindowManagerService; private static IWindowSession sWindowSession; private final Object mLock = new Object(); private final ArrayList<View> mViews = new ArrayList<View>(); private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>(); private final ArrayList<WindowManager.LayoutParams> mParams = new ArrayList<WindowManager.LayoutParams>(); private final ArraySet<View> mDyingViews = new ArraySet<View>();
三个成员变量mViews、mRoots和mParams分别是类型为View、ViewRootImpl和WindowManager.LayoutParams的数组。这里有这样的逻辑关系,每一个View都对应着惟一的一个ViewRootImpl和WindowManager.LayoutRarams,便是1:1:1的关系。这三个数组长度始终保持一致,而且在同一个位置上存放的是互相关联的View、ViewRootImpl和WindowManager.LayoutParams对象。此外还有一个成员变量mDyView,保存的则是已经不须要但还未被系统会收到View。ide
View与LayoutParams比较好理解,那ViewRootImpl对象的做用是什么呢?首先WindowManagerImpl是做为管理类,就像主管同样,根据Acitity和Window的调用请求,找到合适的作事的人;DecorView自己是FrameworkLayout,本事是一个View,所表示的是一种静态的结构;因此这里就须要一个真正作事的人,那就是ViewRootImpl类的工做。总结来说ViewRootImpl的功能以下ui
接下来咱们进入WindowManagerGlobal.addView()方法的代码。
this
public void addView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow) { ... ViewRootImpl root; View panelParentView = null; synchronized (mLock) { ... // If this is a panel window, then find the window it is being // attached to for future reference. // 若是当前添加的是一个子视图,则还须要找他他的父视图 //这里咱们分析的是添加DecorView的逻辑,没有父视图,故不会走到这里,panelParentView为null if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW && wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) { final int count = mViews.size(); for (int i = 0; i < count; i++) { if (mRoots.get(i).mWindow.asBinder() == wparams.token) { panelParentView = mViews.get(i); } } } root = new ViewRootImpl(view.getContext(), display); view.setLayoutParams(wparams); //保存互相对应的View、ViewRootImpl、WindowManager.LayoutParams到数组中 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); } catch (RuntimeException e) { // BadTokenException or InvalidDisplayException, clean up. if (index >= 0) { removeViewLocked(index, true); } throw e; } } }
关注代码中加粗的两个方法,首先会建立一个ViewRootImpl对象,而后调用ViewRootImpl.setView方法,其中panelParentView在addView参数为DecorView是为null。进入ViewRootImpl.setView()代码。
rest
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { synchronized (this) { if (mView == null) { //初始化成员变量mView、mWindowAttraibutes //mAttachInfo是View类的一个内部类AttachInfo类的对象 //该类的主要做用就是储存一组当View attach给它的父Window的时候Activity各类属性的信息 mView = view; mAttachInfo.mDisplayState = mDisplay.getState(); mDisplayManager.registerDisplayListener(mDisplayListener, mHandler); mViewLayoutDirectionInitial = mView.getRawLayoutDirection(); mFallbackEventHandler.setView(view); mWindowAttributes.copyFrom(attrs); ... //继续初始化一些变量,包含针对panelParentView不为null时的父窗口的一些处理 mAdded = true; // Schedule the first layout -before- adding to the window // manager, to make sure we do the relayout before receiving // any other events from the system. // 这里调用异步刷新请求,最终会调用performTraversals方法来完成View的绘制 requestLayout(); if ((mWindowAttributes.inputFeatures & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) { mInputChannel = new InputChannel(); } mForceDecorViewVisibility = (mWindowAttributes.privateFlags & PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY) != 0; try { mOrigWindowType = mWindowAttributes.type; mAttachInfo.mRecomputeGlobalAttributes = true; collectViewAttributes(); res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes, getHostVisibility(), mDisplay.getDisplayId(), mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, mAttachInfo.mOutsets, mInputChannel); } catch (RemoteException e) { mAdded = false; mView = null; mAttachInfo.mRootView = null; mInputChannel = null; mFallbackEventHandler.setView(null); unscheduleTraversals(); setAccessibilityFocus(null, null); throw new RuntimeException("Adding window failed", e); } finally { if (restore) { attrs.restore(); } } ... } } }
相关变量初始化完成后,便会将mAdded设置为true,表示ViewRootImpl与setView传入的View参数已经作好了关联。以后便会调用requestLayout()方法来请求一次异步刷新,该方法后来又会调用到performTraversals()方法来完成view到绘制工做。注意到这里虽然完成了绘制的工做,可是咱们建立Activity的源头是AMS中发起的,咱们从一开始建立Acitivity到相对应的Window、DecorView这一大套对象时,还并未与AMS进程进行反馈。因此以后便会调用mWindowSession.addToDisplay()方法会执行IPC的跨进程通讯,最终调用到AMS中的addWindow方法来在系统进程中执行相关加载Window的操做。code
点击下方连接免费获取Android进阶资料:
https://shimo.im/docs/tXXKHgdjPYj6WT8d/orm