android 窗口启动流程,关于android:Android绘制流程窗口启动流程分析下
原文转载:https://www.cnblogs.com/tiger-wang-ms/p/6517048.html三、handleResumeActivity()流程在文章结尾贴出的第一段AcitityThread.handleLauncherActivity()办法的代码中,执行完performLaunchAcitity()创立好Acitivity后,便会执行到handleResumeActiv
原文转载:https://www.cnblogs.com/tiger-wang-ms/p/6517048.html
三、handleResumeActivity()流程
在文章结尾贴出的第一段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 mViews = new ArrayList();
private final ArrayList mRoots = new ArrayList();
private final ArrayList mParams =
new ArrayList();
private final ArraySet mDyingViews = new ArraySet();
三个成员变量mViews、mRoots和mParams别离是类型为View、ViewRootImpl和WindowManager.LayoutParams的数组。这里有这样的逻辑关系,每个View都对应着惟一的一个ViewRootImpl和WindowManager.LayoutRarams,即是1:1:1的关系。这三个数组长度始终保持统一,并且在同一个地位上寄存的是相互关联的View、ViewRootImpl和WindowManager.LayoutParams对象。此外还有一个成员变量mDyView,保留的则是曾经不须要但还未被零碎会收到View。
View与LayoutParams比拟好了解,那ViewRootImpl对象的作用是什么呢?首先WindowManagerImpl是作为治理类,就像主管一样,依据Acitity和Window的调用申请,找到适合的做事的人;DecorView自身是FrameworkLayout,本事是一个View,所示意的是一种动态的构造;所以这里就须要一个真正做事的人,那就是ViewRootImpl类的工作。总结来讲ViewRootImpl的性能如下
实现了绘制过程。在ViewRootImpl类中,实现了perfromMeasure()、performDraw()、performLayout()等绘制相干的办法。
与零碎服务进行交互,例如与AcitityManagerSerivice,DisplayService、AudioService等进行通信,保障了Acitity相干性能等失常运行。
触屏事件等散发逻辑的实现
接下来咱们进入WindowManagerGlobal.addView()办法的代码。
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()代码。
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的操作。
点击下方链接收费获取Android进阶材料:
https://shimo.im/docs/tXXKHgdjPYj6WT8d/
更多推荐
所有评论(0)